Skip to content

Commit

Permalink
refactor(Store): Only call createFeatureReducer when feature is regis…
Browse files Browse the repository at this point in the history
…tered (#887)
  • Loading branch information
jbedard authored and brandonroberts committed Mar 5, 2018
1 parent a5dcdb1 commit 9b02f79
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
13 changes: 7 additions & 6 deletions modules/store/spec/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { omit, createFeatureReducer } from '../src/utils';
import { omit, createFeatureReducerFactory } from '../src/utils';
import { combineReducers, compose } from '@ngrx/store';

describe(`Store utils`, () => {
Expand Down Expand Up @@ -74,17 +74,18 @@ describe(`Store utils`, () => {
});
});

describe('createFeatureReducer()', () => {
it('should compose a reducer from the provided meta reducers', () => {
describe('createFeatureReducerFactory()', () => {
it('should compose a reducer factory from the provided meta reducers', () => {
const metaReducer = jasmine
.createSpy('metaReducer')
.and.callFake(red => (s: any, a: any) => red(s, a));
const reducer = (state: any, action: any) => state;

const featureReducer = createFeatureReducer(reducer, [metaReducer]);

const featureReducerFactory = createFeatureReducerFactory([metaReducer]);
const initialState = 1;
const state = featureReducer(initialState, undefined);
const featureReducer = featureReducerFactory(reducer, initialState);

const state = featureReducer(undefined, <any>undefined);

expect(metaReducer).toHaveBeenCalled();
expect(state).toBe(initialState);
Expand Down
12 changes: 6 additions & 6 deletions modules/store/src/reducer_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import {
StoreFeature,
} from './models';
import { INITIAL_STATE, INITIAL_REDUCERS, REDUCER_FACTORY } from './tokens';
import { omit, createReducerFactory, createFeatureReducer } from './utils';
import {
omit,
createReducerFactory,
createFeatureReducerFactory,
} from './utils';
import { ActionsSubject } from './actions_subject';

export abstract class ReducerObservable extends Observable<
Expand Down Expand Up @@ -41,11 +45,7 @@ export class ReducerManager extends BehaviorSubject<ActionReducer<any, any>>
}: StoreFeature<any, any>) {
const reducer =
typeof reducers === 'function'
? (state: any, action: any) =>
createFeatureReducer(reducers, metaReducers)(
state === undefined ? initialState : state,
action
)
? createFeatureReducerFactory(metaReducers)(reducers, initialState)
: createReducerFactory(reducerFactory, metaReducers)(
reducers,
initialState
Expand Down
21 changes: 14 additions & 7 deletions modules/store/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,20 @@ export function createReducerFactory<T, V extends Action = Action>(
return reducerFactory;
}

export function createFeatureReducer<T, V extends Action = Action>(
reducer: ActionReducer<T, V>,
export function createFeatureReducerFactory<T, V extends Action = Action>(
metaReducers?: MetaReducer<T, V>[]
): ActionReducer<T, V> {
if (Array.isArray(metaReducers) && metaReducers.length > 0) {
return compose<ActionReducer<T, V>>(...metaReducers)(reducer);
}
): (reducer: ActionReducer<T, V>, initialState?: T) => ActionReducer<T, V> {
const reducerFactory =
Array.isArray(metaReducers) && metaReducers.length > 0
? compose<ActionReducer<T, V>>(...metaReducers)
: (r: ActionReducer<T, V>) => r;

return reducer;
return (reducer: ActionReducer<T, V>, initialState?: T) => {
reducer = reducerFactory(reducer);

return (state: T | undefined, action: V) => {
state = state === undefined ? initialState : state;
return reducer(state, action);
};
};
}

0 comments on commit 9b02f79

Please sign in to comment.