UNPKG

2.02 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const ActionResult_1 = require("./ActionResult");
4/**
5 * Chain the actions, in the given order
6 * @param {ProjectEditor} steps
7 * @return {ProjectEditor}
8 */
9function actionChain(...steps) {
10 return actionChainWithCombiner((r1, r2) => (Object.assign({}, r1, r2)), ...steps);
11}
12exports.actionChain = actionChain;
13function actionChainWithCombiner(combiner, ...steps) {
14 return steps.length === 0 ?
15 exports.NoAction :
16 steps.reduce((c1, c2) => {
17 const ed1 = toAction(c1);
18 const ed2 = toAction(c2);
19 return p => ed1(p).then(r1 => {
20 // console.log("Applied action " + c1.toString());
21 if (!r1.success) {
22 return r1;
23 }
24 else {
25 return ed2(r1.target).then(r2 => {
26 // console.log("Applied action " + c2.toString());
27 const combinedResult = combiner(r1, r2);
28 return combinedResult;
29 });
30 }
31 });
32 }); // Consider adding R as a type parameter to TAction
33}
34exports.actionChainWithCombiner = actionChainWithCombiner;
35function toAction(link) {
36 return p => {
37 try {
38 const oneOrTheOther = link(p);
39 return oneOrTheOther
40 .catch(err => ActionResult_1.failureOn(p, err, link))
41 .then(r => {
42 // See what it returns
43 return ActionResult_1.isActionResult(r) ?
44 r :
45 ActionResult_1.successOn(r);
46 });
47 }
48 catch (error) {
49 // console.error("Failure: " + error.message);
50 return Promise.resolve(ActionResult_1.failureOn(p, error, link));
51 }
52 };
53}
54/**
55 * Useful starting point for chaining
56 */
57exports.NoAction = t => Promise.resolve(ActionResult_1.successOn(t));
58//# sourceMappingURL=actionOps.js.map
\No newline at end of file