Skip to content

Commit 1cbb2c9

Browse files
karptoniteMikeRyanDev
authored andcommitted
fix(Effects): Deprecate toPayload utility function (#266)
1 parent ca544dd commit 1cbb2c9

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

docs/effects/api.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export class SomeEffectsClass {
5555

5656
### ofType
5757

58-
Filter actions by action types.
58+
Filter actions by action types. Specify the action type to allow type-safe mapping to other data on the action, including payload.
5959

6060
Usage:
6161
```ts
@@ -67,7 +67,7 @@ import { Actions, Effect } from '@ngrx/effects';
6767
export class SomeEffectsClass {
6868
constructor(private actions$: Actions) {}
6969

70-
@Effect() authActions$ = this.action$.ofType('LOGIN', 'LOGOUT')
70+
@Effect() authActions$ = this.action$.ofType<LoginAction | LogoutAction>('LOGIN', 'LOGOUT')
7171
.do(action => {
7272
console.log(action);
7373
});
@@ -128,8 +128,8 @@ export class UserEffects implements OnRunEffects {
128128

129129
## Utilities
130130

131-
### toPayload
132-
Maps an action to its payload.
131+
### toPayload (DEPRECATED)
132+
Maps an action to its payload. This function is deprecated, and will be removed in version 5.0.
133133

134134
Usage:
135135
```ts
@@ -150,6 +150,15 @@ export class SomeEffectsClass {
150150
}
151151
```
152152

153+
Recommended alternative to deprecated toPayload function. Note that the type
154+
of the action is specified so that mapping to payload (or whatever data is available in the action) is type-safe.
155+
```ts
156+
@Effect() authActions$ = this.action$.ofType<LoadingAction | LogoutAction>('LOGIN', 'LOGOUT')
157+
.map(action => action.payload)
158+
.do(payload => {
159+
console.log(payload);
160+
```
161+
153162
### mergeEffects
154163
Manually merges all decorated effects into a combined observable.
155164

example-app/app/books/effects/book.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import 'rxjs/add/operator/debounceTime';
55
import 'rxjs/add/operator/skip';
66
import 'rxjs/add/operator/takeUntil';
77
import { Injectable, InjectionToken, Optional, Inject } from '@angular/core';
8-
import { Effect, Actions, toPayload } from '@ngrx/effects';
8+
import { Effect, Actions } from '@ngrx/effects';
99
import { Action } from '@ngrx/store';
1010
import { Observable } from 'rxjs/Observable';
1111
import { Scheduler } from 'rxjs/Scheduler';
@@ -25,12 +25,6 @@ export const SEARCH_SCHEDULER = new InjectionToken<Scheduler>(
2525
/**
2626
* Effects offer a way to isolate and easily test side-effects within your
2727
* application.
28-
* The `toPayload` helper function returns just
29-
* the payload of the currently dispatched action, useful in
30-
* instances where the current state is not necessary.
31-
*
32-
* Documentation on `toPayload` can be found here:
33-
* https://github.com/ngrx/platform/blob/master/docs/effects/api.md#topayload
3428
*
3529
* If you are unfamiliar with the operators being used in these examples, please
3630
* check out the sources below:
@@ -43,9 +37,9 @@ export const SEARCH_SCHEDULER = new InjectionToken<Scheduler>(
4337
export class BookEffects {
4438
@Effect()
4539
search$: Observable<Action> = this.actions$
46-
.ofType(book.SEARCH)
40+
.ofType<book.SearchAction>(book.SEARCH)
4741
.debounceTime(this.debounce, this.scheduler || async)
48-
.map(toPayload)
42+
.map(action => action.payload)
4943
.switchMap(query => {
5044
if (query === '') {
5145
return empty();

modules/effects/src/util.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { Action } from '@ngrx/store';
22

3+
/**
4+
* @deprecated Since version 4.1. Will be deleted in version 5.0.
5+
*/
36
export function toPayload(action: Action): any {
47
return (action as any).payload;
58
}

0 commit comments

Comments
 (0)