@bespunky/angular-zen
Version:
The Angular tools you always wished were there.
1 lines • 11.9 kB
Source Map (JSON)
{"version":3,"file":"bespunky-angular-zen-router-x-utils.mjs","sources":["../../../../libs/angular-zen/router-x/utils/src/lib/use-router-event.ts","../../../../libs/angular-zen/router-x/utils/src/lib/use-route-deep-scan.ts","../../../../libs/angular-zen/router-x/utils/src/lib/use-router-events.ts","../../../../libs/angular-zen/router-x/utils/src/lib/use-router-outlet-tracker.ts","../../../../libs/angular-zen/router-x/utils/src/index.ts","../../../../libs/angular-zen/router-x/utils/src/bespunky-angular-zen-router-x-utils.ts"],"sourcesContent":["import { filter, Observable } from 'rxjs';\nimport { inject, Type } from '@angular/core';\nimport { Router, RouterEvent } from '@angular/router';\n\n/**\n * Creates an observable which emits only the specified routing event.\n *\n * @export\n * @template T The type of router event to observe.\n * @param {Type<T>} eventType The type of router event to observe.\n * @return {Observable<T>} An observable which emits only the specified routing event.\n */\nexport function useRouterEvent<T extends RouterEvent>(eventType: Type<T>): Observable<T>\n{\n const router = inject(Router);\n\n return router.events.pipe(\n filter((event): event is T => event instanceof eventType),\n );\n}\n","import { Observable, switchMap } from 'rxjs';\nimport { inject } from '@angular/core';\nimport { ActivatedRoute, ActivatedRouteSnapshot, NavigationEnd } from '@angular/router';\nimport { RouterOutletComponentBus } from '@bespunky/angular-zen/router-x';\nimport { AnyObject } from '@bespunky/typescript-utils';\nimport { useRouterEvent } from './use-router-event';\n\nexport interface RouteProcessResult\n{\n /**\n * Used for optimizing the recursion of the deep route scanning operation by feeding it back with a 'stop' signal.\n *\n * @type {boolean}\n */\n done: boolean;\n}\n\nexport interface ObservedRouteProcessResult<T> extends RouteProcessResult\n{\n /**\n * Used for feeding a value from your process function back to the deep route scanning operation.\n * The scan operation will emit this value through the observable it returned.\n *\n * @type {T}\n */\n emit?: T;\n}\n\nexport type RouteProcessFunction<TResult extends RouteProcessResult = RouteProcessResult> = (route: ActivatedRouteSnapshot, component?: AnyObject | null) => TResult;\n\nexport type ObservedRouteProcessFunction<TResult> = RouteProcessFunction<ObservedRouteProcessResult<TResult>>;\n\n/**\n * Recoursively runs a processing function on the route and its children.\n * Scan is done from parent to child, meaning the parent is the first to process.\n *\n * @export\n * @param {(RouterOutletComponentBus | null)} componentBus The instance of the component bus.\n * @param {ActivatedRouteSnapshot} route The top route on which to apply the processing function. \n * @param {RouteProcessFunction} process The function to run on the route and its children.\n * The function receives a `route` argument which reflects the route being processed,\n * and a `component` argument which reflects the instance of the component loaded for the route's corresponsind outlet.\n * If the corresponding outlet wasn't marked with the `publishComponent` directive, the `component` argument will be null.\n * @param {number} [levels=-1] (Optional) The number of levels (excluding the parent) to dive deeper into the route tree.\n * A value of 1 for example, will process the route and its first-level children only. By default, scans all levels of the route tree.\n */\nexport function deepScanRoute(\n componentBus: RouterOutletComponentBus | null,\n route: ActivatedRouteSnapshot,\n process: RouteProcessFunction,\n levels: number = -1\n): void\n{\n // Make sure the caller wants scan to proceed, then make sure level limit wasn't reached.\n const processing = process(route, componentBus?.instance(route.outlet));\n\n // Negative values will scan all, positives will scan until reaching zero.\n const shouldScanChildren = !processing.done && levels !== 0;\n \n if (shouldScanChildren && route.children) route.children.forEach(childRoute => deepScanRoute(componentBus, childRoute, process, levels - 1));\n}\n\nfunction observeRouteDeepScan<TReturn>(route: ActivatedRouteSnapshot, componentBus: RouterOutletComponentBus | null, process: ObservedRouteProcessFunction<TReturn>, levels?: number): Observable<TReturn>\n{\n return new Observable(({ next, error, complete }) =>\n {\n const processAndEmit = (...args: Parameters<ObservedRouteProcessFunction<TReturn>>) =>\n {\n const result = process(...args);\n \n if (result.emit) next(result.emit);\n \n return result;\n };\n\n try\n {\n deepScanRoute(componentBus, route, processAndEmit, levels);\n }\n catch (e)\n {\n error(e);\n }\n finally\n {\n complete();\n }\n });\n}\n\n/**\n * Creates an observable which, upon navigation end, deep scans the activated route by running the provided\n * processing function.\n *\n * @export\n * @template TResult The type of the value emitted by the provided processing function.\n * @param {ObservedRouteProcessFunction<TResult>} process The function to run on the activated route and its children.\n * @param {number} [levels=-1] (Optional) The number of levels (excluding the parent) to dive deeper into the route tree.\n * A value of 1 for example, will process the route and its first-level children only. By default, scans all levels of the route tree.\n * @return {Observable<TResult>} An observable which, upon navigation end, deep scans the activated route by running the provided\n * processing function.\n */\nexport function useRouteDeepScan<TResult>(process: ObservedRouteProcessFunction<TResult>, levels: number = -1): Observable<TResult>\n{\n const route = inject(ActivatedRoute);\n const componentBus = inject(RouterOutletComponentBus);\n\n return useRouterEvent(NavigationEnd).pipe(\n switchMap(() => observeRouteDeepScan(route.snapshot, componentBus, process, levels))\n );\n}","import { filter, Observable } from 'rxjs';\nimport { inject, Type } from '@angular/core';\nimport { Router, RouterEvent } from '@angular/router';\n\ntype TypeInstance<T extends Type<any>> = T extends Type<infer Instance> ? Instance : never;\n\n/**\n * Creates an observable which emits only the specified router events.\n *\n * @export\n * @param {readonly [...Events]} eventTypes \n * @template Events The types of events to observe.\n * @returns {Observable<TypeInstance<Events[number]>>} An observable which emits only the specified router events.\n */\nexport function useRouterEvents<Events extends readonly Type<RouterEvent>[]>(...eventTypes: readonly [...Events]): Observable<TypeInstance<Events[number]>>\n{\n return inject(Router).events.pipe(\n filter((event): event is TypeInstance<Events[number]> => eventTypes.some(eventType => event instanceof eventType))\n );\n}","import { map, merge, Observable, shareReplay } from 'rxjs';\nimport { inject } from '@angular/core';\nimport { ActivatedRoute, ActivatedRouteSnapshot, NavigationEnd } from '@angular/router';\nimport { RouterOutletComponentBus } from '@bespunky/angular-zen/router-x';\nimport { AnyObject } from '@bespunky/typescript-utils';\nimport { useRouterEvent } from './use-router-event';\n\n/**\n * Creates an observable which emits the latest state of the published router outlets in the app.\n * The emitted state is a dictionary of outlet names and corresponding component instances.\n *\n * @see `PublishComponentDirective` for more details.\n * \n * @export\n * @return {Observable<Map<string, AnyObject | null>>} An observable which emits the latest state of the published router outlets in the app.\n */\nexport function useRouterOutletStateTracker(): Observable<Map<string, AnyObject | null>>\n{\n const componentBus = inject(RouterOutletComponentBus);\n\n return merge([componentBus.componentPublished, componentBus.componentUnpublished]).pipe(\n map(() => componentBus.outletsState),\n shareReplay(1)\n );\n}\n\nexport type ActivatedRouteWithComponent = {\n component: AnyObject | null;\n route : ActivatedRouteSnapshot;\n};\n\n/**\n * Creates an observable which emits the latest component instance for the currently activated route.\n * \n * @see `PublishComponentDirective` for more details.\n *\n * @export\n * @return {Observable<ActivatedRouteWithComponent>} An observable which emits the component instance for the currently activated route.\n */\nexport function useActivatedRouteComponent(): Observable<ActivatedRouteWithComponent>\n{\n const componentBus = inject(RouterOutletComponentBus);\n const route = inject(ActivatedRoute);\n\n return useRouterEvent(NavigationEnd).pipe(\n map(() => ({\n component: componentBus.instance(route.outlet),\n route : route.snapshot\n } as ActivatedRouteWithComponent)),\n shareReplay(1)\n );\n}\n","// export * from './lib/use-activated-route-component-resolves';\nexport * from './lib/use-route-deep-scan';\nexport * from './lib/use-router-event';\nexport * from './lib/use-router-events';\nexport * from './lib/use-router-outlet-tracker';\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;;;AAIA;;;;;;;AAOG;AACG,SAAU,cAAc,CAAwB,SAAkB,EAAA;AAEpE,IAAA,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC;AAE9B,IAAA,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,CACrB,MAAM,CAAC,CAAC,KAAK,KAAiB,KAAK,YAAY,SAAS,CAAC,CAC5D,CAAC;AACN;;ACaA;;;;;;;;;;;;;AAaG;AACa,SAAA,aAAa,CACzB,YAA6C,EAC7C,KAA6B,EAC7B,OAA6B,EAC7B,MAAiB,GAAA,CAAC,CAAC,EAAA;;AAInB,IAAA,MAAM,UAAU,GAAG,OAAO,CAAC,KAAK,EAAE,YAAY,KAAZ,IAAA,IAAA,YAAY,uBAAZ,YAAY,CAAE,QAAQ,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;;IAGxE,MAAM,kBA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