|
1 | 1 | import 'rxjs/add/operator/map'; |
2 | 2 | import 'rxjs/add/operator/catch'; |
| 3 | +import 'rxjs/add/operator/exhaustMap'; |
3 | 4 | import 'rxjs/add/operator/switchMap'; |
4 | 5 | import 'rxjs/add/operator/mergeMap'; |
5 | 6 | import 'rxjs/add/operator/toArray'; |
6 | 7 | import { Injectable } from '@angular/core'; |
7 | 8 | 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'; |
9 | 15 | import { Database } from '@ngrx/db'; |
10 | 16 | import { Observable } from 'rxjs/Observable'; |
11 | 17 | import { defer } from 'rxjs/observable/defer'; |
12 | 18 | import { of } from 'rxjs/observable/of'; |
13 | 19 |
|
14 | 20 | import * as collection from '../actions/collection'; |
| 21 | +import * as auth from '../../auth/actions/auth'; |
15 | 22 | import { Book } from '../models/book'; |
16 | 23 |
|
17 | 24 | @Injectable() |
18 | | -export class CollectionEffects { |
| 25 | +export class CollectionEffects implements OnRunEffects { |
19 | 26 | /** |
20 | 27 | * This effect does not yield any actions back to the store. Set |
21 | 28 | * `dispatch` to false to hint to @ngrx/effects that it should |
@@ -65,4 +72,22 @@ export class CollectionEffects { |
65 | 72 | ); |
66 | 73 |
|
67 | 74 | 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 | + } |
68 | 93 | } |
0 commit comments