UNPKG

telephone_duplexer

Version:

bi-direction event emitter over a stream

38 lines (31 loc) 887 B
var util = require('util'); var helpers = require('./helpers'); var Writable = require('stream').Writable; function StreamDecoder(emitter) { Writable.apply(this); this.emitter = emitter; } util.inherits(StreamDecoder, Writable); StreamDecoder.prototype.createCallback = function(id) { var self = this; return function() { var args = helpers.parseArgs(arguments); self.emit('reply', id, args); } } StreamDecoder.prototype.handleData = function(data) { var args = data.args; if (data.replies) { args = args.concat(this.createCallback(data.id)); } this.emitter.emit.apply(this.emitter, args); } StreamDecoder.prototype._write = function(chunk, enc, next) { var arr = chunk.toString().split("\n") for (var index in arr) { var line = arr[index]; line && this.handleData(JSON.parse(line.trim())); } next(); } module.exports = StreamDecoder;