MCPcopy Create free account
hub / github.com/angular/angular / initializeNavigationAdapter

Function initializeNavigationAdapter

adev/src/app/routing/router_providers.ts:94–147  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

92 * button or the escape key (again, on desktop).
93 */
94const initializeNavigationAdapter = () => {
95 const router = inject(Router);
96 const window = inject(WINDOW);
97 const navigation = window.navigation;
98 if (!navigation || !inject(DOCUMENT).startViewTransition) {
99 return;
100 }
101
102 let intercept = false;
103 let clearNavigation: (() => void) | undefined;
104 navigation.addEventListener('navigateerror', async () => {
105 if (!clearNavigation) {
106 return;
107 }
108 clearNavigation = undefined;
109 router.currentNavigation()?.abort();
110 });
111 navigation.addEventListener('navigate', (navigateEvent) => {
112 if (!intercept) {
113 return;
114 }
115 navigateEvent.intercept({
116 handler: () =>
117 new Promise<void>((_, reject) => {
118 clearNavigation = () => {
119 clearNavigation = undefined;
120 reject();
121 };
122 }),
123 });
124 });
125
126 merge(transitionCreated.pipe(map(() => 'viewtransition')), router.events).subscribe((e) => {
127 // Skip this for popstate/traversals that are already committed.
128 // The rollback is problematic so we only do it for navigations that
129 // defer the actual update (pushState) on the browser.
130 const currentNavigation = router.currentNavigation();
131 if (currentNavigation?.trigger === 'popstate' || currentNavigation?.extras.replaceUrl) {
132 return;
133 }
134 if (e instanceof NavigationStart) {
135 intercept = true;
136 window.history.replaceState(window.history.state, '', window.location.href);
137 intercept = false;
138 } else if (
139 // viewtransition happens before NavigateEnd
140 e === 'viewtransition' ||
141 e instanceof NavigationCancel ||
142 e instanceof NavigationError
143 ) {
144 clearNavigation?.();
145 }
146 });
147};

Callers 1

Calls 8

injectFunction · 0.90
rejectFunction · 0.85
mergeFunction · 0.85
addEventListenerMethod · 0.65
abortMethod · 0.65
interceptMethod · 0.65
subscribeMethod · 0.65
replaceStateMethod · 0.45

Tested by

no test coverage detected