-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
53 lines (48 loc) · 1.55 KB
/
index.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
var BUFFERED_ACTION_RETURN = 'redux-action-buffer: buffered action'
var setImmediate = typeof global !== 'undefined' &&
typeof global.setImmediate !== 'undefined'
? global.setImmediate
: setTimeout
module.exports = function bufferActions(options, cb) {
var active = true
var queue = []
var passthrough = (options && options.passthrough) || []
var breaker = typeof options === 'object' ? options.breaker : options
var breakerType = typeof breaker
if (breakerType === 'string' || breakerType === 'symbol') {
var actionType = breaker
breaker = function(action) {
if (action.type === actionType) return true
else return false
}
}
return function(store) {
return function(next) {
return function(action) {
// console.log("next", next, action);
if (!active || passthrough.includes(action.type)) return next(action)
if (breaker(action)) {
active = false
var result = next(action)
setImmediate(function() {
var queueResults = []
queue.forEach(function(queuedAction) {
var queuedActionResult = next(queuedAction)
queueResults.push(queuedActionResult)
})
cb &&
cb(null, {
results: queueResults,
queue: queue
})
})
return result
} else {
queue.push(action)
// @TODO consider returning a dummy action, or maybe null for cleanliness
return BUFFERED_ACTION_RETURN
}
}
}
}
}