Skip to content

Commit

Permalink
Merge pull request #48 from welldone-software/cancel-action
Browse files Browse the repository at this point in the history
Cancel action - implementation
  • Loading branch information
vzaidman authored Apr 8, 2019
2 parents 95b2566 + 45c95d5 commit 2e133d0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/redux-toolbelt-observable/src/makeAsyncEpic.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { from, throwError, of } from 'rxjs'
import { ofType } from 'redux-observable'
import { mergeMap, switchMap, map, catchError } from 'rxjs/operators'
import { mergeMap, switchMap, map, catchError, takeUntil } from 'rxjs/operators'

export default function makeAsyncEpic(actionCreator, asyncFn, {cancelPreviousFunctionCalls = false} = {}) {
const mapFunction = cancelPreviousFunctionCalls ? switchMap : mergeMap
Expand All @@ -23,6 +23,7 @@ export default function makeAsyncEpic(actionCreator, asyncFn, {cancelPreviousFun
const meta = { ...origMeta, _toolbeltAsyncFnArgs: payload }

return obs.pipe(
takeUntil(action$.ofType(actionCreator.cancel.TYPE)),
map(payload => actionCreator.success(payload, meta)),
catchError(error => of(actionCreator.failure(error, meta))),
)
Expand Down
23 changes: 23 additions & 0 deletions packages/redux-toolbelt-observable/test/makeAsyncEpic.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,26 @@ test('Dispatches few success actions for resolved promises and cancel previous r
done()
})
})

test('Dispatches actions for resolved promises and cancel requests', done => {
const store = createStoreForTest(() => Promise.resolve(5))
store.dispatch(actionCreator({a: true}))
store.dispatch(actionCreator({b: true}))
store.dispatch(actionCreator.cancel())

const actions = store.getActions()

setImmediate(() => {
expect(actions[0].type).toBe(actionCreator.TYPE)
expect(actions[0].payload).toEqual({a: true})

expect(actions[1].type).toBe(actionCreator.TYPE)
expect(actions[1].payload).toEqual({b: true})

expect(actions[2].type).toBe(actionCreator.cancel.TYPE)

expect(actions).toHaveLength(3)

done()
})
})

0 comments on commit 2e133d0

Please sign in to comment.