Skip to content

Commit

Permalink
fix(plugin-history-sync): replace silent state into flags (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
orionmiz authored Oct 30, 2023
1 parent 75874e4 commit abd670c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 30 deletions.
2 changes: 0 additions & 2 deletions extensions/plugin-history-sync/src/historyState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const STATE_TAG = `@stackflow/plugin-history-sync`;
interface State {
activity: Activity;
step?: ActivityStep;
silent?: boolean;
}

interface SerializedState extends State {
Expand Down Expand Up @@ -45,7 +44,6 @@ function serializeState(state: State): SerializedState {
_TAG: STATE_TAG,
activity: serializeActivity(state.activity),
step: state.step ? serializeStep(state.step) : undefined,
silent: state.silent,
};
}

Expand Down
50 changes: 22 additions & 28 deletions extensions/plugin-history-sync/src/historySyncPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function historySyncPlugin<

return () => {
let pushFlag = 0;
let popFlag = 0;
let silentFlag = false;

const { enqueue } = makeHistoryTaskQueue(history);

Expand Down Expand Up @@ -174,8 +174,8 @@ export function historySyncPlugin<
);

const onPopState: Listener = (e) => {
if (popFlag) {
popFlag -= 1;
if (silentFlag) {
silentFlag = false;
return;
}

Expand All @@ -188,12 +188,6 @@ export function historySyncPlugin<
const targetActivity = historyState.activity;
const targetActivityId = historyState.activity.id;
const targetStep = historyState.step;
const { silent } = historyState;

if (silent) {
historyState.silent = false;
return;
}

const { activities } = getStack();
const currentActivity = activities.find(
Expand Down Expand Up @@ -321,17 +315,17 @@ export function historySyncPlugin<
options.urlPatternOptions,
);

enqueue(() =>
enqueue(() => {
silentFlag = true;
pushState({
history,
pathname: template.fill(activity.params),
state: {
activity,
silent: true,
},
useHash: options.useHash,
}),
);
});
});
},
onStepPushed({ effect: { activity, step } }) {
if (pushFlag) {
Expand All @@ -344,18 +338,18 @@ export function historySyncPlugin<
options.urlPatternOptions,
);

enqueue(() =>
enqueue(() => {
silentFlag = true;
pushState({
history,
pathname: template.fill(activity.params),
state: {
activity,
step,
silent: true,
},
useHash: options.useHash,
}),
);
});
});
},
onReplaced({ effect: { activity } }) {
if (!activity.isActive) {
Expand All @@ -367,17 +361,17 @@ export function historySyncPlugin<
options.urlPatternOptions,
);

enqueue(() =>
enqueue(() => {
silentFlag = true;
replaceState({
history,
pathname: template.fill(activity.params),
state: {
activity,
silent: true,
},
useHash: options.useHash,
}),
);
});
});
},
onStepReplaced({ effect: { activity, step } }) {
if (!activity.isActive) {
Expand All @@ -389,18 +383,18 @@ export function historySyncPlugin<
options.urlPatternOptions,
);

enqueue(() =>
enqueue(() => {
silentFlag = true;
replaceState({
history,
pathname: template.fill(activity.params),
state: {
activity,
step,
silent: true,
},
useHash: options.useHash,
}),
);
});
});
},
onBeforePush({ actionParams, actions: { overrideActionParams } }) {
const template = makeTemplate(
Expand Down Expand Up @@ -451,7 +445,7 @@ export function historySyncPlugin<
for (let i = 0; i < previousActivity.steps.length - 1; i += 1) {
// eslint-disable-next-line no-loop-func
enqueue(() => {
popFlag += 1;
silentFlag = true;
history.back();
});
}
Expand All @@ -466,7 +460,7 @@ export function historySyncPlugin<

if ((currentActivity?.steps.length ?? 0) > 1) {
enqueue(() => {
popFlag += 1;
silentFlag = true;
history.back();
});
}
Expand All @@ -486,7 +480,7 @@ export function historySyncPlugin<
for (let i = 0; i < popCount; i += 1) {
// eslint-disable-next-line no-loop-func
enqueue(() => {
popFlag += 1;
silentFlag = true;
history.back();
});
}
Expand Down

1 comment on commit abd670c

@vercel
Copy link

@vercel vercel bot commented on abd670c Oct 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.