diff --git a/src/combineEpics.js b/src/combineEpics.js index 56d03b9..43362f9 100644 --- a/src/combineEpics.js +++ b/src/combineEpics.js @@ -15,7 +15,8 @@ export const combineEpics = epicsArray => (actions, store) => { throw new TypeError('The array passed to combineEpics must contain only Epics (functions).') } - const out = epic(actions, store) + // withState HOC support + const out = epic.length === 1 ? epic(actions) : epic(store, actions) if (!out || !out.source) { const epicIdentifier = epic.name diff --git a/src/index.js b/src/index.js index 39e411e..1529a37 100644 --- a/src/index.js +++ b/src/index.js @@ -5,3 +5,4 @@ export { EPIC_END } from './constants' export { select } from './select' export { selectArray } from './selectArray' export { withState } from './withState' +export { mergeState } from './mergeState' diff --git a/src/mergeState.js b/src/mergeState.js new file mode 100644 index 0000000..6bd068a --- /dev/null +++ b/src/mergeState.js @@ -0,0 +1,5 @@ +import { snapshot } from '@most/core' + +const toArray = (state, action) => [state, action] + +export const mergeState = snapshot(toArray) diff --git a/src/withState.js b/src/withState.js index db841ca..992e734 100644 --- a/src/withState.js +++ b/src/withState.js @@ -1,5 +1,8 @@ -import { snapshot } from '@most/core' +const unCurry2 = fn => (arg1, arg2) => + fn.length === 1 ? fn(arg1)(arg2) : fn(arg1, arg2) -const toArray = (state, action) => [state, action] - -export const withState = snapshot(toArray) +// withState :: (s$ -> a$ -> a$) -> s$ -> a$ -> a$ +export const withState = epic => { + const uncurriedEpic = unCurry2(epic) + return (state$, action$) => uncurriedEpic(state$, action$) +} diff --git a/tests/combineEpics.test.js b/tests/combineEpics.test.js index 92b5c89..0d40a2b 100644 --- a/tests/combineEpics.test.js +++ b/tests/combineEpics.test.js @@ -1,7 +1,7 @@ import test from 'ava' import { map, tap, runEffects, never, MulticastSource } from '@most/core' import { newDefaultScheduler } from '@most/scheduler' -import { combineEpics, select } from '../src/' +import { combineEpics, select, withState } from '../src/' test('combineEpics should combine an array of epics', t => { const ACTION_1 = 'ACTION_1' @@ -10,14 +10,18 @@ test('combineEpics should combine an array of epics', t => { const DELEGATED_2 = 'DELEGATED_2' const MOCKED_STORE = { I: 'am', a: 'store' } - const epic1 = (actions$, store) => map( - action => ({ type: DELEGATED_1, action, store }), - select(ACTION_1, actions$) + const epic1 = withState( + (store, actions$) => map( + action => ({ type: DELEGATED_1, action, store }), + select(ACTION_1, actions$) + ) ) - const epic2 = (actions$, store) => map( - action => ({ type: DELEGATED_2, action, store }), - select(ACTION_2, actions$) + const epic2 = withState( + (store, actions$) => map( + action => ({ type: DELEGATED_2, action, store }), + select(ACTION_2, actions$) + ) ) const epic = combineEpics([