Skip to content

Commit

Permalink
ClientServerdestroy: allow 'unsubscribed' event handler to retrieve S…
Browse files Browse the repository at this point in the history
…erverClient by evicting nodeIds / clientIds later, after unsubscribe
  • Loading branch information
erictheswift committed Aug 10, 2023
1 parent 303b9c0 commit f8de25f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 2 additions & 3 deletions server-client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,6 @@ export class ServerClient {
}
}
if (this.clientId) {
this.app.clientIds.delete(this.clientId)
this.app.nodeIds.delete(this.nodeId)

for (let channel in this.app.subscribers) {
let subscriber = this.app.subscribers[channel][this.nodeId]
if (subscriber) {
Expand All @@ -176,6 +173,8 @@ export class ServerClient {
this.app.performUnsubscribe(this.nodeId, action, meta)
}
}
this.app.clientIds.delete(this.clientId)
this.app.nodeIds.delete(this.nodeId)
}
if (!this.app.destroying) {
this.app.emitter.emit('disconnected', this)
Expand Down
8 changes: 8 additions & 0 deletions server-client/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,15 @@ it('removes itself on destroy', async () => {
'10:client2': { filters: { '{}': true } }
}
}
let unsubscribedClientNodeIds: string[] = []
test.app.on('unsubscribed', (action, meta, clientNodeId) => {
unsubscribedClientNodeIds.push(clientNodeId)
expect(test.app.nodeIds.get(clientNodeId)).toBeDefined()
})
client1.destroy()
await delay(1)

expect(unsubscribedClientNodeIds).toEqual(['10:client1'])
expect(Array.from(test.app.userIds.keys())).toEqual(['10'])
expect(test.app.subscribers).toEqual({
'user/10': { '10:client2': { filters: { '{}': true } } }
Expand All @@ -272,6 +278,8 @@ it('removes itself on destroy', async () => {

client2.destroy()
await delay(1)

expect(unsubscribedClientNodeIds).toEqual(['10:client1', '10:client2'])
expect(pullNewReports()).toMatchObject([
['unsubscribed', { channel: 'user/10' }],
['disconnect', { nodeId: '10:client2' }]
Expand Down

0 comments on commit f8de25f

Please sign in to comment.