@chix/flow
Version:
171 lines • 6.18 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:outputPort');
var OutputPort = (function (_super) {
__extends(OutputPort, _super);
function OutputPort(portDefinition) {
var _this = _super.call(this, portDefinition) || this;
_this.data = [];
_this.pull = false;
_this._writeListener = _this._writeListener.bind(_this);
return _this;
}
OutputPort.prototype.peek = function () {
if (this.data.length) {
return this.data[0];
}
return null;
};
OutputPort.prototype.size = function () {
return this.data.length;
};
OutputPort.prototype.read = function () {
if (this.data.length) {
return this.data.shift();
}
if (this.hasParent()) {
throw Error("Unable to read from port " + this.parent().identifier + "(" + this.name + ")");
}
throw Error("Unable to read from main port (" + this.name + ")");
};
OutputPort.prototype.write = function (packet) {
if (!packet_1.Packet.isPacket(packet)) {
packet = this.createPacketFromValue(packet);
}
else {
packet.type = this.type;
}
this.fills++;
if (this.pull) {
this.data.push(packet);
}
else {
this.emit(events_1.PortEvents.DATA, packet);
}
};
OutputPort.prototype.open = function () {
if (this.isClosed()) {
_super.prototype.open.call(this);
debug('open %s port', this.name);
this.on(events_1.PortEvents.DATA, this._writeListener);
}
};
OutputPort.prototype.close = function () {
if (this.isOpen()) {
_super.prototype.close.call(this);
debug('close %s port', this.name);
this.removeListener(events_1.PortEvents.DATA, this._writeListener);
}
};
OutputPort.prototype.connect = function (link) {
if (!this.hasConnection(link)) {
this.open();
this._connections.push(link);
}
else {
console.log('Link already connected');
}
};
OutputPort.prototype.plug = function (source) {
if (source.wire) {
if (this.hasConnection(source.wire)) {
console.log('Link already connected');
}
else {
this.open();
debug('plug (%s) -> %s', source.port, source.wire.target.port);
if (source.setting) {
this.setOptions(source.setting);
}
this._connections.push(source.wire);
}
}
else {
throw Error('Wireless connector.');
}
};
OutputPort.prototype.disconnect = function (link) {
if (this.hasConnection(link)) {
if (this._connections.length === 1) {
this.close();
}
return this._connections.splice(this._connections.indexOf(link), 1);
}
console.log('Link already disconnected');
return undefined;
};
OutputPort.prototype.unplug = function (target) {
if (target.wire && this.hasConnection(target.wire)) {
if (this._connections.length === 1) {
this.close();
}
return this._connections.splice(this._connections.indexOf(target.wire), 1);
}
console.log('Link already disconnected');
return undefined;
};
OutputPort.prototype.destroy = function () {
this.removeAllListeners(events_1.PortEvents.DATA);
};
OutputPort.prototype._send = function (connection, packet) {
process.nextTick(function () {
connection.source.write(packet);
});
};
OutputPort.prototype._writeListener = function (packet) {
var e_1, _a;
debug('%s writing to %d connections', this.name, this._connections.length);
var _cp = packet;
try {
for (var _b = __values(this._connections), _c = _b.next(); !_c.done; _c = _b.next()) {
var connection = _c.value;
if (this.hasParent()) {
var node = this.getParent();
debug('write %s (%s) -> %s', node ? node.title || node.name : '', this.name, connection.target.port);
}
this._send(connection, _cp);
_cp = _cp.clone(_cp.owner);
}
}
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; }
}
this.reads +=
this.listenerCount(events_1.PortEvents.DATA) - 1 + this._connections.length;
};
return OutputPort;
}(port_1.Port));
exports.OutputPort = OutputPort;
//# sourceMappingURL=output.js.map