MCPcopy Create free account
hub / github.com/ionic-team/ionic-framework / runGuards

Method runGuards

core/src/components/router/router.tsx:326–354  ·  view source on GitHub ↗

* Executes the beforeLeave hook of the source route and the beforeEnter hook of the target route if they exist. * * When the beforeLeave hook does not return true (to allow navigating) then that value is returned early and the beforeEnter is executed. * Otherwise the beforeEnterHook hook of

(
    to: string[] | null = this.getSegments(),
    from?: string[] | null
  )

Source from the content-addressed store, hash-verified

324 * Otherwise the beforeEnterHook hook of the target route is executed.
325 */
326 private async runGuards(
327 to: string[] | null = this.getSegments(),
328 from?: string[] | null
329 ): Promise<NavigationHookResult> {
330 if (from === undefined) {
331 from = parsePath(this.previousPath).segments;
332 }
333
334 if (!to || !from) {
335 return true;
336 }
337
338 const routes = readRoutes(this.el);
339
340 const fromChain = findChainForSegments(from, routes);
341 // eslint-disable-next-line @typescript-eslint/prefer-optional-chain
342 const beforeLeaveHook = fromChain && fromChain[fromChain.length - 1].beforeLeave;
343
344 const canLeave = beforeLeaveHook ? await beforeLeaveHook() : true;
345 if (canLeave === false || typeof canLeave === 'object') {
346 return canLeave;
347 }
348
349 const toChain = findChainForSegments(to, routes);
350 // eslint-disable-next-line @typescript-eslint/prefer-optional-chain
351 const beforeEnterHook = toChain && toChain[toChain.length - 1].beforeEnter;
352
353 return beforeEnterHook ? beforeEnterHook() : true;
354 }
355
356 private async writeNavState(
357 node: HTMLElement | undefined,

Callers 4

componentWillLoadMethod · 0.95
onPopStateMethod · 0.95
canTransitionMethod · 0.95
pushMethod · 0.95

Calls 4

getSegmentsMethod · 0.95
parsePathFunction · 0.90
readRoutesFunction · 0.90
findChainForSegmentsFunction · 0.90

Tested by

no test coverage detected