Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Store): Add type signature for metareducer #270

Merged
merged 1 commit into from
Aug 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/store/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>): ActionReducer<any> {
return function(state, action) {
console.log('state', state);
console.log('action', action);
Expand All @@ -62,7 +62,7 @@ function debug(reducer) {
}
}

const metaReducers = [debug];
export const metaReducers = [debug];

@NgModule({
imports: [
Expand Down
5 changes: 3 additions & 2 deletions example-app/app/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
createSelector,
createFeatureSelector,
ActionReducer,
MetaReducer,
} from '@ngrx/store';
import { environment } from '../../environments/environment';
import * as fromRouter from '@ngrx/router-store';
Expand Down Expand Up @@ -36,7 +37,7 @@ export const reducers: ActionReducerMap<State> = {
};

// console.log all actions
export function logger(reducer: ActionReducer<State>): ActionReducer<any, any> {
export function logger(reducer: ActionReducer<State>): ActionReducer<State> {
return function(state: State, action: any): State {
console.log('state', state);
console.log('action', action);
Expand All @@ -50,7 +51,7 @@ export function logger(reducer: ActionReducer<State>): ActionReducer<any, any> {
* 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<any, any>[] = !environment.production
export const metaReducers: MetaReducer<State>[] = !environment.production
? [logger]
: [];

Expand Down
3 changes: 2 additions & 1 deletion modules/store/spec/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
combineReducers,
compose,
createReducerFactory,
MetaReducer
} from '@ngrx/store';

describe(`Store utils`, () => {
Expand Down Expand Up @@ -88,7 +89,7 @@ describe(`Store utils`, () => {
const initialState: FruitState = { fruit: 'apple' };

const runWithExpectations = (
metaReducers: any[],
metaReducers: MetaReducer<FruitState>[],
initialState: any,
expectedState: any
) => () => {
Expand Down
1 change: 1 addition & 0 deletions modules/store/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export {
ActionReducer,
ActionReducerMap,
ActionReducerFactory,
MetaReducer,
Selector,
} from './models';
export { StoreModule } from './store_module';
Expand Down
6 changes: 5 additions & 1 deletion modules/store/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ export interface ActionReducerFactory<T, V extends Action = Action> {
): ActionReducer<T, V>;
}

export type MetaReducer<T, V extends Action = Action> = (
reducer: ActionReducer<T, V>
) => ActionReducer<T, V>;

export interface StoreFeature<T, V extends Action = Action> {
key: string;
reducers: ActionReducerMap<T, V> | ActionReducer<T, V>;
reducerFactory: ActionReducerFactory<T, V>;
initialState?: InitialState<T>;
metaReducers?: ActionReducer<any, any>[];
metaReducers?: MetaReducer<T, V>[];
}

export interface Selector<T, V> {
Expand Down
3 changes: 2 additions & 1 deletion modules/store/src/store_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
ActionReducerFactory,
StoreFeature,
InitialState,
MetaReducer,
} from './models';
import { compose, combineReducers, createReducerFactory } from './utils';
import {
Expand Down Expand Up @@ -82,7 +83,7 @@ export class StoreFeatureModule implements OnDestroy {
export type StoreConfig<T, V extends Action = Action> = {
initialState?: InitialState<T>;
reducerFactory?: ActionReducerFactory<T, V>;
metaReducers?: ActionReducer<T, V>[];
metaReducers?: MetaReducer<T, V>[];
};

@NgModule({})
Expand Down
9 changes: 5 additions & 4 deletions modules/store/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ActionReducer,
ActionReducerMap,
ActionReducerFactory,
MetaReducer,
} from './models';

export function combineReducers<T, V extends Action = Action>(
Expand Down Expand Up @@ -84,10 +85,10 @@ export function compose(...functions: any[]) {
};
}

export function createReducerFactory(
reducerFactory: ActionReducerFactory<any, any>,
metaReducers?: ActionReducer<any, any>[]
): ActionReducerFactory<any, any> {
export function createReducerFactory<T, V extends Action = Action>(
reducerFactory: ActionReducerFactory<T, V>,
metaReducers?: MetaReducer<T, V>[]
): ActionReducerFactory<T, V> {
if (Array.isArray(metaReducers) && metaReducers.length > 0) {
return compose(...metaReducers)(reducerFactory) as ActionReducerFactory<
any,
Expand Down