Skip to content

Commit

Permalink
docs(Effects): Add example on initializing effects (#749)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcabanes authored and brandonroberts committed Feb 9, 2018
1 parent 91542dd commit 0e5d209
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/effects/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,6 @@ export class AdminModule {}
- [Controlling Effects](./api.md#controlling-effects)
- [Filtering Actions](./api.md#oftype)
- [Non-dispatching effects](./api.md#non-dispatching-effects)
- [Initializing effect](./api.md#initializing-effect)
- [Utilities](./api.md#utilities)
- [Testing](./testing.md)
41 changes: 41 additions & 0 deletions docs/effects/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,47 @@ export class SomeEffectsClass {
}
```

### Initializing effect
You can execute some code that will be executed directly after the effect class is loaded.
```ts
import { Injectable } from '@angular/core';
import { Actions, Effect, ofType } from '@ngrx/effects';
import { defer } from 'rxjs/observable/defer';
import { tap } from 'rxjs/operators';

@Injectable()
export class SomeEffectsClass {
constructor(private actions$: Actions) { }

@Effect({ dispatch: false }) init$: Observable<any> = defer(() => of(null)).pipe(
tap(() => console.log('init$')),
);
}
```

If you want to trigger another action, be careful to add this effect at the end.
```ts
import { Injectable } from '@angular/core';
import { Actions, Effect, ofType } from '@ngrx/effects';
import { defer } from 'rxjs/observable/defer';
import { LoginAction, LogoutAction } from './auth';

@Injectable()
export class SomeEffectsClass {
constructor(private actions$: Actions) { }

@Effect({ dispatch: false }) authActions$ = this.action$.pipe(
ofType<LoginAction | LogoutAction>('LOGIN', 'LOGOUT'),
tap(action => console.log(action))
);

// Should be your last effect
@Effect() init$: Observable<action> = defer(() => {
return of(new LogoutAction());
});
}
```

## Controlling Effects

### OnRunEffects
Expand Down

0 comments on commit 0e5d209

Please sign in to comment.