UNPKG

2.35 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11Object.defineProperty(exports, "__esModule", { value: true });
12const logger_1 = require("../../util/logger");
13const projectEditor_1 = require("./projectEditor");
14/**
15 * Chain the editors, in the given order
16 * @param {ProjectEditor} projectEditors
17 * @return {ProjectEditor}
18 */
19function chainEditors(...projectEditors) {
20 const asProjectEditors = projectEditors.map(projectEditor_1.toEditor);
21 return (p, ctx, params) => __awaiter(this, void 0, void 0, function* () {
22 try {
23 let cumulativeResult = {
24 target: p,
25 success: true,
26 edited: false,
27 };
28 for (const pe of asProjectEditors) {
29 const lastResult = yield pe(p, ctx, params);
30 cumulativeResult = combineEditResults(lastResult, cumulativeResult);
31 }
32 return cumulativeResult;
33 }
34 catch (error) {
35 logger_1.logger.warn("Editor failure in editorChain: %s", error);
36 return { target: p, edited: false, success: false, error };
37 }
38 });
39}
40exports.chainEditors = chainEditors;
41function combineEditResults(r1, r2) {
42 return Object.assign(Object.assign(Object.assign({}, r1), r2), { edited: (r1.edited || r2.edited) ? true :
43 (r1.edited === false && r2.edited === false) ? false : undefined, success: r1.success && r2.success });
44}
45exports.combineEditResults = combineEditResults;
46/**
47 * Useful starting point for editor chaining
48 * @param {Project} p
49 * @constructor
50 */
51exports.NoOpEditor = p => Promise.resolve(projectEditor_1.successfulEdit(p, false));
52//# sourceMappingURL=projectEditorOps.js.map
\No newline at end of file