Skip to content

Commit

Permalink
refactor(router-store): use environment providers instead of providers
Browse files Browse the repository at this point in the history
  • Loading branch information
yharaskrik committed Aug 23, 2022
1 parent f862c45 commit 2ab35d4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 40 deletions.
19 changes: 0 additions & 19 deletions modules/router-store/src/init_router_store.ts

This file was deleted.

59 changes: 39 additions & 20 deletions modules/router-store/src/provide_router_store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Provider } from '@angular/core';
import { ENVIRONMENT_INITIALIZER, inject, Provider } from '@angular/core';
import {
_createRouterConfig,
_ROUTER_CONFIG,
Expand All @@ -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
Expand All @@ -32,22 +33,40 @@ import { _initRouterStore } from './init_router_store';
*/
export function provideRouterStore<
T extends BaseRouterStoreState = SerializedRouterStateSnapshot
>(config: StoreRouterConfig<T> = {}): 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<T> = {}): 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,
],
};
}
2 changes: 1 addition & 1 deletion modules/router-store/src/router_store_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class StoreRouterConnectingModule {
): ModuleWithProviders<StoreRouterConnectingModule> {
return {
ngModule: StoreRouterConnectingModule,
providers: provideRouterStore(config),
providers: [...provideRouterStore(config).ɵproviders],
};
}
}

0 comments on commit 2ab35d4

Please sign in to comment.