Skip to content

Commit 3d554eb

Browse files
committed
Change race() methods to return PromiseLike instead of Promise
1 parent b014c61 commit 3d554eb

File tree

5 files changed

+20
-12
lines changed

5 files changed

+20
-12
lines changed

src/lib/executors/base.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export abstract class BaseExecution<TResult = unknown> {
3939
protected readonly config: BuilderConfig
4040
protected readonly ctx: TryCtx
4141
protected readonly executionSignal: AbortSignal | undefined
42-
readonly #wrapCtx: WrapCtx
42+
#wrapCtxCache: WrapCtx | undefined
4343
readonly #signalController: SignalController | undefined
4444
readonly #timeoutController: TimeoutController | undefined
4545

@@ -52,7 +52,6 @@ export abstract class BaseExecution<TResult = unknown> {
5252
)
5353
this.executionSignal = this.#signalController?.signal
5454
this.ctx = BaseExecution.createContext(config, this.executionSignal, options.retryLimit)
55-
this.#wrapCtx = BaseExecution.createWrapContext(this.ctx)
5655
}
5756

5857
execute(): TResult {
@@ -77,6 +76,10 @@ export abstract class BaseExecution<TResult = unknown> {
7776
protected abstract executeCore(): TResult
7877

7978
[Symbol.dispose](): void {
79+
if (!this.#timeoutController && !this.#signalController) {
80+
return
81+
}
82+
8083
using disposer = new DisposableStack()
8184
if (this.#timeoutController) {
8285
disposer.use(this.#timeoutController)
@@ -248,4 +251,9 @@ export abstract class BaseExecution<TResult = unknown> {
248251
shouldAttemptRetry,
249252
}
250253
}
254+
255+
get #wrapCtx(): WrapCtx {
256+
this.#wrapCtxCache ??= BaseExecution.createWrapContext(this.ctx)
257+
return this.#wrapCtxCache
258+
}
251259
}

src/lib/executors/run.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class RunExecution<T, E, Ctx extends BaseTryCtx> extends BaseExecution<
132132

133133
if (checkIsPromiseLike(result)) {
134134
// oxlint-disable-next-line no-await-in-loop
135-
const raced = await this.race(Promise.resolve(result))
135+
const raced = await this.race(result)
136136
return raced
137137
}
138138

src/lib/executors/shared.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ export abstract class OrchestrationExecution<TResult> extends BaseExecution<Prom
109109
})
110110
}
111111

112-
protected override async executeCore(): Promise<TResult> {
112+
protected override executeCore(): Promise<TResult> {
113113
// Orchestration executors still share outer wraps/cancellation checks even
114114
// though their task execution strategies differ.
115115
const controlBeforeExecution = this.checkDidControlFail()
@@ -118,7 +118,7 @@ export abstract class OrchestrationExecution<TResult> extends BaseExecution<Prom
118118
throw controlBeforeExecution
119119
}
120120

121-
return await this.executeTasks()
121+
return this.executeTasks()
122122
}
123123

124124
protected abstract executeTasks(): Promise<TResult>

src/lib/modifiers/signal.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@ export class SignalController {
2222
return new CancellationError(undefined, { cause: cause ?? this.signal.reason })
2323
}
2424

25-
async race<V>(promise: PromiseLike<V>, cause?: unknown): Promise<V | CancellationError> {
25+
race<V>(promise: PromiseLike<V>, cause?: unknown): PromiseLike<V | CancellationError> {
2626
const cancelled = this.checkDidCancel(cause)
2727

2828
if (cancelled) {
29-
return cancelled
29+
return Promise.resolve(cancelled)
3030
}
3131

3232
if (!this.signal) {
3333
return promise
3434
}
3535

36-
return await resolveWithAbort(
36+
return resolveWithAbort(
3737
this.signal,
3838
promise,
3939
() => new CancellationError(undefined, { cause: cause ?? this.signal?.reason })

src/lib/modifiers/timeout.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,22 @@ export class TimeoutController {
7373
return this.#abort(cause)
7474
}
7575

76-
async race<V>(promise: PromiseLike<V>, cause?: unknown): Promise<V | TimeoutError> {
76+
race<V>(promise: PromiseLike<V>, cause?: unknown): PromiseLike<V | TimeoutError> {
7777
if (!this.signal) {
7878
return promise
7979
}
8080

8181
if (this.#remaining <= 0) {
82-
return this.#abort(cause)
82+
return Promise.resolve(this.#abort(cause))
8383
}
8484

8585
const timedOut = this.checkDidTimeout(cause)
8686

8787
if (timedOut) {
88-
return timedOut
88+
return Promise.resolve(timedOut)
8989
}
9090

91-
return await resolveWithAbort(
91+
return resolveWithAbort(
9292
this.signal,
9393
promise,
9494
() =>

0 commit comments

Comments
 (0)