From 2ab35d419b9f664587957b8b1e26de0be06c931b Mon Sep 17 00:00:00 2001 From: jaybell Date: Tue, 23 Aug 2022 10:55:29 -0700 Subject: [PATCH] refactor(router-store): use environment providers instead of providers --- modules/router-store/src/init_router_store.ts | 19 ------ .../router-store/src/provide_router_store.ts | 59 ++++++++++++------- .../router-store/src/router_store_module.ts | 2 +- 3 files changed, 40 insertions(+), 40 deletions(-) delete mode 100644 modules/router-store/src/init_router_store.ts diff --git a/modules/router-store/src/init_router_store.ts b/modules/router-store/src/init_router_store.ts deleted file mode 100644 index 74cf2ef9d6..0000000000 --- a/modules/router-store/src/init_router_store.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { ENVIRONMENT_INITIALIZER, inject, Provider } from '@angular/core'; -import { StoreRouterConnectingService } from './store_router_connecting.service'; - -/** - * Initializer for the router-store that ensures the initialization logic is run alongside environment - * initialization. - */ -export const _initRouterStore: Provider[] = [ - { - provide: ENVIRONMENT_INITIALIZER, - multi: true, - useFactory() { - return () => { - inject(StoreRouterConnectingService); - }; - }, - }, - StoreRouterConnectingService, -]; diff --git a/modules/router-store/src/provide_router_store.ts b/modules/router-store/src/provide_router_store.ts index 2683fd0877..aad6ad6d68 100644 --- a/modules/router-store/src/provide_router_store.ts +++ b/modules/router-store/src/provide_router_store.ts @@ -1,4 +1,4 @@ -import { Provider } from '@angular/core'; +import { ENVIRONMENT_INITIALIZER, inject, Provider } from '@angular/core'; import { _createRouterConfig, _ROUTER_CONFIG, @@ -15,7 +15,8 @@ import { BaseRouterStoreState, RouterStateSerializer, } from './serializers/base'; -import { _initRouterStore } from './init_router_store'; +import { StoreRouterConnectingService } from './store_router_connecting.service'; +import { EnvironmentProviders } from '@ngrx/store'; /** * Provider function to connect the RouterModule to the StoreModule @@ -32,22 +33,40 @@ import { _initRouterStore } from './init_router_store'; */ export function provideRouterStore< T extends BaseRouterStoreState = SerializedRouterStateSnapshot ->(config: StoreRouterConfig = {}): Provider[] { - return [ - { provide: _ROUTER_CONFIG, useValue: config }, - { - provide: ROUTER_CONFIG, - useFactory: _createRouterConfig, - deps: [_ROUTER_CONFIG], - }, - { - provide: RouterStateSerializer, - useClass: config.serializer - ? config.serializer - : config.routerState === RouterState.Full - ? FullRouterStateSerializer - : MinimalRouterStateSerializer, - }, - _initRouterStore, - ]; +>(config: StoreRouterConfig = {}): EnvironmentProviders { + return { + ɵproviders: [ + { provide: _ROUTER_CONFIG, useValue: config }, + { + provide: ROUTER_CONFIG, + useFactory: _createRouterConfig, + deps: [_ROUTER_CONFIG], + }, + { + provide: RouterStateSerializer, + useClass: config.serializer + ? config.serializer + : config.routerState === RouterState.Full + ? FullRouterStateSerializer + : MinimalRouterStateSerializer, + }, + { + provide: ENVIRONMENT_INITIALIZER, + multi: true, + useFactory() { + return () => inject(StoreRouterConnectingService); + }, + }, + { + provide: ENVIRONMENT_INITIALIZER, + multi: true, + useFactory() { + return () => { + inject(StoreRouterConnectingService); + }; + }, + }, + StoreRouterConnectingService, + ], + }; } diff --git a/modules/router-store/src/router_store_module.ts b/modules/router-store/src/router_store_module.ts index 5abdb01514..a24e53844e 100644 --- a/modules/router-store/src/router_store_module.ts +++ b/modules/router-store/src/router_store_module.ts @@ -55,7 +55,7 @@ export class StoreRouterConnectingModule { ): ModuleWithProviders { return { ngModule: StoreRouterConnectingModule, - providers: provideRouterStore(config), + providers: [...provideRouterStore(config).ɵproviders], }; } }