@chix/flow
Version:
195 lines • 6.38 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 __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 events_1 = require("events");
var forOf = require("object-forof");
var common_1 = require("../common");
var packet_1 = require("../packet");
var util_1 = require("../util");
var validate_1 = require("../validate");
function PortMix(Base) {
return util_1.mixin(common_1.$Parent)(Base);
}
var Port = (function (_super) {
__extends(Port, _super);
function Port(portDefinition) {
var _this = _super.call(this) || this;
_this.name = '';
_this.indexed = false;
_this._open = false;
_this.args = [];
_this.fills = 0;
_this.reads = 0;
_this.runCount = 0;
_this._connections = [];
Object.assign(_this, portDefinition);
if (!_this.name) {
console.log(portDefinition);
throw Error('Port name is required');
}
if (!_this.type) {
console.log(portDefinition);
throw Error('Port type is required');
}
_this.async = !!_this.fn;
return _this;
}
Port.prototype.isOpen = function () {
return this._open;
};
Port.prototype.isClosed = function () {
return !this._open;
};
Port.prototype.open = function () {
this._open = true;
};
Port.prototype.close = function () {
this._open = false;
};
Port.prototype.hasConnection = function (link) {
return this._connections && this._connections.indexOf(link) >= 0;
};
Port.prototype.hasConnections = function () {
return this._connections.length > 0;
};
Port.prototype.getConnections = function () {
return this._connections;
};
Port.prototype.isFilled = function () {
throw Error('method not implemented');
};
Port.prototype.reset = function () {
this.fills = 0;
this.reads = 0;
this.runCount = 0;
this.close();
};
Port.prototype.isAvailable = function () { };
Port.prototype.getOption = function (opt) {
if (this.hasOwnProperty(opt)) {
return this[opt];
}
else {
return undefined;
}
};
Port.prototype.setOption = function (opt, value) {
;
this[opt] = value;
};
Port.prototype.setOptions = function (options) {
var _this = this;
forOf(function (opt, val) { return _this.setOption(opt, val); }, options);
};
Port.prototype.destroy = function () {
throw Error('method destroy() not implemented');
};
Port.prototype._handleFunctionType = function (packet) {
if (this.type === 'function') {
var data = packet.read(this);
if (data !== null) {
var type = typeof data;
if (type === 'string') {
var args = this.args ? this.args : [];
packet.write(this, new (Function.bind.apply(Function, __spread([void 0], args, [data])))());
}
else {
if (type !== 'function' && type !== 'object') {
var node = this.getParent();
throw Error(node.identifier + ": port " + this.name + " expects a function");
}
}
}
}
};
Port.prototype._validatePacket = function (packet, index) {
var data = packet.read(this);
if (index !== undefined) {
return true;
}
if (data !== null) {
if (!validate_1.validate.data(this.type, data)) {
var real = Object.prototype.toString.call(data).match(/\s(\w+)/)[1];
if (data &&
typeof data === 'object' &&
data.constructor.name === 'Object') {
var tmp = Object.getPrototypeOf(data).constructor.name;
if (tmp) {
real = tmp;
}
}
this.emit('error', Error("Expected '" + this.type + "' got '" + real + "' on port '" + this.name + "'"));
}
}
return true;
};
Port.prototype.createPacketFromValue = function (packet) {
var type;
if (!this.type) {
type = typeof packet;
}
else if (this.type === 'enum') {
type = 'string';
}
else {
type = this.type;
}
return packet_1.Packet.create(packet, type);
};
Port.EMPTY = 0;
Port.FILLED = 1;
Port.PERSIST = 2;
Port.DIRECT = 3;
Port.CONTEXT = 4;
Port.DEFAULT = 5;
Port.NOT_REQUIRED = 6;
Port.NOT_FILLED = 7;
Port.PERSISTED_SET = 8;
Port.CONTEXT_SET = 9;
Port.DEFAULT_SET = 10;
Port.SYNC_PORTS_UNFULFILLED = 11;
Port.Message = [
'not filled',
'connection filled',
'connection persisted',
'direct fill',
'context',
'default',
'required',
];
return Port;
}(PortMix(events_1.EventEmitter)));
exports.Port = Port;
//# sourceMappingURL=port.js.map