Skip to content

Commit ea4a4eb

Browse files
mdimovskaognen
authored andcommitted
fix(core): apply returned update when effect isn't an async function (#95)
1 parent e12b3a9 commit ea4a4eb

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

packages/core/src/effect/__tests__/effects.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ describe('effects API', function() {
4646

4747
// a typical effect that updates the element state when finishing
4848

49-
effect.register(['article'], 'mark', async (context, action) => {
49+
effect.register(['article'], 'mark', (context, action) => {
50+
return el => el.set('marked', action.type)
51+
})
52+
53+
effect.register(['article'], 'mark-async', async (context, action) => {
5054
await sleep(50)
5155
return el => el.set('marked', action.type)
5256
})
@@ -142,15 +146,25 @@ describe('effects API', function() {
142146
test('Effect can return an updating fn state => newState', async () => {
143147
const focus = kernel.focusOn(['element1', 'element2', 'element3'])
144148

149+
expect(focus.query().get('marked')).not.toEqual('mark')
150+
145151
focus.dispatch({ type: 'mark' })
146152

153+
expect(focus.query().get('marked')).toEqual('mark')
154+
})
155+
156+
test('An async effect can return an updating fn state => newState', async () => {
157+
const focus = kernel.focusOn(['element1', 'element2', 'element3'])
158+
159+
focus.dispatch({ type: 'mark-async' })
160+
147161
await sleep(25)
148162

149-
expect(focus.query().get('marked')).not.toEqual('mark')
163+
expect(focus.query().get('marked')).not.toEqual('mark-async')
150164

151165
await sleep(50)
152166

153-
expect(focus.query().get('marked')).toEqual('mark')
167+
expect(focus.query().get('marked')).toEqual('mark-async')
154168
})
155169

156170
test('An async effect can return undefined', () => {

packages/core/src/effect/impl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export const middleware = R.curry((config, store, next, action) => {
4949
error('Exception while executing an effect: ', e)
5050
})
5151
} else if (typeof result === 'function') {
52-
context.dispatch({ type: updateStateAction, result })
52+
context.dispatch({ type: updateStateAction, updateFn: result })
5353
}
5454
} else {
5555
// the effect consumes the action

0 commit comments

Comments
 (0)