Skip to content
This repository has been archived by the owner on Jan 10, 2018. It is now read-only.

Commit

Permalink
Support factory function for _initialState parameter of provideStore
Browse files Browse the repository at this point in the history
ngc does not support function call now. It will be good to support passing a function parameter to provideStore.
  • Loading branch information
iron9light committed Sep 19, 2016
1 parent f04c544 commit 181f7be
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 13 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ typings
dist
*.tgz
.idea
spec/ngc/ngfactory/
spec/ngc/output/
release
spec/ngc*/ngfactory/
spec/ngc*/output/
release
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
46 changes: 46 additions & 0 deletions spec/ngc2/main.ts
Original file line number Diff line number Diff line change
@@ -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: `
<button (click)="increment()"> + </button>
<span> Count : {{ count | async }} </span>
<button (click)="decrement()"> + </button>
`
})
export class NgcSpecComponent {
count: Observable<number>;
constructor(public store:Store<AppState>){
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 {}
17 changes: 17 additions & 0 deletions spec/ngc2/tsconfig.ngc.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
14 changes: 7 additions & 7 deletions spec/store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ interface TodoAppSchema {
todos: Todo[];
}



describe('ngRx Store', () => {

const testFun = (initialValue: any | (() => any)) => () => {
describe('basic store actions', function() {

let injector: ReflectiveInjector;
Expand All @@ -36,8 +33,6 @@ describe('ngRx Store', () => {
counter3: counterReducer
});

const initialValue = { counter1: 0, counter2: 1 };

injector = ReflectiveInjector.resolveAndCreate([
StoreModule.provideStore(rootReducer, initialValue).providers
]);
Expand Down Expand Up @@ -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));
9 changes: 7 additions & 2 deletions src/ng2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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] },
Expand All @@ -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)
Expand Down

0 comments on commit 181f7be

Please sign in to comment.