From 9cf8dbc9f1c0877f21245622fd6d28c256550f42 Mon Sep 17 00:00:00 2001 From: iron9light Date: Sat, 17 Sep 2016 20:35:23 +0800 Subject: [PATCH] Support factory function for _initialState parameter of provideStore ngc does not support function call now. It will be good to support passing a function parameter to provideStore. --- src/ng2.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/ng2.ts b/src/ng2.ts index 2932018..ca70b8a 100644 --- a/src/ng2.ts +++ b/src/ng2.ts @@ -23,6 +23,11 @@ export function _initialStateFactory(initialState, reducer) { if (!initialState) { return reducer(undefined, { type: Dispatcher.INIT }); } + + if (typeof initialState === 'function') { + initialState = initialState(); + } + return initialState; } @@ -41,7 +46,7 @@ export function _reducerFactory(dispatcher, reducer) { /** * @deprecated, use StoreModule.provideStore instead! */ -export function provideStore(_reducer: any, _initialState?: any): any[] { +export function provideStore(_reducer: any, _initialState?: any | (() => any)): any[] { return [ Dispatcher, { provide: Store, useFactory: _storeFactory, deps: [Dispatcher, Reducer, State] }, @@ -57,7 +62,7 @@ export function provideStore(_reducer: any, _initialState?: any): any[] { @NgModule({}) export class StoreModule { - static provideStore(_reducer: any, _initialState?:any): ModuleWithProviders { + static provideStore(_reducer: any, _initialState?: any | (() => any)): ModuleWithProviders { return { ngModule: StoreModule, providers: provideStore(_reducer, _initialState)