From 57633d2a8fc3de6f3f7e6ec38d8c9f83d0c86f73 Mon Sep 17 00:00:00 2001 From: Brandon Date: Mon, 14 Aug 2017 21:34:49 -0500 Subject: [PATCH] fix(Store): Add type signature for metareducer (#270) Closes #264, Related to #170 --- docs/store/api.md | 6 +++--- example-app/app/reducers/index.ts | 5 +++-- modules/store/spec/utils.spec.ts | 3 ++- modules/store/src/index.ts | 1 + modules/store/src/models.ts | 6 +++++- modules/store/src/store_module.ts | 3 ++- modules/store/src/utils.ts | 9 +++++---- 7 files changed, 21 insertions(+), 12 deletions(-) diff --git a/docs/store/api.md b/docs/store/api.md index 4fee0303ba..7d7758c0ec 100644 --- a/docs/store/api.md +++ b/docs/store/api.md @@ -49,11 +49,11 @@ export function getInitialState() { configuration option to provide an array of meta-reducers that are composed from right to left. ```ts -import { StoreModule, combineReducers, compose } from '@ngrx/store'; +import { StoreModule, ActionReducer } from '@ngrx/store'; import { reducers } from './reducers'; // console.log all actions -function debug(reducer) { +export function debug(reducer: ActionReducer): ActionReducer { return function(state, action) { console.log('state', state); console.log('action', action); @@ -62,7 +62,7 @@ function debug(reducer) { } } -const metaReducers = [debug]; +export const metaReducers = [debug]; @NgModule({ imports: [ diff --git a/example-app/app/reducers/index.ts b/example-app/app/reducers/index.ts index 0833cd6774..cb26504f4a 100644 --- a/example-app/app/reducers/index.ts +++ b/example-app/app/reducers/index.ts @@ -3,6 +3,7 @@ import { createSelector, createFeatureSelector, ActionReducer, + MetaReducer, } from '@ngrx/store'; import { environment } from '../../environments/environment'; import * as fromRouter from '@ngrx/router-store'; @@ -36,7 +37,7 @@ export const reducers: ActionReducerMap = { }; // console.log all actions -export function logger(reducer: ActionReducer): ActionReducer { +export function logger(reducer: ActionReducer): ActionReducer { return function(state: State, action: any): State { console.log('state', state); console.log('action', action); @@ -50,7 +51,7 @@ export function logger(reducer: ActionReducer): ActionReducer { * the root meta-reducer. To add more meta-reducers, provide an array of meta-reducers * that will be composed to form the root meta-reducer. */ -export const metaReducers: ActionReducer[] = !environment.production +export const metaReducers: MetaReducer[] = !environment.production ? [logger] : []; diff --git a/modules/store/spec/utils.spec.ts b/modules/store/spec/utils.spec.ts index 63d6407198..a141dc5938 100644 --- a/modules/store/spec/utils.spec.ts +++ b/modules/store/spec/utils.spec.ts @@ -5,6 +5,7 @@ import { combineReducers, compose, createReducerFactory, + MetaReducer } from '@ngrx/store'; describe(`Store utils`, () => { @@ -88,7 +89,7 @@ describe(`Store utils`, () => { const initialState: FruitState = { fruit: 'apple' }; const runWithExpectations = ( - metaReducers: any[], + metaReducers: MetaReducer[], initialState: any, expectedState: any ) => () => { diff --git a/modules/store/src/index.ts b/modules/store/src/index.ts index 054c8576f6..607a423414 100644 --- a/modules/store/src/index.ts +++ b/modules/store/src/index.ts @@ -3,6 +3,7 @@ export { ActionReducer, ActionReducerMap, ActionReducerFactory, + MetaReducer, Selector, } from './models'; export { StoreModule } from './store_module'; diff --git a/modules/store/src/models.ts b/modules/store/src/models.ts index 4c5c4c324f..ef6acfbd78 100644 --- a/modules/store/src/models.ts +++ b/modules/store/src/models.ts @@ -21,12 +21,16 @@ export interface ActionReducerFactory { ): ActionReducer; } +export type MetaReducer = ( + reducer: ActionReducer +) => ActionReducer; + export interface StoreFeature { key: string; reducers: ActionReducerMap | ActionReducer; reducerFactory: ActionReducerFactory; initialState?: InitialState; - metaReducers?: ActionReducer[]; + metaReducers?: MetaReducer[]; } export interface Selector { diff --git a/modules/store/src/store_module.ts b/modules/store/src/store_module.ts index f21bb7ad51..0481dd2ad8 100644 --- a/modules/store/src/store_module.ts +++ b/modules/store/src/store_module.ts @@ -13,6 +13,7 @@ import { ActionReducerFactory, StoreFeature, InitialState, + MetaReducer, } from './models'; import { compose, combineReducers, createReducerFactory } from './utils'; import { @@ -82,7 +83,7 @@ export class StoreFeatureModule implements OnDestroy { export type StoreConfig = { initialState?: InitialState; reducerFactory?: ActionReducerFactory; - metaReducers?: ActionReducer[]; + metaReducers?: MetaReducer[]; }; @NgModule({}) diff --git a/modules/store/src/utils.ts b/modules/store/src/utils.ts index d1812a355c..77d86db5a8 100644 --- a/modules/store/src/utils.ts +++ b/modules/store/src/utils.ts @@ -3,6 +3,7 @@ import { ActionReducer, ActionReducerMap, ActionReducerFactory, + MetaReducer, } from './models'; export function combineReducers( @@ -84,10 +85,10 @@ export function compose(...functions: any[]) { }; } -export function createReducerFactory( - reducerFactory: ActionReducerFactory, - metaReducers?: ActionReducer[] -): ActionReducerFactory { +export function createReducerFactory( + reducerFactory: ActionReducerFactory, + metaReducers?: MetaReducer[] +): ActionReducerFactory { if (Array.isArray(metaReducers) && metaReducers.length > 0) { return compose(...metaReducers)(reducerFactory) as ActionReducerFactory< any,