Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions lib/dispatcher/dispatcher-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class DispatcherBase extends Dispatcher {
/** @type {boolean} */
[kClosed] = false;

/** @type {Array} */
[kOnClosed] = []
/** @type {Array|null} */
[kOnClosed] = null

/** @returns {boolean} */
get destroyed () {
Expand All @@ -49,7 +49,8 @@ class DispatcherBase extends Dispatcher {
}

if (this[kDestroyed]) {
queueMicrotask(() => callback(new ClientDestroyedError(), null))
const err = new ClientDestroyedError()
queueMicrotask(() => callback(err, null))
return
}

Expand All @@ -63,6 +64,7 @@ class DispatcherBase extends Dispatcher {
}

this[kClosed] = true
this[kOnClosed] ??= []
this[kOnClosed].push(callback)

const onClosed = () => {
Expand Down Expand Up @@ -90,7 +92,7 @@ class DispatcherBase extends Dispatcher {
if (callback === undefined) {
return new Promise((resolve, reject) => {
this.destroy(err, (err, data) => {
return err ? /* istanbul ignore next: should never error */ reject(err) : resolve(data)
return err ? reject(err) : resolve(data)
})
})
}
Expand All @@ -113,7 +115,7 @@ class DispatcherBase extends Dispatcher {
}

this[kDestroyed] = true
this[kOnDestroyed] = this[kOnDestroyed] || []
this[kOnDestroyed] ??= []
this[kOnDestroyed].push(callback)

const onDestroyed = () => {
Expand Down
16 changes: 8 additions & 8 deletions types/dispatcher.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,30 @@ declare class Dispatcher extends EventEmitter {
/** Dispatches a request. This API is expected to evolve through semver-major versions and is less stable than the preceding higher level APIs. It is primarily intended for library developers who implement higher level APIs on top of this. */
dispatch (options: Dispatcher.DispatchOptions, handler: Dispatcher.DispatchHandler): boolean
/** Starts two-way communications with the requested resource. */
connect<TOpaque = null>(options: Dispatcher.ConnectOptions<TOpaque>): Promise<Dispatcher.ConnectData<TOpaque>>
connect<TOpaque = null>(options: Dispatcher.ConnectOptions<TOpaque>, callback: (err: Error | null, data: Dispatcher.ConnectData<TOpaque>) => void): void
connect<TOpaque = null>(options: Dispatcher.ConnectOptions<TOpaque>): Promise<Dispatcher.ConnectData<TOpaque>>
/** Compose a chain of dispatchers */
compose (dispatchers: Dispatcher.DispatcherComposeInterceptor[]): Dispatcher.ComposedDispatcher
compose (...dispatchers: Dispatcher.DispatcherComposeInterceptor[]): Dispatcher.ComposedDispatcher
/** Performs an HTTP request. */
request<TOpaque = null>(options: Dispatcher.RequestOptions<TOpaque>): Promise<Dispatcher.ResponseData<TOpaque>>
request<TOpaque = null>(options: Dispatcher.RequestOptions<TOpaque>, callback: (err: Error | null, data: Dispatcher.ResponseData<TOpaque>) => void): void
request<TOpaque = null>(options: Dispatcher.RequestOptions<TOpaque>): Promise<Dispatcher.ResponseData<TOpaque>>
/** For easy use with `stream.pipeline`. */
pipeline<TOpaque = null>(options: Dispatcher.PipelineOptions<TOpaque>, handler: Dispatcher.PipelineHandler<TOpaque>): Duplex
/** A faster version of `Dispatcher.request`. */
stream<TOpaque = null>(options: Dispatcher.RequestOptions<TOpaque>, factory: Dispatcher.StreamFactory<TOpaque>): Promise<Dispatcher.StreamData<TOpaque>>
stream<TOpaque = null>(options: Dispatcher.RequestOptions<TOpaque>, factory: Dispatcher.StreamFactory<TOpaque>, callback: (err: Error | null, data: Dispatcher.StreamData<TOpaque>) => void): void
stream<TOpaque = null>(options: Dispatcher.RequestOptions<TOpaque>, factory: Dispatcher.StreamFactory<TOpaque>): Promise<Dispatcher.StreamData<TOpaque>>
/** Upgrade to a different protocol. */
upgrade (options: Dispatcher.UpgradeOptions): Promise<Dispatcher.UpgradeData>
upgrade (options: Dispatcher.UpgradeOptions, callback: (err: Error | null, data: Dispatcher.UpgradeData) => void): void
upgrade (options: Dispatcher.UpgradeOptions): Promise<Dispatcher.UpgradeData>
/** Closes the client and gracefully waits for enqueued requests to complete before invoking the callback (or returning a promise if no callback is provided). */
close (): Promise<void>
close (callback: () => void): void
close (): Promise<void>
/** Destroy the client abruptly with the given err. All the pending and running requests will be asynchronously aborted and error. Waits until socket is closed before invoking the callback (or returning a promise if no callback is provided). Since this operation is asynchronously dispatched there might still be some progress on dispatched requests. */
destroy (): Promise<void>
destroy (err: Error | null): Promise<void>
destroy (callback: () => void): void
destroy (err: Error | null, callback: () => void): void
destroy (callback: () => void): void
destroy (err: Error | null): Promise<void>
destroy (): Promise<void>

on (eventName: 'connect', callback: (origin: URL, targets: readonly Dispatcher[]) => void): this
on (eventName: 'disconnect', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this
Expand Down
Loading