Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

withState hoc #33

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/combineEpics.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
5 changes: 5 additions & 0 deletions src/mergeState.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { snapshot } from '@most/core'

const toArray = (state, action) => [state, action]

export const mergeState = snapshot(toArray)
11 changes: 7 additions & 4 deletions src/withState.js
Original file line number Diff line number Diff line change
@@ -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$)
}
18 changes: 11 additions & 7 deletions tests/combineEpics.test.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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([
Expand Down