UNPKG

@datorama/akita-ng-router-store

Version:

Bindings to connect the Angular Router with Akita store

277 lines (270 loc) 11.2 kB
import * as i0 from '@angular/core'; import { Injectable, NgModule } from '@angular/core'; import { __decorate, __metadata } from 'tslib'; import * as i3 from '@angular/router'; import { RoutesRecognized, GuardsCheckEnd, ResolveEnd, NavigationCancel, NavigationError, NavigationEnd } from '@angular/router'; import { Store, StoreConfig, filterNilValue, Query, setSkipAction, action } from '@datorama/akita'; import { map, Subject, distinctUntilChanged, pluck, combineLatest } from 'rxjs'; function createInitialRouterState() { return { state: null, navigationId: null }; } let RouterStore = class RouterStore extends Store { constructor() { super(createInitialRouterState()); } }; RouterStore.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: RouterStore, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); RouterStore.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: RouterStore, providedIn: 'root' }); RouterStore = __decorate([ StoreConfig({ name: 'router' }), __metadata("design:paramtypes", []) ], RouterStore); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: RouterStore, decorators: [{ type: Injectable, args: [{ providedIn: 'root' }] }], ctorParameters: function () { return []; } }); function slice(section) { return (source) => { return source.pipe(map((data) => data.state)).pipe(filterNilValue(), map((state) => state[section])); }; } class RouterQuery extends Query { constructor(store) { super(store); this.store = store; this.__navigationCancel = new Subject(); this.__navigationError = new Subject(); } selectParams(names) { if (names === undefined) { return this.select().pipe(slice('params'), distinctUntilChanged()); } const select = (p) => this.select().pipe(slice('params'), pluck(p), distinctUntilChanged()); if (Array.isArray(names)) { const sources = names.map(select); return combineLatest(sources); } return select(names).pipe(distinctUntilChanged()); } getParams(name) { if (this.getValue().state) { const params = this.getValue().state.params; if (name === undefined) { return params; } return params[name]; } return null; } selectQueryParams(names) { if (names === undefined) { return this.select().pipe(slice('queryParams'), distinctUntilChanged()); } const select = (p) => this.select().pipe(slice('queryParams'), pluck(p), distinctUntilChanged()); if (Array.isArray(names)) { const sources = names.map(select); return combineLatest(sources); } return select(names); } getQueryParams(name) { if (this.getValue().state) { const params = this.getValue().state.queryParams; if (name === undefined) { return params; } return params[name]; } return null; } selectFragment() { return this.select().pipe(slice('fragment'), distinctUntilChanged()); } getFragment() { if (this.getValue().state) { return this.getValue().state.fragment; } return null; } selectData(name) { if (name === undefined) { return this.select().pipe(slice('data'), distinctUntilChanged()); } return this.select().pipe(slice('data'), pluck(name), distinctUntilChanged()); } getData(name) { if (this.getValue().state) { const data = this.getValue().state.data; if (name === undefined) { return data; } return data[name]; } return null; } selectNavigationExtras(name) { if (name === undefined) { return this.select().pipe(slice('navigationExtras'), distinctUntilChanged()); } return this.select().pipe(slice('data'), pluck(name), distinctUntilChanged()); } getNavigationExtras(name) { if (this.getValue().state) { const data = this.getValue().state.navigationExtras; if (name === undefined) { return data; } return data[name]; } return null; } selectNavigationCancel() { return this.__navigationCancel.asObservable(); } selectNavigationError() { return this.__navigationError.asObservable(); } } RouterQuery.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: RouterQuery, deps: [{ token: RouterStore }], target: i0.ɵɵFactoryTarget.Injectable }); RouterQuery.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: RouterQuery, providedIn: 'root' }); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: RouterQuery, decorators: [{ type: Injectable, args: [{ providedIn: 'root', }] }], ctorParameters: function () { return [{ type: RouterStore }]; } }); class RouterService { constructor(routerStore, routerQuery, router) { this.routerStore = routerStore; this.routerQuery = routerQuery; this.router = router; this.dispatchTriggeredByRouter = false; this.navigationTriggeredByDispatch = false; } dispatchRouterCancel(event) { this.update({ navigationId: event.id }); this.routerQuery.__navigationCancel.next(event); } dispatchRouterError(event) { this.update({ navigationId: event.id }); this.routerQuery.__navigationError.next(event); } dispatchRouterSuccess() { this.update(this.lastRouterState); } init() { this.setUpStoreListener(); this.setUpStateRollbackEvents(); } update(routerState) { this.dispatchTriggeredByRouter = true; this.routerStore.update((state) => { return { ...state, ...routerState, }; }); this.dispatchTriggeredByRouter = false; this.navigationTriggeredByDispatch = false; } setUpStoreListener() { this.routerQuery .select((state) => state) .subscribe((s) => { this.lastRouterState = s; this.navigateIfNeeded(); }); } navigateIfNeeded() { if (!this.lastRouterState || !this.lastRouterState.state || this.dispatchTriggeredByRouter) { return; } if (this.router.url !== this.lastRouterState.state.url) { this.navigationTriggeredByDispatch = true; setSkipAction(); this.router.navigateByUrl(this.lastRouterState.state.url); } } setUpStateRollbackEvents() { this.router.events.subscribe((e) => { if (e instanceof RoutesRecognized || e instanceof GuardsCheckEnd || e instanceof ResolveEnd) { this.lastRouterState = this.serializeRoute(e); } else if (e instanceof NavigationCancel) { this.dispatchRouterCancel(e); } else if (e instanceof NavigationError) { this.dispatchRouterError(e); } else if (e instanceof NavigationEnd && !this.navigationTriggeredByDispatch) { this.dispatchRouterSuccess(); } }); } serializeRoute(navigationEvent) { let state = navigationEvent.state.root; while (state.firstChild) { state = state.firstChild; } const { params, data, queryParams, fragment } = state; return { navigationId: navigationEvent.id, state: { url: navigationEvent.url, urlAfterRedirects: navigationEvent.urlAfterRedirects, params, queryParams, fragment, data, navigationExtras: this.router.getCurrentNavigation().extras ? this.router.getCurrentNavigation().extras.state : {}, }, }; } } RouterService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: RouterService, deps: [{ token: RouterStore }, { token: RouterQuery }, { token: i3.Router }], target: i0.ɵɵFactoryTarget.Injectable }); RouterService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: RouterService, providedIn: 'root' }); __decorate([ action('Navigation Cancelled'), __metadata("design:type", Function), __metadata("design:paramtypes", [NavigationCancel]), __metadata("design:returntype", void 0) ], RouterService.prototype, "dispatchRouterCancel", null); __decorate([ action('Navigation Error'), __metadata("design:type", Function), __metadata("design:paramtypes", [NavigationError]), __metadata("design:returntype", void 0) ], RouterService.prototype, "dispatchRouterError", null); __decorate([ action('Navigation Succeeded'), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], RouterService.prototype, "dispatchRouterSuccess", null); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: RouterService, decorators: [{ type: Injectable, args: [{ providedIn: 'root', }] }], ctorParameters: function () { return [{ type: RouterStore }, { type: RouterQuery }, { type: i3.Router }]; }, propDecorators: { dispatchRouterCancel: [], dispatchRouterError: [], dispatchRouterSuccess: [] } }); class AkitaNgRouterStoreModule { constructor(routerService) { this.routerService = routerService; this.routerService.init(); } } AkitaNgRouterStoreModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: AkitaNgRouterStoreModule, deps: [{ token: RouterService }], target: i0.ɵɵFactoryTarget.NgModule }); AkitaNgRouterStoreModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "15.0.4", ngImport: i0, type: AkitaNgRouterStoreModule }); AkitaNgRouterStoreModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: AkitaNgRouterStoreModule }); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: AkitaNgRouterStoreModule, decorators: [{ type: NgModule }], ctorParameters: function () { return [{ type: RouterService }]; } }); /** * Generated bundle index. Do not edit. */ export { AkitaNgRouterStoreModule, RouterQuery, RouterService, RouterStore, createInitialRouterState }; //# sourceMappingURL=datorama-akita-ng-router-store.mjs.map //# sourceMappingURL=datorama-akita-ng-router-store.mjs.map