Skip to content

Commit 1b4c646

Browse files
fix(Effects): Export EffectsNotification interface
Also updated documentation and added to example app
1 parent f969676 commit 1b4c646

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

docs/effects/api.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ import 'rxjs/add/operator/takeUntil';
107107
import { Injectable } from '@angular/core';
108108
import { Observable } from 'rxjs/Observable';
109109
import { Action } from '@ngrx/store';
110-
import { Actions, Effect, OnRunEffects } from '@ngrx/effects';
110+
import { Actions, Effect, OnRunEffects, EffectsNotification } from '@ngrx/effects';
111111

112112
@Injectable()
113113
export class UserEffects implements OnRunEffects {
@@ -119,7 +119,7 @@ export class UserEffects implements OnRunEffects {
119119
console.log(action);
120120
});
121121

122-
ngrxOnRunEffects(resolvedEffects$: Observable<Action>) {
122+
ngrxOnRunEffects(resolvedEffects$: Observable<EffectsNotification>) {
123123
return this.actions$.ofType('LOGGED_IN')
124124
.exhaustMap(() => resolvedEffects$.takeUntil('LOGGED_OUT'));
125125
}

example-app/app/books/effects/collection.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
import 'rxjs/add/operator/map';
22
import 'rxjs/add/operator/catch';
3+
import 'rxjs/add/operator/exhaustMap';
34
import 'rxjs/add/operator/switchMap';
45
import 'rxjs/add/operator/mergeMap';
56
import 'rxjs/add/operator/toArray';
67
import { Injectable } from '@angular/core';
78
import { Action } from '@ngrx/store';
8-
import { Effect, Actions } from '@ngrx/effects';
9+
import {
10+
Effect,
11+
Actions,
12+
OnRunEffects,
13+
EffectNotification,
14+
} from '@ngrx/effects';
915
import { Database } from '@ngrx/db';
1016
import { Observable } from 'rxjs/Observable';
1117
import { defer } from 'rxjs/observable/defer';
1218
import { of } from 'rxjs/observable/of';
1319

1420
import * as collection from '../actions/collection';
21+
import * as auth from '../../auth/actions/auth';
1522
import { Book } from '../models/book';
1623

1724
@Injectable()
18-
export class CollectionEffects {
25+
export class CollectionEffects implements OnRunEffects {
1926
/**
2027
* This effect does not yield any actions back to the store. Set
2128
* `dispatch` to false to hint to @ngrx/effects that it should
@@ -65,4 +72,22 @@ export class CollectionEffects {
6572
);
6673

6774
constructor(private actions$: Actions, private db: Database) {}
75+
76+
/**
77+
* Implementing the OnRunEffects interface gives you more control
78+
* over the lifecycle of running effects. All the registered effects
79+
* in this class will only listen for actions and peform side effects
80+
* depending on provided Observable.
81+
*
82+
* The resolved effects will only listen and provide actions once the user
83+
* has logged in and will stop listening for actions once the user has logged
84+
* out.
85+
*/
86+
ngrxOnRunEffects(resolvedEffects$: Observable<EffectNotification>) {
87+
return this.actions$
88+
.ofType(auth.LOGIN_SUCCESS)
89+
.exhaustMap(() =>
90+
resolvedEffects$.takeUntil(this.actions$.ofType(auth.LOGOUT))
91+
);
92+
}
6893
}

modules/effects/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export { EffectsModule } from './effects_module';
55
export { EffectSources } from './effect_sources';
66
export { OnRunEffects } from './on_run_effects';
77
export { toPayload } from './util';
8+
export { EffectNotification } from './effect_notification';

0 commit comments

Comments
 (0)