Permalink
2 comments
on commit
sign in to comment.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
feature(ofType): Type inference for ofType, removal of ActionsObserva…
…ble in favor of just Observable (#681) BREAKING CHANGE: ActionsObservable existed so we could provide an ofType() method to the prototype of action$, before RxJS had pipeable operators. Now that pipeable operators have been out for quite some time we are removing ActionsObservable in favor or using the pipeable ofType() instead. ```js // BEFORE function someEpic(action$) { return action$ .ofType('PING') .mapTo({ type: 'PONG' }); } // AFTER import { ofType } from 'redux-observable'; import { mapTo } from 'rxjs/operators'; function someEpic(action$) { return action$.pipe( ofType('PING') mapTo({ type: 'PONG' }) ); } ```
- Loading branch information
Showing
with
81 additions
and 170 deletions.
- +1 −1 docs/api/combineEpics.md
- +0 −50 src/ActionsObservable.ts
- +2 −2 src/StateObservable.ts
- +37 −17 src/createEpicMiddleware.ts
- +11 −3 src/epic.ts
- +0 −1 src/index.ts
- +24 −9 src/operators.ts
- +0 −80 test/ActionsObservable-spec.ts
- +4 −5 test/combineEpics-spec.ts
- +2 −2 test/createEpicMiddleware-spec.ts
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -1,8 +1,16 @@ | ||
import { Action } from 'redux'; | ||
import { Observable } from 'rxjs'; | ||
import { StateObservable } from './StateObservable'; | ||
|
||
export declare interface Epic< | ||
Input extends Action = any, | ||
Output extends Input = Input, | ||
State = any, | ||
Dependencies = any | ||
> { | ||
( | ||
action$: Observable<Input>, | ||
state$: StateObservable<State>, | ||
dependencies: Dependencies | ||
): Observable<Output>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -1,24 +1,39 @@ | ||
import { Action } from 'redux'; | ||
import { OperatorFunction } from 'rxjs'; | ||
import { filter } from 'rxjs/operators'; | ||
|
||
const keyHasType = (type: unknown, key: unknown) => { | ||
return type === key || (typeof key === 'function' && type === key.toString()); | ||
}; | ||
|
||
/** | ||
* Inferring the types of this is a bit challenging, and only works in newer | ||
* versions of TypeScript. | ||
* | ||
* @param ...types One or more Redux action types you want to filter for, variadic. | ||
*/ | ||
export function ofType< | ||
// All possible actions your app can dispatch | ||
Input extends Action, | ||
// The types you want to filter for | ||
Types extends Input['type'][], | ||
// The resulting actions that match the above types | ||
Output extends Input = Extract<Input, Action<Types[number]>> | ||
>(...types: Types): OperatorFunction<Input, Output> { | ||
return filter((action): action is Output => { | ||
const { type } = action; | ||
const len = types.length; | ||
|
||
if (len === 1) { | ||
return keyHasType(type, types[0]); | ||
} else { | ||
for (let i = 0; i < len; i++) { | ||
if (keyHasType(type, types[i])) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16f083d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is causing me an issue with the following code
If I change it to use ofType exported from your package I get the following error
@jayphelps is there a workaround pending a fix?
16f083d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@thebpmgroup try this