Skip to content

Commit

Permalink
Switch to simple null check and shortcircuit bind
Browse files Browse the repository at this point in the history
  • Loading branch information
arontsang committed Apr 19, 2022
1 parent 80268c0 commit 6b4ee13
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions lib/dispatcher-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,23 +130,6 @@ class DispatcherBase extends Dispatcher {
})
}

get [kInterceptedDispatch] () {
let dispatch = this[kDispatch].bind(this)
for (let i = this[kInterceptors].length - 1; i >= 0; i--) {
const interceptor = this[kInterceptors][i]
if (typeof interceptor !== 'function') {
throw new InvalidArgumentError('interceptor must be an function')
}
dispatch = interceptor(dispatch)
}

// Overwrite this getter with a cached value
Object.defineProperty(this, kInterceptedDispatch, {
value: dispatch
})
return dispatch
}

dispatch (opts, handler) {
if (!handler || typeof handler !== 'object') {
throw new InvalidArgumentError('handler must be an object')
Expand All @@ -165,6 +148,25 @@ class DispatcherBase extends Dispatcher {
throw new ClientClosedError()
}

if (!this[kInterceptedDispatch]) {
function buildInterceptedDispatch () {
if (!this[kInterceptors] || this[kInterceptors].length === 0) {
return this[kDispatch]
}

let dispatch = this[kDispatch].bind(this)
for (let i = this[kInterceptors].length - 1; i >= 0; i--) {
const interceptor = this[kInterceptors][i]
if (typeof interceptor !== 'function') {
throw new InvalidArgumentError('interceptor must be an function')
}
dispatch = interceptor(dispatch)
}
return dispatch
}
this[kInterceptedDispatch] = buildInterceptedDispatch.call(this)
}

return this[kInterceptedDispatch](opts, handler)
} catch (err) {
if (typeof handler.onError !== 'function') {
Expand Down

0 comments on commit 6b4ee13

Please sign in to comment.