forked from WorldBrain/Memex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredux-async-action-creator.js
114 lines (102 loc) · 3.79 KB
/
redux-async-action-creator.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Wrap an action creator to dispatch extra actions when it starts and when it
// completes (or fails).
// Inspired by: http://engineering.blogfoster.com/managing-complexity-in-redux-higher-order-reducers-and-async-state/
import { createAction } from 'redux-act'
export default function asyncActionCreator(
// actionCreator is assumed to create a thunk that returns a promise.
actionCreator,
{
pending = createAction('pending'),
finished = createAction('finished'),
complete = createAction('complete'),
error = createAction('error'),
cancelled = createAction('cancelled'),
// exclusive: lets multiple invocations exclude each other.
// false (default): each action runs independently.
// true: ignore any calls when this action is already running.
// 'takeLast': run every action after cancelling any uncompleted ones.
exclusive = false,
} = {},
) {
// We keep a list of all running instances of the action, to be able to
// check if any are pending or cancel them all.
let runningTransactions = []
function isPending() {
return runningTransactions.length > 0
}
function cancelAll() {
runningTransactions.forEach(transaction => {
transaction.cancel()
})
runningTransactions = []
}
const newActionCreator = (...args) => {
return async dispatch => {
if (isPending() && exclusive === true) {
return
}
if (isPending() && exclusive === 'takeLast') {
cancelAll()
}
// Run the actionCreator
const action = actionCreator(...args)
dispatch(pending({ action, args }))
const removeFromTransactionList = () => {
runningTransactions = runningTransactions.filter(
v => v !== transaction,
)
}
// Cancels this transaction. Put more accurately, we just ignore its
// results, because a Javascript Promise cannot really be cancelled.
const cancel = () => {
cancelled && dispatch(cancelled({ action, args }))
finished &&
dispatch(
finished({
value: undefined,
error: undefined,
cancelled: true,
}),
)
removeFromTransactionList()
}
const transaction = {
cancel,
}
runningTransactions.push(transaction)
let value
let err
try {
const promise = dispatch(action)
value = await promise
} catch (err_) {
err = err_
}
// If the transaction was not cancelled, dispatch some actions.
if (runningTransactions.includes(transaction)) {
if (err) {
error && dispatch(error({ error: err }))
} else {
complete && dispatch(complete({ value }))
}
finished &&
dispatch(
finished({
value,
error: err,
cancelled: false,
}),
)
removeFromTransactionList()
}
}
}
newActionCreator.pending = pending
newActionCreator.finished = finished
newActionCreator.complete = complete
newActionCreator.error = error
newActionCreator.cancelled = cancelled
newActionCreator.isPending = isPending
newActionCreator.cancelAll = cancelAll
return newActionCreator
}