UNPKG

@nent/core

Version:

Functional elements to add routing, data-binding, dynamic HTML, declarative actions, audio, video, and so much more. Supercharge static HTML files into web apps without script or builds.

51 lines (50 loc) 1.71 kB
/*! * NENT 2022 */ /* istanbul ignore file */ import { forceUpdate } from '@stencil/core'; import { routingState, onRoutingChange, } from './state'; /* It subscribes to changes in the routing state and updates the component when the setting changes */ export class RouteStateSubscriber { /** * If the router is already set, subscribe to the router events. Otherwise, wait for the router to be * set and then subscribe to the router events * @param {any} component - any - The component that you want to subscribe to. * @param setting - keyof RoutingStateModel * @param [settingFunction] - This is a function that will be called when the setting is changed. */ constructor(component, setting, settingFunction) { this.component = component; this.setting = setting; this.settingFunction = settingFunction; if (routingState.router) { this.subscribeToEvents(); } else { this.stateSubscription = onRoutingChange('router', router => { var _a; if (router) { this.subscribeToEvents(); } else { (_a = this.subscription) === null || _a === void 0 ? void 0 : _a.call(this); } }); } } subscribeToEvents() { this.subscription = onRoutingChange(this.setting, value => { var _a; (_a = this.settingFunction) === null || _a === void 0 ? void 0 : _a.call(this.component, value); forceUpdate(this.component); }); } /** * It unsubscribes from the observable. */ destroy() { var _a, _b; (_a = this.subscription) === null || _a === void 0 ? void 0 : _a.call(this); (_b = this.stateSubscription) === null || _b === void 0 ? void 0 : _b.call(this); } }