@xylabs/forget 5.0.83 → 5.0.84
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/README.md +47 -5
- package/dist/neutral/ForgetConfig.d.ts +9 -0
- package/dist/neutral/ForgetConfig.d.ts.map +1 -1
- package/dist/neutral/ForgetNodeConfig.d.ts +6 -0
- package/dist/neutral/ForgetNodeConfig.d.ts.map +1 -1
- package/dist/neutral/ForgetPromise.d.ts +16 -0
- package/dist/neutral/ForgetPromise.d.ts.map +1 -1
- package/dist/neutral/ForgetPromiseNode.d.ts +6 -0
- package/dist/neutral/ForgetPromiseNode.d.ts.map +1 -1
- package/dist/neutral/forget.d.ts +5 -0
- package/dist/neutral/forget.d.ts.map +1 -1
- package/dist/neutral/forgetNode.d.ts +5 -0
- package/dist/neutral/forgetNode.d.ts.map +1 -1
- package/dist/neutral/index.mjs +15 -0
- package/dist/neutral/index.mjs.map +1 -1
- package/dist/node/ForgetConfig.d.ts +9 -0
- package/dist/node/ForgetConfig.d.ts.map +1 -1
- package/dist/node/ForgetNodeConfig.d.ts +6 -0
- package/dist/node/ForgetNodeConfig.d.ts.map +1 -1
- package/dist/node/ForgetPromise.d.ts +16 -0
- package/dist/node/ForgetPromise.d.ts.map +1 -1
- package/dist/node/ForgetPromiseNode.d.ts +6 -0
- package/dist/node/ForgetPromiseNode.d.ts.map +1 -1
- package/dist/node/forget.d.ts +5 -0
- package/dist/node/forget.d.ts.map +1 -1
- package/dist/node/forgetNode.d.ts +5 -0
- package/dist/node/forgetNode.d.ts.map +1 -1
- package/dist/node/index.mjs +15 -0
- package/dist/node/index.mjs.map +1 -1
- package/package.json +8 -8
package/README.md
CHANGED
|
@@ -45,6 +45,8 @@ Base functionality used throughout XY Labs TypeScript/JavaScript libraries
|
|
|
45
45
|
|
|
46
46
|
***
|
|
47
47
|
|
|
48
|
+
Node.js extension of ForgetPromise that can terminate the process on exceptions or timeouts.
|
|
49
|
+
|
|
48
50
|
## Extends
|
|
49
51
|
|
|
50
52
|
- `ForgetPromise`
|
|
@@ -75,6 +77,8 @@ ForgetPromise.constructor
|
|
|
75
77
|
static activeForgets: number = 0;
|
|
76
78
|
```
|
|
77
79
|
|
|
80
|
+
Number of currently active (unresolved) forgotten promises.
|
|
81
|
+
|
|
78
82
|
### Inherited from
|
|
79
83
|
|
|
80
84
|
```ts
|
|
@@ -89,6 +93,8 @@ ForgetPromise.activeForgets
|
|
|
89
93
|
static exceptedForgets: number = 0;
|
|
90
94
|
```
|
|
91
95
|
|
|
96
|
+
Number of forgotten promises that threw exceptions.
|
|
97
|
+
|
|
92
98
|
### Inherited from
|
|
93
99
|
|
|
94
100
|
```ts
|
|
@@ -103,6 +109,8 @@ ForgetPromise.exceptedForgets
|
|
|
103
109
|
static logger: Logger = console;
|
|
104
110
|
```
|
|
105
111
|
|
|
112
|
+
Logger instance used for error and warning output.
|
|
113
|
+
|
|
106
114
|
### Inherited from
|
|
107
115
|
|
|
108
116
|
```ts
|
|
@@ -119,6 +127,8 @@ ForgetPromise.logger
|
|
|
119
127
|
get static active(): boolean;
|
|
120
128
|
```
|
|
121
129
|
|
|
130
|
+
Whether any forgotten promises are still active.
|
|
131
|
+
|
|
122
132
|
#### Returns
|
|
123
133
|
|
|
124
134
|
`boolean`
|
|
@@ -137,20 +147,28 @@ ForgetPromise.active
|
|
|
137
147
|
static awaitInactive(interval?, timeout?): Promise<number>;
|
|
138
148
|
```
|
|
139
149
|
|
|
150
|
+
Waits until all forgotten promises have completed.
|
|
151
|
+
|
|
140
152
|
### Parameters
|
|
141
153
|
|
|
142
154
|
#### interval?
|
|
143
155
|
|
|
144
156
|
`number` = `100`
|
|
145
157
|
|
|
158
|
+
Polling interval in milliseconds.
|
|
159
|
+
|
|
146
160
|
#### timeout?
|
|
147
161
|
|
|
148
162
|
`number`
|
|
149
163
|
|
|
164
|
+
Optional maximum wait time in milliseconds.
|
|
165
|
+
|
|
150
166
|
### Returns
|
|
151
167
|
|
|
152
168
|
`Promise`\<`number`\>
|
|
153
169
|
|
|
170
|
+
The number of remaining active forgets (0 if all completed).
|
|
171
|
+
|
|
154
172
|
### Inherited from
|
|
155
173
|
|
|
156
174
|
```ts
|
|
@@ -168,6 +186,8 @@ static exceptionHandler(
|
|
|
168
186
|
externalStackTrace?): void;
|
|
169
187
|
```
|
|
170
188
|
|
|
189
|
+
Handles exceptions, optionally terminating the process based on config.
|
|
190
|
+
|
|
171
191
|
### Parameters
|
|
172
192
|
|
|
173
193
|
#### error
|
|
@@ -200,7 +220,7 @@ ForgetPromise.exceptionHandler
|
|
|
200
220
|
static forget<T>(promise, config?): void;
|
|
201
221
|
```
|
|
202
222
|
|
|
203
|
-
|
|
223
|
+
Forgets a promise using Node.js-specific configuration with process termination support.
|
|
204
224
|
|
|
205
225
|
### Type Parameters
|
|
206
226
|
|
|
@@ -214,14 +234,10 @@ Used to explicitly launch an async function (or Promise) with awaiting it
|
|
|
214
234
|
|
|
215
235
|
`Promisable`\<`T`\>
|
|
216
236
|
|
|
217
|
-
The promise to forget
|
|
218
|
-
|
|
219
237
|
#### config?
|
|
220
238
|
|
|
221
239
|
[`ForgetNodeConfig`](#../interfaces/ForgetNodeConfig)\<`T`\>
|
|
222
240
|
|
|
223
|
-
Configuration of forget settings
|
|
224
|
-
|
|
225
241
|
### Returns
|
|
226
242
|
|
|
227
243
|
`void`
|
|
@@ -243,6 +259,8 @@ static timeoutHandler(
|
|
|
243
259
|
externalStackTrace?): void;
|
|
244
260
|
```
|
|
245
261
|
|
|
262
|
+
Handles timeouts, optionally terminating the process based on config.
|
|
263
|
+
|
|
246
264
|
### Parameters
|
|
247
265
|
|
|
248
266
|
#### time
|
|
@@ -279,6 +297,8 @@ ForgetPromise.timeoutHandler
|
|
|
279
297
|
function forget<T>(promise, config?): void;
|
|
280
298
|
```
|
|
281
299
|
|
|
300
|
+
Node.js variant of forget that can optionally terminate the process on exceptions or timeouts.
|
|
301
|
+
|
|
282
302
|
## Type Parameters
|
|
283
303
|
|
|
284
304
|
### T
|
|
@@ -291,10 +311,14 @@ function forget<T>(promise, config?): void;
|
|
|
291
311
|
|
|
292
312
|
`Promisable`\<`T`\>
|
|
293
313
|
|
|
314
|
+
The promise or promisable value to forget.
|
|
315
|
+
|
|
294
316
|
### config?
|
|
295
317
|
|
|
296
318
|
[`ForgetNodeConfig`](#../interfaces/ForgetNodeConfig)\<`T`\>
|
|
297
319
|
|
|
320
|
+
Optional Node.js-specific configuration including process termination options.
|
|
321
|
+
|
|
298
322
|
## Returns
|
|
299
323
|
|
|
300
324
|
`void`
|
|
@@ -307,6 +331,8 @@ function forget<T>(promise, config?): void;
|
|
|
307
331
|
|
|
308
332
|
***
|
|
309
333
|
|
|
334
|
+
Node.js-specific forget configuration that extends ForgetConfig with process termination options.
|
|
335
|
+
|
|
310
336
|
## Extends
|
|
311
337
|
|
|
312
338
|
- `ForgetConfig`\<`T`\>
|
|
@@ -325,6 +351,8 @@ function forget<T>(promise, config?): void;
|
|
|
325
351
|
optional name: string;
|
|
326
352
|
```
|
|
327
353
|
|
|
354
|
+
Optional name for identifying the forgotten promise in logs.
|
|
355
|
+
|
|
328
356
|
### Inherited from
|
|
329
357
|
|
|
330
358
|
```ts
|
|
@@ -339,6 +367,8 @@ ForgetConfig.name
|
|
|
339
367
|
optional onCancel: () => void;
|
|
340
368
|
```
|
|
341
369
|
|
|
370
|
+
Called when the promise is cancelled due to timeout.
|
|
371
|
+
|
|
342
372
|
### Returns
|
|
343
373
|
|
|
344
374
|
`void`
|
|
@@ -357,6 +387,8 @@ ForgetConfig.onCancel
|
|
|
357
387
|
optional onComplete: (result) => void;
|
|
358
388
|
```
|
|
359
389
|
|
|
390
|
+
Called when the promise completes, with a tuple of [result, error].
|
|
391
|
+
|
|
360
392
|
### Parameters
|
|
361
393
|
|
|
362
394
|
#### result
|
|
@@ -381,6 +413,8 @@ ForgetConfig.onComplete
|
|
|
381
413
|
optional onException: (error) => void;
|
|
382
414
|
```
|
|
383
415
|
|
|
416
|
+
Called when an exception occurs outside the promise itself.
|
|
417
|
+
|
|
384
418
|
### Parameters
|
|
385
419
|
|
|
386
420
|
#### error
|
|
@@ -405,6 +439,8 @@ ForgetConfig.onException
|
|
|
405
439
|
optional timeout: number;
|
|
406
440
|
```
|
|
407
441
|
|
|
442
|
+
Timeout in milliseconds after which the promise is considered timed out.
|
|
443
|
+
|
|
408
444
|
### Inherited from
|
|
409
445
|
|
|
410
446
|
```ts
|
|
@@ -419,6 +455,8 @@ ForgetConfig.timeout
|
|
|
419
455
|
optional terminateOnException: boolean;
|
|
420
456
|
```
|
|
421
457
|
|
|
458
|
+
Terminate the process on an exception that happens outside of the promise being forgotten.
|
|
459
|
+
|
|
422
460
|
***
|
|
423
461
|
|
|
424
462
|
### terminateOnTimeout?
|
|
@@ -427,6 +465,8 @@ optional terminateOnException: boolean;
|
|
|
427
465
|
optional terminateOnTimeout: boolean;
|
|
428
466
|
```
|
|
429
467
|
|
|
468
|
+
Terminate the process if the promise times out.
|
|
469
|
+
|
|
430
470
|
### variables
|
|
431
471
|
|
|
432
472
|
### <a id="defaultForgetNodeConfig"></a>defaultForgetNodeConfig
|
|
@@ -439,6 +479,8 @@ optional terminateOnTimeout: boolean;
|
|
|
439
479
|
const defaultForgetNodeConfig: ForgetNodeConfig<unknown>;
|
|
440
480
|
```
|
|
441
481
|
|
|
482
|
+
Default Node.js forget configuration with termination disabled.
|
|
483
|
+
|
|
442
484
|
|
|
443
485
|
Part of [sdk-js](https://www.npmjs.com/package/@xyo-network/sdk-js)
|
|
444
486
|
|
|
@@ -1,9 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration options for fire-and-forget promises.
|
|
3
|
+
*/
|
|
1
4
|
export interface ForgetConfig<T = any> {
|
|
5
|
+
/** Optional name for identifying the forgotten promise in logs. */
|
|
2
6
|
name?: string;
|
|
7
|
+
/** Called when the promise is cancelled due to timeout. */
|
|
3
8
|
onCancel?: () => void;
|
|
9
|
+
/** Called when the promise completes, with a tuple of [result, error]. */
|
|
4
10
|
onComplete?: (result: [T | undefined, Error | undefined]) => void;
|
|
11
|
+
/** Called when an exception occurs outside the promise itself. */
|
|
5
12
|
onException?: (error: Error) => void;
|
|
13
|
+
/** Timeout in milliseconds after which the promise is considered timed out. */
|
|
6
14
|
timeout?: number;
|
|
7
15
|
}
|
|
16
|
+
/** Default forget configuration with a 30-second timeout. */
|
|
8
17
|
export declare const defaultForgetConfig: ForgetConfig<unknown>;
|
|
9
18
|
//# sourceMappingURL=ForgetConfig.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ForgetConfig.d.ts","sourceRoot":"","sources":["../../src/ForgetConfig.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"ForgetConfig.d.ts","sourceRoot":"","sources":["../../src/ForgetConfig.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,WAAW,YAAY,CAAC,CAAC,GAAG,GAAG;IACnC,mEAAmE;IACnE,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,2DAA2D;IAC3D,QAAQ,CAAC,EAAE,MAAM,IAAI,CAAA;IACrB,0EAA0E;IAC1E,UAAU,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,CAAC,GAAG,SAAS,EAAE,KAAK,GAAG,SAAS,CAAC,KAAK,IAAI,CAAA;IACjE,kEAAkE;IAClE,WAAW,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAA;IACpC,+EAA+E;IAC/E,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB;AAED,6DAA6D;AAC7D,eAAO,MAAM,mBAAmB,EAAE,YAAY,CAAC,OAAO,CAAuB,CAAA"}
|
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import { type ForgetConfig } from './ForgetConfig.ts';
|
|
2
|
+
/**
|
|
3
|
+
* Node.js-specific forget configuration that extends ForgetConfig with process termination options.
|
|
4
|
+
*/
|
|
2
5
|
export interface ForgetNodeConfig<T = any> extends ForgetConfig<T> {
|
|
6
|
+
/** Terminate the process on an exception that happens outside of the promise being forgotten. */
|
|
3
7
|
terminateOnException?: boolean;
|
|
8
|
+
/** Terminate the process if the promise times out. */
|
|
4
9
|
terminateOnTimeout?: boolean;
|
|
5
10
|
}
|
|
11
|
+
/** Default Node.js forget configuration with termination disabled. */
|
|
6
12
|
export declare const defaultForgetNodeConfig: ForgetNodeConfig<unknown>;
|
|
7
13
|
//# sourceMappingURL=ForgetNodeConfig.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ForgetNodeConfig.d.ts","sourceRoot":"","sources":["../../src/ForgetNodeConfig.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,KAAK,YAAY,EAAE,MAAM,mBAAmB,CAAA;
|
|
1
|
+
{"version":3,"file":"ForgetNodeConfig.d.ts","sourceRoot":"","sources":["../../src/ForgetNodeConfig.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,KAAK,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAE1E;;GAEG;AAEH,MAAM,WAAW,gBAAgB,CAAC,CAAC,GAAG,GAAG,CAAE,SAAQ,YAAY,CAAC,CAAC,CAAC;IAChE,iGAAiG;IACjG,oBAAoB,CAAC,EAAE,OAAO,CAAA;IAC9B,sDAAsD;IACtD,kBAAkB,CAAC,EAAE,OAAO,CAAA;CAC7B;AAED,sEAAsE;AACtE,eAAO,MAAM,uBAAuB,EAAE,gBAAgB,CAAC,OAAO,CAI7D,CAAA"}
|
|
@@ -1,13 +1,28 @@
|
|
|
1
1
|
import type { Logger } from '@xylabs/logger';
|
|
2
2
|
import type { Promisable, PromiseEx } from '@xylabs/promise';
|
|
3
3
|
import { type ForgetConfig } from './ForgetConfig.ts';
|
|
4
|
+
/** A function that returns a promisable value. */
|
|
4
5
|
type PromisableFunction<T> = () => Promisable<T>;
|
|
6
|
+
/**
|
|
7
|
+
* Manages fire-and-forget promises with tracking, timeouts, and error handling.
|
|
8
|
+
*/
|
|
5
9
|
export declare class ForgetPromise {
|
|
10
|
+
/** Number of currently active (unresolved) forgotten promises. */
|
|
6
11
|
static activeForgets: number;
|
|
12
|
+
/** Number of forgotten promises that threw exceptions. */
|
|
7
13
|
static exceptedForgets: number;
|
|
14
|
+
/** Logger instance used for error and warning output. */
|
|
8
15
|
static logger: Logger;
|
|
16
|
+
/** Whether any forgotten promises are still active. */
|
|
9
17
|
static get active(): boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Waits until all forgotten promises have completed.
|
|
20
|
+
* @param interval - Polling interval in milliseconds.
|
|
21
|
+
* @param timeout - Optional maximum wait time in milliseconds.
|
|
22
|
+
* @returns The number of remaining active forgets (0 if all completed).
|
|
23
|
+
*/
|
|
10
24
|
static awaitInactive(interval?: number, timeout?: number): Promise<number>;
|
|
25
|
+
/** Handles exceptions from forgotten promises by logging error details. */
|
|
11
26
|
static exceptionHandler(error: Error, { name }: ForgetConfig, externalStackTrace?: string): void;
|
|
12
27
|
/**
|
|
13
28
|
* Used to explicitly launch an async function (or Promise) with awaiting it
|
|
@@ -15,6 +30,7 @@ export declare class ForgetPromise {
|
|
|
15
30
|
* @param config Configuration of forget settings
|
|
16
31
|
*/
|
|
17
32
|
static forget<T>(promise: Promise<T> | PromiseEx<T> | PromisableFunction<T> | T, config?: ForgetConfig<T>): void;
|
|
33
|
+
/** Handles timeout events for forgotten promises by logging timeout details. */
|
|
18
34
|
static timeoutHandler(time: number, { name }: ForgetConfig, externalStackTrace?: string): void;
|
|
19
35
|
}
|
|
20
36
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ForgetPromise.d.ts","sourceRoot":"","sources":["../../src/ForgetPromise.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAI5D,OAAO,EAAuB,KAAK,YAAY,EAAE,MAAM,mBA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AE,MAAM;CAMpG"}
|
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
import type { Promisable } from '@xylabs/promise';
|
|
2
2
|
import { type ForgetNodeConfig } from './ForgetNodeConfig.ts';
|
|
3
3
|
import { ForgetPromise } from './ForgetPromise.ts';
|
|
4
|
+
/**
|
|
5
|
+
* Node.js extension of ForgetPromise that can terminate the process on exceptions or timeouts.
|
|
6
|
+
*/
|
|
4
7
|
export declare class ForgetPromiseNode extends ForgetPromise {
|
|
8
|
+
/** Handles exceptions, optionally terminating the process based on config. */
|
|
5
9
|
static exceptionHandler(error: Error, config: ForgetNodeConfig, externalStackTrace?: string): void;
|
|
10
|
+
/** Forgets a promise using Node.js-specific configuration with process termination support. */
|
|
6
11
|
static forget<T>(promise: Promisable<T>, config?: ForgetNodeConfig<T>): void;
|
|
12
|
+
/** Handles timeouts, optionally terminating the process based on config. */
|
|
7
13
|
static timeoutHandler(time: number, config: ForgetNodeConfig, externalStackTrace?: string): void;
|
|
8
14
|
}
|
|
9
15
|
//# sourceMappingURL=ForgetPromiseNode.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ForgetPromiseNode.d.ts","sourceRoot":"","sources":["../../src/ForgetPromiseNode.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAEjD,OAAO,EAA2B,KAAK,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AACtF,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAElD,qBAAa,iBAAkB,SAAQ,aAAa;
|
|
1
|
+
{"version":3,"file":"ForgetPromiseNode.d.ts","sourceRoot":"","sources":["../../src/ForgetPromiseNode.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAEjD,OAAO,EAA2B,KAAK,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AACtF,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAElD;;GAEG;AACH,qBAAa,iBAAkB,SAAQ,aAAa;IAClD,8EAA8E;WAC9D,gBAAgB,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,gBAAgB,EAAE,kBAAkB,CAAC,EAAE,MAAM;IAUpG,+FAA+F;WAC/E,MAAM,CAAC,CAAC,EAAE,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC;IAO9E,4EAA4E;WAC5D,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,gBAAgB,EAAE,kBAAkB,CAAC,EAAE,MAAM;CAQnG"}
|
package/dist/neutral/forget.d.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
import { type Promisable } from '@xylabs/promise';
|
|
2
2
|
import type { ForgetConfig } from './ForgetConfig.ts';
|
|
3
|
+
/**
|
|
4
|
+
* Explicitly launches an async function or promise without awaiting it (fire-and-forget).
|
|
5
|
+
* @param promise - The promise or promisable value to forget.
|
|
6
|
+
* @param config - Optional configuration for timeout, error handling, and completion callbacks.
|
|
7
|
+
*/
|
|
3
8
|
export declare const forget: <T>(promise: Promisable<T>, config?: ForgetConfig<T>) => void;
|
|
4
9
|
//# sourceMappingURL=forget.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"forget.d.ts","sourceRoot":"","sources":["../../src/forget.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAEjD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;
|
|
1
|
+
{"version":3,"file":"forget.d.ts","sourceRoot":"","sources":["../../src/forget.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAEjD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAGrD;;;;GAIG;AACH,eAAO,MAAM,MAAM,GAAI,CAAC,EAAE,SAAS,UAAU,CAAC,CAAC,CAAC,EAAE,SAAS,YAAY,CAAC,CAAC,CAAC,SAEzE,CAAA"}
|
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
import { type Promisable } from '@xylabs/promise';
|
|
2
2
|
import type { ForgetNodeConfig } from './ForgetNodeConfig.ts';
|
|
3
|
+
/**
|
|
4
|
+
* Node.js variant of forget that can optionally terminate the process on exceptions or timeouts.
|
|
5
|
+
* @param promise - The promise or promisable value to forget.
|
|
6
|
+
* @param config - Optional Node.js-specific configuration including process termination options.
|
|
7
|
+
*/
|
|
3
8
|
export declare const forgetNode: <T>(promise: Promisable<T>, config?: ForgetNodeConfig<T>) => void;
|
|
4
9
|
//# sourceMappingURL=forgetNode.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"forgetNode.d.ts","sourceRoot":"","sources":["../../src/forgetNode.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAEjD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;
|
|
1
|
+
{"version":3,"file":"forgetNode.d.ts","sourceRoot":"","sources":["../../src/forgetNode.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAEjD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AAG7D;;;;GAIG;AACH,eAAO,MAAM,UAAU,GAAI,CAAC,EAAE,SAAS,UAAU,CAAC,CAAC,CAAC,EAAE,SAAS,gBAAgB,CAAC,CAAC,CAAC,SAEjF,CAAA"}
|
package/dist/neutral/index.mjs
CHANGED
|
@@ -13,12 +13,22 @@ import { delay } from "@xylabs/delay";
|
|
|
13
13
|
import { isPromise } from "@xylabs/promise";
|
|
14
14
|
import { isNumber } from "@xylabs/typeof";
|
|
15
15
|
var ForgetPromise = class {
|
|
16
|
+
/** Number of currently active (unresolved) forgotten promises. */
|
|
16
17
|
static activeForgets = 0;
|
|
18
|
+
/** Number of forgotten promises that threw exceptions. */
|
|
17
19
|
static exceptedForgets = 0;
|
|
20
|
+
/** Logger instance used for error and warning output. */
|
|
18
21
|
static logger = console;
|
|
22
|
+
/** Whether any forgotten promises are still active. */
|
|
19
23
|
static get active() {
|
|
20
24
|
return this.activeForgets > 0;
|
|
21
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Waits until all forgotten promises have completed.
|
|
28
|
+
* @param interval - Polling interval in milliseconds.
|
|
29
|
+
* @param timeout - Optional maximum wait time in milliseconds.
|
|
30
|
+
* @returns The number of remaining active forgets (0 if all completed).
|
|
31
|
+
*/
|
|
22
32
|
static async awaitInactive(interval = 100, timeout) {
|
|
23
33
|
let timeoutRemaining = timeout;
|
|
24
34
|
while (this.active) {
|
|
@@ -32,6 +42,7 @@ var ForgetPromise = class {
|
|
|
32
42
|
}
|
|
33
43
|
return 0;
|
|
34
44
|
}
|
|
45
|
+
/** Handles exceptions from forgotten promises by logging error details. */
|
|
35
46
|
static exceptionHandler(error, { name }, externalStackTrace) {
|
|
36
47
|
this.logger.error(`forget promise handler excepted [${name}]: ${error.message}`, error);
|
|
37
48
|
if (externalStackTrace !== void 0) {
|
|
@@ -94,6 +105,7 @@ var ForgetPromise = class {
|
|
|
94
105
|
return;
|
|
95
106
|
}
|
|
96
107
|
}
|
|
108
|
+
/** Handles timeout events for forgotten promises by logging timeout details. */
|
|
97
109
|
static timeoutHandler(time, { name = "unknown" }, externalStackTrace) {
|
|
98
110
|
this.logger.error(`forget promise timeout out after ${time}ms [Cancelling] [${name}]`);
|
|
99
111
|
if (externalStackTrace !== void 0) {
|
|
@@ -104,6 +116,7 @@ var ForgetPromise = class {
|
|
|
104
116
|
|
|
105
117
|
// src/ForgetPromiseNode.ts
|
|
106
118
|
var ForgetPromiseNode = class extends ForgetPromise {
|
|
119
|
+
/** Handles exceptions, optionally terminating the process based on config. */
|
|
107
120
|
static exceptionHandler(error, config, externalStackTrace) {
|
|
108
121
|
super.exceptionHandler(error, config, externalStackTrace);
|
|
109
122
|
if (config?.terminateOnException === true) {
|
|
@@ -111,6 +124,7 @@ var ForgetPromiseNode = class extends ForgetPromise {
|
|
|
111
124
|
process.exit(1);
|
|
112
125
|
}
|
|
113
126
|
}
|
|
127
|
+
/** Forgets a promise using Node.js-specific configuration with process termination support. */
|
|
114
128
|
static forget(promise, config) {
|
|
115
129
|
const resolvedConfig = {
|
|
116
130
|
...defaultForgetNodeConfig,
|
|
@@ -119,6 +133,7 @@ var ForgetPromiseNode = class extends ForgetPromise {
|
|
|
119
133
|
};
|
|
120
134
|
super.forget(promise, resolvedConfig);
|
|
121
135
|
}
|
|
136
|
+
/** Handles timeouts, optionally terminating the process based on config. */
|
|
122
137
|
static timeoutHandler(time, config, externalStackTrace) {
|
|
123
138
|
super.timeoutHandler(time, config, externalStackTrace);
|
|
124
139
|
if (config?.terminateOnTimeout === true) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/ForgetConfig.ts","../../src/ForgetNodeConfig.ts","../../src/ForgetPromise.ts","../../src/ForgetPromiseNode.ts","../../src/forgetNode.ts"],"sourcesContent":["// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport interface ForgetConfig<T = any> {\n name?: string\n onCancel?: () => void\n onComplete?: (result: [T | undefined, Error | undefined]) => void\n onException?: (error: Error) => void\n timeout?: number\n}\n\nexport const defaultForgetConfig: ForgetConfig<unknown> = { timeout: 30_000 }\n","import { defaultForgetConfig, type ForgetConfig } from './ForgetConfig.ts'\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport interface ForgetNodeConfig<T = any> extends ForgetConfig<T> {\n terminateOnException?: boolean // terminate the process on an exception that happens outside of the promise being forgotten\n terminateOnTimeout?: boolean // terminate the process if the promise times out\n}\n\nexport const defaultForgetNodeConfig: ForgetNodeConfig<unknown> = {\n ...defaultForgetConfig,\n terminateOnTimeout: false,\n terminateOnException: false,\n}\n","import { delay } from '@xylabs/delay'\nimport type { Logger } from '@xylabs/logger'\nimport type { Promisable, PromiseEx } from '@xylabs/promise'\nimport { isPromise } from '@xylabs/promise'\nimport { isNumber } from '@xylabs/typeof'\n\nimport { defaultForgetConfig, type ForgetConfig } from './ForgetConfig.ts'\n\ntype PromisableFunction<T> = () => Promisable<T>\n\n// eslint-disable-next-line unicorn/no-static-only-class\nexport class ForgetPromise {\n static activeForgets = 0\n static exceptedForgets = 0\n static logger: Logger = console\n\n static get active() {\n return this.activeForgets > 0\n }\n\n static async awaitInactive(interval = 100, timeout?: number) {\n let timeoutRemaining = timeout\n while (this.active) {\n await delay(interval)\n if (timeoutRemaining !== undefined) {\n timeoutRemaining -= interval\n if (timeoutRemaining <= 0) {\n return this.activeForgets\n }\n }\n }\n return 0\n }\n\n static exceptionHandler(error: Error, { name }: ForgetConfig, externalStackTrace?: string) {\n this.logger.error(`forget promise handler excepted [${name}]: ${error.message}`, error)\n if (externalStackTrace !== undefined) {\n this.logger.warn(`External Stack trace [${name}]:`, externalStackTrace)\n }\n }\n\n /**\n * Used to explicitly launch an async function (or Promise) with awaiting it\n * @param promise The promise to forget\n * @param config Configuration of forget settings\n */\n static forget<T>(promise: Promise<T> | PromiseEx<T> | PromisableFunction<T> | T, config?: ForgetConfig<T>) {\n const externalStackTrace = (new Error('Stack')).stack\n\n // default | global | provided priorities for config (not deep merge)\n const resolvedConfig = {\n ...defaultForgetConfig, ...globalThis.xy?.forget?.config, ...config,\n }\n const resolvedPromise = typeof promise === 'function' ? (promise as PromisableFunction<T>)() : promise\n if (isPromise(resolvedPromise)) {\n try {\n let completed = false\n this.activeForgets++\n\n const promiseWrapper = async () => {\n await resolvedPromise\n .then((result: T) => {\n this.activeForgets--\n completed = true\n resolvedConfig?.onComplete?.([result, undefined])\n })\n .catch((error) => {\n this.activeForgets--\n completed = true\n this.logger.error(`forgotten promise excepted [${config?.name ?? 'unknown'}]: ${error.message}`, error)\n resolvedConfig?.onComplete?.([undefined, error])\n })\n }\n\n const promises = [promiseWrapper()]\n\n // if there is a timeout, add it to the race\n const timeout = resolvedConfig.timeout ?? defaultForgetConfig.timeout\n if (isNumber(timeout)) {\n const timeoutFunc = async () => {\n await delay(timeout)\n if (!completed) {\n resolvedConfig.onCancel?.()\n this.timeoutHandler(timeout, resolvedConfig, externalStackTrace)\n }\n }\n promises.push(timeoutFunc())\n }\n\n const all = Promise.race(promises)\n\n all\n .then(() => {\n return\n })\n .catch(() => {\n return\n })\n } catch (ex) {\n this.exceptedForgets += 1\n resolvedConfig?.onException?.(ex as Error)\n this.exceptionHandler(ex as Error, resolvedConfig, externalStackTrace)\n }\n } else {\n // we do nothing here since if it was a non-promise, it already got invoked.\n return\n }\n }\n\n static timeoutHandler(time: number, { name = 'unknown' }: ForgetConfig, externalStackTrace?: string) {\n this.logger.error(`forget promise timeout out after ${time}ms [Cancelling] [${name}]`)\n if (externalStackTrace !== undefined) {\n this.logger.warn(`External Stack trace [${name}]:`, externalStackTrace)\n }\n }\n}\n","/// <reference types=\"node\" />\n\nimport type { Promisable } from '@xylabs/promise'\n\nimport { defaultForgetNodeConfig, type ForgetNodeConfig } from './ForgetNodeConfig.ts'\nimport { ForgetPromise } from './ForgetPromise.ts'\n\nexport class ForgetPromiseNode extends ForgetPromise {\n static override exceptionHandler(error: Error, config: ForgetNodeConfig, externalStackTrace?: string) {\n // default | global | provided priorities for config (not deep merge)\n super.exceptionHandler(error, config, externalStackTrace)\n if (config?.terminateOnException === true) {\n this.logger.error(`Attempting to terminate process [${config?.name ?? 'unknown'}]...`)\n // eslint-disable-next-line unicorn/no-process-exit\n process.exit(1)\n }\n }\n\n static override forget<T>(promise: Promisable<T>, config?: ForgetNodeConfig<T>) {\n const resolvedConfig = {\n ...defaultForgetNodeConfig, ...globalThis.xy?.forget?.config, ...config,\n }\n super.forget(promise, resolvedConfig)\n }\n\n static override timeoutHandler(time: number, config: ForgetNodeConfig, externalStackTrace?: string) {\n super.timeoutHandler(time, config, externalStackTrace)\n if (config?.terminateOnTimeout === true) {\n this.logger.error(`Attempting to terminate process [${config?.name ?? 'unknown'}]...`)\n // eslint-disable-next-line unicorn/no-process-exit\n process.exit(2)\n }\n }\n}\n","import { type Promisable } from '@xylabs/promise'\n\nimport type { ForgetNodeConfig } from './ForgetNodeConfig.ts'\nimport { ForgetPromiseNode } from './ForgetPromiseNode.ts'\n\n// used to explicitly launch an async function (or Promise) with awaiting it\nexport const forgetNode = <T>(promise: Promisable<T>, config?: ForgetNodeConfig<T>) => {\n ForgetPromiseNode.forget<T>(promise, config)\n}\n"],"mappings":";AASO,IAAM,sBAA6C,EAAE,SAAS,IAAO;;;ACDrE,IAAM,0BAAqD;AAAA,EAChE,GAAG;AAAA,EACH,oBAAoB;AAAA,EACpB,sBAAsB;AACxB;;;ACZA,SAAS,aAAa;AAGtB,SAAS,iBAAiB;AAC1B,SAAS,gBAAgB;AAOlB,IAAM,gBAAN,MAAoB;AAAA,EACzB,OAAO,gBAAgB;AAAA,EACvB,OAAO,kBAAkB;AAAA,EACzB,OAAO,SAAiB;AAAA,EAExB,WAAW,SAAS;AAClB,WAAO,KAAK,gBAAgB;AAAA,EAC9B;AAAA,EAEA,aAAa,cAAc,WAAW,KAAK,SAAkB;AAC3D,QAAI,mBAAmB;AACvB,WAAO,KAAK,QAAQ;AAClB,YAAM,MAAM,QAAQ;AACpB,UAAI,qBAAqB,QAAW;AAClC,4BAAoB;AACpB,YAAI,oBAAoB,GAAG;AACzB,iBAAO,KAAK;AAAA,QACd;AAAA,MACF;AAAA,IACF;AACA,WAAO;AAAA,EACT;AAAA,EAEA,OAAO,iBAAiB,OAAc,EAAE,KAAK,GAAiB,oBAA6B;AACzF,SAAK,OAAO,MAAM,oCAAoC,IAAI,MAAM,MAAM,OAAO,IAAI,KAAK;AACtF,QAAI,uBAAuB,QAAW;AACpC,WAAK,OAAO,KAAK,yBAAyB,IAAI,MAAM,kBAAkB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;AACzC,aAAK,iBAAiB,IAAa,gBAAgB,kBAAkB;AAAA,MACvE;AAAA,IACF,OAAO;AAEL;AAAA,IACF;AAAA,EACF;AAAA,EAEA,OAAO,eAAe,MAAc,EAAE,OAAO,UAAU,GAAiB,oBAA6B;AACnG,SAAK,OAAO,MAAM,oCAAoC,IAAI,oBAAoB,IAAI,GAAG;AACrF,QAAI,uBAAuB,QAAW;AACpC,WAAK,OAAO,KAAK,yBAAyB,IAAI,MAAM,kBAAkB;AAAA,IACxE;AAAA,EACF;AACF;;;AC5GO,IAAM,oBAAN,cAAgC,cAAc;AAAA,EACnD,OAAgB,iBAAiB,OAAc,QAA0B,oBAA6B;AAEpG,UAAM,iBAAiB,OAAO,QAAQ,kBAAkB;AACxD,QAAI,QAAQ,yBAAyB,MAAM;AACzC,WAAK,OAAO,MAAM,oCAAoC,QAAQ,QAAQ,SAAS,MAAM;AAErF,cAAQ,KAAK,CAAC;AAAA,IAChB;AAAA,EACF;AAAA,EAEA,OAAgB,OAAU,SAAwB,QAA8B;AAC9E,UAAM,iBAAiB;AAAA,MACrB,GAAG;AAAA,MAAyB,GAAG,WAAW,IAAI,QAAQ;AAAA,MAAQ,GAAG;AAAA,IACnE;AACA,UAAM,OAAO,SAAS,cAAc;AAAA,EACtC;AAAA,EAEA,OAAgB,eAAe,MAAc,QAA0B,oBAA6B;AAClG,UAAM,eAAe,MAAM,QAAQ,kBAAkB;AACrD,QAAI,QAAQ,uBAAuB,MAAM;AACvC,WAAK,OAAO,MAAM,oCAAoC,QAAQ,QAAQ,SAAS,MAAM;AAErF,cAAQ,KAAK,CAAC;AAAA,IAChB;AAAA,EACF;AACF;;;AC3BO,IAAM,aAAa,CAAI,SAAwB,WAAiC;AACrF,oBAAkB,OAAU,SAAS,MAAM;AAC7C;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/ForgetConfig.ts","../../src/ForgetNodeConfig.ts","../../src/ForgetPromise.ts","../../src/ForgetPromiseNode.ts","../../src/forgetNode.ts"],"sourcesContent":["/**\n * Configuration options for fire-and-forget promises.\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport interface ForgetConfig<T = any> {\n /** Optional name for identifying the forgotten promise in logs. */\n name?: string\n /** Called when the promise is cancelled due to timeout. */\n onCancel?: () => void\n /** Called when the promise completes, with a tuple of [result, error]. */\n onComplete?: (result: [T | undefined, Error | undefined]) => void\n /** Called when an exception occurs outside the promise itself. */\n onException?: (error: Error) => void\n /** Timeout in milliseconds after which the promise is considered timed out. */\n timeout?: number\n}\n\n/** Default forget configuration with a 30-second timeout. */\nexport const defaultForgetConfig: ForgetConfig<unknown> = { timeout: 30_000 }\n","import { defaultForgetConfig, type ForgetConfig } from './ForgetConfig.ts'\n\n/**\n * Node.js-specific forget configuration that extends ForgetConfig with process termination options.\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport interface ForgetNodeConfig<T = any> extends ForgetConfig<T> {\n /** Terminate the process on an exception that happens outside of the promise being forgotten. */\n terminateOnException?: boolean\n /** Terminate the process if the promise times out. */\n terminateOnTimeout?: boolean\n}\n\n/** Default Node.js forget configuration with termination disabled. */\nexport const defaultForgetNodeConfig: ForgetNodeConfig<unknown> = {\n ...defaultForgetConfig,\n terminateOnTimeout: false,\n terminateOnException: false,\n}\n","import { delay } from '@xylabs/delay'\nimport type { Logger } from '@xylabs/logger'\nimport type { Promisable, PromiseEx } from '@xylabs/promise'\nimport { isPromise } from '@xylabs/promise'\nimport { isNumber } from '@xylabs/typeof'\n\nimport { defaultForgetConfig, type ForgetConfig } from './ForgetConfig.ts'\n\n/** A function that returns a promisable value. */\ntype PromisableFunction<T> = () => Promisable<T>\n\n/**\n * Manages fire-and-forget promises with tracking, timeouts, and error handling.\n */\n// eslint-disable-next-line unicorn/no-static-only-class\nexport class ForgetPromise {\n /** Number of currently active (unresolved) forgotten promises. */\n static activeForgets = 0\n /** Number of forgotten promises that threw exceptions. */\n static exceptedForgets = 0\n /** Logger instance used for error and warning output. */\n static logger: Logger = console\n\n /** Whether any forgotten promises are still active. */\n static get active() {\n return this.activeForgets > 0\n }\n\n /**\n * Waits until all forgotten promises have completed.\n * @param interval - Polling interval in milliseconds.\n * @param timeout - Optional maximum wait time in milliseconds.\n * @returns The number of remaining active forgets (0 if all completed).\n */\n static async awaitInactive(interval = 100, timeout?: number) {\n let timeoutRemaining = timeout\n while (this.active) {\n await delay(interval)\n if (timeoutRemaining !== undefined) {\n timeoutRemaining -= interval\n if (timeoutRemaining <= 0) {\n return this.activeForgets\n }\n }\n }\n return 0\n }\n\n /** Handles exceptions from forgotten promises by logging error details. */\n static exceptionHandler(error: Error, { name }: ForgetConfig, externalStackTrace?: string) {\n this.logger.error(`forget promise handler excepted [${name}]: ${error.message}`, error)\n if (externalStackTrace !== undefined) {\n this.logger.warn(`External Stack trace [${name}]:`, externalStackTrace)\n }\n }\n\n /**\n * Used to explicitly launch an async function (or Promise) with awaiting it\n * @param promise The promise to forget\n * @param config Configuration of forget settings\n */\n static forget<T>(promise: Promise<T> | PromiseEx<T> | PromisableFunction<T> | T, config?: ForgetConfig<T>) {\n const externalStackTrace = (new Error('Stack')).stack\n\n // default | global | provided priorities for config (not deep merge)\n const resolvedConfig = {\n ...defaultForgetConfig, ...globalThis.xy?.forget?.config, ...config,\n }\n const resolvedPromise = typeof promise === 'function' ? (promise as PromisableFunction<T>)() : promise\n if (isPromise(resolvedPromise)) {\n try {\n let completed = false\n this.activeForgets++\n\n const promiseWrapper = async () => {\n await resolvedPromise\n .then((result: T) => {\n this.activeForgets--\n completed = true\n resolvedConfig?.onComplete?.([result, undefined])\n })\n .catch((error) => {\n this.activeForgets--\n completed = true\n this.logger.error(`forgotten promise excepted [${config?.name ?? 'unknown'}]: ${error.message}`, error)\n resolvedConfig?.onComplete?.([undefined, error])\n })\n }\n\n const promises = [promiseWrapper()]\n\n // if there is a timeout, add it to the race\n const timeout = resolvedConfig.timeout ?? defaultForgetConfig.timeout\n if (isNumber(timeout)) {\n const timeoutFunc = async () => {\n await delay(timeout)\n if (!completed) {\n resolvedConfig.onCancel?.()\n this.timeoutHandler(timeout, resolvedConfig, externalStackTrace)\n }\n }\n promises.push(timeoutFunc())\n }\n\n const all = Promise.race(promises)\n\n all\n .then(() => {\n return\n })\n .catch(() => {\n return\n })\n } catch (ex) {\n this.exceptedForgets += 1\n resolvedConfig?.onException?.(ex as Error)\n this.exceptionHandler(ex as Error, resolvedConfig, externalStackTrace)\n }\n } else {\n // we do nothing here since if it was a non-promise, it already got invoked.\n return\n }\n }\n\n /** Handles timeout events for forgotten promises by logging timeout details. */\n static timeoutHandler(time: number, { name = 'unknown' }: ForgetConfig, externalStackTrace?: string) {\n this.logger.error(`forget promise timeout out after ${time}ms [Cancelling] [${name}]`)\n if (externalStackTrace !== undefined) {\n this.logger.warn(`External Stack trace [${name}]:`, externalStackTrace)\n }\n }\n}\n","/// <reference types=\"node\" />\n\nimport type { Promisable } from '@xylabs/promise'\n\nimport { defaultForgetNodeConfig, type ForgetNodeConfig } from './ForgetNodeConfig.ts'\nimport { ForgetPromise } from './ForgetPromise.ts'\n\n/**\n * Node.js extension of ForgetPromise that can terminate the process on exceptions or timeouts.\n */\nexport class ForgetPromiseNode extends ForgetPromise {\n /** Handles exceptions, optionally terminating the process based on config. */\n static override exceptionHandler(error: Error, config: ForgetNodeConfig, externalStackTrace?: string) {\n // default | global | provided priorities for config (not deep merge)\n super.exceptionHandler(error, config, externalStackTrace)\n if (config?.terminateOnException === true) {\n this.logger.error(`Attempting to terminate process [${config?.name ?? 'unknown'}]...`)\n // eslint-disable-next-line unicorn/no-process-exit\n process.exit(1)\n }\n }\n\n /** Forgets a promise using Node.js-specific configuration with process termination support. */\n static override forget<T>(promise: Promisable<T>, config?: ForgetNodeConfig<T>) {\n const resolvedConfig = {\n ...defaultForgetNodeConfig, ...globalThis.xy?.forget?.config, ...config,\n }\n super.forget(promise, resolvedConfig)\n }\n\n /** Handles timeouts, optionally terminating the process based on config. */\n static override timeoutHandler(time: number, config: ForgetNodeConfig, externalStackTrace?: string) {\n super.timeoutHandler(time, config, externalStackTrace)\n if (config?.terminateOnTimeout === true) {\n this.logger.error(`Attempting to terminate process [${config?.name ?? 'unknown'}]...`)\n // eslint-disable-next-line unicorn/no-process-exit\n process.exit(2)\n }\n }\n}\n","import { type Promisable } from '@xylabs/promise'\n\nimport type { ForgetNodeConfig } from './ForgetNodeConfig.ts'\nimport { ForgetPromiseNode } from './ForgetPromiseNode.ts'\n\n/**\n * Node.js variant of forget that can optionally terminate the process on exceptions or timeouts.\n * @param promise - The promise or promisable value to forget.\n * @param config - Optional Node.js-specific configuration including process termination options.\n */\nexport const forgetNode = <T>(promise: Promisable<T>, config?: ForgetNodeConfig<T>) => {\n ForgetPromiseNode.forget<T>(promise, config)\n}\n"],"mappings":";AAkBO,IAAM,sBAA6C,EAAE,SAAS,IAAO;;;ACJrE,IAAM,0BAAqD;AAAA,EAChE,GAAG;AAAA,EACH,oBAAoB;AAAA,EACpB,sBAAsB;AACxB;;;AClBA,SAAS,aAAa;AAGtB,SAAS,iBAAiB;AAC1B,SAAS,gBAAgB;AAWlB,IAAM,gBAAN,MAAoB;AAAA;AAAA,EAEzB,OAAO,gBAAgB;AAAA;AAAA,EAEvB,OAAO,kBAAkB;AAAA;AAAA,EAEzB,OAAO,SAAiB;AAAA;AAAA,EAGxB,WAAW,SAAS;AAClB,WAAO,KAAK,gBAAgB;AAAA,EAC9B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,aAAa,cAAc,WAAW,KAAK,SAAkB;AAC3D,QAAI,mBAAmB;AACvB,WAAO,KAAK,QAAQ;AAClB,YAAM,MAAM,QAAQ;AACpB,UAAI,qBAAqB,QAAW;AAClC,4BAAoB;AACpB,YAAI,oBAAoB,GAAG;AACzB,iBAAO,KAAK;AAAA,QACd;AAAA,MACF;AAAA,IACF;AACA,WAAO;AAAA,EACT;AAAA;AAAA,EAGA,OAAO,iBAAiB,OAAc,EAAE,KAAK,GAAiB,oBAA6B;AACz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;AACrF,oBAAkB,OAAU,SAAS,MAAM;AAC7C;","names":[]}
|
|
@@ -1,9 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration options for fire-and-forget promises.
|
|
3
|
+
*/
|
|
1
4
|
export interface ForgetConfig<T = any> {
|
|
5
|
+
/** Optional name for identifying the forgotten promise in logs. */
|
|
2
6
|
name?: string;
|
|
7
|
+
/** Called when the promise is cancelled due to timeout. */
|
|
3
8
|
onCancel?: () => void;
|
|
9
|
+
/** Called when the promise completes, with a tuple of [result, error]. */
|
|
4
10
|
onComplete?: (result: [T | undefined, Error | undefined]) => void;
|
|
11
|
+
/** Called when an exception occurs outside the promise itself. */
|
|
5
12
|
onException?: (error: Error) => void;
|
|
13
|
+
/** Timeout in milliseconds after which the promise is considered timed out. */
|
|
6
14
|
timeout?: number;
|
|
7
15
|
}
|
|
16
|
+
/** Default forget configuration with a 30-second timeout. */
|
|
8
17
|
export declare const defaultForgetConfig: ForgetConfig<unknown>;
|
|
9
18
|
//# sourceMappingURL=ForgetConfig.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ForgetConfig.d.ts","sourceRoot":"","sources":["../../src/ForgetConfig.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"ForgetConfig.d.ts","sourceRoot":"","sources":["../../src/ForgetConfig.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,WAAW,YAAY,CAAC,CAAC,GAAG,GAAG;IACnC,mEAAmE;IACnE,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,2DAA2D;IAC3D,QAAQ,CAAC,EAAE,MAAM,IAAI,CAAA;IACrB,0EAA0E;IAC1E,UAAU,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,CAAC,GAAG,SAAS,EAAE,KAAK,GAAG,SAAS,CAAC,KAAK,IAAI,CAAA;IACjE,kEAAkE;IAClE,WAAW,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAA;IACpC,+EAA+E;IAC/E,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB;AAED,6DAA6D;AAC7D,eAAO,MAAM,mBAAmB,EAAE,YAAY,CAAC,OAAO,CAAuB,CAAA"}
|
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import { type ForgetConfig } from './ForgetConfig.ts';
|
|
2
|
+
/**
|
|
3
|
+
* Node.js-specific forget configuration that extends ForgetConfig with process termination options.
|
|
4
|
+
*/
|
|
2
5
|
export interface ForgetNodeConfig<T = any> extends ForgetConfig<T> {
|
|
6
|
+
/** Terminate the process on an exception that happens outside of the promise being forgotten. */
|
|
3
7
|
terminateOnException?: boolean;
|
|
8
|
+
/** Terminate the process if the promise times out. */
|
|
4
9
|
terminateOnTimeout?: boolean;
|
|
5
10
|
}
|
|
11
|
+
/** Default Node.js forget configuration with termination disabled. */
|
|
6
12
|
export declare const defaultForgetNodeConfig: ForgetNodeConfig<unknown>;
|
|
7
13
|
//# sourceMappingURL=ForgetNodeConfig.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ForgetNodeConfig.d.ts","sourceRoot":"","sources":["../../src/ForgetNodeConfig.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,KAAK,YAAY,EAAE,MAAM,mBAAmB,CAAA;
|
|
1
|
+
{"version":3,"file":"ForgetNodeConfig.d.ts","sourceRoot":"","sources":["../../src/ForgetNodeConfig.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,KAAK,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAE1E;;GAEG;AAEH,MAAM,WAAW,gBAAgB,CAAC,CAAC,GAAG,GAAG,CAAE,SAAQ,YAAY,CAAC,CAAC,CAAC;IAChE,iGAAiG;IACjG,oBAAoB,CAAC,EAAE,OAAO,CAAA;IAC9B,sDAAsD;IACtD,kBAAkB,CAAC,EAAE,OAAO,CAAA;CAC7B;AAED,sEAAsE;AACtE,eAAO,MAAM,uBAAuB,EAAE,gBAAgB,CAAC,OAAO,CAI7D,CAAA"}
|
|
@@ -1,13 +1,28 @@
|
|
|
1
1
|
import type { Logger } from '@xylabs/logger';
|
|
2
2
|
import type { Promisable, PromiseEx } from '@xylabs/promise';
|
|
3
3
|
import { type ForgetConfig } from './ForgetConfig.ts';
|
|
4
|
+
/** A function that returns a promisable value. */
|
|
4
5
|
type PromisableFunction<T> = () => Promisable<T>;
|
|
6
|
+
/**
|
|
7
|
+
* Manages fire-and-forget promises with tracking, timeouts, and error handling.
|
|
8
|
+
*/
|
|
5
9
|
export declare class ForgetPromise {
|
|
10
|
+
/** Number of currently active (unresolved) forgotten promises. */
|
|
6
11
|
static activeForgets: number;
|
|
12
|
+
/** Number of forgotten promises that threw exceptions. */
|
|
7
13
|
static exceptedForgets: number;
|
|
14
|
+
/** Logger instance used for error and warning output. */
|
|
8
15
|
static logger: Logger;
|
|
16
|
+
/** Whether any forgotten promises are still active. */
|
|
9
17
|
static get active(): boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Waits until all forgotten promises have completed.
|
|
20
|
+
* @param interval - Polling interval in milliseconds.
|
|
21
|
+
* @param timeout - Optional maximum wait time in milliseconds.
|
|
22
|
+
* @returns The number of remaining active forgets (0 if all completed).
|
|
23
|
+
*/
|
|
10
24
|
static awaitInactive(interval?: number, timeout?: number): Promise<number>;
|
|
25
|
+
/** Handles exceptions from forgotten promises by logging error details. */
|
|
11
26
|
static exceptionHandler(error: Error, { name }: ForgetConfig, externalStackTrace?: string): void;
|
|
12
27
|
/**
|
|
13
28
|
* Used to explicitly launch an async function (or Promise) with awaiting it
|
|
@@ -15,6 +30,7 @@ export declare class ForgetPromise {
|
|
|
15
30
|
* @param config Configuration of forget settings
|
|
16
31
|
*/
|
|
17
32
|
static forget<T>(promise: Promise<T> | PromiseEx<T> | PromisableFunction<T> | T, config?: ForgetConfig<T>): void;
|
|
33
|
+
/** Handles timeout events for forgotten promises by logging timeout details. */
|
|
18
34
|
static timeoutHandler(time: number, { name }: ForgetConfig, externalStackTrace?: string): void;
|
|
19
35
|
}
|
|
20
36
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ForgetPromise.d.ts","sourceRoot":"","sources":["../../src/ForgetPromise.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAI5D,OAAO,EAAuB,KAAK,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAE1E,KAAK,kBAAkB,CAAC,CAAC,IAAI,MAAM,UAAU,CAAC,CAAC,CAAC,CAAA;
|
|
1
|
+
{"version":3,"file":"ForgetPromise.d.ts","sourceRoot":"","sources":["../../src/ForgetPromise.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAI5D,OAAO,EAAuB,KAAK,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAE1E,kDAAkD;AAClD,KAAK,kBAAkB,CAAC,CAAC,IAAI,MAAM,UAAU,CAAC,CAAC,CAAC,CAAA;AAEhD;;GAEG;AAEH,qBAAa,aAAa;IACxB,kEAAkE;IAClE,MAAM,CAAC,aAAa,SAAI;IACxB,0DAA0D;IAC1D,MAAM,CAAC,eAAe,SAAI;IAC1B,yDAAyD;IACzD,MAAM,CAAC,MAAM,EAAE,MAAM,CAAU;IAE/B,uDAAuD;IACvD,MAAM,KAAK,MAAM,YAEhB;IAED;;;;;OAKG;WACU,aAAa,CAAC,QAAQ,SAAM,EAAE,OAAO,CAAC,EAAE,MAAM;IAc3D,2EAA2E;IAC3E,MAAM,CAAC,gBAAgB,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,EAAE,YAAY,EAAE,kBAAkB,CAAC,EAAE,MAAM;IAOzF;;;;OAIG;IACH,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,GAAG,kBAAkB,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC,EAAE,YAAY,CAAC,CAAC,CAAC;IA+DzG,gFAAgF;IAChF,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,IAAgB,EAAE,EAAE,YAAY,EAAE,kBAAkB,CAAC,EAAE,MAAM;CAMpG"}
|
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
import type { Promisable } from '@xylabs/promise';
|
|
2
2
|
import { type ForgetNodeConfig } from './ForgetNodeConfig.ts';
|
|
3
3
|
import { ForgetPromise } from './ForgetPromise.ts';
|
|
4
|
+
/**
|
|
5
|
+
* Node.js extension of ForgetPromise that can terminate the process on exceptions or timeouts.
|
|
6
|
+
*/
|
|
4
7
|
export declare class ForgetPromiseNode extends ForgetPromise {
|
|
8
|
+
/** Handles exceptions, optionally terminating the process based on config. */
|
|
5
9
|
static exceptionHandler(error: Error, config: ForgetNodeConfig, externalStackTrace?: string): void;
|
|
10
|
+
/** Forgets a promise using Node.js-specific configuration with process termination support. */
|
|
6
11
|
static forget<T>(promise: Promisable<T>, config?: ForgetNodeConfig<T>): void;
|
|
12
|
+
/** Handles timeouts, optionally terminating the process based on config. */
|
|
7
13
|
static timeoutHandler(time: number, config: ForgetNodeConfig, externalStackTrace?: string): void;
|
|
8
14
|
}
|
|
9
15
|
//# sourceMappingURL=ForgetPromiseNode.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ForgetPromiseNode.d.ts","sourceRoot":"","sources":["../../src/ForgetPromiseNode.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAEjD,OAAO,EAA2B,KAAK,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AACtF,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAElD,qBAAa,iBAAkB,SAAQ,aAAa;
|
|
1
|
+
{"version":3,"file":"ForgetPromiseNode.d.ts","sourceRoot":"","sources":["../../src/ForgetPromiseNode.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAEjD,OAAO,EAA2B,KAAK,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AACtF,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAElD;;GAEG;AACH,qBAAa,iBAAkB,SAAQ,aAAa;IAClD,8EAA8E;WAC9D,gBAAgB,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,gBAAgB,EAAE,kBAAkB,CAAC,EAAE,MAAM;IAUpG,+FAA+F;WAC/E,MAAM,CAAC,CAAC,EAAE,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC;IAO9E,4EAA4E;WAC5D,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,gBAAgB,EAAE,kBAAkB,CAAC,EAAE,MAAM;CAQnG"}
|
package/dist/node/forget.d.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
import { type Promisable } from '@xylabs/promise';
|
|
2
2
|
import type { ForgetConfig } from './ForgetConfig.ts';
|
|
3
|
+
/**
|
|
4
|
+
* Explicitly launches an async function or promise without awaiting it (fire-and-forget).
|
|
5
|
+
* @param promise - The promise or promisable value to forget.
|
|
6
|
+
* @param config - Optional configuration for timeout, error handling, and completion callbacks.
|
|
7
|
+
*/
|
|
3
8
|
export declare const forget: <T>(promise: Promisable<T>, config?: ForgetConfig<T>) => void;
|
|
4
9
|
//# sourceMappingURL=forget.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"forget.d.ts","sourceRoot":"","sources":["../../src/forget.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAEjD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;
|
|
1
|
+
{"version":3,"file":"forget.d.ts","sourceRoot":"","sources":["../../src/forget.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAEjD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAGrD;;;;GAIG;AACH,eAAO,MAAM,MAAM,GAAI,CAAC,EAAE,SAAS,UAAU,CAAC,CAAC,CAAC,EAAE,SAAS,YAAY,CAAC,CAAC,CAAC,SAEzE,CAAA"}
|
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
import { type Promisable } from '@xylabs/promise';
|
|
2
2
|
import type { ForgetNodeConfig } from './ForgetNodeConfig.ts';
|
|
3
|
+
/**
|
|
4
|
+
* Node.js variant of forget that can optionally terminate the process on exceptions or timeouts.
|
|
5
|
+
* @param promise - The promise or promisable value to forget.
|
|
6
|
+
* @param config - Optional Node.js-specific configuration including process termination options.
|
|
7
|
+
*/
|
|
3
8
|
export declare const forgetNode: <T>(promise: Promisable<T>, config?: ForgetNodeConfig<T>) => void;
|
|
4
9
|
//# sourceMappingURL=forgetNode.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"forgetNode.d.ts","sourceRoot":"","sources":["../../src/forgetNode.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAEjD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;
|
|
1
|
+
{"version":3,"file":"forgetNode.d.ts","sourceRoot":"","sources":["../../src/forgetNode.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAEjD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AAG7D;;;;GAIG;AACH,eAAO,MAAM,UAAU,GAAI,CAAC,EAAE,SAAS,UAAU,CAAC,CAAC,CAAC,EAAE,SAAS,gBAAgB,CAAC,CAAC,CAAC,SAEjF,CAAA"}
|
package/dist/node/index.mjs
CHANGED
|
@@ -13,12 +13,22 @@ import { delay } from "@xylabs/delay";
|
|
|
13
13
|
import { isPromise } from "@xylabs/promise";
|
|
14
14
|
import { isNumber } from "@xylabs/typeof";
|
|
15
15
|
var ForgetPromise = class {
|
|
16
|
+
/** Number of currently active (unresolved) forgotten promises. */
|
|
16
17
|
static activeForgets = 0;
|
|
18
|
+
/** Number of forgotten promises that threw exceptions. */
|
|
17
19
|
static exceptedForgets = 0;
|
|
20
|
+
/** Logger instance used for error and warning output. */
|
|
18
21
|
static logger = console;
|
|
22
|
+
/** Whether any forgotten promises are still active. */
|
|
19
23
|
static get active() {
|
|
20
24
|
return this.activeForgets > 0;
|
|
21
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Waits until all forgotten promises have completed.
|
|
28
|
+
* @param interval - Polling interval in milliseconds.
|
|
29
|
+
* @param timeout - Optional maximum wait time in milliseconds.
|
|
30
|
+
* @returns The number of remaining active forgets (0 if all completed).
|
|
31
|
+
*/
|
|
22
32
|
static async awaitInactive(interval = 100, timeout) {
|
|
23
33
|
let timeoutRemaining = timeout;
|
|
24
34
|
while (this.active) {
|
|
@@ -32,6 +42,7 @@ var ForgetPromise = class {
|
|
|
32
42
|
}
|
|
33
43
|
return 0;
|
|
34
44
|
}
|
|
45
|
+
/** Handles exceptions from forgotten promises by logging error details. */
|
|
35
46
|
static exceptionHandler(error, { name }, externalStackTrace) {
|
|
36
47
|
this.logger.error(`forget promise handler excepted [${name}]: ${error.message}`, error);
|
|
37
48
|
if (externalStackTrace !== void 0) {
|
|
@@ -94,6 +105,7 @@ var ForgetPromise = class {
|
|
|
94
105
|
return;
|
|
95
106
|
}
|
|
96
107
|
}
|
|
108
|
+
/** Handles timeout events for forgotten promises by logging timeout details. */
|
|
97
109
|
static timeoutHandler(time, { name = "unknown" }, externalStackTrace) {
|
|
98
110
|
this.logger.error(`forget promise timeout out after ${time}ms [Cancelling] [${name}]`);
|
|
99
111
|
if (externalStackTrace !== void 0) {
|
|
@@ -104,6 +116,7 @@ var ForgetPromise = class {
|
|
|
104
116
|
|
|
105
117
|
// src/ForgetPromiseNode.ts
|
|
106
118
|
var ForgetPromiseNode = class extends ForgetPromise {
|
|
119
|
+
/** Handles exceptions, optionally terminating the process based on config. */
|
|
107
120
|
static exceptionHandler(error, config, externalStackTrace) {
|
|
108
121
|
super.exceptionHandler(error, config, externalStackTrace);
|
|
109
122
|
if (config?.terminateOnException === true) {
|
|
@@ -111,6 +124,7 @@ var ForgetPromiseNode = class extends ForgetPromise {
|
|
|
111
124
|
process.exit(1);
|
|
112
125
|
}
|
|
113
126
|
}
|
|
127
|
+
/** Forgets a promise using Node.js-specific configuration with process termination support. */
|
|
114
128
|
static forget(promise, config) {
|
|
115
129
|
const resolvedConfig = {
|
|
116
130
|
...defaultForgetNodeConfig,
|
|
@@ -119,6 +133,7 @@ var ForgetPromiseNode = class extends ForgetPromise {
|
|
|
119
133
|
};
|
|
120
134
|
super.forget(promise, resolvedConfig);
|
|
121
135
|
}
|
|
136
|
+
/** Handles timeouts, optionally terminating the process based on config. */
|
|
122
137
|
static timeoutHandler(time, config, externalStackTrace) {
|
|
123
138
|
super.timeoutHandler(time, config, externalStackTrace);
|
|
124
139
|
if (config?.terminateOnTimeout === true) {
|
package/dist/node/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/ForgetConfig.ts","../../src/ForgetNodeConfig.ts","../../src/ForgetPromise.ts","../../src/ForgetPromiseNode.ts","../../src/forgetNode.ts"],"sourcesContent":["// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport interface ForgetConfig<T = any> {\n name?: string\n onCancel?: () => void\n onComplete?: (result: [T | undefined, Error | undefined]) => void\n onException?: (error: Error) => void\n timeout?: number\n}\n\nexport const defaultForgetConfig: ForgetConfig<unknown> = { timeout: 30_000 }\n","import { defaultForgetConfig, type ForgetConfig } from './ForgetConfig.ts'\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport interface ForgetNodeConfig<T = any> extends ForgetConfig<T> {\n terminateOnException?: boolean // terminate the process on an exception that happens outside of the promise being forgotten\n terminateOnTimeout?: boolean // terminate the process if the promise times out\n}\n\nexport const defaultForgetNodeConfig: ForgetNodeConfig<unknown> = {\n ...defaultForgetConfig,\n terminateOnTimeout: false,\n terminateOnException: false,\n}\n","import { delay } from '@xylabs/delay'\nimport type { Logger } from '@xylabs/logger'\nimport type { Promisable, PromiseEx } from '@xylabs/promise'\nimport { isPromise } from '@xylabs/promise'\nimport { isNumber } from '@xylabs/typeof'\n\nimport { defaultForgetConfig, type ForgetConfig } from './ForgetConfig.ts'\n\ntype PromisableFunction<T> = () => Promisable<T>\n\n// eslint-disable-next-line unicorn/no-static-only-class\nexport class ForgetPromise {\n static activeForgets = 0\n static exceptedForgets = 0\n static logger: Logger = console\n\n static get active() {\n return this.activeForgets > 0\n }\n\n static async awaitInactive(interval = 100, timeout?: number) {\n let timeoutRemaining = timeout\n while (this.active) {\n await delay(interval)\n if (timeoutRemaining !== undefined) {\n timeoutRemaining -= interval\n if (timeoutRemaining <= 0) {\n return this.activeForgets\n }\n }\n }\n return 0\n }\n\n static exceptionHandler(error: Error, { name }: ForgetConfig, externalStackTrace?: string) {\n this.logger.error(`forget promise handler excepted [${name}]: ${error.message}`, error)\n if (externalStackTrace !== undefined) {\n this.logger.warn(`External Stack trace [${name}]:`, externalStackTrace)\n }\n }\n\n /**\n * Used to explicitly launch an async function (or Promise) with awaiting it\n * @param promise The promise to forget\n * @param config Configuration of forget settings\n */\n static forget<T>(promise: Promise<T> | PromiseEx<T> | PromisableFunction<T> | T, config?: ForgetConfig<T>) {\n const externalStackTrace = (new Error('Stack')).stack\n\n // default | global | provided priorities for config (not deep merge)\n const resolvedConfig = {\n ...defaultForgetConfig, ...globalThis.xy?.forget?.config, ...config,\n }\n const resolvedPromise = typeof promise === 'function' ? (promise as PromisableFunction<T>)() : promise\n if (isPromise(resolvedPromise)) {\n try {\n let completed = false\n this.activeForgets++\n\n const promiseWrapper = async () => {\n await resolvedPromise\n .then((result: T) => {\n this.activeForgets--\n completed = true\n resolvedConfig?.onComplete?.([result, undefined])\n })\n .catch((error) => {\n this.activeForgets--\n completed = true\n this.logger.error(`forgotten promise excepted [${config?.name ?? 'unknown'}]: ${error.message}`, error)\n resolvedConfig?.onComplete?.([undefined, error])\n })\n }\n\n const promises = [promiseWrapper()]\n\n // if there is a timeout, add it to the race\n const timeout = resolvedConfig.timeout ?? defaultForgetConfig.timeout\n if (isNumber(timeout)) {\n const timeoutFunc = async () => {\n await delay(timeout)\n if (!completed) {\n resolvedConfig.onCancel?.()\n this.timeoutHandler(timeout, resolvedConfig, externalStackTrace)\n }\n }\n promises.push(timeoutFunc())\n }\n\n const all = Promise.race(promises)\n\n all\n .then(() => {\n return\n })\n .catch(() => {\n return\n })\n } catch (ex) {\n this.exceptedForgets += 1\n resolvedConfig?.onException?.(ex as Error)\n this.exceptionHandler(ex as Error, resolvedConfig, externalStackTrace)\n }\n } else {\n // we do nothing here since if it was a non-promise, it already got invoked.\n return\n }\n }\n\n static timeoutHandler(time: number, { name = 'unknown' }: ForgetConfig, externalStackTrace?: string) {\n this.logger.error(`forget promise timeout out after ${time}ms [Cancelling] [${name}]`)\n if (externalStackTrace !== undefined) {\n this.logger.warn(`External Stack trace [${name}]:`, externalStackTrace)\n }\n }\n}\n","/// <reference types=\"node\" />\n\nimport type { Promisable } from '@xylabs/promise'\n\nimport { defaultForgetNodeConfig, type ForgetNodeConfig } from './ForgetNodeConfig.ts'\nimport { ForgetPromise } from './ForgetPromise.ts'\n\nexport class ForgetPromiseNode extends ForgetPromise {\n static override exceptionHandler(error: Error, config: ForgetNodeConfig, externalStackTrace?: string) {\n // default | global | provided priorities for config (not deep merge)\n super.exceptionHandler(error, config, externalStackTrace)\n if (config?.terminateOnException === true) {\n this.logger.error(`Attempting to terminate process [${config?.name ?? 'unknown'}]...`)\n // eslint-disable-next-line unicorn/no-process-exit\n process.exit(1)\n }\n }\n\n static override forget<T>(promise: Promisable<T>, config?: ForgetNodeConfig<T>) {\n const resolvedConfig = {\n ...defaultForgetNodeConfig, ...globalThis.xy?.forget?.config, ...config,\n }\n super.forget(promise, resolvedConfig)\n }\n\n static override timeoutHandler(time: number, config: ForgetNodeConfig, externalStackTrace?: string) {\n super.timeoutHandler(time, config, externalStackTrace)\n if (config?.terminateOnTimeout === true) {\n this.logger.error(`Attempting to terminate process [${config?.name ?? 'unknown'}]...`)\n // eslint-disable-next-line unicorn/no-process-exit\n process.exit(2)\n }\n }\n}\n","import { type Promisable } from '@xylabs/promise'\n\nimport type { ForgetNodeConfig } from './ForgetNodeConfig.ts'\nimport { ForgetPromiseNode } from './ForgetPromiseNode.ts'\n\n// used to explicitly launch an async function (or Promise) with awaiting it\nexport const forgetNode = <T>(promise: Promisable<T>, config?: ForgetNodeConfig<T>) => {\n ForgetPromiseNode.forget<T>(promise, config)\n}\n"],"mappings":";AASO,IAAM,sBAA6C,EAAE,SAAS,IAAO;;;ACDrE,IAAM,0BAAqD;AAAA,EAChE,GAAG;AAAA,EACH,oBAAoB;AAAA,EACpB,sBAAsB;AACxB;;;ACZA,SAAS,aAAa;AAGtB,SAAS,iBAAiB;AAC1B,SAAS,gBAAgB;AAOlB,IAAM,gBAAN,MAAoB;AAAA,EACzB,OAAO,gBAAgB;AAAA,EACvB,OAAO,kBAAkB;AAAA,EACzB,OAAO,SAAiB;AAAA,EAExB,WAAW,SAAS;AAClB,WAAO,KAAK,gBAAgB;AAAA,EAC9B;AAAA,EAEA,aAAa,cAAc,WAAW,KAAK,SAAkB;AAC3D,QAAI,mBAAmB;AACvB,WAAO,KAAK,QAAQ;AAClB,YAAM,MAAM,QAAQ;A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AI,QAAQ,uBAAuB,MAAM;AACvC,WAAK,OAAO,MAAM,oCAAoC,QAAQ,QAAQ,SAAS,MAAM;AAErF,cAAQ,KAAK,CAAC;AAAA,IAChB;AAAA,EACF;AACF;;;AC3BO,IAAM,aAAa,CAAI,SAAwB,WAAiC;AACrF,oBAAkB,OAAU,SAAS,MAAM;AAC7C;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/ForgetConfig.ts","../../src/ForgetNodeConfig.ts","../../src/ForgetPromise.ts","../../src/ForgetPromiseNode.ts","../../src/forgetNode.ts"],"sourcesContent":["/**\n * Configuration options for fire-and-forget promises.\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport interface ForgetConfig<T = any> {\n /** Optional name for identifying the forgotten promise in logs. */\n name?: string\n /** Called when the promise is cancelled due to timeout. */\n onCancel?: () => void\n /** Called when the promise completes, with a tuple of [result, error]. */\n onComplete?: (result: [T | undefined, Error | undefined]) => void\n /** Called when an exception occurs outside the promise itself. */\n onException?: (error: Error) => void\n /** Timeout in milliseconds after which the promise is considered timed out. */\n timeout?: number\n}\n\n/** Default forget configuration with a 30-second timeout. */\nexport const defaultForgetConfig: ForgetConfig<unknown> = { timeout: 30_000 }\n","import { defaultForgetConfig, type ForgetConfig } from './ForgetConfig.ts'\n\n/**\n * Node.js-specific forget configuration that extends ForgetConfig with process termination options.\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport interface ForgetNodeConfig<T = any> extends ForgetConfig<T> {\n /** Terminate the process on an exception that happens outside of the promise being forgotten. */\n terminateOnException?: boolean\n /** Terminate the process if the promise times out. */\n terminateOnTimeout?: boolean\n}\n\n/** Default Node.js forget configuration with termination disabled. */\nexport const defaultForgetNodeConfig: ForgetNodeConfig<unknown> = {\n ...defaultForgetConfig,\n terminateOnTimeout: false,\n terminateOnException: false,\n}\n","import { delay } from '@xylabs/delay'\nimport type { Logger } from '@xylabs/logger'\nimport type { Promisable, PromiseEx } from '@xylabs/promise'\nimport { isPromise } from '@xylabs/promise'\nimport { isNumber } from '@xylabs/typeof'\n\nimport { defaultForgetConfig, type ForgetConfig } from './ForgetConfig.ts'\n\n/** A function that returns a promisable value. */\ntype PromisableFunction<T> = () => Promisable<T>\n\n/**\n * Manages fire-and-forget promises with tracking, timeouts, and error handling.\n */\n// eslint-disable-next-line unicorn/no-static-only-class\nexport class ForgetPromise {\n /** Number of currently active (unresolved) forgotten promises. */\n static activeForgets = 0\n /** Number of forgotten promises that threw exceptions. */\n static exceptedForgets = 0\n /** Logger instance used for error and warning output. */\n static logger: Logger = console\n\n /** Whether any forgotten promises are still active. */\n static get active() {\n return this.activeForgets > 0\n }\n\n /**\n * Waits until all forgotten promises have completed.\n * @param interval - Polling interval in milliseconds.\n * @param timeout - Optional maximum wait time in milliseconds.\n * @returns The number of remaining active forgets (0 if all completed).\n */\n static async awaitInactive(interval = 100, timeout?: number) {\n let timeoutRemaining = timeout\n while (this.active) {\n await delay(interval)\n if (timeoutRemaining !== undefined) {\n timeoutRemaining -= interval\n if (timeoutRemaining <= 0) {\n return this.activeForgets\n }\n }\n }\n return 0\n }\n\n /** Handles exceptions from forgotten promises by logging error details. */\n static exceptionHandler(error: Error, { name }: ForgetConfig, externalStackTrace?: string) {\n this.logger.error(`forget promise handler excepted [${name}]: ${error.message}`, error)\n if (externalStackTrace !== undefined) {\n this.logger.warn(`External Stack trace [${name}]:`, externalStackTrace)\n }\n }\n\n /**\n * Used to explicitly launch an async function (or Promise) with awaiting it\n * @param promise The promise to forget\n * @param config Configuration of forget settings\n */\n static forget<T>(promise: Promise<T> | PromiseEx<T> | PromisableFunction<T> | T, config?: ForgetConfig<T>) {\n const externalStackTrace = (new Error('Stack')).stack\n\n // default | global | provided priorities for config (not deep merge)\n const resolvedConfig = {\n ...defaultForgetConfig, ...globalThis.xy?.forget?.config, ...config,\n }\n const resolvedPromise = typeof promise === 'function' ? (promise as PromisableFunction<T>)() : promise\n if (isPromise(resolvedPromise)) {\n try {\n let completed = false\n this.activeForgets++\n\n const promiseWrapper = async () => {\n await resolvedPromise\n .then((result: T) => {\n this.activeForgets--\n completed = true\n resolvedConfig?.onComplete?.([result, undefined])\n })\n .catch((error) => {\n this.activeForgets--\n completed = true\n this.logger.error(`forgotten promise excepted [${config?.name ?? 'unknown'}]: ${error.message}`, error)\n resolvedConfig?.onComplete?.([undefined, error])\n })\n }\n\n const promises = [promiseWrapper()]\n\n // if there is a timeout, add it to the race\n const timeout = resolvedConfig.timeout ?? defaultForgetConfig.timeout\n if (isNumber(timeout)) {\n const timeoutFunc = async () => {\n await delay(timeout)\n if (!completed) {\n resolvedConfig.onCancel?.()\n this.timeoutHandler(timeout, resolvedConfig, externalStackTrace)\n }\n }\n promises.push(timeoutFunc())\n }\n\n const all = Promise.race(promises)\n\n all\n .then(() => {\n return\n })\n .catch(() => {\n return\n })\n } catch (ex) {\n this.exceptedForgets += 1\n resolvedConfig?.onException?.(ex as Error)\n this.exceptionHandler(ex as Error, resolvedConfig, externalStackTrace)\n }\n } else {\n // we do nothing here since if it was a non-promise, it already got invoked.\n return\n }\n }\n\n /** Handles timeout events for forgotten promises by logging timeout details. */\n static timeoutHandler(time: number, { name = 'unknown' }: ForgetConfig, externalStackTrace?: string) {\n this.logger.error(`forget promise timeout out after ${time}ms [Cancelling] [${name}]`)\n if (externalStackTrace !== undefined) {\n this.logger.warn(`External Stack trace [${name}]:`, externalStackTrace)\n }\n }\n}\n","/// <reference types=\"node\" />\n\nimport type { Promisable } from '@xylabs/promise'\n\nimport { defaultForgetNodeConfig, type ForgetNodeConfig } from './ForgetNodeConfig.ts'\nimport { ForgetPromise } from './ForgetPromise.ts'\n\n/**\n * Node.js extension of ForgetPromise that can terminate the process on exceptions or timeouts.\n */\nexport class ForgetPromiseNode extends ForgetPromise {\n /** Handles exceptions, optionally terminating the process based on config. */\n static override exceptionHandler(error: Error, config: ForgetNodeConfig, externalStackTrace?: string) {\n // default | global | provided priorities for config (not deep merge)\n super.exceptionHandler(error, config, externalStackTrace)\n if (config?.terminateOnException === true) {\n this.logger.error(`Attempting to terminate process [${config?.name ?? 'unknown'}]...`)\n // eslint-disable-next-line unicorn/no-process-exit\n process.exit(1)\n }\n }\n\n /** Forgets a promise using Node.js-specific configuration with process termination support. */\n static override forget<T>(promise: Promisable<T>, config?: ForgetNodeConfig<T>) {\n const resolvedConfig = {\n ...defaultForgetNodeConfig, ...globalThis.xy?.forget?.config, ...config,\n }\n super.forget(promise, resolvedConfig)\n }\n\n /** Handles timeouts, optionally terminating the process based on config. */\n static override timeoutHandler(time: number, config: ForgetNodeConfig, externalStackTrace?: string) {\n super.timeoutHandler(time, config, externalStackTrace)\n if (config?.terminateOnTimeout === true) {\n this.logger.error(`Attempting to terminate process [${config?.name ?? 'unknown'}]...`)\n // eslint-disable-next-line unicorn/no-process-exit\n process.exit(2)\n }\n }\n}\n","import { type Promisable } from '@xylabs/promise'\n\nimport type { ForgetNodeConfig } from './ForgetNodeConfig.ts'\nimport { ForgetPromiseNode } from './ForgetPromiseNode.ts'\n\n/**\n * Node.js variant of forget that can optionally terminate the process on exceptions or timeouts.\n * @param promise - The promise or promisable value to forget.\n * @param config - Optional Node.js-specific configuration including process termination options.\n */\nexport const forgetNode = <T>(promise: Promisable<T>, config?: ForgetNodeConfig<T>) => {\n ForgetPromiseNode.forget<T>(promise, config)\n}\n"],"mappings":";AAkBO,IAAM,sBAA6C,EAAE,SAAS,IAAO;;;ACJrE,IAAM,0BAAqD;AAAA,EAChE,GAAG;AAAA,EACH,oBAAoB;AAAA,EACpB,sBAAsB;AACxB;;;AClBA,SAAS,aAAa;AAGtB,SAAS,iBAAiB;AAC1B,SAAS,gBAAgB;AAWlB,IAAM,gBAAN,MAAoB;AAAA;AAAA,EAEzB,OAAO,gBAAgB;AAAA;AAAA,EAEvB,OAAO,kBAAkB;AAAA;AAAA,EAEzB,OAAO,SAAiB;AAAA;AAAA,EAGxB,WAAW,SAAS;AAClB,WAAO,KAAK,gBAAgB;AAAA,EAC9B;AAAA;AAAA;AAA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,MAAQ,GAAG;AAAA,IACnE;AACA,UAAM,OAAO,SAAS,cAAc;AAAA,EACtC;AAAA;AAAA,EAGA,OAAgB,eAAe,MAAc,QAA0B,oBAA6B;AAClG,UAAM,eAAe,MAAM,QAAQ,kBAAkB;AACrD,QAAI,QAAQ,uBAAuB,MAAM;AACvC,WAAK,OAAO,MAAM,oCAAoC,QAAQ,QAAQ,SAAS,MAAM;AAErF,cAAQ,KAAK,CAAC;AAAA,IAChB;AAAA,EACF;AACF;;;AC7BO,IAAM,aAAa,CAAI,SAAwB,WAAiC;AACrF,oBAAkB,OAAU,SAAS,MAAM;AAC7C;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xylabs/forget",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.84",
|
|
4
4
|
"description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"forget",
|
|
@@ -46,15 +46,15 @@
|
|
|
46
46
|
"!**/*.test.*"
|
|
47
47
|
],
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@xylabs/delay": "~5.0.
|
|
50
|
-
"@xylabs/logger": "~5.0.
|
|
51
|
-
"@xylabs/promise": "~5.0.
|
|
52
|
-
"@xylabs/typeof": "~5.0.
|
|
49
|
+
"@xylabs/delay": "~5.0.84",
|
|
50
|
+
"@xylabs/logger": "~5.0.84",
|
|
51
|
+
"@xylabs/promise": "~5.0.84",
|
|
52
|
+
"@xylabs/typeof": "~5.0.84"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
|
-
"@xylabs/ts-scripts-yarn3": "~7.4.
|
|
56
|
-
"@xylabs/tsconfig": "~7.4.
|
|
57
|
-
"@xylabs/tsconfig-dom": "~7.4.
|
|
55
|
+
"@xylabs/ts-scripts-yarn3": "~7.4.13",
|
|
56
|
+
"@xylabs/tsconfig": "~7.4.13",
|
|
57
|
+
"@xylabs/tsconfig-dom": "~7.4.13",
|
|
58
58
|
"typescript": "~5.9.3",
|
|
59
59
|
"vitest": "~4.0.18"
|
|
60
60
|
},
|