File

language/src/services/localized-route-aware.service.ts

Description

Integrates with the LanguageIntegrationService and facilitates language related work in route-aware services.

Extends

RouteAware

Index

Properties
Methods

Constructor

constructor(language: LanguageIntegrationService, router: Router, route: ActivatedRoute, componentBus?: RouterOutletComponentBus)

Creates an instance of LocalizedRouteAware.

Provide this when you want your route-aware service to have access to the instance(s) of the activated component(s).

Parameters:
Name Type Optional Description
language LanguageIntegrationService No

The instance of the language integration service.

router Router No

The instance of Angular's router service.

route ActivatedRoute No

The instance of Angular's activated route service.

componentBus RouterOutletComponentBus Yes

(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).

Methods

Protected onLanguageChanged
onLanguageChanged(lang: string)

Called when the current language used by the integrated app has changed. Override to implement.

Parameters:
Name Type Optional Description
lang string No

The language code of the new language.

Returns: void
Protected onLanguageServicesReady
onLanguageServicesReady()

Called when the app's language services have initialized and are ready for use. When language integration is disabled, or no ready observable have been provided by the app this will execute immediatelly on construction time.

Override to implement.

Returns: void
Protected deepScanRoute
deepScanRoute(route: ActivatedRouteSnapshot, process: (route: ActivatedRouteSnapshot,component: any) => void, levels: number)
Inherited from RouteAware

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.

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. 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.

Parameters:
Name Type Optional Default value Description
route ActivatedRouteSnapshot No

The top route on which to apply the processing function.

process function No

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.

levels number No -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.

Returns: void
Protected observeRouterEvent
observeRouterEvent(eventType: Type, autoUnsubscribe: boolean)
Inherited from RouteAware
Type parameters:
  • TEvent

Creates an observable that emits only the specified router events and is automatically destroyed when the service/component is destroyed.

Parameters:
Name Type Optional Default value Description
eventType Type<TEvent> No

The type of router event to emit.

autoUnsubscribe boolean No true

(Optional) true to make the observable complete when the service/component is destroyed; otherwise false. Default is true.

Returns: Observable<TEvent>
Protected resolve
resolve(resolvers: Resolver | Resolver[], ...resolverArgs: any[])
Inherited from RouteAware

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.

Parameters:
Name Type Optional Description
resolvers Resolver | Resolver[] No

The resolver(s) to concat.

resolverArgs any[] No

(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 resolveInMacroTask
resolveInMacroTask(resolvers: Resolver | Resolver[], ...resolverArgs: any[])
Inherited from RouteAware

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.

Parameters:
Name Type Optional Description
resolvers Resolver | Resolver[] No

The resolver(s) to concat.

resolverArgs any[] No

(Optional) Any arguments to pass into the resolvers in addition to the component.

Returns: Observable<any[]>
ngOnDestroy
ngOnDestroy()
Inherited from Destroyable
Returns: void
Protected subscribe
subscribe(observable: Observable, next?: (value?: T) => void, error?: (undefined) => void, complete?: () => void)
Inherited from Destroyable
Type parameters:
  • T

Subscribes to an observable and stores the subscription for automatic disposal. When ngOnDestroy() is called, all subscriptions created with this method will unsubscribe.

Parameters:
Name Type Optional Description
observable Observable<T> No

The observable to subscribe to.

next function Yes

(Optional) A callback function to execute on each emission of the observable.

error function Yes

(Optional) A callback function to execute when the observable errors.

complete function Yes

(Optional) A callback function to execute when the observable completes.

Returns: Subscription

The subscription created for the observable.

Properties

Protected Readonly destroyed
Type: Subject<void>
Default value: new Subject()
Inherited from Destroyable

Emits a value when ngOnDestroy() is called. Pipe together with takeUntil() to auto unsubscribe from your observables.

observable.pipe(takeUntil(this.destroyed)).subscribe(...);
Protected Readonly subscriptions
Type: Subscription
Default value: new Subscription()
Inherited from Destroyable

A list of all subscriptions manually added using the subscribe() method. These are automatically unsubscribed when ngOnDestroy() is called.

import { Directive, Injectable  } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';

import { RouteAware, RouterOutletComponentBus } from '@bespunky/angular-zen/router-x';
import { LanguageIntegrationService           } from './language-integration.service';

/**
 * Integrates with the `LanguageIntegrationService` and facilitates language related work in route-aware services.
 *
 * @export
 * @abstract
 * @class LocalizedRouteAware
 * @extends {RouteAware}
 */
@Directive()  // Originally this was decorated with `Directive` only so angular accepts it as base for both services and components.
@Injectable() // However, compodoc fails to collect abstract classes marked with `Directive` so I marked it as both. Tests pass, POC stackblitz doesn't show side effects.
// eslint-disable-next-line @angular-eslint/directive-class-suffix
export abstract class LocalizedRouteAware extends RouteAware
{
    /**
     * Creates an instance of LocalizedRouteAware.
     * 
     * @param {LanguageIntegrationService} language The instance of the language integration service.
     * @param {Router} router The instance of Angular's router service.
     * @param {ActivatedRoute} route The instance of Angular's activated 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(
        protected language     : LanguageIntegrationService,
                  router       : Router,
                  route        : ActivatedRoute,
                  componentBus?: RouterOutletComponentBus
    )
    {
        super(router, route, componentBus);
        
        if (this.language.enabled) this.initLanguageSupport();
    }

    private initLanguageSupport(): void
    {
        this.subscribe(this.language.ready   , this.onLanguageServicesReady.bind(this));
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        this.subscribe(this.language.changed!, this.onLanguageChanged.bind(this));
    }
    
    /**
     * Called when the app's language services have initialized and are ready for use.
     * When language integration is disabled, or no ready observable have been provided by the app
     * this will execute immediatelly on construction time.
     * 
     * Override to implement.
     *
     * @virtual
     * @protected
     */
    protected onLanguageServicesReady(): void { void 0; }

    /**
     * Called when the current language used by the integrated app has changed. Override to implement.
     * 
     * @virtual
     * @protected
     * @param {*} lang The language code of the new language.
     */
    protected onLanguageChanged(lang: string): void { void 0; }
}

results matching ""

    No results matching ""