@xylabs/forget 5.0.80 → 5.0.81

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 CHANGED
@@ -17,7 +17,428 @@ Base functionality used throughout XY Labs TypeScript/JavaScript libraries
17
17
 
18
18
  ## Reference
19
19
 
20
- Reference generation failed.
20
+ **@xylabs/forget**
21
+
22
+ ***
23
+
24
+ ## Classes
25
+
26
+ - [ForgetPromise](#classes/ForgetPromise)
27
+
28
+ ## Interfaces
29
+
30
+ - [ForgetNodeConfig](#interfaces/ForgetNodeConfig)
31
+
32
+ ## Variables
33
+
34
+ - [defaultForgetNodeConfig](#variables/defaultForgetNodeConfig)
35
+
36
+ ## Functions
37
+
38
+ - [forget](#functions/forget)
39
+
40
+ ### classes
41
+
42
+ ### <a id="ForgetPromise"></a>ForgetPromise
43
+
44
+ [**@xylabs/forget**](#../README)
45
+
46
+ ***
47
+
48
+ ## Extends
49
+
50
+ - `ForgetPromise`
51
+
52
+ ## Constructors
53
+
54
+ ### Constructor
55
+
56
+ ```ts
57
+ new ForgetPromise(): ForgetPromiseNode;
58
+ ```
59
+
60
+ ### Returns
61
+
62
+ `ForgetPromiseNode`
63
+
64
+ ### Inherited from
65
+
66
+ ```ts
67
+ ForgetPromise.constructor
68
+ ```
69
+
70
+ ## Properties
71
+
72
+ ### activeForgets
73
+
74
+ ```ts
75
+ static activeForgets: number = 0;
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
+ ```
111
+
112
+ ## Accessors
113
+
114
+ ### active
115
+
116
+ ### Get Signature
117
+
118
+ ```ts
119
+ get static active(): boolean;
120
+ ```
121
+
122
+ #### Returns
123
+
124
+ `boolean`
125
+
126
+ ### Inherited from
127
+
128
+ ```ts
129
+ ForgetPromise.active
130
+ ```
131
+
132
+ ## Methods
133
+
134
+ ### awaitInactive()
135
+
136
+ ```ts
137
+ static awaitInactive(interval?, timeout?): Promise<number>;
138
+ ```
139
+
140
+ ### Parameters
141
+
142
+ #### interval?
143
+
144
+ `number` = `100`
145
+
146
+ #### timeout?
147
+
148
+ `number`
149
+
150
+ ### Returns
151
+
152
+ `Promise`\<`number`\>
153
+
154
+ ### Inherited from
155
+
156
+ ```ts
157
+ ForgetPromise.awaitInactive
158
+ ```
159
+
160
+ ***
161
+
162
+ ### exceptionHandler()
163
+
164
+ ```ts
165
+ static exceptionHandler(
166
+ error,
167
+ config,
168
+ externalStackTrace?): void;
169
+ ```
170
+
171
+ ### Parameters
172
+
173
+ #### error
174
+
175
+ `Error`
176
+
177
+ #### config
178
+
179
+ [`ForgetNodeConfig`](#../interfaces/ForgetNodeConfig)
180
+
181
+ #### externalStackTrace?
182
+
183
+ `string`
184
+
185
+ ### Returns
186
+
187
+ `void`
188
+
189
+ ### Overrides
190
+
191
+ ```ts
192
+ ForgetPromise.exceptionHandler
193
+ ```
194
+
195
+ ***
196
+
197
+ ### forget()
198
+
199
+ ```ts
200
+ static forget<T>(promise, config?): void;
201
+ ```
202
+
203
+ Used to explicitly launch an async function (or Promise) with awaiting it
204
+
205
+ ### Type Parameters
206
+
207
+ #### T
208
+
209
+ `T`
210
+
211
+ ### Parameters
212
+
213
+ #### promise
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
224
+
225
+ ### Returns
226
+
227
+ `void`
228
+
229
+ ### Overrides
230
+
231
+ ```ts
232
+ ForgetPromise.forget
233
+ ```
234
+
235
+ ***
236
+
237
+ ### timeoutHandler()
238
+
239
+ ```ts
240
+ static timeoutHandler(
241
+ time,
242
+ config,
243
+ externalStackTrace?): void;
244
+ ```
245
+
246
+ ### Parameters
247
+
248
+ #### time
249
+
250
+ `number`
251
+
252
+ #### config
253
+
254
+ [`ForgetNodeConfig`](#../interfaces/ForgetNodeConfig)
255
+
256
+ #### externalStackTrace?
257
+
258
+ `string`
259
+
260
+ ### Returns
261
+
262
+ `void`
263
+
264
+ ### Overrides
265
+
266
+ ```ts
267
+ ForgetPromise.timeoutHandler
268
+ ```
269
+
270
+ ### functions
271
+
272
+ ### <a id="forget"></a>forget
273
+
274
+ [**@xylabs/forget**](#../README)
275
+
276
+ ***
277
+
278
+ ```ts
279
+ function forget<T>(promise, config?): void;
280
+ ```
281
+
282
+ ## Type Parameters
283
+
284
+ ### T
285
+
286
+ `T`
287
+
288
+ ## Parameters
289
+
290
+ ### promise
291
+
292
+ `Promisable`\<`T`\>
293
+
294
+ ### config?
295
+
296
+ [`ForgetNodeConfig`](#../interfaces/ForgetNodeConfig)\<`T`\>
297
+
298
+ ## Returns
299
+
300
+ `void`
301
+
302
+ ### interfaces
303
+
304
+ ### <a id="ForgetNodeConfig"></a>ForgetNodeConfig
305
+
306
+ [**@xylabs/forget**](#../README)
307
+
308
+ ***
309
+
310
+ ## Extends
311
+
312
+ - `ForgetConfig`\<`T`\>
313
+
314
+ ## Type Parameters
315
+
316
+ ### T
317
+
318
+ `T` = `any`
319
+
320
+ ## Properties
321
+
322
+ ### name?
323
+
324
+ ```ts
325
+ optional name: string;
326
+ ```
327
+
328
+ ### Inherited from
329
+
330
+ ```ts
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
+ ```
429
+
430
+ ### variables
431
+
432
+ ### <a id="defaultForgetNodeConfig"></a>defaultForgetNodeConfig
433
+
434
+ [**@xylabs/forget**](#../README)
435
+
436
+ ***
437
+
438
+ ```ts
439
+ const defaultForgetNodeConfig: ForgetNodeConfig<unknown>;
440
+ ```
441
+
21
442
 
22
443
  Part of [sdk-js](https://www.npmjs.com/package/@xyo-network/sdk-js)
23
444
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xylabs/forget",
3
- "version": "5.0.80",
3
+ "version": "5.0.81",
4
4
  "description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries",
5
5
  "keywords": [
6
6
  "forget",
@@ -31,33 +31,30 @@
31
31
  "types": "./dist/node/index.d.ts",
32
32
  "node": {
33
33
  "types": "./dist/node/index.d.ts",
34
- "source": "./src/node/index.ts",
35
34
  "default": "./dist/node/index.mjs"
36
35
  },
37
- "source": "./src/node/index.ts",
38
36
  "default": "./dist/node/index.mjs"
39
37
  },
40
38
  "./package.json": "./package.json"
41
39
  },
42
40
  "module": "./dist/node/index.mjs",
43
- "source": "./src/node/index.ts",
44
41
  "types": "./dist/node/index.d.ts",
45
42
  "files": [
46
43
  "dist",
47
- "src",
48
44
  "!**/*.bench.*",
49
45
  "!**/*.spec.*",
50
46
  "!**/*.test.*"
51
47
  ],
52
48
  "dependencies": {
53
- "@xylabs/delay": "~5.0.80",
54
- "@xylabs/logger": "~5.0.80",
55
- "@xylabs/promise": "~5.0.80",
56
- "@xylabs/typeof": "~5.0.80"
49
+ "@xylabs/delay": "~5.0.81",
50
+ "@xylabs/logger": "~5.0.81",
51
+ "@xylabs/promise": "~5.0.81",
52
+ "@xylabs/typeof": "~5.0.81"
57
53
  },
58
54
  "devDependencies": {
59
- "@xylabs/ts-scripts-yarn3": "~7.3.2",
60
- "@xylabs/tsconfig-dom": "~7.3.2",
55
+ "@xylabs/ts-scripts-yarn3": "~7.4.11",
56
+ "@xylabs/tsconfig": "~7.4.11",
57
+ "@xylabs/tsconfig-dom": "~7.4.11",
61
58
  "typescript": "~5.9.3",
62
59
  "vitest": "~4.0.18"
63
60
  },
@@ -1,10 +0,0 @@
1
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
2
- export interface ForgetConfig<T = any> {
3
- name?: string
4
- onCancel?: () => void
5
- onComplete?: (result: [T | undefined, Error | undefined]) => void
6
- onException?: (error: Error) => void
7
- timeout?: number
8
- }
9
-
10
- export const defaultForgetConfig: ForgetConfig<unknown> = { timeout: 30_000 }
@@ -1,13 +0,0 @@
1
- import { defaultForgetConfig, type ForgetConfig } from './ForgetConfig.ts'
2
-
3
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
4
- export interface ForgetNodeConfig<T = any> extends ForgetConfig<T> {
5
- terminateOnException?: boolean // terminate the process on an exception that happens outside of the promise being forgotten
6
- terminateOnTimeout?: boolean // terminate the process if the promise times out
7
- }
8
-
9
- export const defaultForgetNodeConfig: ForgetNodeConfig<unknown> = {
10
- ...defaultForgetConfig,
11
- terminateOnTimeout: false,
12
- terminateOnException: false,
13
- }
@@ -1,116 +0,0 @@
1
- import { delay } from '@xylabs/delay'
2
- import type { Logger } from '@xylabs/logger'
3
- import type { Promisable, PromiseEx } from '@xylabs/promise'
4
- import { isPromise } from '@xylabs/promise'
5
- import { isNumber } from '@xylabs/typeof'
6
-
7
- import { defaultForgetConfig, type ForgetConfig } from './ForgetConfig.ts'
8
-
9
- type PromisableFunction<T> = () => Promisable<T>
10
-
11
- // eslint-disable-next-line unicorn/no-static-only-class
12
- export class ForgetPromise {
13
- static activeForgets = 0
14
- static exceptedForgets = 0
15
- static logger: Logger = console
16
-
17
- static get active() {
18
- return this.activeForgets > 0
19
- }
20
-
21
- static async awaitInactive(interval = 100, timeout?: number) {
22
- let timeoutRemaining = timeout
23
- while (this.active) {
24
- await delay(interval)
25
- if (timeoutRemaining !== undefined) {
26
- timeoutRemaining -= interval
27
- if (timeoutRemaining <= 0) {
28
- return this.activeForgets
29
- }
30
- }
31
- }
32
- return 0
33
- }
34
-
35
- static exceptionHandler(error: Error, { name }: ForgetConfig, externalStackTrace?: string) {
36
- this.logger.error(`forget promise handler excepted [${name}]: ${error.message}`, error)
37
- if (externalStackTrace !== undefined) {
38
- this.logger.warn(`External Stack trace [${name}]:`, externalStackTrace)
39
- }
40
- }
41
-
42
- /**
43
- * Used to explicitly launch an async function (or Promise) with awaiting it
44
- * @param promise The promise to forget
45
- * @param config Configuration of forget settings
46
- */
47
- static forget<T>(promise: Promise<T> | PromiseEx<T> | PromisableFunction<T> | T, config?: ForgetConfig<T>) {
48
- const externalStackTrace = (new Error('Stack')).stack
49
-
50
- // default | global | provided priorities for config (not deep merge)
51
- const resolvedConfig = {
52
- ...defaultForgetConfig, ...globalThis.xy?.forget?.config, ...config,
53
- }
54
- const resolvedPromise = typeof promise === 'function' ? (promise as PromisableFunction<T>)() : promise
55
- if (isPromise(resolvedPromise)) {
56
- try {
57
- let completed = false
58
- this.activeForgets++
59
-
60
- const promiseWrapper = async () => {
61
- await resolvedPromise
62
- .then((result: T) => {
63
- this.activeForgets--
64
- completed = true
65
- resolvedConfig?.onComplete?.([result, undefined])
66
- })
67
- .catch((error) => {
68
- this.activeForgets--
69
- completed = true
70
- this.logger.error(`forgotten promise excepted [${config?.name ?? 'unknown'}]: ${error.message}`, error)
71
- resolvedConfig?.onComplete?.([undefined, error])
72
- })
73
- }
74
-
75
- const promises = [promiseWrapper()]
76
-
77
- // if there is a timeout, add it to the race
78
- const timeout = resolvedConfig.timeout ?? defaultForgetConfig.timeout
79
- if (isNumber(timeout)) {
80
- const timeoutFunc = async () => {
81
- await delay(timeout)
82
- if (!completed) {
83
- resolvedConfig.onCancel?.()
84
- this.timeoutHandler(timeout, resolvedConfig, externalStackTrace)
85
- }
86
- }
87
- promises.push(timeoutFunc())
88
- }
89
-
90
- const all = Promise.race(promises)
91
-
92
- all
93
- .then(() => {
94
- return
95
- })
96
- .catch(() => {
97
- return
98
- })
99
- } catch (ex) {
100
- this.exceptedForgets += 1
101
- resolvedConfig?.onException?.(ex as Error)
102
- this.exceptionHandler(ex as Error, resolvedConfig, externalStackTrace)
103
- }
104
- } else {
105
- // we do nothing here since if it was a non-promise, it already got invoked.
106
- return
107
- }
108
- }
109
-
110
- static timeoutHandler(time: number, { name = 'unknown' }: ForgetConfig, externalStackTrace?: string) {
111
- this.logger.error(`forget promise timeout out after ${time}ms [Cancelling] [${name}]`)
112
- if (externalStackTrace !== undefined) {
113
- this.logger.warn(`External Stack trace [${name}]:`, externalStackTrace)
114
- }
115
- }
116
- }
@@ -1,34 +0,0 @@
1
- /// <reference types="node" />
2
-
3
- import type { Promisable } from '@xylabs/promise'
4
-
5
- import { defaultForgetNodeConfig, type ForgetNodeConfig } from './ForgetNodeConfig.ts'
6
- import { ForgetPromise } from './ForgetPromise.ts'
7
-
8
- export class ForgetPromiseNode extends ForgetPromise {
9
- static override exceptionHandler(error: Error, config: ForgetNodeConfig, externalStackTrace?: string) {
10
- // default | global | provided priorities for config (not deep merge)
11
- super.exceptionHandler(error, config, externalStackTrace)
12
- if (config?.terminateOnException === true) {
13
- this.logger.error(`Attempting to terminate process [${config?.name ?? 'unknown'}]...`)
14
- // eslint-disable-next-line unicorn/no-process-exit
15
- process.exit(1)
16
- }
17
- }
18
-
19
- static override forget<T>(promise: Promisable<T>, config?: ForgetNodeConfig<T>) {
20
- const resolvedConfig = {
21
- ...defaultForgetNodeConfig, ...globalThis.xy?.forget?.config, ...config,
22
- }
23
- super.forget(promise, resolvedConfig)
24
- }
25
-
26
- static override timeoutHandler(time: number, config: ForgetNodeConfig, externalStackTrace?: string) {
27
- super.timeoutHandler(time, config, externalStackTrace)
28
- if (config?.terminateOnTimeout === true) {
29
- this.logger.error(`Attempting to terminate process [${config?.name ?? 'unknown'}]...`)
30
- // eslint-disable-next-line unicorn/no-process-exit
31
- process.exit(2)
32
- }
33
- }
34
- }
package/src/forget.ts DELETED
@@ -1,9 +0,0 @@
1
- import { type Promisable } from '@xylabs/promise'
2
-
3
- import type { ForgetConfig } from './ForgetConfig.ts'
4
- import { ForgetPromise } from './ForgetPromise.ts'
5
-
6
- // used to explicitly launch an async function (or Promise) with awaiting it
7
- export const forget = <T>(promise: Promisable<T>, config?: ForgetConfig<T>) => {
8
- ForgetPromise.forget<T>(promise, config)
9
- }
package/src/forgetNode.ts DELETED
@@ -1,9 +0,0 @@
1
- import { type Promisable } from '@xylabs/promise'
2
-
3
- import type { ForgetNodeConfig } from './ForgetNodeConfig.ts'
4
- import { ForgetPromiseNode } from './ForgetPromiseNode.ts'
5
-
6
- // used to explicitly launch an async function (or Promise) with awaiting it
7
- export const forgetNode = <T>(promise: Promisable<T>, config?: ForgetNodeConfig<T>) => {
8
- ForgetPromiseNode.forget<T>(promise, config)
9
- }
package/src/index.ts DELETED
@@ -1 +0,0 @@
1
- export * from './node/index.ts'
@@ -1,3 +0,0 @@
1
- export * from '../forget.ts'
2
- export * from '../ForgetConfig.ts'
3
- export * from '../ForgetPromise.ts'
package/src/node/index.ts DELETED
@@ -1,3 +0,0 @@
1
- export { forgetNode as forget } from '../forgetNode.ts'
2
- export * from '../ForgetNodeConfig.ts'
3
- export { ForgetPromiseNode as ForgetPromise } from '../ForgetPromiseNode.ts'
@@ -1,9 +0,0 @@
1
- import type { ForgetNodeConfig } from './ForgetNodeConfig.ts'
2
-
3
- declare global {
4
- var xy: {
5
- forget: {
6
- config: ForgetNodeConfig | undefined
7
- } | undefined
8
- } | undefined
9
- }
@@ -1,9 +0,0 @@
1
- import type { ForgetConfig } from './ForgetConfig.ts'
2
-
3
- declare global {
4
- var xy: {
5
- forget: {
6
- config: ForgetConfig | undefined
7
- } | undefined
8
- } | undefined
9
- }