1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { isPromise } from '../../util'
const defaultTypes = ['PENDING', 'FULFILLED', 'REJECTED']
export default function promiseMiddleware (config = {}) {
const promiseTypeSuffixes = config.promiseTypeSuffixes || defaultTypes
return (_ref) => {
const dispatch = _ref.dispatch
return next => action => {
if (!isPromise(action.payload)) {
return next(action)
}
const { type, payload, meta, params = {} } = action
const { promise, data } = payload
const [ PENDING, FULFILLED, REJECTED ] = (meta || {}).promiseTypeSuffixes || promiseTypeSuffixes
/**
* Dispatch the first async handler. This tells the
* reducer that an async action has been dispatched.
*/
next({
type: `${type}_${PENDING}`,
...!!data ? { payload: data, params } : {},
...!!meta ? { meta } : {}
})
const isAction = resolved => resolved && (resolved.meta || resolved.payload)
const isThunk = resolved => typeof resolved === 'function'
const getResolveAction = isError => ({
type: `${type}_${isError ? REJECTED : FULFILLED}`,
...!!meta ? { meta } : {},
...!!isError ? { error: true } : {}
})
/**
* Re-dispatch one of:
* 1. a thunk, bound to a resolved/rejected object containing ?meta and type
* 2. the resolved/rejected object, if it looks like an action, merged into action
* 3. a resolve/rejected action with the resolve/rejected object as a payload
*/
action.payload.promise = promise.then(
(resolved = {}) => {
const resolveAction = getResolveAction()
return dispatch(isThunk(resolved) ? resolved.bind(null, resolveAction) : {
...resolveAction,
...isAction(resolved) ? resolved : {
...!!resolved && { payload: resolved, params }
}
})
},
(rejected = {}) => {
const resolveAction = getResolveAction(true)
return dispatch(isThunk(rejected) ? rejected.bind(null, resolveAction) : {
...resolveAction,
...isAction(rejected) ? rejected : {
...!!rejected && { payload: rejected, params }
}
})
}
)
return action
}
}
}