UNPKG

noflo

Version:

Flow-Based Programming environment for JavaScript

239 lines (219 loc) 6.63 kB
(function() { var BasePort, IP, InPort, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; BasePort = require('./BasePort'); IP = require('./IP'); InPort = (function(_super) { __extends(InPort, _super); function InPort(options, process) { this.process = null; if (!process && typeof options === 'function') { process = options; options = {}; } if (options == null) { options = {}; } if (options.buffered == null) { options.buffered = false; } if (options.control == null) { options.control = false; } if (options.triggering == null) { options.triggering = true; } if (!process && options && options.process) { process = options.process; delete options.process; } if (process) { if (typeof process !== 'function') { throw new Error('process must be a function'); } this.process = process; } if (options.handle) { if (typeof options.handle !== 'function') { throw new Error('handle must be a function'); } this.handle = options.handle; delete options.handle; } InPort.__super__.constructor.call(this, options); this.prepareBuffer(); } InPort.prototype.attachSocket = function(socket, localId) { if (localId == null) { localId = null; } if (this.hasDefault()) { if (this.handle) { socket.setDataDelegate((function(_this) { return function() { return new IP('data', _this.options["default"]); }; })(this)); } else { socket.setDataDelegate((function(_this) { return function() { return _this.options["default"]; }; })(this)); } } socket.on('connect', (function(_this) { return function() { return _this.handleSocketEvent('connect', socket, localId); }; })(this)); socket.on('begingroup', (function(_this) { return function(group) { return _this.handleSocketEvent('begingroup', group, localId); }; })(this)); socket.on('data', (function(_this) { return function(data) { _this.validateData(data); return _this.handleSocketEvent('data', data, localId); }; })(this)); socket.on('endgroup', (function(_this) { return function(group) { return _this.handleSocketEvent('endgroup', group, localId); }; })(this)); socket.on('disconnect', (function(_this) { return function() { return _this.handleSocketEvent('disconnect', socket, localId); }; })(this)); return socket.on('ip', (function(_this) { return function(ip) { return _this.handleIP(ip, localId); }; })(this)); }; InPort.prototype.handleIP = function(ip, id) { var buf; if (this.process) { return; } if (this.options.control && ip.type !== 'data') { return; } ip.owner = this.nodeInstance; ip.index = id; if (ip.scope) { if (!(ip.scope in this.scopedBuffer)) { this.scopedBuffer[ip.scope] = []; } buf = this.scopedBuffer[ip.scope]; } else { buf = this.buffer; } buf.push(ip); if (this.options.control && buf.length > 1) { buf.shift(); } if (this.handle) { this.handle(ip, this.nodeInstance); } return this.emit('ip', ip, id); }; InPort.prototype.handleSocketEvent = function(event, payload, id) { if (this.isBuffered()) { this.buffer.push({ event: event, payload: payload, id: id }); if (this.isAddressable()) { if (this.process) { this.process(event, id, this.nodeInstance); } this.emit(event, id); } else { if (this.process) { this.process(event, this.nodeInstance); } this.emit(event); } return; } if (this.process) { if (this.isAddressable()) { this.process(event, payload, id, this.nodeInstance); } else { this.process(event, payload, this.nodeInstance); } } if (this.isAddressable()) { return this.emit(event, payload, id); } return this.emit(event, payload); }; InPort.prototype.hasDefault = function() { return this.options["default"] !== void 0; }; InPort.prototype.prepareBuffer = function() { this.buffer = []; return this.scopedBuffer = {}; }; InPort.prototype.validateData = function(data) { if (!this.options.values) { return; } if (this.options.values.indexOf(data) === -1) { throw new Error("Invalid data='" + data + "' received, not in [" + this.options.values + "]"); } }; InPort.prototype.receive = function() { if (!this.isBuffered()) { throw new Error('Receive is only possible on buffered ports'); } return this.buffer.shift(); }; InPort.prototype.contains = function() { if (!this.isBuffered()) { throw new Error('Contains query is only possible on buffered ports'); } return this.buffer.filter(function(packet) { if (packet.event === 'data') { return true; } }).length; }; InPort.prototype.get = function(scope) { var buf; if (scope) { if (!(scope in this.scopedBuffer)) { return void 0; } buf = this.scopedBuffer[scope]; } else { buf = this.buffer; } if (this.options.control) { return buf[buf.length - 1]; } else { return buf.shift(); } }; InPort.prototype.length = function(scope) { if (scope) { if (!(scope in this.scopedBuffer)) { return 0; } return this.scopedBuffer[scope].length; } return this.buffer.length; }; InPort.prototype.ready = function(scope) { return this.length(scope) > 0; }; return InPort; })(BasePort); module.exports = InPort; }).call(this);