Skip to content

Commit

Permalink
Fix popstate event is not correctly accepted
Browse files Browse the repository at this point in the history
  • Loading branch information
ypresto committed Oct 7, 2024
1 parent 0d004a9 commit 09f9386
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/hooks/useInterceptPopState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,17 @@ function createHandlePopState(

// Wait for all callbacks to be resolved
(async () => {
let i = -1;

for (const def of defs) {
i++;

if (!def.enabled({ to, type: "popstate" })) continue;
if (DEBUG) {
console.log(
`useInterceptPopState(): confirmation for listener index ${i}`
);
}

const confirm = await def.callback({ to, type: "popstate" });
// TODO: check cancel while waiting for navigation guard
Expand All @@ -125,17 +134,21 @@ function createHandlePopState(
);
}
if (delta !== 0) {
// discard event
window.history.go(-delta);
}
return;
}
}

// accept
dispatchedState = nextState;
window.dispatchEvent(
new PopStateEvent("popstate", { state: nextState })
if (DEBUG) {
console.log(
`useInterceptPopState(): Accept popstate event, ${nextIndex}`
);
}
// accept
dispatchedState = nextState;
window.dispatchEvent(new PopStateEvent("popstate", { state: nextState }));
})();

// Return false to call stopImmediatePropagation()
Expand Down

0 comments on commit 09f9386

Please sign in to comment.