@chix/flow
Version:
141 lines • 5.46 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 __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;
};
Object.defineProperty(exports, "__esModule", { value: true });
var _debug = require("debug");
var events_1 = require("../events");
var packet_1 = require("../packet");
var util_1 = require("./util");
var debug = _debug('chix:io');
function $Control(Base) {
return (function (_super) {
__extends(Control$, _super);
function Control$() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this._heldItems = [];
_this._hold = false;
_this._shutdown = false;
_this.onDataHandler = function (packet, source) {
var wire = source.wire;
if (!wire) {
throw Error('Could not find link on connector.');
}
if (source.get('collect')) {
debug('%s: collecting packets', wire.ioid);
throw Error('Collect not implemented');
}
_this.emit(events_1.IOEvents.SEND, wire);
_this.send(wire, packet);
};
return _this;
}
Control$.prototype.reset = function () {
this._shutdown = true;
};
Control$.prototype.drop = function (packet, origin) {
debug('IoHandler: Dropping packet %s %s', packet, origin);
this.emit(events_1.IOEvents.DROP, packet);
};
Control$.prototype.hold = function () {
this._hold = true;
this._heldItems = [];
};
Control$.prototype.step = function () {
if (this._heldItems.length) {
var _a = __read(this._heldItems.shift(), 2), link = _a[0], packet = _a[1];
this.send(link, packet, true);
}
};
Control$.prototype.resume = function () {
this._hold = false;
var item;
if (this._heldItems.length) {
while ((item = this._heldItems.shift())) {
this.send(item[0], item[1], true);
}
}
};
Control$.prototype.send = function (link, packet, step) {
if (this._hold && !step) {
this._heldItems.push([link, packet]);
}
else {
if (!packet_1.Packet.isPacket(packet)) {
throw Error('send expects a packet');
}
debug('sending packet');
this.__sendData(link, packet);
}
};
Control$.prototype.__sendData = function (link, packet) {
if (this._shutdown) {
packet.release(link);
this.drop(packet, link);
}
else {
var packetClone_1;
if (link.source.has('index') && !packet.hasOwnProperty('index')) {
packetClone_1 = packet.clone(packet.owner);
if (undefined ===
packetClone_1.read(packet.owner)[link.source.get('index')]) {
debug('%s: INDEX UNDEFINED %s', link.ioid, link.source, packetClone_1.read(packetClone_1.owner));
return undefined;
}
else {
util_1.handleIndex(link, packetClone_1.read(packetClone_1.owner), packetClone_1);
}
}
else {
packetClone_1 = packet;
}
if (link.target.has('mask')) {
packetClone_1.point(packetClone_1.owner, link.target.get('mask'));
}
this.emit(events_1.IOEvents.DATA, {
data: packetClone_1.read(link),
link: link,
});
this.emit(events_1.IOEvents.PACKET, {
data: packetClone_1,
link: link,
});
debug('writing packet %s -> %s', link.source.port, link.target.port);
process.nextTick(function () {
packetClone_1.release(link);
link.target.write(packetClone_1);
});
this.emit(events_1.IOEvents.RECEIVE, link);
}
};
return Control$;
}(Base));
}
exports.$Control = $Control;
//# sourceMappingURL=control.js.map