Skip to content

Commit

Permalink
Add support for injecting a custom argument into epics
Browse files Browse the repository at this point in the history
Raised in issue joshburgess#19 but dismissed in favour of a more elegant HOF
approach, after four months with no project activity, I've gone for
the zero-impact pragmatic solution
  • Loading branch information
alex2 committed Feb 9, 2018
1 parent 9f39f8b commit 4f7209b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/combineEpics.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { mergeArray } from 'most'
import { findIndex, map } from '@most/prelude'

export const combineEpics = epicsArray => (actions, store) => {
export const combineEpics = epicsArray => (actions, store, dependencies) => {
if (!epicsArray || !Array.isArray(epicsArray)) {
throw new TypeError('You must provide an array of Epics to combineEpics.')
}
Expand All @@ -15,7 +15,7 @@ export const combineEpics = epicsArray => (actions, store) => {
throw new TypeError('The array passed to combineEpics must contain only Epics (functions).')
}

const out = epic(actions, store)
const out = epic(actions, store, dependencies)

if (!out || !out.source) {
const epicIdentifier = epic.name
Expand Down
6 changes: 3 additions & 3 deletions src/createEpicMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { async } from 'most-subject'
import { epicBegin, epicEnd } from './actions'
import { STATE_STREAM_SYMBOL } from './constants'

export const createEpicMiddleware = epic => {
export const createEpicMiddleware = (epic, dependencies) => {
if (typeof epic !== 'function') {
throw new TypeError('You must provide an Epic (a function) to createEpicMiddleware.')
}
Expand Down Expand Up @@ -33,9 +33,9 @@ export const createEpicMiddleware = epic => {

return isUsingStateStreamEnhancer
// new style API (declarative only, no dispatch/getState)
? nextEpic(actionsIn$, state$)
? nextEpic(actionsIn$, state$, dependencies)
// redux-observable style Epic API
: nextEpic(actionsIn$, middlewareApi)
: nextEpic(actionsIn$, middlewareApi, dependencies)
}

const actionsOut$ = switchLatest(map(callNextEpic, epic$))
Expand Down
9 changes: 5 additions & 4 deletions tests/combineEpics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ test('combineEpics should combine an array of epics', t => {
const DELEGATED_1 = 'DELEGATED_1'
const DELEGATED_2 = 'DELEGATED_2'
const MOCKED_STORE = { I: 'am', a: 'store' }
const DEPENDENCIES = 'deps'

const epic1 = (actions$, store) => map(
action => ({ type: DELEGATED_1, action, store }),
select(ACTION_1, actions$)
)

const epic2 = (actions$, store) => map(
action => ({ type: DELEGATED_2, action, store }),
const epic2 = (actions$, store, deps) => map(
action => ({ type: DELEGATED_2, action, store, deps }),
select(ACTION_2, actions$)
)

Expand All @@ -27,7 +28,7 @@ test('combineEpics should combine an array of epics', t => {

const store = MOCKED_STORE
const actions$ = sync()
const result$ = epic(actions$, store)
const result$ = epic(actions$, store, DEPENDENCIES)
const emittedActions = []

observe(emittedAction => emittedActions.push(emittedAction), result$)
Expand All @@ -37,7 +38,7 @@ test('combineEpics should combine an array of epics', t => {

const MOCKED_EMITTED_ACTIONS = [
{ type: DELEGATED_1, action: { type: ACTION_1 }, store },
{ type: DELEGATED_2, action: { type: ACTION_2 }, store },
{ type: DELEGATED_2, action: { type: ACTION_2 }, store, deps: DEPENDENCIES },
]

t.deepEqual(MOCKED_EMITTED_ACTIONS, emittedActions)
Expand Down

0 comments on commit 4f7209b

Please sign in to comment.