@chix/flow
Version:
205 lines • 8.01 kB
JavaScript
;
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 __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 forOf = require("object-forof");
var common_1 = require("../common");
var connector_1 = require("../connector");
var events_1 = require("../events");
var IIP_1 = require("../IIP");
var packet_1 = require("../packet");
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");
function $IIP(Base) {
return (function (_super) {
__extends(IIP$, _super);
function IIP$() {
return _super !== null && _super.apply(this, arguments) || this;
}
IIP$.prototype.createIIP = function (iip) {
if (!this.id) {
throw Error('id is not set.');
}
var xIIP = IIP_1.IIP.create({
source: {
id: this.id,
port: ':iip',
},
target: iip.target,
});
xIIP.set('dispose', true);
if (iip.data === undefined) {
throw Error('IIP data is `undefined`');
}
xIIP.data = iip.data;
this.event(events_1.FlowEvents.ADD_IIP, xIIP.toJSON());
return xIIP;
};
IIP$.prototype.connectIIP = function (iip) {
if (iip.target.constructor.name !== 'Connector') {
var target_1 = new connector_1.Connector();
if (!iip.target.id) {
throw Error('target id is not set.');
}
if (!iip.target.port) {
throw Error('target port is not set.');
}
target_1.plug(iip.target.id, iip.target.port);
forOf(function (key, setting) { return target_1.set(key, setting); }, iip.target.setting);
iip.target = target_1;
}
if (!this.id) {
throw Error('Actor must contain an id');
}
return this.addLink(iip);
};
IIP$.prototype.sendIIPs = function (iips) {
var links = iips
.map(this.createIIP.bind(this))
.map(this.connectIIP.bind(this))
.map(this.__sendIIP.bind(this));
return links;
};
IIP$.prototype.sendIIPsOutstanding = function () {
var e_1, _a;
try {
for (var _b = __values(this.getIIPs()), _c = _b.next(); !_c.done; _c = _b.next()) {
var iip = _c.value;
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; }
}
};
IIP$.prototype.sendIIP = function (target, data) {
if (!this.id) {
throw Error('Actor must contain an id');
}
if (undefined === data) {
throw Error('Refused to send IIP without data');
}
var iipDefinition = {
data: data,
source: {
id: this.id,
port: ':iip',
},
target: target,
};
var iip = this.createIIP(iipDefinition);
this.connectIIP(iip);
this.__sendIIP(iip);
return iip;
};
IIP$.prototype.__sendIIP = function (iip) {
if (!iip.target.id) {
throw Error('target id is not set.');
}
if (!iip.target.port) {
throw Error('target port is not set.');
}
var targetNode = this.getNode(iip.target.id);
if (!targetNode) {
throw Error('Target node not found.');
}
var targetPortType = targetNode.getPortType('input', iip.target.port);
var packet = packet_1.Packet.create(iip.data, targetPortType);
iip.source.write(packet);
return iip;
};
IIP$.prototype.clearIIP = function (iip) {
var e_2, _a;
try {
for (var _b = __values(this.getIIPs()), _c = _b.next(); !_c.done; _c = _b.next()) {
var link = _c.value;
var oldIIP = link;
if ((oldIIP.source.port === ':iip' ||
oldIIP.target.port === iip.target.port ||
oldIIP.target.port === ':start') &&
oldIIP.target.id === iip.target.id) {
this.unplugPort('input', oldIIP.target);
this.ioHandler.disconnect(oldIIP);
this.links.delete(oldIIP.id);
this.event(events_1.FlowEvents.REMOVE_IIP, oldIIP);
}
}
}
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; }
}
};
IIP$.prototype.clearIIPs = function (target) {
var e_3, _a;
try {
for (var _b = __values(this.getIIPs()), _c = _b.next(); !_c.done; _c = _b.next()) {
var link = _c.value;
var iip = link;
if (!target ||
(target.id === iip.target.id && target.port === iip.target.port)) {
this.clearIIP(iip);
}
}
}
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; }
}
};
IIP$.prototype.getIIPs = function () {
return Array.from(this.links.values()).filter(function (link) { return link instanceof IIP_1.IIP; });
};
Object.defineProperty(IIP$.prototype, "iips", {
get: function () {
return this.getIIPs().reduce(function (iips, iip) { return iips.set(iip.id, iip); }, new Map());
},
enumerable: true,
configurable: true
});
return IIP$;
}(Base));
}
exports.$IIP = $IIP;
(function ($IIP) {
function create(Base) {
return util_1.mixin($IIP, 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);
}
$IIP.create = create;
})($IIP = exports.$IIP || (exports.$IIP = {}));
//# sourceMappingURL=iip.js.map