@bespunky/angular-zen
Version:
The Angular tools you always wished were there.
131 lines (130 loc) • 8.05 kB
TypeScript
import { InteropObservable, Observable } from 'rxjs';
import { Type } from '@angular/core';
import { Router, ActivatedRoute, ActivatedRouteSnapshot, Event } from '@angular/router';
import { Destroyable } from '@bespunky/angular-zen/core';
import { RouterOutletComponentBus } from '../outlet/router-outlet-component-bus.service';
import * as i0 from "@angular/core";
/** Represents a function that creates an async task to be run (normally on a component). */
export declare type Resolver = (component: any, ...resolverArgs: any[]) => Observable<any> | InteropObservable<any> | Promise<any>;
/**
* The prefix of the id generated for zone macro tasks when calling `RouteAware.resolveInMacroTask()`.
*
* Generated ids will confrom to a `{prefix}-{random number}` format.
*/
export declare const ResolverMacroTaskIdPrefix = "route-aware-resolver";
/**
* Provides functionality for extending class to easily work with routes and process changes.
*
* @export
* @abstract
* @class RouteAware
* @extends {Destroyable}
*/
export declare abstract class RouteAware extends Destroyable {
protected router: Router;
protected route: ActivatedRoute;
protected componentBus?: RouterOutletComponentBus | undefined;
/**
* Creates an instance of RouteAware.
*
* @param {Router} router The instance of Angular's router service.
* @param {ActivatedRoute} route The instance of Angular's active route service.
* @param {RouterOutletComponentBus} [componentBus] (Optional) The component bus for router-x functionality.
* Provide this when you want your route-aware service to have access to the instance(s) of the activated component(s).
*/
constructor(router: Router, route: ActivatedRoute, componentBus?: RouterOutletComponentBus | undefined);
/**
* Checks if a handler method for the specific event type exists on the service and calls it.
* Handler methods should comply with `onEventType` naming (lowercase 'on', first-upper event type).
*
* @private
* @param {Event} event The event data received from the router.
*/
private dispatchRouterEvent;
/**
* Creates an observable that emits only the specified router events and is automatically destroyed when the service/component is destroyed.
*
* @protected
* @template TEvent The type of router event to emit.
* @param {Type<TEvent>} eventType The type of router event to emit.
* @param {boolean} [autoUnsubscribe=true] (Optional) `true` to make the observable complete when the service/component is destroyed; otherwise `false`. Default is `true`.
* @returns {Observable<TEvent>}
*/
protected observeRouterEvent<TEvent extends Event>(eventType: Type<TEvent>, autoUnsubscribe?: boolean): Observable<TEvent>;
/**
* Recoursively runs a processing function on the route and its children.
* Scan is done from parent to child, meaning the parent is the first to process.
*
* @ignore
* @protected
* @param {ActivatedRouteSnapshot} route The top route on which to apply the processing function.
* @param {(route: ActivatedRouteSnapshot, component: any) => boolean} process The function to run on the route and its children. The function receives a `route` argument which reflects the route being processed,
* and a `component` argument which reflects the component that was loaded for the route's outlet.
* If the corresponding outlet wasn't marked with the `publishComponent` directive, the `component` argument will be null.
*
* Returning `true` from the process function is equal to saying 'work has completed' and will stop propogation to the route's children.
* @param {number} [levels=-1] (Optional) The number of levels (excluding the parent) to dive deeper into the route tree.
* 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.
*/
protected deepScanRoute(route: ActivatedRouteSnapshot, process: (route: ActivatedRouteSnapshot, component: any) => boolean, levels?: number): void;
/**
* Recoursively runs a processing function on the route and its children.
* Scan is done from parent to child, meaning the parent is the first to process.
*
* @ignore
* @protected
* @param {ActivatedRouteSnapshot} route The top route on which to apply the processing function.
* @param {(route: ActivatedRouteSnapshot, component: any) => void} process The function to run on the route and its children. The function receives a `route` argument which reflects the route being processed,
* and a `component` argument which reflects the component that was loaded for the route's outlet.
* If the corresponding outlet wasn't marked with the `publishComponent` directive, the `component` argument will be null.
*
* Returning `true` from the process function is equal to saying 'work has completed' and will stop propogation to the route's children.
* @param {number} [levels=-1] (Optional) The number of levels (excluding the parent) to dive deeper into the route tree.
* 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.
*/
protected deepScanRoute(route: ActivatedRouteSnapshot, process: (route: ActivatedRouteSnapshot, component: any) => void, levels?: number): void;
/**
* Creates an observable that runs all the specified resolvers and concats their results as an array.
* The resolvers will be passed with the instance of the component for the currently activated route.
*
* @protected
* @param {(Resolver | Resolver[])} resolvers The resolver(s) to concat.
* @param {...any[]} resolverArgs (Optional) Any arguments to pass into the resolvers in addition to the component.
* @returns {Observable<any[]>} An array with the concatenated results of the resolvers.
*/
protected resolve(resolvers: Resolver | Resolver[], ...resolverArgs: any[]): Observable<any[]>;
/**
* Creates an observable that runs all the specified resolvers and concats their results as an array.
* The resolvers will be passed with the instance of the component for the currently activated route.
*
* **Angular Universal:**
* In SSR, the server doesn't wait for async code to complete. The result is scrapers and search engines receiving a page without resolved data,
* which is bad in case you need them to read some resolved metadata tags for example.
*
* Using `Zone` directly, this method creates a macro task and completes it when resolves are done or have errored.
* This makes the server block and wait until everything is resolved or errors before returning the rendered page.
*
* > *ℹ Make sure your resolves and process function are fast enough so that the server won't hang too much trying to render.*
*
* @see https://stackoverflow.com/a/50065783/4371525 for the discussion.
*
* @see {ResolverMacroTaskIdPrefix} if you need to identify the created macro task in your code.
*
* @protected
* @param {(Resolver | Resolver[])} resolvers The resolver(s) to concat.
* @param {...any[]} resolverArgs (Optional) Any arguments to pass into the resolvers in addition to the component.
*/
protected resolveInMacroTask(resolvers: Resolver | Resolver[], ...resolverArgs: any[]): Observable<any[]>;
/**
* The instance of the component created for the currently activated route.
* If no component bus was supplied at construction time, this will be `undefined`.
*
* @readonly
* @protected
* @type {(any | null)}
*/
protected get activatedRouteComponent(): any | null;
static ɵfac: i0.ɵɵFactoryDeclaration<RouteAware, never>;
static ɵdir: i0.ɵɵDirectiveDeclaration<RouteAware, never, never, {}, {}, never, never, false>;
static ɵprov: i0.ɵɵInjectableDeclaration<RouteAware>;
}