Skip to content

Commit

Permalink
fix(Effects): Export EffectsNotification interface
Browse files Browse the repository at this point in the history
Also updated documentation and added to example app
  • Loading branch information
brandonroberts committed Aug 3, 2017
1 parent f969676 commit 1b4c646
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
4 changes: 2 additions & 2 deletions docs/effects/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -119,7 +119,7 @@ export class UserEffects implements OnRunEffects {
console.log(action);
});

ngrxOnRunEffects(resolvedEffects$: Observable<Action>) {
ngrxOnRunEffects(resolvedEffects$: Observable<EffectsNotification>) {
return this.actions$.ofType('LOGGED_IN')
.exhaustMap(() => resolvedEffects$.takeUntil('LOGGED_OUT'));
}
Expand Down
29 changes: 27 additions & 2 deletions example-app/app/books/effects/collection.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<EffectNotification>) {
return this.actions$
.ofType(auth.LOGIN_SUCCESS)
.exhaustMap(() =>
resolvedEffects$.takeUntil(this.actions$.ofType(auth.LOGOUT))
);
}
}
1 change: 1 addition & 0 deletions modules/effects/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

0 comments on commit 1b4c646

Please sign in to comment.