forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwaitUntilService.js
More file actions
66 lines (60 loc) · 2 KB
/
Copy pathwaitUntilService.js
File metadata and controls
66 lines (60 loc) · 2 KB
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
const WAIT_UNTIL_TYPE = "@@service/waitUntil";
/**
* A middleware which acts like a service, because it is stateful
* and "long-running" in the background. It provides the ability
* for actions to install a function to be run once when a specific
* condition is met by an action coming through the system. Think of
* it as a thunk that blocks until the condition is met. Example:
*
* ```js
* const services = { WAIT_UNTIL: require('wait-service').NAME };
*
* { type: services.WAIT_UNTIL,
* predicate: action => action.type === "ADD_ITEM",
* run: (dispatch, getState, action) => {
* // Do anything here. You only need to accept the arguments
* // if you need them. `action` is the action that satisfied
* // the predicate.
* }
* }
* ```
*/
function waitUntilService({ dispatch, getState }) {
let pending = [];
function checkPending(action) {
const readyRequests = [];
const stillPending = [];
// Find the pending requests whose predicates are satisfied with
// this action. Wait to run the requests until after we update the
// pending queue because the request handler may synchronously
// dispatch again and run this service (that use case is
// completely valid).
for (const request of pending) {
if (request.predicate(action)) {
readyRequests.push(request);
} else {
stillPending.push(request);
}
}
pending = stillPending;
for (const request of readyRequests) {
request.run(dispatch, getState, action);
}
}
return (next: Function) => (action: Object) => {
if (action.type === WAIT_UNTIL_TYPE) {
pending.push(action);
return null;
}
const result = next(action);
checkPending(action);
return result;
};
}
module.exports = {
WAIT_UNTIL_TYPE,
waitUntilService
};