Skip to content

Commit

Permalink
chore(docs): Update documentation with enums and lettable operators (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonroberts authored and MikeRyanDev committed Jan 3, 2018
1 parent d9b37cd commit 06aeac4
Show file tree
Hide file tree
Showing 15 changed files with 167 additions and 185 deletions.
47 changes: 23 additions & 24 deletions docs/effects/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ RxJS powered side effect model for @ngrx/store

- Listen for actions dispatched from @ngrx/store
- Isolate side effects from components, allowing for more _pure_ components that select state and dispatch actions
- Provide new sources of actions to reduce state based on external
interactions such as network requests, web socket messages and time-based events.
- Provide [new sources](https://martinfowler.com/eaaDev/EventSourcing.html) of actions to reduce state based on external interactions such as network requests, web socket messages and time-based events.

### Installation
Install @ngrx/effects from npm:
Expand All @@ -25,45 +24,45 @@ Effects are injectable service classes that use two main APIs.

### Effect decorator

Use the `@Effect()` decorator to hint to @ngrx/effects observable side-effects
on the effects class. @ngrx/effects dispatches actions emitted by every decorated
effect to the store.
The `@Effect()` decorator provides metadata to register observable side-effects in the effects class. Registered effects provide new actions provided by the source Observable to the store.

### Actions Observable

The `Actions` observable represents an observable of all actions dispatched to the
store. The `ofType` operator lets you filter for actions of a certain type in which you
want to use to perform a side effect.
- Represents an observable of all actions dispatched to the store.
- Emits the latest action _after_ the action has passed through all reducers.
- The `ofType` operator lets you filter for actions of a certain type in which you want to use to perform a side effect.

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

```ts
// ./effects/auth.ts
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/mergeMap';
import 'rxjs/add/operator/catch';
// ./effects/auth.effects.ts
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { HttpClient } from '@angular/common/http';
import { Action } from '@ngrx/store';
import { Actions, Effect } from '@ngrx/effects';
import { Actions, Effect, ofType } from '@ngrx/effects';
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
import { catchError, map, mergeMap } from 'rxjs/operators';

@Injectable()
export class AuthEffects {
// Listen for the 'LOGIN' action
@Effect() login$: Observable<Action> = this.actions$.ofType('LOGIN')
.mergeMap(action =>
this.http.post('/auth', action.payload)
@Effect() login$: Observable<Action> = this.actions$.pipe(
ofType('LOGIN'),
mergeMap(action =>
this.http.post('/auth', action.payload).pipe(
// If successful, dispatch success action with result
.map(data => ({ type: 'LOGIN_SUCCESS', payload: data }))
map(data => ({ type: 'LOGIN_SUCCESS', payload: data }))
// If request fails, dispatch failed action
.catch(() => of({ type: 'LOGIN_FAILED' }))
);
catchError(() => of({ type: 'LOGIN_FAILED' }))
)
)
);


constructor(
private http: Http,
private http: HttpClient,
private actions$: Actions
) {}
}
Expand All @@ -74,7 +73,7 @@ your root `NgModule` for the effects providers to be registered and start when y

```ts
import { EffectsModule } from '@ngrx/effects';
import { AuthEffects } from './effects/auth';
import { AuthEffects } from './effects/auth.effects';

@NgModule({
imports: [
Expand All @@ -90,7 +89,7 @@ For feature modules, register your effects via `EffectsModule.forFeature` method

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

@NgModule({
imports: [
Expand Down
79 changes: 25 additions & 54 deletions docs/effects/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,18 @@ Filter actions by action types. Specify the action type to allow type-safe mappi

Usage:
```ts
import 'rxjs/add/operator/do';
import { Injectable } from '@angular/core';
import { Actions, Effect } from '@ngrx/effects';
import { Actions, Effect, ofType } from '@ngrx/effects';
import { tap } from 'rxjs/operators';

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

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

Expand All @@ -79,18 +79,17 @@ Pass `{ dispatch: false }` to the decorator to prevent dispatching.

Usage:
```ts
import 'rxjs/add/operator/do';
import { Injectable } from '@angular/core';
import { Actions, Effect } from '@ngrx/effects';
import { Actions, Effect, ofType } from '@ngrx/effects';
import { tap } from 'rxjs/operators';

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

@Effect({ dispatch: false }) logActions$ = this.actions$
.do(action => {
console.log(action);
});
@Effect({ dispatch: false }) logActions$ = this.actions$.pipe(
tap(action => console.log(action))
);
}
```

Expand All @@ -101,64 +100,36 @@ By default, effects are merged and subscribed to the store. Implement the `OnRun

Usage:
```ts
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 { Actions, Effect, OnRunEffects, EffectNotification, ofType } from '@ngrx/effects';
import { Action } from '@ngrx/store';
import { Actions, Effect, OnRunEffects, EffectNotification } from '@ngrx/effects';
import { Observable } from 'rxjs/Observable';
import { exhaustMap, takeUntil, tap } from 'rxjs/operators';

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

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

ngrxOnRunEffects(resolvedEffects$: Observable<EffectNotification>) {
return this.actions$.ofType('LOGGED_IN')
.exhaustMap(() => resolvedEffects$.takeUntil(this.actions$.ofType('LOGGED_OUT')));
return this.actions$.pipe(
ofType('LOGGED_IN'),
exhaustMap(() => resolvedEffects$.pipe(
takeUntil(this.actions$.pipe(ofType('LOGGED_OUT')))
)
);
}
}
```
## Utilities
### toPayload (DEPRECATED)
Maps an action to its payload. This function is deprecated, and will be removed in version 5.0.

Usage:
```ts
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/map';
import { Injectable } from '@angular/core';
import { Actions, Effect, 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);
});
}
```

Recommended alternative to deprecated toPayload function. Note that the type
of the action is specified so that mapping to payload (or whatever data is available in the action) is type-safe.
```ts
@Effect() authActions$ = this.action$.ofType<LoadingAction | LogoutAction>('LOGIN', 'LOGOUT')
.map(action => action.payload)
.do(payload => {
console.log(payload);
```
### mergeEffects
Manually merges all decorated effects into a combined observable.
Expand Down
3 changes: 1 addition & 2 deletions docs/effects/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

### provideMockActions
Provides a mock test provider of the `Actions` Observable for testing effects. This works well with writing
marble tests and tests using the `subscribe` method on an Observable. The mock Actions will deliver a new Observable
to subscribe to for each test.
marble tests and tests using the `subscribe` method on an Observable. The mock Actions will deliver a new Observable to subscribe to for each test.

Details on marble tests and their syntax, as shown in the `hot` and `cold` methods, can be found in [Writing Marble Tests](https://github.com/ReactiveX/rxjs/blob/master/doc/writing-marble-tests.md).

Expand Down
1 change: 0 additions & 1 deletion docs/entity/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ Install @ngrx/entity from npm:

`npm install @ngrx/entity --save` OR `yarn add @ngrx/entity`


### Nightly builds

`npm install github:ngrx/entity-builds` OR `yarn add github:ngrx/entity-builds`
Expand Down
Loading

0 comments on commit 06aeac4

Please sign in to comment.