Skip to content

Commit 959cfac

Browse files
dummdidummbrandonroberts
authored andcommitted
feat(router-store): Add support for serializers with injected values
BREAKING CHANGE: StoreRouterConfigFunction is removed. It is no longer possible to pass a function returning a StoreRouterConfig to StoreRouterConnectingModule.forRoot If you still need this, pass a provider like this: { provide: ROUTER_CONFIG, useFactory: _createRouterConfig // you function }
1 parent 293f960 commit 959cfac

File tree

3 files changed

+20
-36
lines changed

3 files changed

+20
-36
lines changed

modules/router-store/spec/integration.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, Provider } from '@angular/core';
1+
import { Component, Provider, Injectable } from '@angular/core';
22
import { TestBed } from '@angular/core/testing';
33
import { NavigationEnd, Router, RouterStateSnapshot } from '@angular/router';
44
import { RouterTestingModule } from '@angular/router/testing';
@@ -416,7 +416,11 @@ describe('integration spec', () => {
416416
: null;
417417
};
418418

419+
@Injectable()
419420
class CustomSerializer implements RouterStateSerializer<SerializedState> {
421+
constructor(store: Store<any>) {
422+
// Requiring store to test Serializer with injected arguments works.
423+
}
420424
serialize(routerState: RouterStateSnapshot): SerializedState {
421425
const url = `${routerState.url}-custom`;
422426
const params = { test: 1 };

modules/router-store/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ export {
1919
RouterNavigatedPayload,
2020
StoreRouterConnectingModule,
2121
StoreRouterConfig,
22-
StoreRouterConfigFunction,
2322
NavigationActionTiming,
2423
ROUTER_CONFIG,
2524
DEFAULT_ROUTER_FEATURENAME,

modules/router-store/src/router_store_module.ts

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ export function routerReducer<
175175

176176
export interface StoreRouterConfig {
177177
stateKey?: string;
178-
serializer?: new () => RouterStateSerializer;
178+
serializer?: new (...args: any[]) => RouterStateSerializer;
179179
/**
180180
* By default, ROUTER_NAVIGATION is dispatched before guards and resolvers run.
181181
* Therefore, the action could run too soon, for example
@@ -200,34 +200,16 @@ export const ROUTER_CONFIG = new InjectionToken(
200200
export const DEFAULT_ROUTER_FEATURENAME = 'router';
201201

202202
export function _createRouterConfig(
203-
config: StoreRouterConfig | StoreRouterConfigFunction
203+
config: StoreRouterConfig
204204
): StoreRouterConfig {
205-
let _config: StoreRouterConfig;
206-
207-
if (typeof config === 'function') {
208-
_config = config();
209-
} else {
210-
_config = config || {};
211-
}
212-
213205
return {
214206
stateKey: DEFAULT_ROUTER_FEATURENAME,
215207
serializer: DefaultRouterStateSerializer,
216208
navigationActionTiming: NavigationActionTiming.PreActivation,
217-
..._config,
209+
...config,
218210
};
219211
}
220212

221-
export function _createSerializer(
222-
config: StoreRouterConfig
223-
): RouterStateSerializer {
224-
// This function gets handed a complete config-object from _createRouterConfig,
225-
// so we know the serializer property exists
226-
return new config.serializer!();
227-
}
228-
229-
export type StoreRouterConfigFunction = () => StoreRouterConfig;
230-
231213
enum RouterTrigger {
232214
NONE = 1,
233215
ROUTER = 2,
@@ -280,10 +262,7 @@ enum RouterTrigger {
280262
providers: [
281263
{
282264
provide: _ROUTER_CONFIG,
283-
useValue: {
284-
stateKey: DEFAULT_ROUTER_FEATURENAME,
285-
serializer: DefaultRouterStateSerializer,
286-
},
265+
useValue: {},
287266
},
288267
{
289268
provide: ROUTER_CONFIG,
@@ -292,21 +271,23 @@ enum RouterTrigger {
292271
},
293272
{
294273
provide: RouterStateSerializer,
295-
deps: [ROUTER_CONFIG],
296-
useFactory: _createSerializer,
274+
useClass: DefaultRouterStateSerializer,
297275
},
298276
],
299277
})
300278
export class StoreRouterConnectingModule {
301-
static forRoot(
302-
config?: StoreRouterConfig | StoreRouterConfigFunction
303-
): ModuleWithProviders;
304-
static forRoot(
305-
config: StoreRouterConfig | StoreRouterConfigFunction = {}
306-
): ModuleWithProviders {
279+
static forRoot(config: StoreRouterConfig = {}): ModuleWithProviders {
307280
return {
308281
ngModule: StoreRouterConnectingModule,
309-
providers: [{ provide: _ROUTER_CONFIG, useValue: config }],
282+
providers: [
283+
{ provide: _ROUTER_CONFIG, useValue: config },
284+
{
285+
provide: RouterStateSerializer,
286+
useClass: config.serializer
287+
? config.serializer
288+
: DefaultRouterStateSerializer,
289+
},
290+
],
310291
};
311292
}
312293

0 commit comments

Comments
 (0)