/
index.js
260 lines (244 loc) · 8.05 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
module.exports = RpcEmitter
var MAX_INT = Math.pow(2, 32) - 1
var MAX_INT_HALF = Math.round(MAX_INT / 2)
var RpcEngine = require('rpc-engine')
var inherits = require('inherits')
var Emitter = require('events')
inherits(RpcEmitter, RpcEngine)
function RpcEmitter (opts) {
RpcEngine.call(this, opts)
this._subscriptions = {
local: {},
remote: {}
}
this._subscriptionSequence = {
local: Math.round(Math.random() * MAX_INT),
remote: null
}
var self = this
this.on('interface-add', function (iface, path) {
if (path === '') {
iface.subscribe = self._subscribe.bind(self)
iface.unsubscribe = self._unsubscribe.bind(self)
}
})
this.on('interface-remove', this._oninterfaceRemove)
this.setInterface('', new Emitter())
this.close = this.close.bind(this)
}
RpcEmitter.prototype.open = function () {
var self = this
Object.keys(this._subscriptions.remote).forEach(function (remoteIfaceName) {
var remoteIface = self._subscriptions.remote[remoteIfaceName]
Object.keys(remoteIface).forEach(function (remoteEventName) {
var subscription = remoteIface[remoteEventName]
var cb = self._callbacks[subscription.id]
if (cb && cb._isTeardownHandler) {
var path = remoteIfaceName ? (remoteIfaceName + self.pathDelimiter + remoteEventName) : remoteEventName
self._dosubscribe(remoteIfaceName, remoteIface, remoteEventName, subscription, path)
}
})
})
}
RpcEmitter.prototype.closeRemote = function () {
for (var localIfaceName in this._subscriptions.local) {
var localIface = this._subscriptions.local[localIfaceName]
var iface = this._interfaces[localIfaceName]
for (var localEventName in localIface) {
var subscription = localIface[localEventName]
if (iface) {
var fn = subscription[0]
iface.removeListener(localEventName, fn)
}
}
delete this._subscriptions.local[localIfaceName]
}
this._subscriptionSequence.remote = null
}
RpcEmitter.prototype.closeLocal = function () {
var self = this
for (var remoteIfaceName in this._subscriptions.remote) {
var remoteIface = this._subscriptions.remote[remoteIfaceName]
for (var remoteEventName in remoteIface) {
var subscription = remoteIface[remoteEventName]
subscription.handlers.forEach(function (handler) {
self.removeListener(remoteEventName, handler[0])
})
delete this._callbacks[subscription.id]
}
delete this._subscriptions.remote[remoteIfaceName]
}
}
RpcEmitter.prototype.close = function () {
this.closeRemote()
this.closeLocal()
RpcEngine.prototype.close.call(this)
}
RpcEmitter.prototype.subscribe = function (path, fn, cb) {
var sep = path.lastIndexOf(this.pathDelimiter)
var remoteIfaceName = sep > 0 ? path.slice(0, sep) : ''
var remoteIface = this._subscriptions.remote[remoteIfaceName]
if (!remoteIface) {
remoteIface = this._subscriptions.remote[remoteIfaceName] = {}
}
var remoteEventName = sep > 0 ? path.slice(sep + 1) : path
var subscription = remoteIface[remoteEventName]
this.on(path, fn)
if (subscription) {
subscription.handlers.push([fn, cb])
} else {
var id = this.generateCallId()
subscription = remoteIface[remoteEventName] = { id, handlers: [[fn, cb]] }
this._dosubscribe(remoteIfaceName, remoteIface, remoteEventName, subscription, path)
}
}
RpcEmitter.prototype._dosubscribe = function (remoteIfaceName, remoteIface, remoteEventName, subscription, path) {
var id = subscription.id
var self = this
this.call(id, 'subscribe', this._generateLocalSubscriptionSequence(), path, function (err) {
if (err) {
if (err.data && err.data.existingCallbackId) {
id = err.data.existingCallbackId
} else {
teardown(err)
return
}
}
teardown._isTeardownHandler = true
self._callbacks[id] = teardown
function teardown (err) {
subscription.handlers.forEach(function (handler) {
var fn = handler[0]
var cb = handler[1]
self.removeListener(path, fn)
if (cb) cb(err)
})
delete remoteIface[remoteEventName]
if (Object.keys(remoteIface).length === 0) {
delete self._subscriptions.remote[remoteIfaceName]
}
}
})
}
RpcEmitter.prototype.unsubscribe = function (path, fn) {
var sep = path.lastIndexOf(this.pathDelimiter)
var remoteIfaceName = sep > 0 ? path.slice(0, sep) : ''
var remoteIface = this._subscriptions.remote[remoteIfaceName]
if (!remoteIface) {
return
}
var remoteEventName = sep > 0 ? path.slice(sep + 1) : path
var subscription = remoteIface[remoteEventName]
if (!subscription) {
return
}
for (var i = subscription.handlers.length - 1; i > -1; i--) {
var handler = subscription.handlers[i]
if (handler[0] === fn) {
this.removeListener(path, fn)
subscription.handlers.splice(i, 1)
break
}
}
if (subscription.handlers.length === 0) {
var cb = this._callbacks[subscription.id]
if (cb) clearTimeout(cb.timeout)
delete this._callbacks[subscription.id]
delete remoteIface[remoteEventName]
if (Object.keys(remoteIface).length === 0) {
delete this._subscriptions.remote[remoteIfaceName]
}
this.call('unsubscribe', this._generateLocalSubscriptionSequence(), path)
}
}
RpcEmitter.prototype._subscribe = function (i, path, cb) {
if (!this._isRemoteSubscriptionSequenceValid(i)) return
var sep = path.lastIndexOf(this.pathDelimiter)
var localIfaceName = sep > 0 ? path.slice(0, sep) : ''
var iface = this._interfaces[localIfaceName]
if (!iface) {
var err = new Error('Interface not found')
err.code = -32000
cb(err)
return
}
var localIface = this._subscriptions.local[localIfaceName]
if (!localIface) {
localIface = this._subscriptions.local[localIfaceName] = {}
}
var localEventName = sep > 0 ? path.slice(sep + 1) : path
var subscription = localIface[localEventName]
if (subscription) {
var err = new Error('Subscription exists')
var _cb = subscription[1]
err.data = {
existingCallbackId: _cb.id
}
err.code = -32001
cb(err)
return
}
iface.on(localEventName, fn)
localIface[localEventName] = [fn, cb]
var self = this
cb()
function fn () {
var args = Array.prototype.slice.call(arguments)
args.unshift(path)
self.call.apply(self, args)
}
}
RpcEmitter.prototype._unsubscribe = function (i, path) {
if (!this._isRemoteSubscriptionSequenceValid(i)) return
var sep = path.lastIndexOf(this.pathDelimiter)
var localIfaceName = sep > 0 ? path.slice(0, sep) : ''
var localIface = this._subscriptions.local[localIfaceName]
if (!localIface) return
var localEventName = sep > 0 ? path.slice(sep + 1) : path
var subscription = localIface[localEventName]
if (!subscription) return
delete localIface[localEventName]
var iface = this._interfaces[localIfaceName]
if (iface) {
iface.removeListener(localEventName, subscription[0])
}
if (Object.keys(localIface).length === 0) {
delete this._subscriptions.local[localIfaceName]
}
return subscription
}
RpcEmitter.prototype._oninterfaceRemove = function (iface, path) {
var sep = path.lastIndexOf(this.pathDelimiter)
var localIface = this._subscriptions.local[path]
if (!localIface) return
delete this._subscriptions.local[path]
for (var localEventName in localIface) {
var subscription = localIface[localEventName]
if (subscription) {
iface.removeListener(localEventName, subscription[0])
var cb = subscription[1]
if (cb) {
var err = new Error('Interface was removed')
err.code = -32002
cb(err)
}
}
}
}
RpcEmitter.prototype._generateLocalSubscriptionSequence = function () {
var i = ++this._subscriptionSequence.local
if (i >= MAX_INT) {
i = this._subscriptionSequence.local = 0
}
return i
}
RpcEmitter.prototype._isRemoteSubscriptionSequenceValid = function (i) {
if (i >= 0) {
if (this._subscriptionSequence.remote === null ||
this._subscriptionSequence.remote < i ||
this._subscriptionSequence.remote - MAX_INT_HALF > i) {
this._subscriptionSequence.remote = i
return true
}
}
}