Skip to content

Commit 4900bd1

Browse files
committed
Dispatches an event to indicate effect firing and fixes bug
1 parent b4abe08 commit 4900bd1

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

src/__tests__/easy-peasy.test.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ beforeEach(() => {
99
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ = undefined
1010
})
1111

12+
const trackActionsMiddleware = () => {
13+
const middleware = () => next => action => {
14+
middleware.actions.push(action)
15+
return next(action)
16+
}
17+
middleware.actions = []
18+
return middleware
19+
}
20+
1221
test('empty object in state', () => {
1322
// arrange
1423
const model = {
@@ -297,7 +306,7 @@ test('supports initial state', () => {
297306
foo: {
298307
bar: {
299308
stuff: [3, 4],
300-
other: 'qux',
309+
invalid: 'qux',
301310
},
302311
},
303312
}
@@ -310,10 +319,27 @@ test('supports initial state', () => {
310319
foo: {
311320
bar: {
312321
stuff: [3, 4],
313-
other: 'qux',
314322
},
315323
color: 'red',
316324
},
317325
baz: 'bob',
318326
})
319327
})
328+
329+
test('dispatches an action to represent the start of an effect', () => {
330+
// arrange
331+
const model = {
332+
foo: {
333+
doSomething: effect(() => undefined),
334+
},
335+
}
336+
const trackActions = trackActionsMiddleware()
337+
const store = createStore(model, { middleware: [trackActions] })
338+
const payload = 'hello'
339+
340+
// act
341+
store.dispatch.foo.doSomething(payload)
342+
343+
// assert
344+
expect(trackActions.actions).toEqual([{ type: 'foo.doSomething', payload }])
345+
})

src/easy-peasy.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,20 @@ export const createStore = (model, options = {}) => {
5252
const value = current[key]
5353
const path = [...parentPath, key]
5454
if (typeof value === 'function') {
55+
const actionName = path.join('.')
56+
5557
if (value[effectSymbol]) {
5658
// Effect Action
57-
const action = payload =>
58-
value(references.dispatch, payload, {
59+
const action = payload => {
60+
references.dispatch({
61+
type: actionName,
62+
payload,
63+
})
64+
return value(references.dispatch, payload, {
5965
getState: references.getState,
6066
})
67+
}
68+
action.actionName = actionName
6169
set(path, actionEffects, action)
6270

6371
// Effect Action Creator
@@ -74,7 +82,7 @@ export const createStore = (model, options = {}) => {
7482
getState: references.getState,
7583
}),
7684
)
77-
action.actionName = path.join('.')
85+
action.actionName = actionName
7886
set(path, actionReducers, action)
7987

8088
// Reducer Action Creator
@@ -85,7 +93,7 @@ export const createStore = (model, options = {}) => {
8593
}),
8694
)
8795
}
88-
} else if (isObject(value) && Object.keys(value).length > 1) {
96+
} else if (isObject(value) && Object.keys(value).length > 0) {
8997
extract(value, path)
9098
} else {
9199
// State

0 commit comments

Comments
 (0)