diff --git a/.gitignore b/.gitignore index 847f939..d12644b 100644 --- a/.gitignore +++ b/.gitignore @@ -31,6 +31,6 @@ typings dist *.tgz .idea -spec/ngc/ngfactory/ -spec/ngc/output/ -release \ No newline at end of file +spec/ngc*/ngfactory/ +spec/ngc*/output/ +release diff --git a/package.json b/package.json index dba0667..f44d389 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,8 @@ "karma": "karma start --single-run", "test:unit": "npm run karma", "test:ngc": "ngc -p ./spec/ngc/tsconfig.ngc.json", - "test": "npm run test:unit && npm run test:ngc", + "test:ngc2": "ngc -p ./spec/ngc2/tsconfig.ngc.json", + "test": "npm run test:unit && npm run test:ngc && npm run test:ngc2", "clean:pre": "rimraf release", "clean:post": "rimraf \"src/**/*.ngfactory.ts\"", "copy": "cpy LICENSE package.json README.md release", diff --git a/spec/ngc2/main.ts b/spec/ngc2/main.ts new file mode 100644 index 0000000..9e63106 --- /dev/null +++ b/spec/ngc2/main.ts @@ -0,0 +1,46 @@ +import { NgModule, Component } from '@angular/core'; +import { platformDynamicServer } from '@angular/platform-server'; +import { BrowserModule } from '@angular/platform-browser'; +import { Store, StoreModule } from '../../'; +import { counterReducer, INCREMENT, DECREMENT } from '../fixtures/counter'; +import { Observable } from 'rxjs/Observable'; + +export interface AppState { + count: number; +} + +export const storeConfig = {count: counterReducer}; +export function initialState() { + return { count : 0 }; +} + +@Component({ + selector: 'ngc-spec-component', + template: ` + + Count : {{ count | async }} + + ` +}) +export class NgcSpecComponent { + count: Observable; + constructor(public store:Store){ + this.count = store.select(state => state.count); + } + increment(){ + this.store.dispatch({ type: INCREMENT }); + } + decrement(){ + this.store.dispatch({ type: DECREMENT }); + } +} + +@NgModule({ + imports: [ + BrowserModule, + StoreModule.provideStore(storeConfig, initialState) + ], + declarations: [NgcSpecComponent], + bootstrap: [NgcSpecComponent] +}) +export class NgcSpecModule {} diff --git a/spec/ngc2/tsconfig.ngc.json b/spec/ngc2/tsconfig.ngc.json new file mode 100644 index 0000000..5b15db0 --- /dev/null +++ b/spec/ngc2/tsconfig.ngc.json @@ -0,0 +1,17 @@ +{ + "compilerOptions": { + "target": "ES5", + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "module": "commonjs", + "moduleResolution": "node", + "outDir": "./output", + "lib": ["es2015", "dom"] + }, + "files": [ + "main.ts" + ], + "angularCompilerOptions": { + "genDir": "ngfactory" + } +} diff --git a/spec/store.spec.ts b/spec/store.spec.ts index 01c8c51..94903c8 100644 --- a/spec/store.spec.ts +++ b/spec/store.spec.ts @@ -19,10 +19,7 @@ interface TodoAppSchema { todos: Todo[]; } - - -describe('ngRx Store', () => { - +const testFun = (initialValue: any | (() => any)) => () => { describe('basic store actions', function() { let injector: ReflectiveInjector; @@ -36,8 +33,6 @@ describe('ngRx Store', () => { counter3: counterReducer }); - const initialValue = { counter1: 0, counter2: 1 }; - injector = ReflectiveInjector.resolveAndCreate([ StoreModule.provideStore(rootReducer, initialValue).providers ]); @@ -192,4 +187,9 @@ describe('ngRx Store', () => { expect(dispatcherSubscription.closed).toBe(false); }); }); -}); +}; + +const initialValue = { counter1: 0, counter2: 1 }; +describe('ngRx Store with initial value', testFun(initialValue)); + +describe('ngRx Store with initial function', testFun(() => initialValue)); 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)