UNPKG

@chix/flow

Version:
319 lines 13.3 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; Object.defineProperty(exports, "__esModule", { value: true }); var _debug = require("debug"); var common_1 = require("../common/"); var connector_1 = require("../connector"); var events_1 = require("../events"); var handler_1 = require("../io/handler"); var util_1 = require("../util"); var iip_1 = require("./iip"); var link_1 = require("./link"); var node_1 = require("./node"); var status_1 = require("./status"); var debug = _debug('chix:actor'); function $Control(Base) { return (function (_super) { __extends(Control$, _super); function Control$() { var _this = _super !== null && _super.apply(this, arguments) || this; _this._hold = false; _this.active = false; _this.ready = true; _this.runCount = 0; _this.ioHandler = new handler_1.IoHandler(); return _this; } Control$.prototype.setIoHandler = function (ioHandler) { this.ioHandler = ioHandler; return this; }; Control$.prototype.hold = function (id) { if (id) { this.getNode(id).hold(); this.setStatus(status_1.$Status.HOLD); } else { this._hold = true; this.stop(); } return this; }; Control$.prototype.isHalted = function (id) { if (id) { this.getNode(id).isHalted(); } return this._hold; }; Control$.prototype.complete = function () { this.ready = false; this.active = false; }; Control$.prototype.push = function () { var e_1, _a; this.setStatus(status_1.$Status.RUNNING); debug('%s: push()', this.identifier); try { for (var _b = __values(this.nodes.values()), _c = _b.next(); !_c.done; _c = _b.next()) { var node = _c.value; if (node.isStartable() && node.id) { var iip = new connector_1.Connector(); iip.plug(node.id, ':start'); this.sendIIP(iip, ''); } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_1) throw e_1.error; } } this.setStatus('running'); return this; }; Control$.prototype.start = function (push) { var e_2, _a; if (['created', 'running', 'stopped'].indexOf(this.status) >= 0) { this.setStatus('created'); debug('%s: start', this.identifier); try { for (var _b = __values(this.nodes.values()), _c = _b.next(); !_c.done; _c = _b.next()) { var node = _c.value; if (node.type === 'flow') { node.start(); } else { ; node.onPortFill(); this.setStatus(status_1.$Status.RUNNING); } } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_2) throw e_2.error; } } if (push !== false) { } else { this.setStatus('started'); } this.event(events_1.FlowEvents.START, { node: this, }); return this; } throw Error("To start actor must be in created state, current status: " + this.status); }; Control$.prototype.stop = function () { this.status = status_1.$Status.STOPPED; if (this.ioHandler) { this.ioHandler.reset(); this.clearIIPs(); this.event(events_1.FlowEvents.STOP, { node: this, }); this.ioHandler._shutdown = false; } }; Control$.prototype.pause = function () { var e_3, _a; this.status = status_1.$Status.STOPPED; try { for (var _b = __values(this.nodes.values()), _c = _b.next(); !_c.done; _c = _b.next()) { var node = _c.value; node.hold(); } } catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_3) throw e_3.error; } } return this; }; Control$.prototype.resume = function () { var e_4, _a; this.status = status_1.$Status.RUNNING; try { for (var _b = __values(this.nodes.values()), _c = _b.next(); !_c.done; _c = _b.next()) { var node = _c.value; node.release(); } } catch (e_4_1) { e_4 = { error: e_4_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_4) throw e_4.error; } } return this; }; Control$.prototype.release = function (id) { if (id) { return this.getNode(id).release(); } this._hold = false; this.resume(); }; Control$.prototype.shutdown = function () { }; Control$.prototype.destroy = function () { var e_5, _a; try { for (var _b = __values(this.nodes.values()), _c = _b.next(); !_c.done; _c = _b.next()) { var node = _c.value; node.destroy(); } } catch (e_5_1) { e_5 = { error: e_5_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_5) throw e_5.error; } } }; Control$.prototype.reset = function () { var e_6, _a; try { for (var _b = __values(this.nodes.values()), _c = _b.next(); !_c.done; _c = _b.next()) { var node = _c.value; node.reset(); } } catch (e_6_1) { e_6 = { error: e_6_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_6) throw e_6.error; } } this.runCount = 0; if (this.identifier === 'flow:main' && this.ioHandler) { this.ioHandler.reset(); } return this; }; Control$.prototype.clear = function () { return __awaiter(this, void 0, void 0, function () { var total, _a, _b, node, e_7_1; var e_7, _c; return __generator(this, function (_d) { switch (_d.label) { case 0: total = this.nodes.size; if (!(total > 0)) return [3, 8]; _d.label = 1; case 1: _d.trys.push([1, 6, 7, 8]); _a = __values(this.nodes.values()), _b = _a.next(); _d.label = 2; case 2: if (!!_b.done) return [3, 5]; node = _b.value; if (!node.id) return [3, 4]; return [4, this.removeNode(node.id)]; case 3: _d.sent(); this.removeLinks(node.id); _d.label = 4; case 4: _b = _a.next(); return [3, 2]; case 5: return [3, 8]; case 6: e_7_1 = _d.sent(); e_7 = { error: e_7_1 }; return [3, 8]; case 7: try { if (_b && !_b.done && (_c = _a.return)) _c.call(_a); } finally { if (e_7) throw e_7.error; } return [7]; case 8: return [2]; } }); }); }; Control$.prototype.run = function () { throw Error('Run is deprecated.'); }; return Control$; }(Base)); } exports.$Control = $Control; (function ($Control) { function create(Base) { return util_1.mixin($Control, common_1.$Event.create, common_1.$Identity.create, iip_1.$IIP.create, link_1.$Link.create, node_1.$Node.create, common_1.$Status.create)(Base); } $Control.create = create; })($Control = exports.$Control || (exports.$Control = {})); //# sourceMappingURL=control.js.map