11import { DDGProxy , DDGReflect , isBeingFramed } from './utils.js' ;
22import ContentFeature from './content-feature.js' ;
33
4+ /**
5+ * @typedef {'push' | 'replace' | 'reload' | 'traverse' | 'unknown' } NavigationType
6+ * An enumerated value representing the type of navigation.
7+ *
8+ * Possible values:
9+ * - `'push'` - A new location is navigated to, causing a new entry to be pushed onto the history list.
10+ * - `'replace'` - The Navigation.currentEntry is replaced with a new history entry.
11+ * - `'reload'` - The Navigation.currentEntry is reloaded.
12+ * - `'traverse'` - The browser navigates from one existing history entry to another existing history entry.
13+ * - `'unknown'` - Fallback but highly unlikely. If the WeakMap lookup fails that means the navigate event wasn't captured.
14+ *
15+ * @see https://developer.mozilla.org/en-US/docs/Web/API/NavigateEvent/navigationType
16+ */
17+
18+ /**
19+ * @typedef {(navigationType: NavigationType) => void } URLChangeListener
20+ */
21+
422const urlChangeListeners = new Set ( ) ;
23+
524/**
625 * Register a listener to be called when the URL changes.
7- * @param {function } listener
26+ * @param {URLChangeListener } listener - Callback function that receives the navigation type
827 */
928export function registerForURLChanges ( listener ) {
1029 if ( urlChangeListeners . size === 0 ) {
@@ -13,6 +32,9 @@ export function registerForURLChanges(listener) {
1332 urlChangeListeners . add ( listener ) ;
1433}
1534
35+ /**
36+ * @param {NavigationType } navigationType - The type of navigation that occurred
37+ */
1638function handleURLChange ( navigationType = 'unknown' ) {
1739 for ( const listener of urlChangeListeners ) {
1840 listener ( navigationType ) ;
@@ -32,7 +54,7 @@ function listenForURLChanges() {
3254 navigations . set ( event . target , event . navigationType ) ;
3355 } ) ;
3456 globalThis . navigation . addEventListener ( 'navigatesuccess' , ( event ) => {
35- const navigationType = navigations . get ( event . target ) || 'unknown' ;
57+ const navigationType = navigations . get ( event . target ) ;
3658 handleURLChange ( navigationType ) ;
3759 navigations . delete ( event . target ) ;
3860 } ) ;
@@ -56,6 +78,6 @@ function listenForURLChanges() {
5678 historyMethodProxy . overload ( ) ;
5779 // listen for popstate events in order to run on back/forward navigations
5880 window . addEventListener ( 'popstate' , ( ) => {
59- handleURLChange ( 'popState ' ) ;
81+ handleURLChange ( 'traverse ' ) ;
6082 } ) ;
6183}
0 commit comments