/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ "use strict";
/** *Amiddlewarewhichactslikeaservice,becauseitisstateful *and"long-running"inthebackground.Itprovidestheability *foractionstoinstallafunctiontoberunoncewhenaspecific *conditionismetbyanactioncomingthroughthesystem.Thinkof *itasathunkthatblocksuntiltheconditionismet.Example: * *```js *constservices={WAIT_UNTIL:require('wait-service').NAME}; * *{type:services.WAIT_UNTIL, *predicate:action=>action.type===constants.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. *} *} *```
*/ const NAME = (exports.NAME = "@@service/waitUntil");
function waitUntilService({ dispatch, getState }) {
let pending = [];
// 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 => action => { if (action.type === NAME) {
pending.push(action); returnnull;
} const result = next(action);
checkPending(action); return result;
};
}
exports.waitUntilService = waitUntilService;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.