Skip to content

Commit

Permalink
fixuP
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Jun 25, 2024
1 parent 8844ee6 commit f1e14a3
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions lib/dispatcher/dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ const kOnConnect = Symbol('onConnect')
const kOnDisconnect = Symbol('onDisconnect')

Check failure on line 44 in lib/dispatcher/dispatcher.js

View workflow job for this annotation

GitHub Actions / Lint

'kOnDisconnect' is assigned a value but never used
const kOnConnectionError = Symbol('onConnectionError')

Check failure on line 45 in lib/dispatcher/dispatcher.js

View workflow job for this annotation

GitHub Actions / Lint

'kOnConnectionError' is assigned a value but never used

const registry = new FinalizationRegistry(({ dispatcher, handlers }) => {
for (const [event, listener] of handlers) {
dispatcher.off(event, listener)
}
});

Check failure on line 51 in lib/dispatcher/dispatcher.js

View workflow job for this annotation

GitHub Actions / Lint

Extra semicolon

class ComposedDispatcher extends Dispatcher {
#dispatcher
#dispatch
Expand All @@ -54,17 +60,18 @@ class ComposedDispatcher extends Dispatcher {
this.#dispatcher = dispatcher
this.#dispatch = dispatch

this[kOnDrain] = (...args) => this.emit('drain', ...args)
this[kOnConnect] = (...args) => this.emit('connect', ...args)
this[kOnDisconnect] = (...args) => this.emit('disconnect', ...args)
this[kOnConnectionError] = (...args) => this.emit('connectionError', ...args)

// TODO (fix): These must be weak references...
this.#dispatcher
.on('drain', this[kOnDrain])
.on('connect', this[kOnConnect])
.on('disconnect', this[kOnDisconnect])
.on('connectionError', this[kOnConnectionError])
const weakThis = new WeakRef(this)
const handlers = []

for (const event of ['drain', 'connect', 'disconnect', 'connectionError']) {
const listener = function (...args) {
weakThis.deref()?.emit(event, ...args)
}
handlers.push([event, listener])
dispatcher.on(event, listener)
}

registry.register(this, { dispatcher, handlers })
}

dispatch (...args) {
Expand Down

0 comments on commit f1e14a3

Please sign in to comment.