@chix/flow
Version:
147 lines • 6.41 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 __());
};
})();
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 nodebox_1 = require("./nodebox");
var ports_1 = require("./ports");
var start_1 = require("./start");
var debug = _debug('chix:node');
function $Fill(Base) {
return (function (_super) {
__extends(Fill$, _super);
function Fill$() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.notFilled = [];
return _this;
}
Fill$.prototype.onPortFill = function () {
var _this = this;
debug('%s onPortFill', this.identifier);
if (this.status === 'hold') {
this.__halted = true;
this.event(events_1.NodeEvents.PORT_FILL, this);
return undefined;
}
if (this.status !== 'running') {
throw Error('onPortFill: node must be in `running` state, current state: ' +
this.status);
}
var inputPorts = this.ports.input;
this.notFilled = [];
var res = forOf(function (name, port) {
if (port.isSync()) {
var ret = port.isFilled();
if (ret) {
debug('%s.%s: ready using `%s`', _this.identifier, name, port_1.Port.Message[ret]);
return true;
}
debug('%s.%s: not ready', _this.identifier, name);
_this.notFilled.push(name);
return undefined;
}
debug('%s.%s: ready using `%s`', _this.identifier, name, 'async always ready');
return true;
}, inputPorts).length;
if (res === this.inPorts.length) {
debug('%s:onPortFill running', this.identifier);
this.setStatus('running');
if (this.async) {
var atLeastOneAsyncFilled = forOf(function (_portName, port) {
return (port.isAsync() && port.isFilled()) || undefined;
}, inputPorts).length;
if (atLeastOneAsyncFilled) {
this.event(events_1.NodeEvents.EXECUTE, {});
var input_1 = {};
forOf(function (name, port) {
if (port.isSync()) {
input_1[name] = port.read();
}
else if (port.isFilled()) {
input_1[name] = port.read();
}
}, inputPorts);
var packetContainer_1;
try {
packetContainer_1 = packet_1.PacketContainer.create(input_1);
}
catch (e) {
console.log('%s: INPUT TRIED TO MERGE', this.identifier, JSON.stringify(input_1, null, 2));
throw Error('stop');
}
forOf(function (name, port) {
if (!port.isSync() && input_1[name]) {
debug('%s.%s: execute %s', _this.identifier, name, port.fn.name);
port.run(packetContainer_1, _this.state);
_this.state = _this.nodebox.state = port.fn.state;
}
}, inputPorts);
}
else {
this.event(events_1.NodeEvents.PORT_FILL, this);
return false;
}
}
else {
this.event(events_1.NodeEvents.EXECUTE, {});
var input_2 = {};
forOf(function (portName, port) {
input_2[portName] = port.read();
}, inputPorts);
var params = void 0;
try {
params = packet_1.PacketContainer.create(input_2);
}
catch (e) {
this.emit('error', {
msg: e.toString(),
node: this,
});
this.event(events_1.NodeEvents.PORT_FILL, this);
return false;
}
this.__start(params);
}
this.runCount++;
this.event(events_1.NodeEvents.EXECUTED, {
node: this,
});
this.complete();
this.event(events_1.NodeEvents.PORT_FILL, this);
return true;
}
else {
debug('%s:onPortFill ports: %s not ready', this.identifier, this.notFilled);
this.event(events_1.NodeEvents.PORT_FILL, this);
return undefined;
}
};
return Fill$;
}(Base));
}
exports.$Fill = $Fill;
(function ($Fill) {
function create(Base) {
return util_1.mixin($Fill, control_1.$Control.create, common_1.$Event.create, nodebox_1.$Nodebox.create, common_1.$Identity.create, ports_1.$Ports.create, common_1.$Status.create, start_1.$Start.create)(Base);
}
$Fill.create = create;
})($Fill = exports.$Fill || (exports.$Fill = {}));
//# sourceMappingURL=fill.js.map