Skip to content

Commit 6c6cbbf

Browse files
committed
fixed subscription handling for instances that disappeared
1 parent ab8b0f3 commit 6c6cbbf

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
3333

3434
### Fixed
3535

36+
- now unsubscribing from events on instances that aren't available anymore
3637
- invalid log message triggered when JSON return values are circular
3738
- multiple emitter registration under the same clientId
3839
- issue when loading files that are not ending with ".js"

Diff for: browser/vrpc.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: vrpc/VrpcClient.js

+27
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@ class VrpcClient extends EventEmitter {
232232
this._agents[agent].status = status
233233
this._agents[agent].hostname = hostname
234234
this._agents[agent].version = version
235+
if (status === 'offline') {
236+
this._clearCachedSubscriptions({ lostAgent: agent })
237+
}
235238
this.emit('agent', { domain, agent, status, hostname, version })
236239
// ClassInfo message
237240
} else if (
@@ -251,6 +254,7 @@ class VrpcClient extends EventEmitter {
251254
json
252255
if (removed.length !== 0) {
253256
this.emit('instanceGone', removed, { domain, agent, className })
257+
this._clearCachedSubscriptions({ lostInstance: removed })
254258
}
255259
if (added.length !== 0) {
256260
this.emit('instanceNew', added, { domain, agent, className })
@@ -1179,6 +1183,29 @@ class VrpcClient extends EventEmitter {
11791183
return id
11801184
}
11811185

1186+
_clearCachedSubscriptions ({ lostAgent, lostInstance }) {
1187+
const obsoleteTopics = Object.keys(this._cachedSubscriptions).filter(
1188+
topic => {
1189+
const [, agent, , instanceEvent] = topic.split('/')
1190+
const [instance] = instanceEvent.split(':')
1191+
if (agent === lostAgent || instance === lostInstance) {
1192+
return true
1193+
}
1194+
return false
1195+
}
1196+
)
1197+
obsoleteTopics.forEach(topic => {
1198+
const id = `__e__${topic}`
1199+
this._log.info(`Clearing subscriptions for obsolete topic: ${topic}`)
1200+
const subscriptions = this._cachedSubscriptions[topic]
1201+
subscriptions.forEach(({ handler }) => {
1202+
this._eventEmitter.removeListener(id, handler)
1203+
})
1204+
this._mqttUnsubscribe(topic)
1205+
delete this._cachedSubscriptions[topic]
1206+
})
1207+
}
1208+
11821209
_stripSignature (method) {
11831210
const pos = method.indexOf('-')
11841211
if (pos > 0) return method.substring(0, pos)

0 commit comments

Comments
 (0)