@chix/flow
Version:
445 lines • 16.4 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 __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 events_1 = require("../events");
var packet_1 = require("../packet/");
var port_1 = require("./port");
var debug = _debug('chix:inputPort');
var InputPort = (function (_super) {
__extends(InputPort, _super);
function InputPort(portDefinition) {
var _this = _super.call(this, portDefinition) || this;
_this.data = [];
_this.required = true;
_this._isCycling = false;
_this.receiveListener = _this.receiveListener.bind(_this);
if (portDefinition) {
if (portDefinition.hasOwnProperty('default')) {
_this.setDefault(portDefinition.default);
}
if (portDefinition.context) {
_this.setContext(portDefinition.context);
}
if (portDefinition.required === false) {
_this.required = false;
}
}
_this.reset();
return _this;
}
Object.defineProperty(InputPort.prototype, "persist", {
get: function () {
return this._persist;
},
set: function (persist) {
this._persist = persist;
},
enumerable: true,
configurable: true
});
InputPort.prototype.receiveListener = function (packet, target) {
this.receive(packet, target.get('index'), target);
};
InputPort.prototype.close = function () {
_super.prototype.close.call(this);
debug('close %s port', this.name);
};
InputPort.prototype.isSync = function () {
return true;
};
InputPort.prototype.isAsync = function () {
return false;
};
InputPort.prototype.cycle = function (packet, index, target) {
var e_1, _a;
var values = packet.read(this);
this._isCycling = true;
if (Array.isArray(values) && this.type !== 'array') {
try {
for (var values_1 = __values(values), values_1_1 = values_1.next(); !values_1_1.done; values_1_1 = values_1.next()) {
var value = values_1_1.value;
this.receive(packet.clone(this).write(this, value), index, target);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (values_1_1 && !values_1_1.done && (_a = values_1.return)) _a.call(values_1);
}
finally { if (e_1) throw e_1.error; }
}
}
this._isCycling = false;
};
InputPort.prototype.receive = function (value, index, target) {
var packet = packet_1.Packet.isPacket(value)
? value
: this.createPacketFromValue(value);
packet.setOwner(this);
if (!this._isCycling && target && target.has('cyclic')) {
this.cycle(packet, index, target);
return this;
}
if (this._validatePacket(packet, index)) {
if (index === undefined) {
this.fill(packet, target);
}
else {
this.indexed = true;
this.fillIndex(packet, index, target);
}
}
return this;
};
InputPort.prototype.fill = function (packet, target) {
if (this.hasParent()) {
var _node = this.getParent();
debug('fill -> (%s) %s', this.name, _node ? _node.title || _node.name : '');
}
if (!packet_1.Packet.isPacket(packet)) {
packet = this.createPacketFromValue(packet).setOwner(this);
}
if (this.persist ||
(target && target.setting.persist)) {
this.__persist = packet.clone(this);
}
this._handleFunctionType(packet);
if (this._validatePacket(packet)) {
this.data.push(packet);
this.fills++;
this.emit(events_1.PortEvents.FILL);
}
return this;
};
InputPort.prototype.fillIndex = function (packet, index, target) {
var e_2, _a, _b;
if (this.hasParent()) {
var _node = this.getParent();
debug('fill -> [%s] (%s) %s', index, this.name, _node ? _node.title || _node.name : '');
}
try {
for (var _c = __values(this.data), _d = _c.next(); !_d.done; _d = _c.next()) {
var packetData = _d.value;
var value = packetData.read(this);
if (value[index] === undefined) {
value[index] = packet.read(this);
packet_1.Packet.metaMerge(packetData._meta, packet._meta);
this.emit(events_1.PortEvents.FILL);
return;
}
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
}
finally { if (e_2) throw e_2.error; }
}
var data;
if (this.type === 'object') {
data = (_b = {}, _b[index] = packet.read(this), _b);
}
else if (this.type === 'array') {
data = [];
data[index] = packet.read(this);
}
else {
throw Error('index on non object/array port');
}
this.fill(this.createPacketFromValue(data).setOwner(this), target);
};
InputPort.prototype.reset = function () {
this.clearInput();
_super.prototype.reset.call(this);
return this;
};
Object.defineProperty(InputPort.prototype, "filled", {
get: function () {
return this.isFilled() !== port_1.Port.EMPTY;
},
enumerable: true,
configurable: true
});
InputPort.prototype.isFilled = function () {
var e_3, _a, e_4, _b;
if (this.hasConnections()) {
if (this.indexed) {
if (this.data.length) {
var data = this.data[0].read(this);
if (typeof data === 'object' && this.type === 'object') {
if (Object.keys(data).length !== this._connections.length) {
return port_1.Port.EMPTY;
}
}
else if (Array.isArray(data) && this.type === 'array') {
if (this._connections.length &&
data.length !== this._connections.length) {
return port_1.Port.EMPTY;
}
try {
for (var data_1 = __values(data), data_1_1 = data_1.next(); !data_1_1.done; data_1_1 = data_1.next()) {
var packet = data_1_1.value;
if (packet === undefined) {
return port_1.Port.EMPTY;
}
}
}
catch (e_3_1) { e_3 = { error: e_3_1 }; }
finally {
try {
if (data_1_1 && !data_1_1.done && (_a = data_1.return)) _a.call(data_1);
}
finally { if (e_3) throw e_3.error; }
}
}
else {
throw Error('non array/object port cannot be indexed');
}
}
}
if (this.data.length) {
return port_1.Port.FILLED;
}
else if (this.__persist) {
return port_1.Port.PERSIST;
}
}
else if (this.data.length) {
if (this.indexed) {
var data = this.data[0].read(this);
try {
for (var data_2 = __values(data), data_2_1 = data_2.next(); !data_2_1.done; data_2_1 = data_2.next()) {
var packet = data_2_1.value;
if (packet === undefined) {
return port_1.Port.EMPTY;
}
}
}
catch (e_4_1) { e_4 = { error: e_4_1 }; }
finally {
try {
if (data_2_1 && !data_2_1.done && (_b = data_2.return)) _b.call(data_2);
}
finally { if (e_4) throw e_4.error; }
}
}
return port_1.Port.DIRECT;
}
else if (this.context) {
return port_1.Port.CONTEXT;
}
else if (this.default) {
return port_1.Port.DEFAULT;
}
else if (this.required === false) {
return port_1.Port.NOT_REQUIRED;
}
return port_1.Port.EMPTY;
};
InputPort.prototype.isRequired = function () {
return !!this.required;
};
InputPort.prototype.read = function () {
if (this.hasConnections()) {
if (this.data.length) {
this.indexed = false;
this.reads++;
return this.data.shift().release(this);
}
else if (this.__persist) {
this.reads++;
return this.__persist.clone(this).release(this);
}
}
else if (this.data.length) {
this.reads++;
return this.data.shift().release(this);
}
else if (this.context) {
this.reads++;
return this.context.clone(this).release(this);
}
else if (this.default) {
this.reads++;
return this.default.clone(this).release(this);
}
else if (!this.required) {
return new packet_1.Packet(undefined);
}
console.log(this);
throw Error('Unable to read port data');
};
InputPort.prototype.peek = function () {
if (this.hasConnections()) {
if (this.data.length) {
this.indexed = false;
return this.data[0].release(this);
}
else if (this.__persist) {
return this.__persist.clone(this).release(this);
}
}
else if (this.data.length) {
return this.data[0].release(this);
}
else if (this.context) {
return this.context.clone(this).release(this);
}
else if (this.default) {
return this.default.clone(this).release(this);
}
else if (!this.required) {
return new packet_1.Packet(undefined);
}
throw Error('Unable to read port data');
};
InputPort.prototype.connect = function (link) {
if (!this.hasConnection(link)) {
this.open();
debug('Connecting link');
link.target.on(events_1.ConnectorEvents.DATA, this.receiveListener);
this._connections.push(link);
}
else {
console.log('Link already connected');
}
return this;
};
InputPort.prototype.plug = function (target) {
if (target.wire && !this.hasConnection(target.wire)) {
this.open();
var index = target.get('index');
if (index !== undefined && this.type === 'any') {
this.type = isNaN(parseInt(index, 10)) ? 'object' : 'array';
}
debug('plug %s -> %s(%s)', target.wire.source.port, index === undefined ? '' : "[" + index + "] ", target.port);
target.on(events_1.ConnectorEvents.DATA, this.receiveListener);
this._connections.push(target.wire);
}
else {
console.log('Link already connected');
}
return this;
};
InputPort.prototype.disconnect = function (link) {
if (this.hasConnection(link)) {
if (this._connections.length === 1) {
this.close();
if (this.__persist) {
this.__persist = undefined;
}
}
link.target.removeListener(events_1.ConnectorEvents.DATA, this.receiveListener);
this._connections.splice(this._connections.indexOf(link), 1);
}
else {
console.log('Link already disconnected');
}
};
InputPort.prototype.unplug = function (target) {
if (target.wire && this.hasConnection(target.wire)) {
var index = target.get('index');
if (index !== undefined && this.type === 'any') {
this.type = isNaN(parseInt(index, 10)) ? 'object' : 'array';
}
debug('unplug %s -> %s(%s)', target.wire.source.port, index === undefined ? '' : "[" + index + "] ", target.port);
if (this._connections.length === 1) {
this.close();
if (this.__persist) {
this.__persist = undefined;
}
}
target.removeListener(events_1.ConnectorEvents.DATA, this.receiveListener);
this._connections.splice(this._connections.indexOf(target.wire), 1);
}
else {
console.log('Link already disconnected');
}
};
InputPort.prototype.clearInput = function () {
this.data = [];
return this;
};
InputPort.prototype.setDefault = function (val) {
var p = packet_1.Packet.isPacket(val) ? val : this.createPacketFromValue(val);
this._handleFunctionType(p);
if (this._validatePacket(p)) {
this.default = p;
}
return this;
};
InputPort.prototype.hasDefault = function () {
return this.default !== undefined;
};
InputPort.prototype.clearDefault = function () {
this.default = undefined;
return this;
};
InputPort.prototype.setContext = function (value, trigger) {
if (value === undefined) {
throw Error('Refuse to set context to undefined');
}
var packet = packet_1.Packet.isPacket(value)
? value
: this.createPacketFromValue(value);
this._handleFunctionType(packet);
if (this._validatePacket(packet)) {
this.context = packet;
if (trigger) {
this.emit(events_1.PortEvents.FILL);
}
}
return this;
};
InputPort.prototype.hasContext = function () {
return this.context !== undefined;
};
InputPort.prototype.clearContext = function () {
this.context = undefined;
return this;
};
InputPort.prototype.setPersist = function (value) {
this.persist = value;
return this;
};
InputPort.prototype.hasPersist = function () {
return this.__persist !== undefined;
};
InputPort.prototype.clearPersist = function () {
this.__persist = undefined;
return this;
};
InputPort.prototype.destroy = function () {
this.clearContext();
this.clearPersist();
this.clearInput();
};
return InputPort;
}(port_1.Port));
exports.InputPort = InputPort;
//# sourceMappingURL=input.js.map