@mnaoumov/typescript-lib 0.0.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/@types/debug/package.json +4 -0
- package/LICENSE +21 -0
- package/README.md +7 -1
- package/dist/lib/cjs/@types/debug.d.cts +21 -0
- package/dist/lib/cjs/abort-controller.cjs +202 -0
- package/dist/lib/cjs/abort-controller.d.cts +135 -0
- package/dist/lib/cjs/aggregate-error.cjs +98 -0
- package/dist/lib/cjs/aggregate-error.d.cts +24 -0
- package/dist/lib/cjs/array-buffer.cjs +96 -0
- package/dist/lib/cjs/array-buffer.d.cts +16 -0
- package/dist/lib/cjs/array.cjs +134 -0
- package/dist/lib/cjs/array.d.cts +46 -0
- package/dist/lib/cjs/async-events.cjs +466 -0
- package/dist/lib/cjs/async-events.d.cts +456 -0
- package/dist/lib/cjs/async-operation-hook.cjs +113 -0
- package/dist/lib/cjs/async-operation-hook.d.cts +59 -0
- package/dist/lib/cjs/async.cjs +521 -0
- package/dist/lib/cjs/async.d.cts +414 -0
- package/dist/lib/cjs/beeper.cjs +126 -0
- package/dist/lib/cjs/beeper.d.cts +21 -0
- package/dist/lib/cjs/blob.cjs +160 -0
- package/dist/lib/cjs/blob.d.cts +59 -0
- package/dist/lib/cjs/bytes.cjs +114 -0
- package/dist/lib/cjs/bytes.d.cts +17 -0
- package/dist/lib/cjs/debug-controller.cjs +80 -0
- package/dist/lib/cjs/debug-controller.d.cts +87 -0
- package/dist/lib/cjs/debug.cjs +235 -0
- package/dist/lib/cjs/debug.d.cts +66 -0
- package/dist/lib/cjs/desktop-zip-extractor.cjs +215 -0
- package/dist/lib/cjs/desktop-zip-extractor.d.cts +95 -0
- package/dist/lib/cjs/disposable.cjs +414 -0
- package/dist/lib/cjs/disposable.d.cts +387 -0
- package/dist/lib/cjs/enum.cjs +100 -0
- package/dist/lib/cjs/enum.d.cts +24 -0
- package/dist/lib/cjs/error.cjs +297 -0
- package/dist/lib/cjs/error.d.cts +181 -0
- package/dist/lib/cjs/function.cjs +141 -0
- package/dist/lib/cjs/function.d.cts +128 -0
- package/dist/lib/cjs/html-element.cjs +341 -0
- package/dist/lib/cjs/html-element.d.cts +199 -0
- package/dist/lib/cjs/index.cjs +195 -0
- package/dist/lib/cjs/index.d.cts +33 -0
- package/dist/lib/cjs/jpeg-metadata.cjs +228 -0
- package/dist/lib/cjs/jpeg-metadata.d.cts +42 -0
- package/dist/lib/cjs/library.cjs +158 -0
- package/dist/lib/cjs/library.d.cts +96 -0
- package/dist/lib/cjs/object-utils.cjs +617 -0
- package/dist/lib/cjs/object-utils.d.cts +298 -0
- package/dist/lib/cjs/path.cjs +202 -0
- package/dist/lib/cjs/path.d.cts +154 -0
- package/dist/lib/cjs/reg-exp.cjs +254 -0
- package/dist/lib/cjs/reg-exp.d.cts +87 -0
- package/dist/lib/cjs/string.cjs +326 -0
- package/dist/lib/cjs/string.d.cts +361 -0
- package/dist/lib/cjs/transformers/date-transformer.cjs +129 -0
- package/dist/lib/cjs/transformers/date-transformer.d.cts +39 -0
- package/dist/lib/cjs/transformers/group-transformer.cjs +173 -0
- package/dist/lib/cjs/transformers/group-transformer.d.cts +70 -0
- package/dist/lib/cjs/transformers/index.cjs +123 -0
- package/dist/lib/cjs/transformers/index.d.cts +9 -0
- package/dist/lib/cjs/transformers/map-transformer.cjs +129 -0
- package/dist/lib/cjs/transformers/map-transformer.d.cts +41 -0
- package/dist/lib/cjs/transformers/set-transformer.cjs +129 -0
- package/dist/lib/cjs/transformers/set-transformer.d.cts +39 -0
- package/dist/lib/cjs/transformers/skip-private-property-transformer.cjs +127 -0
- package/dist/lib/cjs/transformers/skip-private-property-transformer.d.cts +36 -0
- package/dist/lib/cjs/transformers/transformer.cjs +170 -0
- package/dist/lib/cjs/transformers/transformer.d.cts +70 -0
- package/dist/lib/cjs/transformers/two-way-map-transformer.cjs +130 -0
- package/dist/lib/cjs/transformers/two-way-map-transformer.d.cts +42 -0
- package/dist/lib/cjs/transformers/typed-transformer.cjs +94 -0
- package/dist/lib/cjs/transformers/typed-transformer.d.cts +39 -0
- package/dist/lib/cjs/two-way-map.cjs +208 -0
- package/dist/lib/cjs/two-way-map.d.cts +101 -0
- package/dist/lib/cjs/type-guards.cjs +128 -0
- package/dist/lib/cjs/type-guards.d.cts +84 -0
- package/dist/lib/cjs/type.cjs +121 -0
- package/dist/lib/cjs/type.d.cts +122 -0
- package/dist/lib/cjs/typescript-lib-state.cjs +126 -0
- package/dist/lib/cjs/typescript-lib-state.d.cts +56 -0
- package/dist/lib/cjs/url.cjs +136 -0
- package/dist/lib/cjs/url.d.cts +74 -0
- package/dist/lib/cjs/value-provider.cjs +100 -0
- package/dist/lib/cjs/value-provider.d.cts +28 -0
- package/dist/lib/cjs/value-wrapper.cjs +144 -0
- package/dist/lib/cjs/value-wrapper.d.cts +47 -0
- package/dist/lib/esm/@types/debug.d.mts +21 -0
- package/dist/lib/esm/abort-controller.d.mts +135 -0
- package/dist/lib/esm/abort-controller.mjs +159 -0
- package/dist/lib/esm/aggregate-error.d.mts +24 -0
- package/dist/lib/esm/aggregate-error.mjs +60 -0
- package/dist/lib/esm/array-buffer.d.mts +16 -0
- package/dist/lib/esm/array-buffer.mjs +58 -0
- package/dist/lib/esm/array.d.mts +46 -0
- package/dist/lib/esm/array.mjs +93 -0
- package/dist/lib/esm/async-events.d.mts +456 -0
- package/dist/lib/esm/async-events.mjs +427 -0
- package/dist/lib/esm/async-operation-hook.d.mts +59 -0
- package/dist/lib/esm/async-operation-hook.mjs +71 -0
- package/dist/lib/esm/async.d.mts +414 -0
- package/dist/lib/esm/async.mjs +479 -0
- package/dist/lib/esm/beeper.d.mts +21 -0
- package/dist/lib/esm/beeper.mjs +88 -0
- package/dist/lib/esm/blob.d.mts +59 -0
- package/dist/lib/esm/blob.mjs +121 -0
- package/dist/lib/esm/bytes.d.mts +17 -0
- package/dist/lib/esm/bytes.mjs +76 -0
- package/dist/lib/esm/debug-controller.d.mts +87 -0
- package/dist/lib/esm/debug-controller.mjs +50 -0
- package/dist/lib/esm/debug.d.mts +66 -0
- package/dist/lib/esm/debug.mjs +185 -0
- package/dist/lib/esm/desktop-zip-extractor.d.mts +95 -0
- package/dist/lib/esm/desktop-zip-extractor.mjs +180 -0
- package/dist/lib/esm/disposable.d.mts +387 -0
- package/dist/lib/esm/disposable.mjs +360 -0
- package/dist/lib/esm/enum.d.mts +24 -0
- package/dist/lib/esm/enum.mjs +61 -0
- package/dist/lib/esm/error.d.mts +181 -0
- package/dist/lib/esm/error.mjs +248 -0
- package/dist/lib/esm/function.d.mts +128 -0
- package/dist/lib/esm/function.mjs +96 -0
- package/dist/lib/esm/html-element.d.mts +199 -0
- package/dist/lib/esm/html-element.mjs +291 -0
- package/dist/lib/esm/index.d.mts +33 -0
- package/dist/lib/esm/index.mjs +116 -0
- package/dist/lib/esm/jpeg-metadata.d.mts +42 -0
- package/dist/lib/esm/jpeg-metadata.mjs +189 -0
- package/dist/lib/esm/library.d.mts +96 -0
- package/dist/lib/esm/library.mjs +118 -0
- package/dist/lib/esm/object-utils.d.mts +298 -0
- package/dist/lib/esm/object-utils.mjs +567 -0
- package/dist/lib/esm/path.d.mts +154 -0
- package/dist/lib/esm/path.mjs +139 -0
- package/dist/lib/esm/reg-exp.d.mts +87 -0
- package/dist/lib/esm/reg-exp.mjs +213 -0
- package/dist/lib/esm/string.d.mts +361 -0
- package/dist/lib/esm/string.mjs +274 -0
- package/dist/lib/esm/transformers/date-transformer.d.mts +39 -0
- package/dist/lib/esm/transformers/date-transformer.mjs +91 -0
- package/dist/lib/esm/transformers/group-transformer.d.mts +70 -0
- package/dist/lib/esm/transformers/group-transformer.mjs +138 -0
- package/dist/lib/esm/transformers/index.d.mts +9 -0
- package/dist/lib/esm/transformers/index.mjs +68 -0
- package/dist/lib/esm/transformers/map-transformer.d.mts +41 -0
- package/dist/lib/esm/transformers/map-transformer.mjs +91 -0
- package/dist/lib/esm/transformers/set-transformer.d.mts +39 -0
- package/dist/lib/esm/transformers/set-transformer.mjs +91 -0
- package/dist/lib/esm/transformers/skip-private-property-transformer.d.mts +36 -0
- package/dist/lib/esm/transformers/skip-private-property-transformer.mjs +89 -0
- package/dist/lib/esm/transformers/transformer.d.mts +70 -0
- package/dist/lib/esm/transformers/transformer.mjs +132 -0
- package/dist/lib/esm/transformers/two-way-map-transformer.d.mts +42 -0
- package/dist/lib/esm/transformers/two-way-map-transformer.mjs +92 -0
- package/dist/lib/esm/transformers/typed-transformer.d.mts +39 -0
- package/dist/lib/esm/transformers/typed-transformer.mjs +56 -0
- package/dist/lib/esm/two-way-map.d.mts +101 -0
- package/dist/lib/esm/two-way-map.mjs +170 -0
- package/dist/lib/esm/type-guards.d.mts +84 -0
- package/dist/lib/esm/type-guards.mjs +85 -0
- package/dist/lib/esm/type.d.mts +122 -0
- package/dist/lib/esm/type.mjs +83 -0
- package/dist/lib/esm/typescript-lib-state.d.mts +56 -0
- package/dist/lib/esm/typescript-lib-state.mjs +84 -0
- package/dist/lib/esm/url.d.mts +74 -0
- package/dist/lib/esm/url.mjs +94 -0
- package/dist/lib/esm/value-provider.d.mts +28 -0
- package/dist/lib/esm/value-provider.mjs +62 -0
- package/dist/lib/esm/value-wrapper.d.mts +47 -0
- package/dist/lib/esm/value-wrapper.mjs +106 -0
- package/package.json +111 -13
- package/transformers/date-transformer/package.json +6 -0
- package/transformers/group-transformer/package.json +6 -0
- package/transformers/index/package.json +6 -0
- package/transformers/map-transformer/package.json +6 -0
- package/transformers/package.json +6 -0
- package/transformers/set-transformer/package.json +6 -0
- package/transformers/skip-private-property-transformer/package.json +6 -0
- package/transformers/transformer/package.json +6 -0
- package/transformers/two-way-map-transformer/package.json +6 -0
- package/transformers/typed-transformer/package.json +6 -0
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
/// <reference path="./library.d.mts" />
|
|
2
|
+
/**
|
|
3
|
+
* @file
|
|
4
|
+
*
|
|
5
|
+
* Contains utility functions for asynchronous operations.
|
|
6
|
+
*/
|
|
7
|
+
import type { Promisable } from 'type-fest';
|
|
8
|
+
import type { GenericAsyncFunction, GenericFunction, GenericVoidFunction } from './function.mjs';
|
|
9
|
+
/**
|
|
10
|
+
* Parameters for {@link invokeAsyncSafelyAfterDelay}.
|
|
11
|
+
*/
|
|
12
|
+
export interface InvokeAsyncSafelyAfterDelayParams {
|
|
13
|
+
/**
|
|
14
|
+
* The abort signal to listen to.
|
|
15
|
+
*/
|
|
16
|
+
readonly abortSignal?: AbortSignal;
|
|
17
|
+
/**
|
|
18
|
+
* The asynchronous function to invoke.
|
|
19
|
+
*
|
|
20
|
+
* @param abortSignal - The abort signal to listen to.
|
|
21
|
+
*/
|
|
22
|
+
readonly asyncFunction: (abortSignal: AbortSignal) => Promisable<void>;
|
|
23
|
+
/**
|
|
24
|
+
* The delay in milliseconds.
|
|
25
|
+
*
|
|
26
|
+
* @default `0`
|
|
27
|
+
*/
|
|
28
|
+
readonly delayInMilliseconds?: number;
|
|
29
|
+
/**
|
|
30
|
+
* The stack trace of the source function.
|
|
31
|
+
*/
|
|
32
|
+
readonly stackTrace?: string;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* A type representing a function that resolves a {@link Promise}.
|
|
36
|
+
*
|
|
37
|
+
* @typeParam T - The type of the value.
|
|
38
|
+
*/
|
|
39
|
+
export type PromiseResolve<T> = undefined extends T ? (value?: PromiseLike<T> | T) => void : (value: PromiseLike<T> | T) => void;
|
|
40
|
+
/**
|
|
41
|
+
* Options for {@link retryWithTimeout}.
|
|
42
|
+
*/
|
|
43
|
+
export interface RetryOptions {
|
|
44
|
+
/**
|
|
45
|
+
* A abort signal to cancel the retry operation.
|
|
46
|
+
*/
|
|
47
|
+
readonly abortSignal?: AbortSignal;
|
|
48
|
+
/**
|
|
49
|
+
* A delay in milliseconds between retry attempts.
|
|
50
|
+
*
|
|
51
|
+
* @default `100`
|
|
52
|
+
*/
|
|
53
|
+
readonly retryDelayInMilliseconds?: number;
|
|
54
|
+
/**
|
|
55
|
+
* Whether to retry the function on error.
|
|
56
|
+
*
|
|
57
|
+
* @default `false`
|
|
58
|
+
*/
|
|
59
|
+
readonly shouldRetryOnError?: boolean;
|
|
60
|
+
/**
|
|
61
|
+
* A maximum time in milliseconds to wait before giving up on retrying.
|
|
62
|
+
*
|
|
63
|
+
* @default `5000`
|
|
64
|
+
*/
|
|
65
|
+
readonly timeoutInMilliseconds?: number;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Adds an error handler to a {@link Promise} that catches any errors and emits an async error event.
|
|
69
|
+
*
|
|
70
|
+
* @param asyncFunction - The asynchronous function to add an error handler to.
|
|
71
|
+
* @param stackTrace - The stack trace of the source function.
|
|
72
|
+
* @returns A {@link Promise} that resolves when the asynchronous function completes or emits async error event.
|
|
73
|
+
*/
|
|
74
|
+
export declare function addErrorHandler(asyncFunction: () => Promise<unknown>, stackTrace?: string): Promise<void>;
|
|
75
|
+
/**
|
|
76
|
+
* Filters an array asynchronously, keeping only the elements that satisfy the provided predicate function.
|
|
77
|
+
*
|
|
78
|
+
* @typeParam T - The type of elements in the input array.
|
|
79
|
+
* @param array - The array to filter.
|
|
80
|
+
* @param predicate - The predicate function to test each element.
|
|
81
|
+
* @returns A {@link Promise} that resolves with an array of elements that satisfy the predicate function.
|
|
82
|
+
*/
|
|
83
|
+
export declare function asyncFilter<T>(array: T[], predicate: (value: T, index: number, array: T[]) => Promisable<boolean>): Promise<T[]>;
|
|
84
|
+
/**
|
|
85
|
+
* Filters an array asynchronously in place, keeping only the elements that satisfy the provided predicate function.
|
|
86
|
+
*
|
|
87
|
+
* @typeParam T - The type of elements in the input array.
|
|
88
|
+
* @param array - The array to filter.
|
|
89
|
+
* @param predicate - The predicate function to test each element.
|
|
90
|
+
* @returns A {@link Promise} that resolves when the array is filtered.
|
|
91
|
+
*/
|
|
92
|
+
export declare function asyncFilterInPlace<T>(array: T[], predicate: (value: T, index: number, array: T[]) => Promisable<boolean>): Promise<void>;
|
|
93
|
+
/**
|
|
94
|
+
* Maps over an array asynchronously, applying the provided callback function to each element, and then flattens the results into a single array.
|
|
95
|
+
*
|
|
96
|
+
* @typeParam T - The type of elements in the input array.
|
|
97
|
+
* @typeParam U - The type of elements in the output array.
|
|
98
|
+
* @param array - The array to map over and flatten.
|
|
99
|
+
* @param callback - The callback function to apply to each element.
|
|
100
|
+
* @returns A {@link Promise} that resolves with a flattened array of the results of the callback function.
|
|
101
|
+
*/
|
|
102
|
+
export declare function asyncFlatMap<T, U>(array: T[], callback: (value: T, index: number, array: T[]) => Promisable<U[]>): Promise<U[]>;
|
|
103
|
+
/**
|
|
104
|
+
* Maps over an array asynchronously, applying the provided callback function to each element.
|
|
105
|
+
*
|
|
106
|
+
* @typeParam T - The type of elements in the input array.
|
|
107
|
+
* @typeParam U - The type of elements in the output array.
|
|
108
|
+
* @param array - The array to map over.
|
|
109
|
+
* @param callback - The callback function to apply to each element.
|
|
110
|
+
* @returns A {@link Promise} that resolves with an array of the results of the callback function.
|
|
111
|
+
*/
|
|
112
|
+
export declare function asyncMap<T, U>(array: T[], callback: (value: T, index: number, array: T[]) => Promisable<U>): Promise<U[]>;
|
|
113
|
+
/**
|
|
114
|
+
* Converts an asynchronous function to a synchronous one by automatically handling the Promise rejection.
|
|
115
|
+
*
|
|
116
|
+
* @typeParam Arguments - The types of the arguments the function accepts.
|
|
117
|
+
* @param asyncFunction - The asynchronous function to convert.
|
|
118
|
+
* @param stackTrace - The stack trace of the source function.
|
|
119
|
+
* @returns A function that wraps the asynchronous function in a synchronous interface.
|
|
120
|
+
*/
|
|
121
|
+
export declare function convertAsyncToSync<Arguments extends unknown[]>(asyncFunction: GenericAsyncFunction<Arguments>, stackTrace?: string): GenericVoidFunction<Arguments>;
|
|
122
|
+
/**
|
|
123
|
+
* Converts a synchronous function to an asynchronous one by wrapping it in a {@link Promise}.
|
|
124
|
+
*
|
|
125
|
+
* @typeParam Arguments - The types of the arguments the function accepts.
|
|
126
|
+
* @typeParam Result - The type of the function's return value.
|
|
127
|
+
* @param syncFunction - The synchronous function to convert.
|
|
128
|
+
* @returns A function that wraps the synchronous function in an asynchronous interface.
|
|
129
|
+
*/
|
|
130
|
+
export declare function convertSyncToAsync<Arguments extends unknown[], Result>(syncFunction: GenericFunction<Arguments, Result>): GenericAsyncFunction<Arguments, Result>;
|
|
131
|
+
/**
|
|
132
|
+
* Handles a silent error.
|
|
133
|
+
*
|
|
134
|
+
* @param error - The error to handle.
|
|
135
|
+
* @returns Whether the error is a silent error.
|
|
136
|
+
*/
|
|
137
|
+
export declare function handleSilentError(error: unknown): boolean;
|
|
138
|
+
/**
|
|
139
|
+
* Ignores an error that is thrown by an asynchronous function.
|
|
140
|
+
*
|
|
141
|
+
* @param promise - The promise to ignore the error of.
|
|
142
|
+
* @param fallbackValue - Always `undefined`.
|
|
143
|
+
* @returns A {@link Promise} that resolves when the asynchronous function completes or fails.
|
|
144
|
+
*/
|
|
145
|
+
export declare function ignoreError(promise: Promise<unknown>, fallbackValue?: undefined): Promise<void>;
|
|
146
|
+
/**
|
|
147
|
+
* Invokes an asynchronous function and returns a fallback value if an error is thrown.
|
|
148
|
+
*
|
|
149
|
+
* @typeParam T - The type of the value returned by the asynchronous function.
|
|
150
|
+
* @param promise - The promise to ignore the error of.
|
|
151
|
+
* @param fallbackValue - The value to return if an error is thrown.
|
|
152
|
+
* @returns A {@link Promise} that resolves with the value returned by the asynchronous function or the fallback value if an error is thrown.
|
|
153
|
+
*/
|
|
154
|
+
export declare function ignoreError<T>(promise: Promise<T>, fallbackValue: T): Promise<T>;
|
|
155
|
+
/**
|
|
156
|
+
* Invokes a {@link Promise} and safely handles any errors by catching them and emitting an async error event.
|
|
157
|
+
*
|
|
158
|
+
* @param asyncFunction - The asynchronous function to invoke safely.
|
|
159
|
+
* @param stackTrace - The stack trace of the source function.
|
|
160
|
+
*/
|
|
161
|
+
export declare function invokeAsyncSafely(asyncFunction: () => Promisable<unknown>, stackTrace?: string): void;
|
|
162
|
+
/**
|
|
163
|
+
* Invokes an asynchronous function after a delay.
|
|
164
|
+
*
|
|
165
|
+
* @param params - The parameters for the function.
|
|
166
|
+
*/
|
|
167
|
+
export declare function invokeAsyncSafelyAfterDelay(params: InvokeAsyncSafelyAfterDelayParams): void;
|
|
168
|
+
/**
|
|
169
|
+
* Executes async functions sequentially.
|
|
170
|
+
*
|
|
171
|
+
* @typeParam T - The type of the value.
|
|
172
|
+
* @param asyncFns - The async functions to execute sequentially.
|
|
173
|
+
* @returns A {@link Promise} that resolves with an array of the results of the async functions.
|
|
174
|
+
*/
|
|
175
|
+
export declare function promiseAllAsyncFnsSequentially<T>(asyncFns: (() => Promisable<T>)[]): Promise<T[]>;
|
|
176
|
+
/**
|
|
177
|
+
* Executes promises sequentially.
|
|
178
|
+
*
|
|
179
|
+
* @typeParam T - The type of the value.
|
|
180
|
+
* @param promises - The promises to execute sequentially.
|
|
181
|
+
* @returns A {@link Promise} that resolves with an array of the results of the promises.
|
|
182
|
+
*/
|
|
183
|
+
export declare function promiseAllSequentially<T>(promises: Promisable<T>[]): Promise<T[]>;
|
|
184
|
+
/**
|
|
185
|
+
* Options for {@link retryWithTimeout}.
|
|
186
|
+
*/
|
|
187
|
+
export interface RetryWithTimeoutParams {
|
|
188
|
+
/**
|
|
189
|
+
* The function to handle the timeout.
|
|
190
|
+
*
|
|
191
|
+
* @param context - The timeout context.
|
|
192
|
+
*/
|
|
193
|
+
readonly onTimeout?: (context: TimeoutContext) => void;
|
|
194
|
+
/**
|
|
195
|
+
* The function to execute.
|
|
196
|
+
*
|
|
197
|
+
* @param abortSignal - The abort signal to listen to.
|
|
198
|
+
* @returns The result of the function.
|
|
199
|
+
*/
|
|
200
|
+
readonly operationFunction: (abortSignal: AbortSignal) => Promisable<boolean>;
|
|
201
|
+
/**
|
|
202
|
+
* The name of the operation.
|
|
203
|
+
*
|
|
204
|
+
* @default `''`
|
|
205
|
+
*/
|
|
206
|
+
readonly operationName?: string;
|
|
207
|
+
/**
|
|
208
|
+
* The retry options.
|
|
209
|
+
*/
|
|
210
|
+
readonly retryOptions?: RetryOptions;
|
|
211
|
+
/**
|
|
212
|
+
* The stack trace of the source function.
|
|
213
|
+
*/
|
|
214
|
+
readonly stackTrace?: string;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Options for {@link runWithTimeout}.
|
|
218
|
+
*
|
|
219
|
+
* @typeParam Result - The type of the result returned by the operation function.
|
|
220
|
+
*/
|
|
221
|
+
export interface RunWithTimeoutParams<Result> {
|
|
222
|
+
/**
|
|
223
|
+
* The context of the function.
|
|
224
|
+
*/
|
|
225
|
+
readonly context?: unknown;
|
|
226
|
+
/**
|
|
227
|
+
* The function to handle the timeout.
|
|
228
|
+
*
|
|
229
|
+
* @param context - The timeout context.
|
|
230
|
+
*/
|
|
231
|
+
readonly onTimeout?: (context: TimeoutContext) => void;
|
|
232
|
+
/**
|
|
233
|
+
* The operation function to execute.
|
|
234
|
+
*
|
|
235
|
+
* @param abortSignal - The abort signal to listen to.
|
|
236
|
+
* @returns The result of the function.
|
|
237
|
+
*/
|
|
238
|
+
readonly operationFunction: (abortSignal: AbortSignal) => Promisable<Result>;
|
|
239
|
+
/**
|
|
240
|
+
* The name of the operation.
|
|
241
|
+
*
|
|
242
|
+
* @default `''`
|
|
243
|
+
*/
|
|
244
|
+
readonly operationName?: string;
|
|
245
|
+
/**
|
|
246
|
+
* The stack trace of the source function.
|
|
247
|
+
*/
|
|
248
|
+
readonly stackTrace?: string | undefined;
|
|
249
|
+
/**
|
|
250
|
+
* The maximum time to wait in milliseconds.
|
|
251
|
+
*/
|
|
252
|
+
readonly timeoutInMilliseconds: number;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Parameters for {@link sleep}.
|
|
256
|
+
*/
|
|
257
|
+
export interface SleepParams {
|
|
258
|
+
/**
|
|
259
|
+
* The abort signal to listen to.
|
|
260
|
+
*/
|
|
261
|
+
readonly abortSignal?: AbortSignal;
|
|
262
|
+
/**
|
|
263
|
+
* The time to wait in milliseconds.
|
|
264
|
+
*/
|
|
265
|
+
readonly milliseconds: number;
|
|
266
|
+
/**
|
|
267
|
+
* Whether to throw an error if the abort signal is aborted.
|
|
268
|
+
*
|
|
269
|
+
* @default `false`
|
|
270
|
+
*/
|
|
271
|
+
readonly shouldThrowOnAbort?: boolean;
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Context provided to the timeout handler.
|
|
275
|
+
*/
|
|
276
|
+
export interface TimeoutContext {
|
|
277
|
+
/**
|
|
278
|
+
* The duration in milliseconds since the operation started.
|
|
279
|
+
*/
|
|
280
|
+
readonly duration: number;
|
|
281
|
+
/**
|
|
282
|
+
* Registers a callback to be invoked when the operation completes.
|
|
283
|
+
*
|
|
284
|
+
* @param callback - The function to call when the operation completes.
|
|
285
|
+
*/
|
|
286
|
+
onOperationCompleted: (callback: () => void) => void;
|
|
287
|
+
/**
|
|
288
|
+
* The name of the operation.
|
|
289
|
+
*/
|
|
290
|
+
readonly operationName: string;
|
|
291
|
+
/**
|
|
292
|
+
* Terminates the operation that timed out.
|
|
293
|
+
*/
|
|
294
|
+
terminateOperation: () => void;
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Parameters for {@link timeout}.
|
|
298
|
+
*/
|
|
299
|
+
export interface TimeoutParams {
|
|
300
|
+
/**
|
|
301
|
+
* The abort signal to listen to.
|
|
302
|
+
*/
|
|
303
|
+
readonly abortSignal?: AbortSignal;
|
|
304
|
+
/**
|
|
305
|
+
* Whether to throw an error if the abort signal is aborted.
|
|
306
|
+
*
|
|
307
|
+
* @default `false`
|
|
308
|
+
*/
|
|
309
|
+
readonly shouldThrowOnAbort?: boolean;
|
|
310
|
+
/**
|
|
311
|
+
* The timeout period in milliseconds.
|
|
312
|
+
*/
|
|
313
|
+
readonly timeoutInMilliseconds: number;
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Chains a promise with another promise.
|
|
317
|
+
*
|
|
318
|
+
* @param chainPromise - Represents the chained promise.
|
|
319
|
+
* @param promisableFunction - The function to chain.
|
|
320
|
+
* @returns Chained promise or `null` if no async logic is chained.
|
|
321
|
+
*/
|
|
322
|
+
export declare function chain(chainPromise: null | Promise<void>, promisableFunction: () => null | Promisable<void>): null | Promise<void>;
|
|
323
|
+
/**
|
|
324
|
+
* Marks an error to terminate retry logic.
|
|
325
|
+
*
|
|
326
|
+
* @param error - The error to mark to terminate retry logic.
|
|
327
|
+
*/
|
|
328
|
+
export declare function marksAsTerminateRetry(error: Error): void;
|
|
329
|
+
/**
|
|
330
|
+
* An async function that never ends.
|
|
331
|
+
*
|
|
332
|
+
* @returns A {@link Promise} that never resolves.
|
|
333
|
+
*/
|
|
334
|
+
export declare function neverEnds(): Promise<never>;
|
|
335
|
+
/**
|
|
336
|
+
* Gets the next tick.
|
|
337
|
+
*
|
|
338
|
+
* @returns A promise that resolves when the next tick is available.
|
|
339
|
+
*/
|
|
340
|
+
export declare function nextTickAsync(): Promise<void>;
|
|
341
|
+
/**
|
|
342
|
+
* Converts to native promisable, converting {@link PromiseLike} (aka Thenable) to a native {@link Promise}.
|
|
343
|
+
*
|
|
344
|
+
* @typeParam T - The type of the value.
|
|
345
|
+
* @param promisable - The promisable to normalize.
|
|
346
|
+
* @returns The value itself or a native {@link Promise} if the value is a {@link PromiseLike}.
|
|
347
|
+
*/
|
|
348
|
+
export declare function normalizePromisable<T>(promisable: Promisable<T>): Promise<T> | T;
|
|
349
|
+
/**
|
|
350
|
+
* Gets the next queue microtask.
|
|
351
|
+
*
|
|
352
|
+
* @returns A promise that resolves when the next queue microtask is available.
|
|
353
|
+
*/
|
|
354
|
+
export declare function queueMicrotaskAsync(): Promise<void>;
|
|
355
|
+
/**
|
|
356
|
+
* Gets the next request animation frame.
|
|
357
|
+
*
|
|
358
|
+
* @param fallbackTimeoutInMilliseconds - The fallback timeout in milliseconds to resolve the promise if `requestAnimationFrame` does not fire (e.g. the window is not active).
|
|
359
|
+
* Defaults to `100` milliseconds. Use `0` to disable the fallback.
|
|
360
|
+
* @returns A promise that resolves when the next request animation frame is available (or when the fallback time is reached).
|
|
361
|
+
*/
|
|
362
|
+
export declare function requestAnimationFrameAsync(fallbackTimeoutInMilliseconds?: number): Promise<void>;
|
|
363
|
+
/**
|
|
364
|
+
* Retries the provided function until it returns `true` or the timeout is reached.
|
|
365
|
+
*
|
|
366
|
+
* @param params - The parameters for the function.
|
|
367
|
+
* @returns A {@link Promise} that resolves when the function returns `true` or rejects when the timeout is reached.
|
|
368
|
+
*/
|
|
369
|
+
export declare function retryWithTimeout(params: RetryWithTimeoutParams): Promise<void>;
|
|
370
|
+
/**
|
|
371
|
+
* Executes a function with a timeout. If the function does not complete within the specified time, it is considered to have timed out.
|
|
372
|
+
*
|
|
373
|
+
* If `DEBUG=obsidian-dev-utils:Async:runWithTimeout` is set, the execution is not terminated after the timeout and the function is allowed to run indefinitely.
|
|
374
|
+
*
|
|
375
|
+
* @typeParam Result - The type of the result from the asynchronous function.
|
|
376
|
+
* @param params - The parameters for the function.
|
|
377
|
+
* @returns A {@link Promise} that resolves with the result of the asynchronous function or rejects if it times out.
|
|
378
|
+
*/
|
|
379
|
+
export declare function runWithTimeout<Result>(params: RunWithTimeoutParams<Result>): Promise<Result>;
|
|
380
|
+
/**
|
|
381
|
+
* Gets the next set immediate.
|
|
382
|
+
*
|
|
383
|
+
* @returns A promise that resolves when the next set immediate is available.
|
|
384
|
+
*/
|
|
385
|
+
export declare function setImmediateAsync(): Promise<void>;
|
|
386
|
+
/**
|
|
387
|
+
* Delays execution for a specified number of milliseconds.
|
|
388
|
+
*
|
|
389
|
+
* @param delay - The time to wait in milliseconds.
|
|
390
|
+
* @returns A {@link Promise} that resolves after the specified delay.
|
|
391
|
+
*/
|
|
392
|
+
export declare function setTimeoutAsync(delay?: number): Promise<void>;
|
|
393
|
+
/**
|
|
394
|
+
* Delays execution for a specified number of milliseconds.
|
|
395
|
+
*
|
|
396
|
+
* @param params - The parameters for the function.
|
|
397
|
+
* @returns A {@link Promise} that resolves after the specified delay.
|
|
398
|
+
*/
|
|
399
|
+
export declare function sleep(params: SleepParams): Promise<void>;
|
|
400
|
+
/**
|
|
401
|
+
* Returns a {@link Promise} that rejects after the specified timeout period.
|
|
402
|
+
*
|
|
403
|
+
* @param params - The parameters for the function.
|
|
404
|
+
* @returns A {@link Promise} that always rejects with a timeout error.
|
|
405
|
+
*/
|
|
406
|
+
export declare function timeout(params: TimeoutParams): Promise<never>;
|
|
407
|
+
/**
|
|
408
|
+
* Converts an AsyncIterableIterator to an array by consuming all its elements.
|
|
409
|
+
*
|
|
410
|
+
* @typeParam T - The type of elements produced by the AsyncIterableIterator.
|
|
411
|
+
* @param iter - The AsyncIterableIterator to convert.
|
|
412
|
+
* @returns A {@link Promise} that resolves with an array of all the elements in the AsyncIterableIterator.
|
|
413
|
+
*/
|
|
414
|
+
export declare function toArray<T>(iter: AsyncIterableIterator<T>): Promise<T[]>;
|