diff --git a/docs/effects/api.md b/docs/effects/api.md index d27d7bd8e2..ffdbf151a0 100644 --- a/docs/effects/api.md +++ b/docs/effects/api.md @@ -107,7 +107,7 @@ import 'rxjs/add/operator/takeUntil'; import { Injectable } from '@angular/core'; import { Observable } from 'rxjs/Observable'; import { Action } from '@ngrx/store'; -import { Actions, Effect, OnRunEffects } from '@ngrx/effects'; +import { Actions, Effect, OnRunEffects, EffectsNotification } from '@ngrx/effects'; @Injectable() export class UserEffects implements OnRunEffects { @@ -119,7 +119,7 @@ export class UserEffects implements OnRunEffects { console.log(action); }); - ngrxOnRunEffects(resolvedEffects$: Observable) { + ngrxOnRunEffects(resolvedEffects$: Observable) { return this.actions$.ofType('LOGGED_IN') .exhaustMap(() => resolvedEffects$.takeUntil('LOGGED_OUT')); } diff --git a/example-app/app/books/effects/collection.ts b/example-app/app/books/effects/collection.ts index d14ecebba3..0dce588c36 100644 --- a/example-app/app/books/effects/collection.ts +++ b/example-app/app/books/effects/collection.ts @@ -1,21 +1,28 @@ import 'rxjs/add/operator/map'; import 'rxjs/add/operator/catch'; +import 'rxjs/add/operator/exhaustMap'; import 'rxjs/add/operator/switchMap'; import 'rxjs/add/operator/mergeMap'; import 'rxjs/add/operator/toArray'; import { Injectable } from '@angular/core'; import { Action } from '@ngrx/store'; -import { Effect, Actions } from '@ngrx/effects'; +import { + Effect, + Actions, + OnRunEffects, + EffectNotification, +} from '@ngrx/effects'; import { Database } from '@ngrx/db'; import { Observable } from 'rxjs/Observable'; import { defer } from 'rxjs/observable/defer'; import { of } from 'rxjs/observable/of'; import * as collection from '../actions/collection'; +import * as auth from '../../auth/actions/auth'; import { Book } from '../models/book'; @Injectable() -export class CollectionEffects { +export class CollectionEffects implements OnRunEffects { /** * This effect does not yield any actions back to the store. Set * `dispatch` to false to hint to @ngrx/effects that it should @@ -65,4 +72,22 @@ export class CollectionEffects { ); constructor(private actions$: Actions, private db: Database) {} + + /** + * Implementing the OnRunEffects interface gives you more control + * over the lifecycle of running effects. All the registered effects + * in this class will only listen for actions and peform side effects + * depending on provided Observable. + * + * The resolved effects will only listen and provide actions once the user + * has logged in and will stop listening for actions once the user has logged + * out. + */ + ngrxOnRunEffects(resolvedEffects$: Observable) { + return this.actions$ + .ofType(auth.LOGIN_SUCCESS) + .exhaustMap(() => + resolvedEffects$.takeUntil(this.actions$.ofType(auth.LOGOUT)) + ); + } } diff --git a/modules/effects/src/index.ts b/modules/effects/src/index.ts index c314508b9a..cd112564fb 100644 --- a/modules/effects/src/index.ts +++ b/modules/effects/src/index.ts @@ -5,3 +5,4 @@ export { EffectsModule } from './effects_module'; export { EffectSources } from './effect_sources'; export { OnRunEffects } from './on_run_effects'; export { toPayload } from './util'; +export { EffectNotification } from './effect_notification';