-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder.ts
More file actions
110 lines (96 loc) Β· 3.18 KB
/
builder.ts
File metadata and controls
110 lines (96 loc) Β· 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import type {
CancellationError,
RetryExhaustedError,
TimeoutError,
UnhandledException,
} from "./errors"
import type { BuilderConfig, TaskMap, TimeoutOptions, TimeoutPolicy, WrapFn } from "./types/builder"
import type {
DefaultTryCtxFeatures,
SetTryCtxFeature,
TryCtxFeatures,
TryCtxFor,
} from "./types/core"
import type { RetryOptions } from "./types/retry"
import type {
AsyncRunInput,
AsyncRunTryFn,
RunOptions,
SyncRunInput,
SyncRunTryFn,
} from "./types/run"
import { Panic } from "./errors"
import { normalizeRetryPolicy } from "./retry"
import { executeRunAsync, executeRunSync } from "./runner"
type ConfigRunErrors = RetryExhaustedError | TimeoutError | CancellationError
type WithRetry<CtxFeatures extends TryCtxFeatures> = SetTryCtxFeature<CtxFeatures, "retry">
function normalizeTimeoutOptions(options: TimeoutOptions): TimeoutPolicy {
if (typeof options === "number") {
return { ms: options, scope: "total" }
}
return options
}
export class TryBuilder<
E extends ConfigRunErrors = never,
CanRunSync extends boolean = true,
CtxFeatures extends TryCtxFeatures = DefaultTryCtxFeatures,
> {
readonly #config: BuilderConfig
constructor(config: BuilderConfig = {}) {
this.#config = config
}
retry<P extends RetryOptions>(
policy: P
): TryBuilder<E | RetryExhaustedError, P extends number ? true : false, WithRetry<CtxFeatures>>
retry(
policy: RetryOptions
): TryBuilder<E | RetryExhaustedError, boolean, WithRetry<CtxFeatures>> {
return new TryBuilder({
...this.#config,
retry: normalizeRetryPolicy(policy),
})
}
timeout(options: TimeoutOptions): TryBuilder<E | TimeoutError, CanRunSync, CtxFeatures> {
return new TryBuilder({
...this.#config,
timeout: normalizeTimeoutOptions(options),
})
}
signal(signal: AbortSignal): TryBuilder<E | CancellationError, CanRunSync, CtxFeatures> {
return new TryBuilder({ ...this.#config, signal })
}
wrap(fn: WrapFn): TryBuilder<E, CanRunSync, CtxFeatures> {
return new TryBuilder({
...this.#config,
wraps: [...(this.#config.wraps ?? []), fn],
})
}
run<T>(
tryFn: CanRunSync extends true ? SyncRunTryFn<T, TryCtxFor<CtxFeatures>> : never
): T | UnhandledException | E
run<T, C>(
options: CanRunSync extends true ? RunOptions<T, C, TryCtxFor<CtxFeatures>> : never
): T | C | E
run<T, C>(input: SyncRunInput<T, C, TryCtxFor<CtxFeatures>>) {
return executeRunSync(this.#config, input)
}
runAsync<T>(
tryFn: SyncRunTryFn<T, TryCtxFor<CtxFeatures>> | AsyncRunTryFn<T, TryCtxFor<CtxFeatures>>
): Promise<T | UnhandledException | E>
runAsync<T, C>(options: AsyncRunInput<T, C, TryCtxFor<CtxFeatures>>): Promise<T | C | E>
runAsync<T, C>(input: AsyncRunInput<T, C, TryCtxFor<CtxFeatures>>) {
return executeRunAsync(this.#config, input)
}
all(_tasks: TaskMap): never {
void this.#config
throw new Panic({ message: "all is not implemented yet" })
}
allSettled(_tasks: TaskMap): never {
void this.#config
throw new Panic({ message: "allSettled is not implemented yet" })
}
flow(_tasks: TaskMap): never {
void this.#config
throw new Panic({ message: "flow is not implemented yet" })
}
}