Skip to content

Commit e9cc9ae

Browse files
timdeschryverbrandonroberts
authored andcommitted
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.
1 parent 6311b89 commit e9cc9ae

File tree

4 files changed

+13
-111
lines changed

4 files changed

+13
-111
lines changed

docs/effects/api.md

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,31 +50,6 @@ Usage:
5050
export class FeatureModule {}
5151
```
5252

53-
### UPDATE_EFFECTS
54-
55-
After feature effects are registered, an `UPDATE_EFFECTS` action is dispatched.
56-
57-
```ts
58-
type UpdateEffects = {
59-
type: typeof UPDATE_EFFECTS;
60-
effects: string[];
61-
};
62-
```
63-
64-
For example, when you register your feature module as `EffectsModule.forFeature([SomeEffectsClass, AnotherEffectsClass])`,
65-
it has `SomeEffectsClass` and `AnotherEffectsClass` in an array as its payload.
66-
67-
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.
68-
69-
```ts
70-
@Effect()
71-
init = this.actions.pipe(
72-
ofType<UpdateEffects>(UPDATE_EFFECTS)
73-
filter(action => action.effects.includes('SomeEffectsClass')),
74-
map(action => ...)
75-
);
76-
```
77-
7853
## Actions
7954

8055
Stream of all actions dispatched in your application including actions dispatched by effect streams.

modules/effects/spec/effects_feature_module.spec.ts

Lines changed: 7 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Injectable, NgModule } from '@angular/core';
22
import { TestBed } from '@angular/core/testing';
3-
import { combineLatest } from 'rxjs';
43
import {
54
Action,
65
createFeatureSelector,
@@ -10,38 +9,23 @@ import {
109
StoreModule,
1110
} from '@ngrx/store';
1211
import { map, withLatestFrom, filter } from 'rxjs/operators';
13-
import { Actions, Effect, EffectsModule, ofType } from '../';
14-
import {
15-
EffectsFeatureModule,
16-
UPDATE_EFFECTS,
17-
UpdateEffects,
18-
} from '../src/effects_feature_module';
12+
import { Actions, Effect, EffectsModule, ofType, OnInitEffects } from '../';
13+
import { EffectsFeatureModule } from '../src/effects_feature_module';
1914
import { EffectsRootModule } from '../src/effects_root_module';
2015
import { FEATURE_EFFECTS } from '../src/tokens';
2116

2217
describe('Effects Feature Module', () => {
2318
describe('when registered', () => {
24-
class SourceA {}
25-
class SourceB {}
26-
class SourceC {}
27-
28-
const sourceA = new SourceA();
29-
const sourceB = new SourceB();
30-
const sourceC = new SourceC();
19+
const sourceA = 'sourceA';
20+
const sourceB = 'sourceB';
21+
const sourceC = 'sourceC';
22+
const effectSourceGroups = [[sourceA], [sourceB], [sourceC]];
3123

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

3626
beforeEach(() => {
3727
TestBed.configureTestingModule({
3828
providers: [
39-
{
40-
provide: Store,
41-
useValue: {
42-
dispatch: jasmine.createSpy('dispatch'),
43-
},
44-
},
4529
{
4630
provide: EffectsRootModule,
4731
useValue: {
@@ -57,7 +41,6 @@ describe('Effects Feature Module', () => {
5741
});
5842

5943
mockEffectSources = TestBed.get(EffectsRootModule);
60-
mockStore = TestBed.get(Store);
6144
});
6245

6346
it('should add all effects when instantiated', () => {
@@ -67,20 +50,6 @@ describe('Effects Feature Module', () => {
6750
expect(mockEffectSources.addEffects).toHaveBeenCalledWith(sourceB);
6851
expect(mockEffectSources.addEffects).toHaveBeenCalledWith(sourceC);
6952
});
70-
71-
it('should dispatch update-effects actions when instantiated', () => {
72-
TestBed.get(EffectsFeatureModule);
73-
74-
expect(mockStore.dispatch).toHaveBeenCalledWith({
75-
type: UPDATE_EFFECTS,
76-
effects: ['SourceA'],
77-
});
78-
79-
expect(mockStore.dispatch).toHaveBeenCalledWith({
80-
type: UPDATE_EFFECTS,
81-
effects: ['SourceB', 'SourceC'],
82-
});
83-
});
8453
});
8554

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

10776
store.dispatch(action);
10877

109-
combineLatest(
110-
store.pipe(select(getDataState)),
111-
store.pipe(select(getInitialized))
112-
).subscribe(([data, initialized]) => {
78+
store.pipe(select(getDataState)).subscribe(data => {
11379
expect(data).toBe(110);
114-
expect(initialized).toBe(true);
11580
done();
11681
});
11782
});
@@ -126,22 +91,14 @@ interface State {
12691

12792
interface DataState {
12893
data: number;
129-
initialized: boolean;
13094
}
13195

13296
const initialState: DataState = {
13397
data: 100,
134-
initialized: false,
13598
};
13699

137100
function reducer(state: DataState = initialState, action: Action) {
138101
switch (action.type) {
139-
case 'INITIALIZE_FEATURE': {
140-
return {
141-
...state,
142-
initialized: true,
143-
};
144-
}
145102
case 'INCREASE':
146103
return {
147104
...state,
@@ -154,22 +111,11 @@ function reducer(state: DataState = initialState, action: Action) {
154111
const getFeatureState = createFeatureSelector<DataState>(FEATURE_KEY);
155112

156113
const getDataState = createSelector(getFeatureState, state => state.data);
157-
const getInitialized = createSelector(
158-
getFeatureState,
159-
state => state.initialized
160-
);
161114

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

166-
@Effect()
167-
init = this.actions.pipe(
168-
ofType<UpdateEffects>(UPDATE_EFFECTS),
169-
filter(action => action.effects.includes('FeatureEffects')),
170-
map(action => ({ type: 'INITIALIZE_FEATURE' }))
171-
);
172-
173119
@Effect()
174120
effectWithStore = this.actions.pipe(
175121
ofType('INCREMENT'),
Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,20 @@
11
import { NgModule, Inject, Optional } from '@angular/core';
2-
import { StoreRootModule, StoreFeatureModule, Store } from '@ngrx/store';
2+
import { StoreRootModule, StoreFeatureModule } from '@ngrx/store';
33
import { EffectsRootModule } from './effects_root_module';
44
import { FEATURE_EFFECTS } from './tokens';
5-
import { getSourceForInstance } from './effects_metadata';
6-
7-
export const UPDATE_EFFECTS = '@ngrx/effects/update-effects';
8-
export type UpdateEffects = {
9-
type: typeof UPDATE_EFFECTS;
10-
effects: string[];
11-
};
125

136
@NgModule({})
147
export class EffectsFeatureModule {
158
constructor(
169
root: EffectsRootModule,
17-
store: Store<any>,
1810
@Inject(FEATURE_EFFECTS) effectSourceGroups: any[][],
1911
@Optional() storeRootModule: StoreRootModule,
2012
@Optional() storeFeatureModule: StoreFeatureModule
2113
) {
22-
effectSourceGroups.forEach(group => {
23-
let effectSourceNames: string[] = [];
24-
25-
group.forEach(effectSourceInstance => {
26-
root.addEffects(effectSourceInstance);
27-
28-
const { constructor } = getSourceForInstance(effectSourceInstance);
29-
effectSourceNames.push(constructor.name);
30-
});
31-
32-
store.dispatch(<UpdateEffects>{
33-
type: UPDATE_EFFECTS,
34-
effects: effectSourceNames,
35-
});
36-
});
14+
effectSourceGroups.forEach(group =>
15+
group.forEach(effectSourceInstance =>
16+
root.addEffects(effectSourceInstance)
17+
)
18+
);
3719
}
3820
}

modules/effects/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@ export { EffectsModule } from './effects_module';
99
export { EffectSources } from './effect_sources';
1010
export { EffectNotification } from './effect_notification';
1111
export { ROOT_EFFECTS_INIT } from './effects_root_module';
12-
export { UPDATE_EFFECTS, UpdateEffects } from './effects_feature_module';
1312
export { OnIdentifyEffects, OnRunEffects } from './lifecycle_hooks';

0 commit comments

Comments
 (0)