UNPKG

teeworlds-econ

Version:
62 lines (51 loc) 1.88 kB
var EconError, EventEmitter, Transaction, generateTransactionId, extend = 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; }, hasProp = {}.hasOwnProperty; generateTransactionId = require('./utils').generateTransactionId; EventEmitter = require('events').EventEmitter; EconError = require('./errors').EconError; Transaction = (function(superClass) { extend(Transaction, superClass); function Transaction(command, arg) { var timeout; timeout = arg.timeout; this.command = command; this.data = []; this.started = false; this.done = false; this.id = generateTransactionId(); this.beginRe = new RegExp("^\\[Console\\]: begin " + this.id + "$"); this.endRe = new RegExp("^\\[Console\\]: end " + this.id + "$"); this.timeoutTimer = setTimeout(((function(_this) { return function() { return _this.emit('error', new EconError('Transaction timeout')); }; })(this)), timeout); } Transaction.prototype.getCommand = function() { return "echo \"begin " + this.id + "\"; " + this.command + "; echo \"end " + this.id + "\""; }; Transaction.prototype.handleMessage = function(message) { if (this.beginRe.test(message)) { this.started = true; this.emit('begin', { id: this.id }); return; } if (this.endRe.test(message)) { clearTimeout(this.timeoutTimer); this.done = true; this.emit('end', { id: this.id, result: this.data.join('\n') }); return; } if (this.started) { return this.data.push(message); } }; return Transaction; })(EventEmitter); module.exports = Transaction;