-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy patheffects_module.ts
More file actions
133 lines (125 loc) · 3.23 KB
/
effects_module.ts
File metadata and controls
133 lines (125 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import {
Injector,
ModuleWithProviders,
NgModule,
Optional,
Self,
SkipSelf,
Type,
} from '@angular/core';
import { Actions } from './actions';
import { EffectSources } from './effect_sources';
import { EffectsFeatureModule } from './effects_feature_module';
import { defaultEffectsErrorHandler } from './effects_error_handler';
import { EffectsRootModule } from './effects_root_module';
import { EffectsRunner } from './effects_runner';
import {
_FEATURE_EFFECTS,
_ROOT_EFFECTS,
_ROOT_EFFECTS_GUARD,
EFFECTS_ERROR_HANDLER,
FEATURE_EFFECTS,
ROOT_EFFECTS,
USER_PROVIDED_EFFECTS,
} from './tokens';
@NgModule({})
export class EffectsModule {
static forFeature(
featureEffects: Type<any>[] = []
): ModuleWithProviders<EffectsFeatureModule> {
return {
ngModule: EffectsFeatureModule,
providers: [
featureEffects,
{
provide: _FEATURE_EFFECTS,
multi: true,
useValue: featureEffects,
},
{
provide: USER_PROVIDED_EFFECTS,
multi: true,
useValue: [],
},
{
provide: FEATURE_EFFECTS,
multi: true,
useFactory: createEffects,
deps: [Injector, _FEATURE_EFFECTS, USER_PROVIDED_EFFECTS],
},
],
};
}
static forRoot(
rootEffects: Type<any>[] = []
): ModuleWithProviders<EffectsRootModule> {
return {
ngModule: EffectsRootModule,
providers: [
{
provide: EFFECTS_ERROR_HANDLER,
useValue: defaultEffectsErrorHandler,
},
EffectsRunner,
EffectSources,
Actions,
rootEffects,
{
provide: _ROOT_EFFECTS,
useValue: [rootEffects],
},
{
provide: _ROOT_EFFECTS_GUARD,
useFactory: _provideForRootGuard,
deps: [
[EffectsRunner, new Optional(), new SkipSelf()],
[_ROOT_EFFECTS, new Self()],
],
},
{
provide: USER_PROVIDED_EFFECTS,
multi: true,
useValue: [],
},
{
provide: ROOT_EFFECTS,
useFactory: createEffects,
deps: [Injector, _ROOT_EFFECTS, USER_PROVIDED_EFFECTS],
},
],
};
}
}
export function createEffects(
injector: Injector,
effectGroups: Type<any>[][],
userProvidedEffectGroups: Type<any>[][]
): any[] {
const mergedEffects: Type<any>[] = [];
for (let effectGroup of effectGroups) {
mergedEffects.push(...effectGroup);
}
for (let userProvidedEffectGroup of userProvidedEffectGroups) {
mergedEffects.push(...userProvidedEffectGroup);
}
return createEffectInstances(injector, mergedEffects);
}
export function createEffectInstances(
injector: Injector,
effects: Type<any>[]
): any[] {
return effects.map((effect) => injector.get(effect));
}
export function _provideForRootGuard(
runner: EffectsRunner,
rootEffects: any[][]
): any {
// check whether any effects are actually passed
const hasEffects = !(rootEffects.length === 1 && rootEffects[0].length === 0);
if (hasEffects && runner) {
throw new TypeError(
`EffectsModule.forRoot() called twice. Feature modules should use EffectsModule.forFeature() instead.`
);
}
return 'guarded';
}