Skip to content

Commit

Permalink
fix: forward dispatch return value (nodejs#3368)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Jun 27, 2024
1 parent 71358de commit 3fd1fe1
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 28 deletions.
27 changes: 3 additions & 24 deletions lib/dispatcher/dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,9 @@ class Dispatcher extends EventEmitter {
}
}

return new ComposedDispatcher(this, dispatch)
}
}

class ComposedDispatcher extends Dispatcher {
#dispatcher = null
#dispatch = null

constructor (dispatcher, dispatch) {
super()
this.#dispatcher = dispatcher
this.#dispatch = dispatch
}

dispatch (...args) {
this.#dispatch(...args)
}

close (...args) {
return this.#dispatcher.close(...args)
}

destroy (...args) {
return this.#dispatcher.destroy(...args)
return new Proxy(this, {
get: (target, key) => key === 'dispatch' ? dispatch : target[key]
})
}
}

Expand Down
5 changes: 1 addition & 4 deletions test/dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@ test('dispatcher implementation', (t) => {
})

test('dispatcher.compose', (t) => {
t = tspl(t, { plan: 10 })
t = tspl(t, { plan: 7 })

const dispatcher = new Dispatcher()
const interceptor = () => (opts, handler) => {}
// Should return a new dispatcher
t.ok(Object.getPrototypeOf(dispatcher.compose(interceptor)) instanceof Dispatcher)
t.ok(Object.getPrototypeOf(dispatcher.compose(interceptor, interceptor)) instanceof Dispatcher)
t.ok(Object.getPrototypeOf(dispatcher.compose([interceptor, interceptor])) instanceof Dispatcher)
t.ok(dispatcher.compose(interceptor) !== dispatcher)
t.throws(() => dispatcher.dispatch({}), Error, 'invalid interceptor')
t.throws(() => dispatcher.dispatch(() => null), Error, 'invalid interceptor')
Expand Down

0 comments on commit 3fd1fe1

Please sign in to comment.