@chix/flow
Version:
375 lines • 15.1 kB
JavaScript
"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 __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spread = (this && this.__spread) || function () {
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
var _debug = require("debug");
var forOf = require("object-forof");
var common_1 = require("../common");
var events_1 = require("../events");
var packet_1 = require("../packet");
var port_1 = require("../port");
var util_1 = require("../util");
var control_1 = require("./control");
var fill_1 = require("./fill");
var nodebox_1 = require("./nodebox");
var port_2 = require("./port");
var debug = _debug('chix:node');
exports.createPorts = util_1.mixin(nodebox_1.$Nodebox, control_1.$Control, common_1.$Event, fill_1.$Fill, common_1.$Identity, port_2.$Port);
function $Ports(Base) {
return (function (_super) {
__extends(Ports$, _super);
function Ports$() {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
var _this = _super.apply(this, __spread(args)) || this;
_this.async = false;
_this.ports = {};
var node = util_1.getNodeArgument(args).node;
if (!node.ports) {
throw Error('NodeDefinition does not declare any ports');
}
return _this;
}
Object.defineProperty(Ports$.prototype, "openPorts", {
get: function () {
var ports = [];
forOf(function (name, port) {
if (port.isOpen()) {
ports.push(name);
}
}, this.ports.input);
return ports;
},
enumerable: true,
configurable: true
});
Ports$.prototype.handleLinkSettings = function (target) {
if (!target.port) {
throw Error('Cannot determine target port.');
}
if (target.has('hold')) {
this.hold();
}
else if (target.has('persist')) {
var index = target.get('index');
var ports = this.ports.input;
if (ports && ports[target.port]) {
var _port = ports[target.port];
if (index) {
if (!Array.isArray(_port.persist)) {
_port.persist = [];
}
;
_port.persist.push(index);
}
else {
_port.persist = true;
}
}
else {
throw Error("Could not find input port " + target.port);
}
}
};
Ports$.prototype.sendPortOutput = function (port, output) {
if (port === 'error' && output.read(this) === null) {
return undefined;
}
if (/error/.test(port)) {
if (!this.portExists('output', port)) {
this.emit('error', {
msg: output.read(output.owner),
node: this,
});
return undefined;
}
var errorPort = this.getPort('output', port);
if (!errorPort.hasConnections()) {
this.emit('error', {
msg: output.read(output.owner),
node: this,
});
return undefined;
}
}
if (!packet_1.Packet.isPacket(output)) {
console.error(output);
throw Error(this.identifier + ": Output for port '" + port + "' must be a Packet");
}
var outputPorts = this.ports.output;
if (outputPorts && outputPorts.hasOwnProperty(port)) {
debug('%s:sendPortOutput port `%s`', this.identifier, port);
this.event(events_1.NodeEvents.OUTPUT, {
node: this,
out: output,
port: port,
});
var _port = outputPorts[port];
_port.write(output);
return undefined;
}
throw Error(this.identifier + ": no such output port " + port);
};
Ports$.prototype._initStartPort = function () {
if (!this.portExists('input', ':start')) {
debug('%s:%s initialized', this.identifier, ':start');
this.addPort('input', ':start', {
name: ':start',
required: false,
type: 'any',
});
}
};
Ports$.prototype.addPort = function (type, port, portDefinition) {
var _this = this;
if (this.portExists(type, port)) {
throw Error(this.identifier + ": Port " + port + " already exists");
}
var _portDefinition = __assign({}, portDefinition);
if (this.nodebox && this.nodebox.on.input.hasOwnProperty(port)) {
_portDefinition.fn = this._createPortBox(this.nodebox.on.input[port].toString(), ('__' + port + '__').toUpperCase());
this.async = true;
portDefinition.async = true;
}
else if (_portDefinition.fn) {
_portDefinition.fn = this._createPortBox(_portDefinition.fn, ('__' + port + '__').toUpperCase());
this.async = true;
_portDefinition.async = true;
}
var _port = port_1.portFactory(type, __assign(__assign({}, _portDefinition), { name: port }));
if (type === 'input' && _port.setState) {
;
_port.setState(this.state);
}
if (portDefinition.options) {
forOf(function (opt, val) {
_this.setPortOption('input', port, opt, val);
}, portDefinition.options);
}
_port.setParent(this);
_port.on(events_1.PortEvents.FILL, this.onPortFill.bind(this));
this.ports[type][port] = _port;
return _port;
};
Ports$.prototype.removePort = function (type, port) {
if (this.portExists(type, port)) {
var _port = this.getPort(type, port);
_port.destroy();
this._deletePort(type, port);
return true;
}
throw Error(this.identifier + ": Port '" + port + "' does not exist");
};
Ports$.prototype.renamePort = function (type, from, to) {
if (this.portExists(type, from)) {
if (this.portExists(type, to)) {
throw Error("Refusing to rename " + type + " port " + from + " to " + to + ", port with that name already exists");
}
var port = this.ports[type];
port[to] = port[from];
this._deletePort(type, from);
return true;
}
throw Error(this.identifier + ": Port '" + from + "' does not exist");
};
Ports$.prototype.getPort = function (type, name) {
if (type === 'input' && name === ':start') {
this._initStartPort();
}
if (this.portExists(type, name)) {
var ports = this.ports[type];
return ports[name];
}
else {
throw new Error(this.identifier + ": Port '" + name + "' does not exist");
}
};
Ports$.prototype.getInputPort = function (name) {
return this.getPort('input', name);
};
Ports$.prototype.getOutputPort = function (name) {
return this.getPort('output', name);
};
Ports$.prototype.getOutputPorts = function () {
var _this = this;
return this.outPorts.map(function (name) { return _this.getOutputPort(name); });
};
Ports$.prototype.getInputPorts = function () {
var _this = this;
return this.inPorts.map(function (name) { return _this.getInputPort(name); });
};
Ports$.prototype.getPortOption = function (type, name, opt) {
return this.getPort(type, name).getOption(opt);
};
Ports$.prototype.portExists = function (type, port) {
var ports = this.ports[type];
return Boolean(ports && ports.hasOwnProperty(port));
};
Ports$.prototype.getPortType = function (kind, port) {
var type;
var ports = this.ports[kind];
if (Array.isArray(port)) {
var obj = ports;
if (obj) {
for (var i = 0; i < port.length; i++) {
if (i === 0) {
if (obj[port[i]]) {
obj = obj[port[i]];
}
}
else {
if (obj.properties) {
obj = obj.properties[port[i]];
}
else {
throw Error(kind + " path " + port + " does not exists");
}
}
}
type = obj.type;
}
else {
throw Error("No " + kind + " ports found");
}
}
else {
if (this.portExists(kind, port)) {
type = ports[port].type;
}
else {
throw new Error(this.identifier + ": Port '" + port + "' does not exist");
}
}
if (type) {
return type;
}
throw Error(this.identifier + ": Unable to determine type for port " + port);
};
Ports$.prototype.setPortOption = function (type, name, opt, value) {
this.getPort(type, name).setOption(opt, value);
};
Ports$.prototype.setPortOptions = function (type, options) {
var _this = this;
forOf(function (port, opt, _val) {
return _this.setPortOption(type, port, opt, options[opt]);
}, options);
};
Ports$.prototype.createPorts = function (portsDefinition) {
var _this = this;
if (!portsDefinition.output) {
portsDefinition.output = {};
}
if (!portsDefinition.input) {
portsDefinition.input = {};
}
this.ports = { input: {}, output: {} };
forOf(function (name, portDefinition) {
return _this.addPort('input', name, portDefinition);
}, portsDefinition.input);
forOf(function (name, portDefinition) {
return _this.addPort('output', name, portDefinition);
}, portsDefinition.output);
forOf(function (_name, event) {
if (event.expose) {
var port = ":" + event.name;
_this.addPort('output', port, { name: port, type: 'any' });
}
}, events_1.NodeEvents);
};
Ports$.prototype._portsAvailable = function () {
var inputPorts = this.ports.input;
return forOf(function (_name, port) {
if ((!port.hasOwnProperty('required') || port.required === true) &&
!port.hasOwnProperty('default')) {
return port + '*';
}
return port;
}, inputPorts).join(', ');
};
Ports$.prototype.closePort = function (_name) { };
Ports$.prototype._deletePort = function (type, port) {
var ports = this.ports[type];
delete ports[port];
};
Object.defineProperty(Ports$.prototype, "inPorts", {
get: function () {
var inputPorts = this.ports.input;
return inputPorts ? Object.keys(inputPorts) : [];
},
enumerable: true,
configurable: true
});
Object.defineProperty(Ports$.prototype, "outPorts", {
get: function () {
var outputPorts = this.ports.output;
return outputPorts ? Object.keys(outputPorts) : [];
},
enumerable: true,
configurable: true
});
Object.defineProperty(Ports$.prototype, "filled", {
get: function () {
var inputPorts = this.ports.input;
if (inputPorts) {
return forOf(function (_name, port) { return port.isFilled() || undefined; }, inputPorts).length;
}
return 0;
},
enumerable: true,
configurable: true
});
return Ports$;
}(Base));
}
exports.$Ports = $Ports;
(function ($Ports) {
function create(Base) {
return util_1.mixin($Ports, nodebox_1.$Nodebox.create, control_1.$Control.create, common_1.$Event.create, fill_1.$Fill.create, common_1.$Identity.create, port_2.$Port.create)(Base);
}
$Ports.create = create;
})($Ports = exports.$Ports || (exports.$Ports = {}));
//# sourceMappingURL=ports.js.map