Created
July 22, 2017 14:13
-
-
Save typoerr/baeb2ffd9e1707f306608fdecd7380e6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| export interface Action<T = any, K extends keyof T= any> { | |
| type: K; | |
| payload: T[K]; | |
| } | |
| /////////////////////////////////////////////////////////// | |
| import { Stream, mergeArray } from 'most'; | |
| import { Action } from './types'; | |
| import { async } from 'most-subject'; | |
| export interface Dispatch<A> { | |
| <K extends keyof A>(type: K, payload: A[K]): Action<A, K>; | |
| } | |
| export interface MergedCtx<A> { | |
| dispatch: Dispatch<A>; | |
| } | |
| export interface Epic<A, C = any> { | |
| (action$: Stream<Action<A>>, context: C & MergedCtx<A>): Stream<any>; | |
| } | |
| export default function connect<A, C>(epics: Epic<A, C>[]) { | |
| return (context: C) => { | |
| const ctx: C & MergedCtx<A> = Object.assign({ dispatch }, context); | |
| const actionIn$ = async<any>(); | |
| const epicArray$ = epics.map(ep => ep(actionIn$, ctx)); | |
| mergeArray(epicArray$).drain(); | |
| return dispatch as Dispatch<A>; | |
| function dispatch<K extends keyof A>(type: K, payload: A[K]) { | |
| const action = { type, payload }; | |
| actionIn$.next(action); | |
| return action; | |
| } | |
| }; | |
| } | |
| /////////////////////////////////////////////// | |
| import { Stream } from 'most'; | |
| import { Action } from './types'; | |
| export default function select<T, K extends keyof T>(type: K, action$: Stream<Action<T>>): Stream<T[K]> { | |
| return (action$ as Stream<Action<T, K>>) | |
| .filter(x => x.type === type) | |
| .map(x => x.payload); | |
| } | |
| /////////////////////////////////////////////////// |
typoerr
commented
Jul 22, 2017
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment