Skip to content

Commit 6365a90

Browse files
authored
fix: minor changes in dispatcher-base.js and types for Dispatcher (#4556)
* fix: minor changes in dispatcher-base.js and types for Dispatcher * specify array type
1 parent a09b5ee commit 6365a90

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

lib/dispatcher/dispatcher-base.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ class DispatcherBase extends Dispatcher {
1616
/** @type {boolean} */
1717
[kDestroyed] = false;
1818

19-
/** @type {Array|null} */
19+
/** @type {Array<Function|null} */
2020
[kOnDestroyed] = null;
2121

2222
/** @type {boolean} */
2323
[kClosed] = false;
2424

25-
/** @type {Array} */
26-
[kOnClosed] = []
25+
/** @type {Array<Function>|null} */
26+
[kOnClosed] = null
2727

2828
/** @returns {boolean} */
2929
get destroyed () {
@@ -49,7 +49,8 @@ class DispatcherBase extends Dispatcher {
4949
}
5050

5151
if (this[kDestroyed]) {
52-
queueMicrotask(() => callback(new ClientDestroyedError(), null))
52+
const err = new ClientDestroyedError()
53+
queueMicrotask(() => callback(err, null))
5354
return
5455
}
5556

@@ -63,6 +64,7 @@ class DispatcherBase extends Dispatcher {
6364
}
6465

6566
this[kClosed] = true
67+
this[kOnClosed] ??= []
6668
this[kOnClosed].push(callback)
6769

6870
const onClosed = () => {
@@ -90,7 +92,7 @@ class DispatcherBase extends Dispatcher {
9092
if (callback === undefined) {
9193
return new Promise((resolve, reject) => {
9294
this.destroy(err, (err, data) => {
93-
return err ? /* istanbul ignore next: should never error */ reject(err) : resolve(data)
95+
return err ? reject(err) : resolve(data)
9496
})
9597
})
9698
}
@@ -113,7 +115,7 @@ class DispatcherBase extends Dispatcher {
113115
}
114116

115117
this[kDestroyed] = true
116-
this[kOnDestroyed] = this[kOnDestroyed] || []
118+
this[kOnDestroyed] ??= []
117119
this[kOnDestroyed].push(callback)
118120

119121
const onDestroyed = () => {

types/dispatcher.d.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,30 @@ declare class Dispatcher extends EventEmitter {
1919
/** 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. */
2020
dispatch (options: Dispatcher.DispatchOptions, handler: Dispatcher.DispatchHandler): boolean
2121
/** Starts two-way communications with the requested resource. */
22-
connect<TOpaque = null>(options: Dispatcher.ConnectOptions<TOpaque>): Promise<Dispatcher.ConnectData<TOpaque>>
2322
connect<TOpaque = null>(options: Dispatcher.ConnectOptions<TOpaque>, callback: (err: Error | null, data: Dispatcher.ConnectData<TOpaque>) => void): void
23+
connect<TOpaque = null>(options: Dispatcher.ConnectOptions<TOpaque>): Promise<Dispatcher.ConnectData<TOpaque>>
2424
/** Compose a chain of dispatchers */
2525
compose (dispatchers: Dispatcher.DispatcherComposeInterceptor[]): Dispatcher.ComposedDispatcher
2626
compose (...dispatchers: Dispatcher.DispatcherComposeInterceptor[]): Dispatcher.ComposedDispatcher
2727
/** Performs an HTTP request. */
28-
request<TOpaque = null>(options: Dispatcher.RequestOptions<TOpaque>): Promise<Dispatcher.ResponseData<TOpaque>>
2928
request<TOpaque = null>(options: Dispatcher.RequestOptions<TOpaque>, callback: (err: Error | null, data: Dispatcher.ResponseData<TOpaque>) => void): void
29+
request<TOpaque = null>(options: Dispatcher.RequestOptions<TOpaque>): Promise<Dispatcher.ResponseData<TOpaque>>
3030
/** For easy use with `stream.pipeline`. */
3131
pipeline<TOpaque = null>(options: Dispatcher.PipelineOptions<TOpaque>, handler: Dispatcher.PipelineHandler<TOpaque>): Duplex
3232
/** A faster version of `Dispatcher.request`. */
33-
stream<TOpaque = null>(options: Dispatcher.RequestOptions<TOpaque>, factory: Dispatcher.StreamFactory<TOpaque>): Promise<Dispatcher.StreamData<TOpaque>>
3433
stream<TOpaque = null>(options: Dispatcher.RequestOptions<TOpaque>, factory: Dispatcher.StreamFactory<TOpaque>, callback: (err: Error | null, data: Dispatcher.StreamData<TOpaque>) => void): void
34+
stream<TOpaque = null>(options: Dispatcher.RequestOptions<TOpaque>, factory: Dispatcher.StreamFactory<TOpaque>): Promise<Dispatcher.StreamData<TOpaque>>
3535
/** Upgrade to a different protocol. */
36-
upgrade (options: Dispatcher.UpgradeOptions): Promise<Dispatcher.UpgradeData>
3736
upgrade (options: Dispatcher.UpgradeOptions, callback: (err: Error | null, data: Dispatcher.UpgradeData) => void): void
37+
upgrade (options: Dispatcher.UpgradeOptions): Promise<Dispatcher.UpgradeData>
3838
/** Closes the client and gracefully waits for enqueued requests to complete before invoking the callback (or returning a promise if no callback is provided). */
39-
close (): Promise<void>
4039
close (callback: () => void): void
40+
close (): Promise<void>
4141
/** 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. */
42-
destroy (): Promise<void>
43-
destroy (err: Error | null): Promise<void>
44-
destroy (callback: () => void): void
4542
destroy (err: Error | null, callback: () => void): void
43+
destroy (callback: () => void): void
44+
destroy (err: Error | null): Promise<void>
45+
destroy (): Promise<void>
4646

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

0 commit comments

Comments
 (0)