@xylabs/forget 5.0.83 → 5.0.86
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 +87 -216
- 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
|
@@ -15,6 +15,8 @@
|
|
|
15
15
|
|
|
16
16
|
Base functionality used throughout XY Labs TypeScript/JavaScript libraries
|
|
17
17
|
|
|
18
|
+
|
|
19
|
+
|
|
18
20
|
## Reference
|
|
19
21
|
|
|
20
22
|
**@xylabs/forget**
|
|
@@ -23,19 +25,27 @@ Base functionality used throughout XY Labs TypeScript/JavaScript libraries
|
|
|
23
25
|
|
|
24
26
|
## Classes
|
|
25
27
|
|
|
26
|
-
|
|
28
|
+
| Class | Description |
|
|
29
|
+
| ------ | ------ |
|
|
30
|
+
| [ForgetPromise](#classes/ForgetPromise) | Node.js extension of ForgetPromise that can terminate the process on exceptions or timeouts. |
|
|
27
31
|
|
|
28
32
|
## Interfaces
|
|
29
33
|
|
|
30
|
-
|
|
34
|
+
| Interface | Description |
|
|
35
|
+
| ------ | ------ |
|
|
36
|
+
| [ForgetNodeConfig](#interfaces/ForgetNodeConfig) | Node.js-specific forget configuration that extends ForgetConfig with process termination options. |
|
|
31
37
|
|
|
32
38
|
## Variables
|
|
33
39
|
|
|
34
|
-
|
|
40
|
+
| Variable | Description |
|
|
41
|
+
| ------ | ------ |
|
|
42
|
+
| [defaultForgetNodeConfig](#variables/defaultForgetNodeConfig) | Default Node.js forget configuration with termination disabled. |
|
|
35
43
|
|
|
36
44
|
## Functions
|
|
37
45
|
|
|
38
|
-
|
|
46
|
+
| Function | Description |
|
|
47
|
+
| ------ | ------ |
|
|
48
|
+
| [forget](#functions/forget) | Node.js variant of forget that can optionally terminate the process on exceptions or timeouts. |
|
|
39
49
|
|
|
40
50
|
### classes
|
|
41
51
|
|
|
@@ -45,6 +55,8 @@ Base functionality used throughout XY Labs TypeScript/JavaScript libraries
|
|
|
45
55
|
|
|
46
56
|
***
|
|
47
57
|
|
|
58
|
+
Node.js extension of ForgetPromise that can terminate the process on exceptions or timeouts.
|
|
59
|
+
|
|
48
60
|
## Extends
|
|
49
61
|
|
|
50
62
|
- `ForgetPromise`
|
|
@@ -69,45 +81,11 @@ ForgetPromise.constructor
|
|
|
69
81
|
|
|
70
82
|
## Properties
|
|
71
83
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
static
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
### Inherited from
|
|
79
|
-
|
|
80
|
-
```ts
|
|
81
|
-
ForgetPromise.activeForgets
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
***
|
|
85
|
-
|
|
86
|
-
### exceptedForgets
|
|
87
|
-
|
|
88
|
-
```ts
|
|
89
|
-
static exceptedForgets: number = 0;
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
### Inherited from
|
|
93
|
-
|
|
94
|
-
```ts
|
|
95
|
-
ForgetPromise.exceptedForgets
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
***
|
|
99
|
-
|
|
100
|
-
### logger
|
|
101
|
-
|
|
102
|
-
```ts
|
|
103
|
-
static logger: Logger = console;
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
### Inherited from
|
|
107
|
-
|
|
108
|
-
```ts
|
|
109
|
-
ForgetPromise.logger
|
|
110
|
-
```
|
|
84
|
+
| Property | Modifier | Type | Default value | Description | Inherited from |
|
|
85
|
+
| ------ | ------ | ------ | ------ | ------ | ------ |
|
|
86
|
+
| <a id="activeforgets"></a> `activeForgets` | `static` | `number` | `0` | Number of currently active (unresolved) forgotten promises. | `ForgetPromise.activeForgets` |
|
|
87
|
+
| <a id="exceptedforgets"></a> `exceptedForgets` | `static` | `number` | `0` | Number of forgotten promises that threw exceptions. | `ForgetPromise.exceptedForgets` |
|
|
88
|
+
| <a id="logger"></a> `logger` | `static` | `Logger` | `console` | Logger instance used for error and warning output. | `ForgetPromise.logger` |
|
|
111
89
|
|
|
112
90
|
## Accessors
|
|
113
91
|
|
|
@@ -119,6 +97,8 @@ ForgetPromise.logger
|
|
|
119
97
|
get static active(): boolean;
|
|
120
98
|
```
|
|
121
99
|
|
|
100
|
+
Whether any forgotten promises are still active.
|
|
101
|
+
|
|
122
102
|
#### Returns
|
|
123
103
|
|
|
124
104
|
`boolean`
|
|
@@ -134,23 +114,24 @@ ForgetPromise.active
|
|
|
134
114
|
### awaitInactive()
|
|
135
115
|
|
|
136
116
|
```ts
|
|
137
|
-
static awaitInactive(interval
|
|
117
|
+
static awaitInactive(interval?: number, timeout?: number): Promise<number>;
|
|
138
118
|
```
|
|
139
119
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
#### interval?
|
|
120
|
+
Waits until all forgotten promises have completed.
|
|
143
121
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
#### timeout?
|
|
122
|
+
### Parameters
|
|
147
123
|
|
|
148
|
-
|
|
124
|
+
| Parameter | Type | Default value | Description |
|
|
125
|
+
| ------ | ------ | ------ | ------ |
|
|
126
|
+
| `interval` | `number` | `100` | Polling interval in milliseconds. |
|
|
127
|
+
| `timeout?` | `number` | `undefined` | Optional maximum wait time in milliseconds. |
|
|
149
128
|
|
|
150
129
|
### Returns
|
|
151
130
|
|
|
152
131
|
`Promise`\<`number`\>
|
|
153
132
|
|
|
133
|
+
The number of remaining active forgets (0 if all completed).
|
|
134
|
+
|
|
154
135
|
### Inherited from
|
|
155
136
|
|
|
156
137
|
```ts
|
|
@@ -163,24 +144,20 @@ ForgetPromise.awaitInactive
|
|
|
163
144
|
|
|
164
145
|
```ts
|
|
165
146
|
static exceptionHandler(
|
|
166
|
-
error,
|
|
167
|
-
config,
|
|
168
|
-
externalStackTrace
|
|
147
|
+
error: Error,
|
|
148
|
+
config: ForgetNodeConfig,
|
|
149
|
+
externalStackTrace?: string): void;
|
|
169
150
|
```
|
|
170
151
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
#### error
|
|
174
|
-
|
|
175
|
-
`Error`
|
|
176
|
-
|
|
177
|
-
#### config
|
|
152
|
+
Handles exceptions, optionally terminating the process based on config.
|
|
178
153
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
#### externalStackTrace?
|
|
154
|
+
### Parameters
|
|
182
155
|
|
|
183
|
-
|
|
156
|
+
| Parameter | Type |
|
|
157
|
+
| ------ | ------ |
|
|
158
|
+
| `error` | `Error` |
|
|
159
|
+
| `config` | [`ForgetNodeConfig`](#../interfaces/ForgetNodeConfig) |
|
|
160
|
+
| `externalStackTrace?` | `string` |
|
|
184
161
|
|
|
185
162
|
### Returns
|
|
186
163
|
|
|
@@ -197,30 +174,23 @@ ForgetPromise.exceptionHandler
|
|
|
197
174
|
### forget()
|
|
198
175
|
|
|
199
176
|
```ts
|
|
200
|
-
static forget<T>(promise
|
|
177
|
+
static forget<T>(promise: Promisable<T>, config?: ForgetNodeConfig<T>): void;
|
|
201
178
|
```
|
|
202
179
|
|
|
203
|
-
|
|
180
|
+
Forgets a promise using Node.js-specific configuration with process termination support.
|
|
204
181
|
|
|
205
182
|
### Type Parameters
|
|
206
183
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
`T`
|
|
184
|
+
| Type Parameter |
|
|
185
|
+
| ------ |
|
|
186
|
+
| `T` |
|
|
210
187
|
|
|
211
188
|
### Parameters
|
|
212
189
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
`Promisable`\<`T`\>
|
|
216
|
-
|
|
217
|
-
The promise to forget
|
|
218
|
-
|
|
219
|
-
#### config?
|
|
220
|
-
|
|
221
|
-
[`ForgetNodeConfig`](#../interfaces/ForgetNodeConfig)\<`T`\>
|
|
222
|
-
|
|
223
|
-
Configuration of forget settings
|
|
190
|
+
| Parameter | Type |
|
|
191
|
+
| ------ | ------ |
|
|
192
|
+
| `promise` | `Promisable`\<`T`\> |
|
|
193
|
+
| `config?` | [`ForgetNodeConfig`](#../interfaces/ForgetNodeConfig)\<`T`\> |
|
|
224
194
|
|
|
225
195
|
### Returns
|
|
226
196
|
|
|
@@ -238,24 +208,20 @@ ForgetPromise.forget
|
|
|
238
208
|
|
|
239
209
|
```ts
|
|
240
210
|
static timeoutHandler(
|
|
241
|
-
time,
|
|
242
|
-
config,
|
|
243
|
-
externalStackTrace
|
|
211
|
+
time: number,
|
|
212
|
+
config: ForgetNodeConfig,
|
|
213
|
+
externalStackTrace?: string): void;
|
|
244
214
|
```
|
|
245
215
|
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
#### time
|
|
216
|
+
Handles timeouts, optionally terminating the process based on config.
|
|
249
217
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
#### config
|
|
253
|
-
|
|
254
|
-
[`ForgetNodeConfig`](#../interfaces/ForgetNodeConfig)
|
|
255
|
-
|
|
256
|
-
#### externalStackTrace?
|
|
218
|
+
### Parameters
|
|
257
219
|
|
|
258
|
-
|
|
220
|
+
| Parameter | Type |
|
|
221
|
+
| ------ | ------ |
|
|
222
|
+
| `time` | `number` |
|
|
223
|
+
| `config` | [`ForgetNodeConfig`](#../interfaces/ForgetNodeConfig) |
|
|
224
|
+
| `externalStackTrace?` | `string` |
|
|
259
225
|
|
|
260
226
|
### Returns
|
|
261
227
|
|
|
@@ -276,24 +242,23 @@ ForgetPromise.timeoutHandler
|
|
|
276
242
|
***
|
|
277
243
|
|
|
278
244
|
```ts
|
|
279
|
-
function forget<T>(promise
|
|
245
|
+
function forget<T>(promise: Promisable<T>, config?: ForgetNodeConfig<T>): void;
|
|
280
246
|
```
|
|
281
247
|
|
|
282
|
-
|
|
248
|
+
Node.js variant of forget that can optionally terminate the process on exceptions or timeouts.
|
|
283
249
|
|
|
284
|
-
|
|
250
|
+
## Type Parameters
|
|
285
251
|
|
|
286
|
-
|
|
252
|
+
| Type Parameter |
|
|
253
|
+
| ------ |
|
|
254
|
+
| `T` |
|
|
287
255
|
|
|
288
256
|
## Parameters
|
|
289
257
|
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
`Promisable`\<`T`\>
|
|
293
|
-
|
|
294
|
-
### config?
|
|
295
|
-
|
|
296
|
-
[`ForgetNodeConfig`](#../interfaces/ForgetNodeConfig)\<`T`\>
|
|
258
|
+
| Parameter | Type | Description |
|
|
259
|
+
| ------ | ------ | ------ |
|
|
260
|
+
| `promise` | `Promisable`\<`T`\> | The promise or promisable value to forget. |
|
|
261
|
+
| `config?` | [`ForgetNodeConfig`](#../interfaces/ForgetNodeConfig)\<`T`\> | Optional Node.js-specific configuration including process termination options. |
|
|
297
262
|
|
|
298
263
|
## Returns
|
|
299
264
|
|
|
@@ -307,125 +272,29 @@ function forget<T>(promise, config?): void;
|
|
|
307
272
|
|
|
308
273
|
***
|
|
309
274
|
|
|
275
|
+
Node.js-specific forget configuration that extends ForgetConfig with process termination options.
|
|
276
|
+
|
|
310
277
|
## Extends
|
|
311
278
|
|
|
312
279
|
- `ForgetConfig`\<`T`\>
|
|
313
280
|
|
|
314
281
|
## Type Parameters
|
|
315
282
|
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
`T`
|
|
283
|
+
| Type Parameter | Default type |
|
|
284
|
+
| ------ | ------ |
|
|
285
|
+
| `T` | `any` |
|
|
319
286
|
|
|
320
287
|
## Properties
|
|
321
288
|
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
ForgetConfig.name
|
|
332
|
-
```
|
|
333
|
-
|
|
334
|
-
***
|
|
335
|
-
|
|
336
|
-
### onCancel()?
|
|
337
|
-
|
|
338
|
-
```ts
|
|
339
|
-
optional onCancel: () => void;
|
|
340
|
-
```
|
|
341
|
-
|
|
342
|
-
### Returns
|
|
343
|
-
|
|
344
|
-
`void`
|
|
345
|
-
|
|
346
|
-
### Inherited from
|
|
347
|
-
|
|
348
|
-
```ts
|
|
349
|
-
ForgetConfig.onCancel
|
|
350
|
-
```
|
|
351
|
-
|
|
352
|
-
***
|
|
353
|
-
|
|
354
|
-
### onComplete()?
|
|
355
|
-
|
|
356
|
-
```ts
|
|
357
|
-
optional onComplete: (result) => void;
|
|
358
|
-
```
|
|
359
|
-
|
|
360
|
-
### Parameters
|
|
361
|
-
|
|
362
|
-
#### result
|
|
363
|
-
|
|
364
|
-
\[`T` \| `undefined`, `Error` \| `undefined`\]
|
|
365
|
-
|
|
366
|
-
### Returns
|
|
367
|
-
|
|
368
|
-
`void`
|
|
369
|
-
|
|
370
|
-
### Inherited from
|
|
371
|
-
|
|
372
|
-
```ts
|
|
373
|
-
ForgetConfig.onComplete
|
|
374
|
-
```
|
|
375
|
-
|
|
376
|
-
***
|
|
377
|
-
|
|
378
|
-
### onException()?
|
|
379
|
-
|
|
380
|
-
```ts
|
|
381
|
-
optional onException: (error) => void;
|
|
382
|
-
```
|
|
383
|
-
|
|
384
|
-
### Parameters
|
|
385
|
-
|
|
386
|
-
#### error
|
|
387
|
-
|
|
388
|
-
`Error`
|
|
389
|
-
|
|
390
|
-
### Returns
|
|
391
|
-
|
|
392
|
-
`void`
|
|
393
|
-
|
|
394
|
-
### Inherited from
|
|
395
|
-
|
|
396
|
-
```ts
|
|
397
|
-
ForgetConfig.onException
|
|
398
|
-
```
|
|
399
|
-
|
|
400
|
-
***
|
|
401
|
-
|
|
402
|
-
### timeout?
|
|
403
|
-
|
|
404
|
-
```ts
|
|
405
|
-
optional timeout: number;
|
|
406
|
-
```
|
|
407
|
-
|
|
408
|
-
### Inherited from
|
|
409
|
-
|
|
410
|
-
```ts
|
|
411
|
-
ForgetConfig.timeout
|
|
412
|
-
```
|
|
413
|
-
|
|
414
|
-
***
|
|
415
|
-
|
|
416
|
-
### terminateOnException?
|
|
417
|
-
|
|
418
|
-
```ts
|
|
419
|
-
optional terminateOnException: boolean;
|
|
420
|
-
```
|
|
421
|
-
|
|
422
|
-
***
|
|
423
|
-
|
|
424
|
-
### terminateOnTimeout?
|
|
425
|
-
|
|
426
|
-
```ts
|
|
427
|
-
optional terminateOnTimeout: boolean;
|
|
428
|
-
```
|
|
289
|
+
| Property | Type | Description | Inherited from |
|
|
290
|
+
| ------ | ------ | ------ | ------ |
|
|
291
|
+
| <a id="name"></a> `name?` | `string` | Optional name for identifying the forgotten promise in logs. | `ForgetConfig.name` |
|
|
292
|
+
| <a id="oncancel"></a> `onCancel?` | () => `void` | Called when the promise is cancelled due to timeout. | `ForgetConfig.onCancel` |
|
|
293
|
+
| <a id="oncomplete"></a> `onComplete?` | (`result`: \[`T` \| `undefined`, `Error` \| `undefined`\]) => `void` | Called when the promise completes, with a tuple of [result, error]. | `ForgetConfig.onComplete` |
|
|
294
|
+
| <a id="onexception"></a> `onException?` | (`error`: `Error`) => `void` | Called when an exception occurs outside the promise itself. | `ForgetConfig.onException` |
|
|
295
|
+
| <a id="timeout"></a> `timeout?` | `number` | Timeout in milliseconds after which the promise is considered timed out. | `ForgetConfig.timeout` |
|
|
296
|
+
| <a id="terminateonexception"></a> `terminateOnException?` | `boolean` | Terminate the process on an exception that happens outside of the promise being forgotten. | - |
|
|
297
|
+
| <a id="terminateontimeout"></a> `terminateOnTimeout?` | `boolean` | Terminate the process if the promise times out. | - |
|
|
429
298
|
|
|
430
299
|
### variables
|
|
431
300
|
|
|
@@ -439,6 +308,8 @@ optional terminateOnTimeout: boolean;
|
|
|
439
308
|
const defaultForgetNodeConfig: ForgetNodeConfig<unknown>;
|
|
440
309
|
```
|
|
441
310
|
|
|
311
|
+
Default Node.js forget configuration with termination disabled.
|
|
312
|
+
|
|
442
313
|
|
|
443
314
|
Part of [sdk-js](https://www.npmjs.com/package/@xyo-network/sdk-js)
|
|
444
315
|
|
|
@@ -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/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;AAAA,IACxE;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,OAAO,OAAU,SAAgE,QAA0B;AACzG,UAAM,qBAAsB,IAAI,MAAM,OAAO,EAAG;AAGhD,UAAM,iBAAiB;AAAA,MACrB,GAAG;AAAA,MAAqB,GAAG,WAAW,IAAI,QAAQ;AAAA,MAAQ,GAAG;AAAA,IAC/D;AACA,UAAM,kBAAkB,OAAO,Y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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;AACzF,SAAK,OAAO,MAAM,oCAAoC,IAAI,MAAM,MAAM,OAAO,IAAI,KAAK;AACtF,QAAI,uBAAuB,QAAW;AACpC,WAAK,OAAO,KAAK,yBAAyB,IAAI,MAAM,kBAAkB;AAAA,IACxE;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,OAAO,OAAU,SAAgE,QAA0B;AACzG,UAAM,qBAAsB,IAAI,MAAM,OAAO,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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;;;ACzHO,IAAM,oBAAN,cAAgC,cAAc;AAAA;AAAA,EAEnD,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;AAAA,EAGA,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;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":[]}
|
|
@@ -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,CA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AE,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;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,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/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,EAC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AC;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.86",
|
|
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.86",
|
|
50
|
+
"@xylabs/logger": "~5.0.86",
|
|
51
|
+
"@xylabs/promise": "~5.0.86",
|
|
52
|
+
"@xylabs/typeof": "~5.0.86"
|
|
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.16",
|
|
56
|
+
"@xylabs/tsconfig": "~7.4.16",
|
|
57
|
+
"@xylabs/tsconfig-dom": "~7.4.16",
|
|
58
58
|
"typescript": "~5.9.3",
|
|
59
59
|
"vitest": "~4.0.18"
|
|
60
60
|
},
|