Skip to content

Commit

Permalink
revert(Effects): dispatch init feature effects action on init #1305
Browse files Browse the repository at this point in the history
During a prod build the contructor name of an effect gets magnled,
therefore this implementation can't be used.
  • Loading branch information
timdeschryver authored and brandonroberts committed Dec 20, 2018
1 parent 6311b89 commit e9cc9ae
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 111 deletions.
25 changes: 0 additions & 25 deletions docs/effects/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,31 +50,6 @@ Usage:
export class FeatureModule {}
```

### UPDATE_EFFECTS

After feature effects are registered, an `UPDATE_EFFECTS` action is dispatched.

```ts
type UpdateEffects = {
type: typeof UPDATE_EFFECTS;
effects: string[];
};
```

For example, when you register your feature module as `EffectsModule.forFeature([SomeEffectsClass, AnotherEffectsClass])`,
it has `SomeEffectsClass` and `AnotherEffectsClass` in an array as its payload.

To dispatch an action when the `SomeEffectsClass` effect has been registered, listen to the `UPDATE_EFFECTS` action and use the `effects` payload to filter out non-important effects.

```ts
@Effect()
init = this.actions.pipe(
ofType<UpdateEffects>(UPDATE_EFFECTS)
filter(action => action.effects.includes('SomeEffectsClass')),
map(action => ...)
);
```

## Actions

Stream of all actions dispatched in your application including actions dispatched by effect streams.
Expand Down
68 changes: 7 additions & 61 deletions modules/effects/spec/effects_feature_module.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Injectable, NgModule } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { combineLatest } from 'rxjs';
import {
Action,
createFeatureSelector,
Expand All @@ -10,38 +9,23 @@ import {
StoreModule,
} from '@ngrx/store';
import { map, withLatestFrom, filter } from 'rxjs/operators';
import { Actions, Effect, EffectsModule, ofType } from '../';
import {
EffectsFeatureModule,
UPDATE_EFFECTS,
UpdateEffects,
} from '../src/effects_feature_module';
import { Actions, Effect, EffectsModule, ofType, OnInitEffects } from '../';
import { EffectsFeatureModule } from '../src/effects_feature_module';
import { EffectsRootModule } from '../src/effects_root_module';
import { FEATURE_EFFECTS } from '../src/tokens';

describe('Effects Feature Module', () => {
describe('when registered', () => {
class SourceA {}
class SourceB {}
class SourceC {}

const sourceA = new SourceA();
const sourceB = new SourceB();
const sourceC = new SourceC();
const sourceA = 'sourceA';
const sourceB = 'sourceB';
const sourceC = 'sourceC';
const effectSourceGroups = [[sourceA], [sourceB], [sourceC]];

const effectSourceGroups = [[sourceA], [sourceB, sourceC]];
let mockEffectSources: { addEffects: jasmine.Spy };
let mockStore: { dispatch: jasmine.Spy };

beforeEach(() => {
TestBed.configureTestingModule({
providers: [
{
provide: Store,
useValue: {
dispatch: jasmine.createSpy('dispatch'),
},
},
{
provide: EffectsRootModule,
useValue: {
Expand All @@ -57,7 +41,6 @@ describe('Effects Feature Module', () => {
});

mockEffectSources = TestBed.get(EffectsRootModule);
mockStore = TestBed.get(Store);
});

it('should add all effects when instantiated', () => {
Expand All @@ -67,20 +50,6 @@ describe('Effects Feature Module', () => {
expect(mockEffectSources.addEffects).toHaveBeenCalledWith(sourceB);
expect(mockEffectSources.addEffects).toHaveBeenCalledWith(sourceC);
});

it('should dispatch update-effects actions when instantiated', () => {
TestBed.get(EffectsFeatureModule);

expect(mockStore.dispatch).toHaveBeenCalledWith({
type: UPDATE_EFFECTS,
effects: ['SourceA'],
});

expect(mockStore.dispatch).toHaveBeenCalledWith({
type: UPDATE_EFFECTS,
effects: ['SourceB', 'SourceC'],
});
});
});

describe('when registered in a different NgModule from the feature state', () => {
Expand All @@ -106,12 +75,8 @@ describe('Effects Feature Module', () => {

store.dispatch(action);

combineLatest(
store.pipe(select(getDataState)),
store.pipe(select(getInitialized))
).subscribe(([data, initialized]) => {
store.pipe(select(getDataState)).subscribe(data => {
expect(data).toBe(110);
expect(initialized).toBe(true);
done();
});
});
Expand All @@ -126,22 +91,14 @@ interface State {

interface DataState {
data: number;
initialized: boolean;
}

const initialState: DataState = {
data: 100,
initialized: false,
};

function reducer(state: DataState = initialState, action: Action) {
switch (action.type) {
case 'INITIALIZE_FEATURE': {
return {
...state,
initialized: true,
};
}
case 'INCREASE':
return {
...state,
Expand All @@ -154,22 +111,11 @@ function reducer(state: DataState = initialState, action: Action) {
const getFeatureState = createFeatureSelector<DataState>(FEATURE_KEY);

const getDataState = createSelector(getFeatureState, state => state.data);
const getInitialized = createSelector(
getFeatureState,
state => state.initialized
);

@Injectable()
class FeatureEffects {
constructor(private actions: Actions, private store: Store<State>) {}

@Effect()
init = this.actions.pipe(
ofType<UpdateEffects>(UPDATE_EFFECTS),
filter(action => action.effects.includes('FeatureEffects')),
map(action => ({ type: 'INITIALIZE_FEATURE' }))
);

@Effect()
effectWithStore = this.actions.pipe(
ofType('INCREMENT'),
Expand Down
30 changes: 6 additions & 24 deletions modules/effects/src/effects_feature_module.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,20 @@
import { NgModule, Inject, Optional } from '@angular/core';
import { StoreRootModule, StoreFeatureModule, Store } from '@ngrx/store';
import { StoreRootModule, StoreFeatureModule } from '@ngrx/store';
import { EffectsRootModule } from './effects_root_module';
import { FEATURE_EFFECTS } from './tokens';
import { getSourceForInstance } from './effects_metadata';

export const UPDATE_EFFECTS = '@ngrx/effects/update-effects';
export type UpdateEffects = {
type: typeof UPDATE_EFFECTS;
effects: string[];
};

@NgModule({})
export class EffectsFeatureModule {
constructor(
root: EffectsRootModule,
store: Store<any>,
@Inject(FEATURE_EFFECTS) effectSourceGroups: any[][],
@Optional() storeRootModule: StoreRootModule,
@Optional() storeFeatureModule: StoreFeatureModule
) {
effectSourceGroups.forEach(group => {
let effectSourceNames: string[] = [];

group.forEach(effectSourceInstance => {
root.addEffects(effectSourceInstance);

const { constructor } = getSourceForInstance(effectSourceInstance);
effectSourceNames.push(constructor.name);
});

store.dispatch(<UpdateEffects>{
type: UPDATE_EFFECTS,
effects: effectSourceNames,
});
});
effectSourceGroups.forEach(group =>
group.forEach(effectSourceInstance =>
root.addEffects(effectSourceInstance)
)
);
}
}
1 change: 0 additions & 1 deletion modules/effects/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ export { EffectsModule } from './effects_module';
export { EffectSources } from './effect_sources';
export { EffectNotification } from './effect_notification';
export { ROOT_EFFECTS_INIT } from './effects_root_module';
export { UPDATE_EFFECTS, UpdateEffects } from './effects_feature_module';
export { OnIdentifyEffects, OnRunEffects } from './lifecycle_hooks';

0 comments on commit e9cc9ae

Please sign in to comment.