-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
82 lines (70 loc) · 2.08 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
var EventEmitter = require('events').EventEmitter
var through = require('through')
var serializer = require('stream-serializer')
var util = require('util');
var events = require('events');
module.exports = RemoteEventEmitter
function RemoteEventEmitter (opts) {
EventEmitter.call(this)
this.buffer = []
//XXX RemoteEventEmitters start off disconnected!
//THIS IS MORE REALISTIC
//REMEMBER to call connect() !
this.connected = false
var self = this
this._opts = opts || {}
this.on('connect', this.flush.bind(this))
}
util.inherits(RemoteEventEmitter, events.EventEmitter);
RemoteEventEmitter.prototype.flush = function () {
while(this.buffer.length && this.connected)
this.emit.apply(this, this.buffer.shift())
}
RemoteEventEmitter.prototype.getStream = function (raw) {
if (this.stream && !this._stream.ended)
return this.stream
var self = this
this._stream = through(function (data) {
self.localEmit.apply(self, data)
}, function () {
this.emit('end')
self.disconnect()
})
this.stream = raw ? this._stream
: serializer(this._opts.wrap)(this._stream)
var pipe = this.stream.pipe
this.stream.pipe = function (other, opts) {
var r = pipe.call(this, other, opts)
process.nextTick(function () {
self.connected = true
self.localEmit('connect')
})
return r
}
return this.stream
}
RemoteEventEmitter.prototype.disconnect = function () {
if(!this.connected) return
this.connected = false
if(this._stream && this._stream.writable && !this._stream.ended)
this._stream.emit('end')
this._stream = null
this.stream.destroy()
this.stream = null
this.localEmit('disconnect')
}
RemoteEventEmitter.prototype.emit = function () {
var args = [].slice.call(arguments)
if(this.connected)
return this._stream.emit('data', args)
else
this.buffer.push(args)
}
/*
sometimes you need to access this, so I'm not using
_emit ... that means this is a part of the API.
*/
RemoteEventEmitter.prototype.localEmit = function () {
var args = [].slice.call(arguments)
return EventEmitter.prototype.emit.apply(this, args)
}