From 6b4ad99c6e01ab3cbcd3f47bf953cc7f4483fac9 Mon Sep 17 00:00:00 2001 From: timdeschryver <28659384+timdeschryver@users.noreply.github.com> Date: Wed, 12 Dec 2018 20:30:39 +0100 Subject: [PATCH] revert(Effects): dispatch init feature effects action on init #1305 During a prod build the contructor name of an effect gets magnled, therefore this implementation can't be used. --- docs/effects/api.md | 25 ------- .../spec/effects_feature_module.spec.ts | 68 ++----------------- modules/effects/src/effects_feature_module.ts | 30 ++------ modules/effects/src/index.ts | 1 - 4 files changed, 13 insertions(+), 111 deletions(-) diff --git a/docs/effects/api.md b/docs/effects/api.md index 90be21282b..99db895942 100644 --- a/docs/effects/api.md +++ b/docs/effects/api.md @@ -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(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. diff --git a/modules/effects/spec/effects_feature_module.spec.ts b/modules/effects/spec/effects_feature_module.spec.ts index 12bb12c02e..c6c57bff58 100644 --- a/modules/effects/spec/effects_feature_module.spec.ts +++ b/modules/effects/spec/effects_feature_module.spec.ts @@ -1,6 +1,5 @@ import { Injectable, NgModule } from '@angular/core'; import { TestBed } from '@angular/core/testing'; -import { combineLatest } from 'rxjs'; import { Action, createFeatureSelector, @@ -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: { @@ -57,7 +41,6 @@ describe('Effects Feature Module', () => { }); mockEffectSources = TestBed.get(EffectsRootModule); - mockStore = TestBed.get(Store); }); it('should add all effects when instantiated', () => { @@ -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', () => { @@ -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(); }); }); @@ -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, @@ -154,22 +111,11 @@ function reducer(state: DataState = initialState, action: Action) { const getFeatureState = createFeatureSelector(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) {} - @Effect() - init = this.actions.pipe( - ofType(UPDATE_EFFECTS), - filter(action => action.effects.includes('FeatureEffects')), - map(action => ({ type: 'INITIALIZE_FEATURE' })) - ); - @Effect() effectWithStore = this.actions.pipe( ofType('INCREMENT'), diff --git a/modules/effects/src/effects_feature_module.ts b/modules/effects/src/effects_feature_module.ts index 71ca6f63ab..330e5bd861 100644 --- a/modules/effects/src/effects_feature_module.ts +++ b/modules/effects/src/effects_feature_module.ts @@ -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, @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({ - type: UPDATE_EFFECTS, - effects: effectSourceNames, - }); - }); + effectSourceGroups.forEach(group => + group.forEach(effectSourceInstance => + root.addEffects(effectSourceInstance) + ) + ); } } diff --git a/modules/effects/src/index.ts b/modules/effects/src/index.ts index ddc0431c0a..6b6b1dda8e 100644 --- a/modules/effects/src/index.ts +++ b/modules/effects/src/index.ts @@ -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';