Skip to content

Commit

Permalink
fixup: rename clientt to dispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Mar 30, 2021
1 parent 2b2557c commit 7a2eed1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
22 changes: 11 additions & 11 deletions lib/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ class Agent extends Dispatcher {

const agent = this

this[kOnDrain] = function onDrain (origin, targets) {
this[kOnDrain] = (origin, targets) => {
agent.emit('drain', origin, [agent, ...targets])
}

this[kOnConnect] = function onConnect (origin, targets) {
this[kOnConnect] = (origin, targets) => {
agent.emit('connect', origin, [agent, ...targets])
}

this[kOnDisconnect] = function onDisconnect (origin, targets, err) {
this[kOnDisconnect] = (origin, targets, err) => {
agent.emit('disconnect', origin, [agent, ...targets], err)
}
}
Expand Down Expand Up @@ -138,15 +138,15 @@ class Agent extends Dispatcher {

const ref = this[kClients].get(opts.origin)

let client = ref ? ref.deref() : null
if (!client) {
client = this[kFactory](opts.origin, this[kOptions])
let dispatcher = ref ? ref.deref() : null
if (!dispatcher) {
dispatcher = this[kFactory](opts.origin, this[kOptions])
.on('connect', this[kOnConnect])
.on('disconnect', this[kOnDisconnect])
.on('drain', this[kOnDrain])

this[kClients].set(opts.origin, new WeakRef(client))
this[kFinalizer].register(client, opts.origin)
this[kClients].set(opts.origin, new WeakRef(dispatcher))
this[kFinalizer].register(dispatcher, opts.origin)
}

const { maxRedirections = this[kMaxRedirections] } = opts
Expand All @@ -156,14 +156,14 @@ class Agent extends Dispatcher {
}

if (!maxRedirections) {
return client.dispatch(opts, handler)
return dispatcher.dispatch(opts, handler)
}

if (util.isStream(opts.body) && util.bodyLength(opts.body) !== 0) {
// TODO (fix): Provide some way for the user to cache the file to e.g. /tmp
// so that it can be dispatched again?
// TODO (fix): Do we need 100-expect support to provide a way to do this properly?
return client.dispatch(opts, handler)
return dispatcher.dispatch(opts, handler)
}

/* istanbul ignore next */
Expand All @@ -174,7 +174,7 @@ class Agent extends Dispatcher {
})
}

return client.dispatch(opts, new RedirectHandler(this, opts, handler))
return dispatcher.dispatch(opts, new RedirectHandler(this, opts, handler))
} catch (err) {
if (typeof handler.onError !== 'function') {
throw new InvalidArgumentError('invalid onError method')
Expand Down
20 changes: 10 additions & 10 deletions lib/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ class Pool extends Dispatcher {
}
}

this[kOnConnect] = function onConnect (origin, targets) {
this[kOnConnect] = (origin, targets) => {
pool.emit('connect', origin, [pool, ...targets])
}

this[kOnDisconnect] = function onDisconnect (origin, targets, err) {
this[kOnDisconnect] = (origin, targets, err) => {
pool.emit('disconnect', origin, [pool, ...targets], err)
}

Expand Down Expand Up @@ -179,9 +179,9 @@ class Pool extends Dispatcher {
throw new ClientClosedError()
}

let client = this[kClients].find(client => !client[kNeedDrain])
let dispatcher = this[kClients].find(dispatcher => !dispatcher[kNeedDrain])

if (!client) {
if (!dispatcher) {
if (!this[kConnections] || this[kClients].length < this[kConnections]) {
let options = this[kOptions]

Expand All @@ -194,25 +194,25 @@ class Pool extends Dispatcher {
options = { ...options, tls: { ...options.tls, session: this[kTLSSession] } }
}

client = this[kFactory](this[kUrl], options)
dispatcher = this[kFactory](this[kUrl], options)
.on('drain', this[kOnDrain])
.on('connect', this[kOnConnect])
.on('disconnect', this[kOnDisconnect])

if (!options.tls || (options.tls.reuseSessions !== false && !options.tls.session)) {
client.on('session', this[kOnTLSSession])
dispatcher.on('session', this[kOnTLSSession])
}

this[kClients].push(client)
this[kClients].push(dispatcher)
}
}

if (!client) {
if (!dispatcher) {
this[kNeedDrain] = true
this[kQueue].push({ opts, handler })
this[kPending]++
} else if (!client.dispatch(opts, handler)) {
client[kNeedDrain] = true
} else if (!dispatcher.dispatch(opts, handler)) {
dispatcher[kNeedDrain] = true
this[kNeedDrain] = this[kConnections] && this[kClients].length === this[kConnections]
}
} catch (err) {
Expand Down

0 comments on commit 7a2eed1

Please sign in to comment.