/
index.js
74 lines (65 loc) · 1.76 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
var EventEmitter = require('events').EventEmitter
var through = require('through')
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.on('connect', this.flush.bind(this))
}
var ree = RemoteEventEmitter.prototype = new EventEmitter ()
ree.flush = function () {
while(this.buffer.length && this.connected)
this.emit.apply(this, this.buffer.shift())
}
ree.getStream = function () {
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.once('end', function () {
self.disconnect()
})
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
}
ree.disconnect = function () {
if(!this.connected) return
this.connected = false
if(this.stream && this.stream.writable && !this.stream.ended)
this.stream.end()
this.stream = null
this.localEmit('disconnect')
}
ree.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.
*/
ree.localEmit = function () {
var args = [].slice.call(arguments)
return EventEmitter.prototype.emit.apply(this, args)
}