1
1
import { DDGProxy , DDGReflect , isBeingFramed } from './utils.js' ;
2
2
import ContentFeature from './content-feature.js' ;
3
3
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
+
4
22
const urlChangeListeners = new Set ( ) ;
23
+
5
24
/**
6
25
* 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
8
27
*/
9
28
export function registerForURLChanges ( listener ) {
10
29
if ( urlChangeListeners . size === 0 ) {
@@ -13,6 +32,9 @@ export function registerForURLChanges(listener) {
13
32
urlChangeListeners . add ( listener ) ;
14
33
}
15
34
35
+ /**
36
+ * @param {NavigationType } navigationType - The type of navigation that occurred
37
+ */
16
38
function handleURLChange ( navigationType = 'unknown' ) {
17
39
for ( const listener of urlChangeListeners ) {
18
40
listener ( navigationType ) ;
@@ -32,7 +54,7 @@ function listenForURLChanges() {
32
54
navigations . set ( event . target , event . navigationType ) ;
33
55
} ) ;
34
56
globalThis . navigation . addEventListener ( 'navigatesuccess' , ( event ) => {
35
- const navigationType = navigations . get ( event . target ) || 'unknown' ;
57
+ const navigationType = navigations . get ( event . target ) ;
36
58
handleURLChange ( navigationType ) ;
37
59
navigations . delete ( event . target ) ;
38
60
} ) ;
@@ -56,6 +78,6 @@ function listenForURLChanges() {
56
78
historyMethodProxy . overload ( ) ;
57
79
// listen for popstate events in order to run on back/forward navigations
58
80
window . addEventListener ( 'popstate' , ( ) => {
59
- handleURLChange ( 'popState ' ) ;
81
+ handleURLChange ( 'traverse ' ) ;
60
82
} ) ;
61
83
}
0 commit comments