Skip to content

Commit

Permalink
docs(Effects): Updated documentation for @ngrx/effects (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonroberts authored and MikeRyanDev committed Jun 11, 2017
1 parent 015107f commit 3459bc5
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 87 deletions.
41 changes: 22 additions & 19 deletions docs/effects/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,7 @@ store. The `ofType` operator lets you filter for actions of a certain type in wh
want to use to perform a side effect.

## Example
1. Register the EffectsModule in your application root imports:
```ts
import { EffectsModule } from '@ngrx/effects';

@NgModule({
imports: [
EffectsModule.forRoot()
]
})
export class AppModule { }
```

2. Create an AuthEffects service that describes a source of login actions:
1. Create an AuthEffects service that describes a source of login actions:

```ts
// ./effects/auth.ts
Expand Down Expand Up @@ -82,22 +70,37 @@ export class AuthEffects {
}
```

3. Register your effects via `EffectsModule.run` method in your module's `imports`:

2. Register the EffectsModule in your application root imports:
```ts
import { EffectsModule } from '@ngrx/effects';
import { AuthEffects } from './effects/auth';

@NgModule({
imports: [
EffectsModule.run(AuthEffects)
EffectsModule.forRoot([AuthEffects])
]
})
export class AppModule {}
```

## Feature Modules

For feature modules, register your effects via `EffectsModule.forFeature` method in your module's `imports`:

```ts
import { EffectsModule } from '@ngrx/effects';
import { AdminEffects } from './effects/admin';

@NgModule({
imports: [
EffectsModule.forFeature([AdminEffects])
]
})
export class YourAuthModule { }
export class AdminModule {}
```

## API Documentation
- [Run effects after bootstrap](./api.md#runafterbootstrap)
- [Controlling Effects](./api.md#controlling-effects)
- [Filtering Actions](./api.md#oftype)
- [Non-dispatching effects](./api.md#non-dispatching-effects)
- [Managing Effects Subscriptions](./api.md#effectssubscription)
- [Utilities](./api.md#utilities)
159 changes: 95 additions & 64 deletions docs/effects/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,89 @@

## EffectsModule

Feature module for @ngrx/effects.
NgModule for @ngrx/effects.

### forRoot
Registers internal @ngrx/effects services to run in your application.
This is required once in your root app module.

### run

Registers an effects class to run when the target module bootstraps.
Call once per each effect class you want to run.

Note that running an effects class multiple times (for example via
different lazy loaded modules) will not cause Effects to run multiple
times.
Registers internal @ngrx/effects services to run in your application. This is required once in your root NgModule.

Usage:
```ts
@NgModule({
imports: [
EffectsModule.run(SomeEffectsClass)
EffectsModule.forRoot([
FirstEffectsClass,
SecondEffectsClass,
])
]
})
export class AppModule { }
export class FeatureModule { }
```

### runAfterBootstrap
### forFeature
Registers @ngrx/effects services to run with your feature modules.

Registers an effects class to run after root components bootstrap.
Must use in the root module. Useful if your effects class requires
the a bootstrapped component.
**Note**: Running an effects class multiple times (for example via different lazy loaded modules) will not cause Effects to run multiple times.

Usage:
```ts
@NgModule({
imports: [
EffectsModule.runAfterBootstrap(SomeEffectsClass)
EffectsModule.forFeature([
SomeEffectsClass,
AnotherEffectsClass,
])
]
})
export class FeatureModule {}
```


## Actions

Stream of all actions dispatched in your application including actions
dispatched by effect streams.
Stream of all actions dispatched in your application including actions dispatched by effect streams.

Usage:
```ts
import { Injectable } from '@angular/core';
import { Actions } from '@ngrx/effects';

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

### ofType

Filter actions by action types.

Usage:

```ts
actions$.ofType('LOGIN', 'LOGOUT');
```
import 'rxjs/add/operator/do';
import { Injectable } from '@angular/core';
import { Actions } from '@ngrx/effects';

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

@Effect() authActions$ = this.action$.ofType('LOGIN', 'LOGOUT')
.do(action => {
console.log(action);
});
}
```

### Non-dispatching Effects
Pass `{ dispatch: false }` to the decorator to prevent dispatching.

Usage:
```ts
class MyEffects {
import 'rxjs/add/operator/do';
import { Injectable } from '@angular/core';
import { Actions } from '@ngrx/effects';

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

@Effect({ dispatch: false }) logActions$ = this.actions$
Expand All @@ -74,63 +94,74 @@ class MyEffects {
}
```

## Utilities

### toPayload
Maps an action to its payload.

Usage:
```ts
actions$.ofType('LOGIN').map(toPayload);
```
## Controlling Effects

### mergeEffects
Manually merges all decorated effects into a combined observable.
### OnRunEffects
By default, effects are merged and subscribed to the store. Implement the `OnRunEffects` interface to control the lifecycle of the resolved effects.

Usage:
```ts
export class MyService {
constructor(effects: SomeEffectsClass) {
mergeEffects(effects).subscribe(result => {

import 'rxjs/add/operator/do';
import 'rxjs/add/operator/exhaustMap';
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';

@Injectable()
export class UserEffects implements OnRunEffects {
constructor(private actions$: Actions) {}

@Effect() updateUser$: Observable<Action> = this.actions$
.ofType('UPDATE_USER')
.do(action => {
console.log(action);
});

ngrxOnRunEffects(resolvedEffects$: Observable<Action>) {
return this.actions$.ofType('LOGGED_IN')
.exhaustMap(() => resolvedEffects$.takeUntil('LOGGED_OUT'));
}
}
```

## EffectsSubscription
## Utilities

An RxJS subscription of all effects running for the current injector. Can be
used to stop all running effects contained in the subscription.
### toPayload
Maps an action to its payload.

Usage:
```ts
class MyComponent {
constructor(private subscription: EffectsSubscription) { }

ngOnDestroy() {
this.subscription.unsubscribe();
}
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/map';
import { Injectable } from '@angular/core';
import { Actions, toPayload } from '@ngrx/effects';

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

@Effect() authActions$ = this.action$.ofType('LOGIN', 'LOGOUT')
.map(toPayload)
.do(payload => {
console.log(payload);
});
}
```

### addEffects
Add instances of effect classes to the subscription.
### mergeEffects
Manually merges all decorated effects into a combined observable.

Usage:
```ts
class MyComponent {
constructor(moreEffects: MoreEffects, subscription: EffectsSubscription) {
subscription.addEffects([ moreEffects ]);
import { mergeEffects } from '@ngrx/effects';

export class MyService {
constructor(effects: SomeEffectsClass) {
mergeEffects(effects).subscribe(result => {
console.log(result);
});
}
}
```

### parent
A pointer to the parent subscription. Used to access an entire tree of
subscriptions.

Usage:
```ts
subscription.parent.unsubscribe();
```
10 changes: 6 additions & 4 deletions docs/store/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ import { StoreModule, combineReducers, compose } from '@ngrx/store';
import { reducers } from './reducers';

// console.log all actions
function debug(state, action) {
console.log('state', state);
console.log('action', action);
function debug(reducer) {
return function(state, action) {
console.log('state', state);
console.log('action', action);

return state;
return reducer(state, action);
}
}

const debugReducerFactory = compose(debug, combineReducers);
Expand Down

0 comments on commit 3459bc5

Please sign in to comment.