Skip to content
This repository was archived by the owner on Dec 26, 2017. It is now read-only.

Commit cddb9ae

Browse files
committed
feat(EffectsSubscription): Add child subscriptions to parent
Parent feature modules using the effects subscription can now stop all child effects
1 parent d75b9d2 commit cddb9ae

2 files changed

Lines changed: 54 additions & 4 deletions

File tree

spec/effects-subscription.spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { ReflectiveInjector } from '@angular/core';
2+
import { of } from 'rxjs/observable/of';
3+
import { Effect } from '../src/effects';
4+
import { EffectsSubscription } from '../src/effects-subscription';
5+
6+
7+
describe('Effects Subscription', () => {
8+
it('should add itself to a parent subscription if one exists', () => {
9+
const observer: any = { next() { } };
10+
const root = new EffectsSubscription(observer, null, null);
11+
12+
spyOn(root, 'add');
13+
const child = new EffectsSubscription(observer, root, null);
14+
15+
expect(root.add).toHaveBeenCalledWith(child);
16+
});
17+
18+
it('should unsubscribe for all effects when destroyed', () => {
19+
const observer: any = { next() { } };
20+
const subscription = new EffectsSubscription(observer, null, null);
21+
22+
spyOn(subscription, 'unsubscribe');
23+
subscription.ngOnDestroy();
24+
25+
expect(subscription.unsubscribe).toHaveBeenCalled();
26+
});
27+
28+
it('should merge effects instances and subscribe them to the observer', () => {
29+
class Source {
30+
@Effect() a$ = of('a');
31+
@Effect() b$ = of('b');
32+
@Effect() c$ = of('c')
33+
}
34+
const instance = new Source();
35+
const observer: any = { next: jasmine.createSpy('next') };
36+
37+
const subscription = new EffectsSubscription(observer, null, [ instance ]);
38+
39+
expect(observer.next).toHaveBeenCalledTimes(3);
40+
expect(observer.next).toHaveBeenCalledWith('a');
41+
expect(observer.next).toHaveBeenCalledWith('b');
42+
expect(observer.next).toHaveBeenCalledWith('c');
43+
});
44+
});

src/effects-subscription.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { OpaqueToken, Inject, Optional, Injectable, OnDestroy } from '@angular/core';
1+
import { OpaqueToken, Inject, SkipSelf, Optional, Injectable, OnDestroy } from '@angular/core';
22
import { Store } from '@ngrx/store';
3+
import { Observer } from 'rxjs/Observer';
34
import { Subscription } from 'rxjs/Subscription';
45
import { merge } from 'rxjs/observable/merge';
56
import { mergeEffects } from './effects';
@@ -11,17 +12,22 @@ export const effects = new OpaqueToken('ngrx/effects: Effects');
1112
@Injectable()
1213
export class EffectsSubscription extends Subscription implements OnDestroy {
1314
constructor(
14-
private store: Store<any>,
15+
@Inject(Store) private store: Observer<Actions>,
16+
@Optional() @SkipSelf() public parent: EffectsSubscription,
1517
@Optional() @Inject(effects) effectInstances?: any[]
1618
) {
1719
super();
1820

19-
if (effectInstances) {
21+
if (Boolean(parent)) {
22+
parent.add(this);
23+
}
24+
25+
if (Boolean(effectInstances)) {
2026
this.addEffects(effectInstances);
2127
}
2228
}
2329

24-
addEffects(effectInstances) {
30+
addEffects(effectInstances: any[]) {
2531
const sources = effectInstances.map(mergeEffects);
2632
const merged = merge(...sources);
2733

0 commit comments

Comments
 (0)