UNPKG

@chix/flow

Version:
235 lines 10.2 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 __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; 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 util = require("util"); var common_1 = require("../common/"); var events_1 = require("../events"); var IIP_1 = require("../IIP"); var link_1 = require("../link"); var util_1 = require("../util"); var control_1 = require("./control"); var node_1 = require("./node"); var port_1 = require("./port"); var process_1 = require("./process"); var debug = _debug('chix:actor'); function $Link(Base) { return (function (_super) { __extends(Link$, _super); function Link$() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.links = new Map(); return _this; } Link$.prototype.addLink = function (link) { var _this = this; debug('%s: addLink()', this.identifier); var sourceNode; if (!(link instanceof link_1.Link)) { throw Error('Link must be of type Link'); } var existingLink = this._findLink(link); if (!(link instanceof IIP_1.IIP) && existingLink) { console.log('WARNING: link already added, FIXME'); this.event(events_1.FlowEvents.ADD_LINK, existingLink); return existingLink; } if (link.source.id !== this.id) { sourceNode = this.getNode(link.source.id); if (!sourceNode.portExists('output', link.source.port)) { if (!sourceNode.ports.output) { throw Error("Target node (" + sourceNode.identifier + ") does not have any input ports named " + link.source.port); } else { throw Error(util.format('Source node (%s) does not have an output port named `%s`\n\n' + '\tOutput ports available:\t%s\n', sourceNode.identifier, link.source.port, Object.keys(sourceNode.ports.output).join(', '))); } } } var targetNode = this.getNode(link.target.id); debug('%s: %s %s -> %s %s', this.identifier, sourceNode ? sourceNode.identifier : '', link.source ? link.source.port : '', link.target.port, targetNode.identifier); if (link.target.port !== ':start' && !targetNode.portExists('input', link.target.port)) { if (!targetNode.ports.input) { throw Error("Target node (" + targetNode.identifier + ") does not have any input ports named " + link.target.port); } else { var inputPorts = Object.keys(targetNode.ports.input); throw Error(util.format('Target node (%s) does not have an input port named `%s`\n\n' + '\tInput ports available:\t%s\n', targetNode.identifier, link.target.port, inputPorts.join(', '))); } } if (link.target.has('sync')) { link.target.set('sync', this.getNode(link.target.get('sync')).pid); } link.graphId = this.id; link.graphPid = this.pid; if (link.source.id) { if (link.source.id === this.id) { link.setSourcePid(this.pid || this.id); } else { link.setSourcePid(this.getNode(link.source.id).pid); } } link.setTargetPid(this.getNode(link.target.id).pid); this.links.set(link.id, link); this.ioHandler.connect(link); this.plugPort('input', link.target); if (link.source.id !== this.id) { this.plugPort('output', link.source); } link.on('change', function () { _this.event(events_1.FlowEvents.CHANGE_LINK, link); }); this.event(events_1.FlowEvents.ADD_LINK, link); return link; }; Link$.prototype.getLink = function (id) { return this.links.get(id); }; Link$.prototype.findLink = function (ln) { var foundLink = this._findLink(ln); if (!foundLink) { throw Error('findLink: Unable to find link.'); } return foundLink; }; Link$.prototype.hasLink = function (ln) { return Boolean(this._findLink(ln)); }; Link$.prototype.removeLink = function (ln) { var link = this.links.get(ln.id); if (!link) { console.warn('FIXME: cannot find link'); return this; } this.unplugPort('input', link.target); if (link.source.id !== this.id) { this.unplugPort('output', link.source); } this.ioHandler.disconnect(link); if (this.links.has(link.id)) { var oldLink = this.links.get(link.id); this.links.delete(link.id); this.event(events_1.FlowEvents.REMOVE_LINK, oldLink); return this; } throw Error("Unable to remove link with id: " + link.id); }; Link$.prototype.getLinks = function () { return Array.from(this.links.values()).filter(function (link) { return !(link instanceof IIP_1.IIP); }); }; Link$.prototype.hasLinks = function (nodeId) { var e_1, _a; if (nodeId) { try { for (var _b = __values(this.links.values()), _c = _b.next(); !_c.done; _c = _b.next()) { var link = _c.value; if (link.source.id === nodeId || link.target.id === nodeId) { return true; } } } 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; } } return false; } return this.links.size > 0; }; Link$.prototype.removeLinks = function (nodeId) { var e_2, _a; try { for (var _b = __values(this.links.values()), _c = _b.next(); !_c.done; _c = _b.next()) { var link = _c.value; if (link.source.id === nodeId || link.target.id === nodeId) { this.removeLink(link); } } } 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; } } }; Link$.prototype._findLink = function (ln) { var foundLink = Array.from(this.links.values()).find(function (_link) { var link = __assign({ source: {} }, _link); if (ln.source && link.source && ln.source.id === link.source.id && ln.target && link.target && ln.target.id === link.target.id && ln.source.port === link.source.port && ln.target.port === link.target.port) { return ((!ln.source.setting || !ln.source.setting.index || !ln.source.setting || !ln.source.setting.index || !link.source.setting || ln.source.setting.index === link.source.setting.index) && (!ln.target.setting || !ln.target.setting.index || !link.target.setting || !link.target.setting.index || !link.target.setting || ln.target.setting.index === link.target.setting.index)); } return false; }); return foundLink; }; return Link$; }(Base)); } exports.$Link = $Link; (function ($Link) { function create(Base) { return util_1.mixin($Link, control_1.$Control.create, common_1.$Event.create, common_1.$Identity.create, node_1.$Node.create, port_1.$Port.create, process_1.$Process.create)(Base); } $Link.create = create; })($Link = exports.$Link || (exports.$Link = {})); //# sourceMappingURL=link.js.map