UNPKG

2.28 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 Operation_1 = require("./Operation");
13/**
14 * Operation Subclass that accepts a function as an argument which will be the task to run.
15 * Additionally, multiple functions can be appended to run simultaneously as the same task.
16 */
17class BlockOperation extends Operation_1.Operation {
18 constructor() {
19 let id;
20 let block;
21 const first = arguments[0];
22 if (typeof first === 'number') {
23 id = first;
24 const second = arguments[1];
25 if (typeof second === 'function') {
26 block = second;
27 }
28 }
29 else if (typeof first === 'function') {
30 block = first;
31 }
32 else {
33 throw new Error('Wrong arguments passed: missing ID and/or function');
34 }
35 super(id);
36 this.blocks = [block];
37 }
38 /**
39 *
40 */
41 run() {
42 return __awaiter(this, void 0, void 0, function* () {
43 const promises = [];
44 this.blocks.forEach(block => {
45 promises.push(block(this));
46 });
47 const results = yield Promise.all(promises);
48 return results.length === 1
49 ? results[0]
50 : results;
51 });
52 }
53 /**
54 * Append another function to run simultaneously as the same task
55 * @param {function} block - function to add
56 */
57 addBlock(block) {
58 this.blocks.push(block);
59 }
60}
61exports.BlockOperation = BlockOperation;
62//# sourceMappingURL=BlockOperation.js.map
\No newline at end of file