@xylabs/forget 4.11.9 → 4.11.11
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/dist/neutral/index.mjs +10 -4
- package/dist/neutral/index.mjs.map +1 -1
- package/dist/node/index-node.mjs +12 -16
- package/dist/node/index-node.mjs.map +1 -1
- package/dist/types/ForgetPromise.d.ts +3 -1
- package/dist/types/ForgetPromise.d.ts.map +1 -1
- package/dist/types/ForgetPromiseNode.d.ts +0 -1
- package/dist/types/ForgetPromiseNode.d.ts.map +1 -1
- package/package.json +8 -7
- package/src/ForgetPromise.ts +12 -4
- package/src/ForgetPromiseNode.ts +2 -13
package/dist/neutral/index.mjs
CHANGED
|
@@ -10,6 +10,7 @@ var defaultForgetConfig = { timeout: 3e4 };
|
|
|
10
10
|
var ForgetPromise = class {
|
|
11
11
|
static activeForgets = 0;
|
|
12
12
|
static exceptedForgets = 0;
|
|
13
|
+
static logger = console;
|
|
13
14
|
static get active() {
|
|
14
15
|
return this.activeForgets > 0;
|
|
15
16
|
}
|
|
@@ -26,8 +27,11 @@ var ForgetPromise = class {
|
|
|
26
27
|
}
|
|
27
28
|
return 0;
|
|
28
29
|
}
|
|
29
|
-
static exceptionHandler(error, _config) {
|
|
30
|
-
|
|
30
|
+
static exceptionHandler(error, _config, externalStackTrace) {
|
|
31
|
+
this.logger.error(`forget promise handler excepted: ${error.message}`, error);
|
|
32
|
+
if (externalStackTrace !== void 0) {
|
|
33
|
+
this.logger.warn("External Stack trace:", externalStackTrace);
|
|
34
|
+
}
|
|
31
35
|
}
|
|
32
36
|
/**
|
|
33
37
|
* Used to explicitly launch an async function (or Promise) with awaiting it
|
|
@@ -35,6 +39,7 @@ var ForgetPromise = class {
|
|
|
35
39
|
* @param config Configuration of forget settings
|
|
36
40
|
*/
|
|
37
41
|
static forget(promise, config) {
|
|
42
|
+
const externalStackTrace = new Error("Stack").stack;
|
|
38
43
|
const resolvedConfig = {
|
|
39
44
|
...defaultForgetConfig,
|
|
40
45
|
...globalThis.xy?.forget?.config,
|
|
@@ -53,6 +58,7 @@ var ForgetPromise = class {
|
|
|
53
58
|
}).catch((error) => {
|
|
54
59
|
this.activeForgets--;
|
|
55
60
|
completed = true;
|
|
61
|
+
this.logger.error(`forgotten promise excepted: ${error.message}`, error);
|
|
56
62
|
resolvedConfig?.onComplete?.([void 0, error]);
|
|
57
63
|
});
|
|
58
64
|
};
|
|
@@ -77,14 +83,14 @@ var ForgetPromise = class {
|
|
|
77
83
|
} catch (ex) {
|
|
78
84
|
this.exceptedForgets += 1;
|
|
79
85
|
resolvedConfig?.onException?.(ex);
|
|
80
|
-
this.exceptionHandler(ex, resolvedConfig);
|
|
86
|
+
this.exceptionHandler(ex, resolvedConfig, externalStackTrace);
|
|
81
87
|
}
|
|
82
88
|
} else {
|
|
83
89
|
return;
|
|
84
90
|
}
|
|
85
91
|
}
|
|
86
92
|
static timeoutHandler(time, _config) {
|
|
87
|
-
|
|
93
|
+
this.logger.error(`forget promise timeout out after ${time}ms [Cancelling]`);
|
|
88
94
|
}
|
|
89
95
|
};
|
|
90
96
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/ForgetPromise.ts","../../src/ForgetConfig.ts","../../src/forget.ts"],"sourcesContent":["import { delay } from '@xylabs/delay'\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\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, _config: ForgetConfig) {\n
|
|
1
|
+
{"version":3,"sources":["../../src/ForgetPromise.ts","../../src/ForgetConfig.ts","../../src/forget.ts"],"sourcesContent":["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, _config: ForgetConfig, externalStackTrace?: string) {\n this.logger.error(`forget promise handler excepted: ${error.message}`, error)\n if (externalStackTrace !== undefined) {\n this.logger.warn('External Stack trace:', 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: ${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)\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, _config: ForgetConfig) {\n this.logger.error(`forget promise timeout out after ${time}ms [Cancelling]`)\n }\n}\n","// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport interface ForgetConfig<T = any> {\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 { type Promisable } from '@xylabs/promise'\n\nimport type { ForgetConfig } from './ForgetConfig.ts'\nimport { ForgetPromise } from './ForgetPromise.ts'\n\n// used to explicitly launch an async function (or Promise) with awaiting it\nexport const forget = <T>(promise: Promisable<T>, config?: ForgetConfig<T>) => {\n ForgetPromise.forget<T>(promise, config)\n}\n"],"mappings":";AAAA,SAAS,aAAa;AAGtB,SAAS,iBAAiB;AAC1B,SAAS,gBAAgB;;;ACIlB,IAAM,sBAA6C,EAAE,SAAS,IAAO;;;ADGrE,IAAM,gBAAN,MAAoB;AAAA,EACzB,OAAO,gBAAgB;AAAA,EACvB,OAAO,kBAAkB;AAAA,EACzB,OA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eAAe,CAAC;AAGlC,cAAM,UAAU,eAAe,WAAW,oBAAoB;AAC9D,YAAI,SAAS,OAAO,GAAG;AACrB,gBAAM,cAAc,YAAY;AAC9B,kBAAM,MAAM,OAAO;AACnB,gBAAI,CAAC,WAAW;AACd,6BAAe,WAAW;AAC1B,mBAAK,eAAe,SAAS,cAAc;AAAA,YAC7C;AAAA,UACF;AACA,mBAAS,KAAK,YAAY,CAAC;AAAA,QAC7B;AAEA,cAAM,MAAM,QAAQ,KAAK,QAAQ;AAEjC,YACG,KAAK,MAAM;AACV;AAAA,QACF,CAAC,EACA,MAAM,MAAM;AACX;AAAA,QACF,CAAC;AAAA,MACL,SAAS,IAAI;AACX,aAAK,mBAAmB;AACxB,wBAAgB,cAAc,EAAW;AACzC,aAAK,iBAAiB,IAAa,gBAAgB,kBAAkB;AAAA,MACvE;AAAA,IACF,OAAO;AAEL;AAAA,IACF;AAAA,EACF;AAAA,EAEA,OAAO,eAAe,MAAc,SAAuB;AACzD,SAAK,OAAO,MAAM,oCAAoC,IAAI,iBAAiB;AAAA,EAC7E;AACF;;;AE1GO,IAAM,SAAS,CAAI,SAAwB,WAA6B;AAC7E,gBAAc,OAAU,SAAS,MAAM;AACzC;","names":[]}
|
package/dist/node/index-node.mjs
CHANGED
|
@@ -15,6 +15,7 @@ import { isNumber } from "@xylabs/typeof";
|
|
|
15
15
|
var ForgetPromise = class {
|
|
16
16
|
static activeForgets = 0;
|
|
17
17
|
static exceptedForgets = 0;
|
|
18
|
+
static logger = console;
|
|
18
19
|
static get active() {
|
|
19
20
|
return this.activeForgets > 0;
|
|
20
21
|
}
|
|
@@ -31,8 +32,11 @@ var ForgetPromise = class {
|
|
|
31
32
|
}
|
|
32
33
|
return 0;
|
|
33
34
|
}
|
|
34
|
-
static exceptionHandler(error, _config) {
|
|
35
|
-
|
|
35
|
+
static exceptionHandler(error, _config, externalStackTrace) {
|
|
36
|
+
this.logger.error(`forget promise handler excepted: ${error.message}`, error);
|
|
37
|
+
if (externalStackTrace !== void 0) {
|
|
38
|
+
this.logger.warn("External Stack trace:", externalStackTrace);
|
|
39
|
+
}
|
|
36
40
|
}
|
|
37
41
|
/**
|
|
38
42
|
* Used to explicitly launch an async function (or Promise) with awaiting it
|
|
@@ -40,6 +44,7 @@ var ForgetPromise = class {
|
|
|
40
44
|
* @param config Configuration of forget settings
|
|
41
45
|
*/
|
|
42
46
|
static forget(promise, config) {
|
|
47
|
+
const externalStackTrace = new Error("Stack").stack;
|
|
43
48
|
const resolvedConfig = {
|
|
44
49
|
...defaultForgetConfig,
|
|
45
50
|
...globalThis.xy?.forget?.config,
|
|
@@ -58,6 +63,7 @@ var ForgetPromise = class {
|
|
|
58
63
|
}).catch((error) => {
|
|
59
64
|
this.activeForgets--;
|
|
60
65
|
completed = true;
|
|
66
|
+
this.logger.error(`forgotten promise excepted: ${error.message}`, error);
|
|
61
67
|
resolvedConfig?.onComplete?.([void 0, error]);
|
|
62
68
|
});
|
|
63
69
|
};
|
|
@@ -82,32 +88,23 @@ var ForgetPromise = class {
|
|
|
82
88
|
} catch (ex) {
|
|
83
89
|
this.exceptedForgets += 1;
|
|
84
90
|
resolvedConfig?.onException?.(ex);
|
|
85
|
-
this.exceptionHandler(ex, resolvedConfig);
|
|
91
|
+
this.exceptionHandler(ex, resolvedConfig, externalStackTrace);
|
|
86
92
|
}
|
|
87
93
|
} else {
|
|
88
94
|
return;
|
|
89
95
|
}
|
|
90
96
|
}
|
|
91
97
|
static timeoutHandler(time, _config) {
|
|
92
|
-
|
|
98
|
+
this.logger.error(`forget promise timeout out after ${time}ms [Cancelling]`);
|
|
93
99
|
}
|
|
94
100
|
};
|
|
95
101
|
|
|
96
102
|
// src/ForgetPromiseNode.ts
|
|
97
|
-
var getStackTrace = () => {
|
|
98
|
-
try {
|
|
99
|
-
throw new Error("Getting stack trace");
|
|
100
|
-
} catch (ex) {
|
|
101
|
-
const error = ex;
|
|
102
|
-
return error.stack;
|
|
103
|
-
}
|
|
104
|
-
};
|
|
105
103
|
var ForgetPromiseNode = class extends ForgetPromise {
|
|
106
104
|
static exceptionHandler(error, config) {
|
|
107
105
|
super.exceptionHandler(error, config);
|
|
108
|
-
console.warn(getStackTrace());
|
|
109
106
|
if (config?.terminateOnException === true) {
|
|
110
|
-
|
|
107
|
+
this.logger.log("Attempting to terminate process...");
|
|
111
108
|
process.exit(1);
|
|
112
109
|
}
|
|
113
110
|
}
|
|
@@ -121,9 +118,8 @@ var ForgetPromiseNode = class extends ForgetPromise {
|
|
|
121
118
|
}
|
|
122
119
|
static timeoutHandler(time, config) {
|
|
123
120
|
super.timeoutHandler(time, config);
|
|
124
|
-
console.warn(getStackTrace());
|
|
125
121
|
if (config?.terminateOnTimeout === true) {
|
|
126
|
-
|
|
122
|
+
this.logger.log("Attempting to terminate process...");
|
|
127
123
|
process.exit(2);
|
|
128
124
|
}
|
|
129
125
|
}
|
|
@@ -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 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 { 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\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, _config: ForgetConfig) {\n
|
|
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 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, _config: ForgetConfig, externalStackTrace?: string) {\n this.logger.error(`forget promise handler excepted: ${error.message}`, error)\n if (externalStackTrace !== undefined) {\n this.logger.warn('External Stack trace:', 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: ${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)\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, _config: ForgetConfig) {\n this.logger.error(`forget promise timeout out after ${time}ms [Cancelling]`)\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) {\n // default | global | provided priorities for config (not deep merge)\n super.exceptionHandler(error, config)\n if (config?.terminateOnException === true) {\n this.logger.log('Attempting to terminate process...')\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) {\n super.timeoutHandler(time, config)\n if (config?.terminateOnTimeout === true) {\n this.logger.log('Attempting to terminate process...')\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":";AAQO,IAAM,sBAA6C,EAAE,SAAS,IAAO;;;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GAAG,WAAW,IAAI,QAAQ;AAAA,MAAQ,GAAG;AAAA,IACnE;AACA,UAAM,OAAO,SAAS,cAAc;AAAA,EACtC;AAAA,EAEA,OAAgB,eAAe,MAAc,QAA0B;AACrE,UAAM,eAAe,MAAM,MAAM;AACjC,QAAI,QAAQ,uBAAuB,MAAM;AACvC,WAAK,OAAO,IAAI,oCAAoC;AAEpD,cAAQ,KAAK,CAAC;AAAA,IAChB;AAAA,EACF;AACF;;;AC3BO,IAAM,aAAa,CAAI,SAAwB,WAAiC;AACrF,oBAAkB,OAAU,SAAS,MAAM;AAC7C;","names":[]}
|
|
@@ -1,12 +1,14 @@
|
|
|
1
|
+
import type { Logger } from '@xylabs/logger';
|
|
1
2
|
import type { Promisable, PromiseEx } from '@xylabs/promise';
|
|
2
3
|
import { type ForgetConfig } from './ForgetConfig.ts';
|
|
3
4
|
type PromisableFunction<T> = () => Promisable<T>;
|
|
4
5
|
export declare class ForgetPromise {
|
|
5
6
|
static activeForgets: number;
|
|
6
7
|
static exceptedForgets: number;
|
|
8
|
+
static logger: Logger;
|
|
7
9
|
static get active(): boolean;
|
|
8
10
|
static awaitInactive(interval?: number, timeout?: number): Promise<number>;
|
|
9
|
-
static exceptionHandler(error: Error, _config: ForgetConfig): void;
|
|
11
|
+
static exceptionHandler(error: Error, _config: ForgetConfig, externalStackTrace?: string): void;
|
|
10
12
|
/**
|
|
11
13
|
* Used to explicitly launch an async function (or Promise) with awaiting it
|
|
12
14
|
* @param promise The promise to forget
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ForgetPromise.d.ts","sourceRoot":"","sources":["../../src/ForgetPromise.ts"],"names":[],"mappings":"AACA,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;AAGhD,qBAAa,aAAa;IACxB,MAAM,CAAC,aAAa,SAAI;IACxB,MAAM,CAAC,eAAe,SAAI;
|
|
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;AAGhD,qBAAa,aAAa;IACxB,MAAM,CAAC,aAAa,SAAI;IACxB,MAAM,CAAC,eAAe,SAAI;IAC1B,MAAM,CAAC,MAAM,EAAE,MAAM,CAAU;IAE/B,MAAM,KAAK,MAAM,YAEhB;WAEY,aAAa,CAAC,QAAQ,SAAM,EAAE,OAAO,CAAC,EAAE,MAAM;IAc3D,MAAM,CAAC,gBAAgB,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE,kBAAkB,CAAC,EAAE,MAAM;IAOxF;;;;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,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY;CAG1D"}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type { Promisable } from '@xylabs/promise';
|
|
2
2
|
import { type ForgetNodeConfig } from './ForgetNodeConfig.ts';
|
|
3
3
|
import { ForgetPromise } from './ForgetPromise.ts';
|
|
4
|
-
export declare const getStackTrace: () => string | undefined;
|
|
5
4
|
export declare class ForgetPromiseNode extends ForgetPromise {
|
|
6
5
|
static exceptionHandler(error: Error, config: ForgetNodeConfig): void;
|
|
7
6
|
static forget<T>(promise: Promisable<T>, config?: ForgetNodeConfig<T>): void;
|
|
@@ -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,
|
|
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;WAClC,gBAAgB,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,gBAAgB;WAUvD,MAAM,CAAC,CAAC,EAAE,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC;WAO9D,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,gBAAgB;CAQtE"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xylabs/forget",
|
|
3
|
-
"version": "4.11.
|
|
3
|
+
"version": "4.11.11",
|
|
4
4
|
"description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"forget",
|
|
@@ -40,15 +40,16 @@
|
|
|
40
40
|
"module": "./dist/node/index-node.mjs",
|
|
41
41
|
"types": "./dist/types/index.d.ts",
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@xylabs/delay": "^4.11.
|
|
44
|
-
"@xylabs/
|
|
45
|
-
"@xylabs/
|
|
43
|
+
"@xylabs/delay": "^4.11.11",
|
|
44
|
+
"@xylabs/logger": "^4.11.11",
|
|
45
|
+
"@xylabs/promise": "^4.11.11",
|
|
46
|
+
"@xylabs/typeof": "^4.11.11"
|
|
46
47
|
},
|
|
47
48
|
"devDependencies": {
|
|
48
|
-
"@xylabs/ts-scripts-yarn3": "^6.5.
|
|
49
|
-
"@xylabs/tsconfig-dom": "^6.5.
|
|
49
|
+
"@xylabs/ts-scripts-yarn3": "^6.5.8",
|
|
50
|
+
"@xylabs/tsconfig-dom": "^6.5.8",
|
|
50
51
|
"typescript": "^5.8.3",
|
|
51
|
-
"vitest": "^3.2.
|
|
52
|
+
"vitest": "^3.2.1"
|
|
52
53
|
},
|
|
53
54
|
"engines": {
|
|
54
55
|
"node": ">=18"
|
package/src/ForgetPromise.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { delay } from '@xylabs/delay'
|
|
2
|
+
import type { Logger } from '@xylabs/logger'
|
|
2
3
|
import type { Promisable, PromiseEx } from '@xylabs/promise'
|
|
3
4
|
import { isPromise } from '@xylabs/promise'
|
|
4
5
|
import { isNumber } from '@xylabs/typeof'
|
|
@@ -11,6 +12,7 @@ type PromisableFunction<T> = () => Promisable<T>
|
|
|
11
12
|
export class ForgetPromise {
|
|
12
13
|
static activeForgets = 0
|
|
13
14
|
static exceptedForgets = 0
|
|
15
|
+
static logger: Logger = console
|
|
14
16
|
|
|
15
17
|
static get active() {
|
|
16
18
|
return this.activeForgets > 0
|
|
@@ -30,8 +32,11 @@ export class ForgetPromise {
|
|
|
30
32
|
return 0
|
|
31
33
|
}
|
|
32
34
|
|
|
33
|
-
static exceptionHandler(error: Error, _config: ForgetConfig) {
|
|
34
|
-
|
|
35
|
+
static exceptionHandler(error: Error, _config: ForgetConfig, externalStackTrace?: string) {
|
|
36
|
+
this.logger.error(`forget promise handler excepted: ${error.message}`, error)
|
|
37
|
+
if (externalStackTrace !== undefined) {
|
|
38
|
+
this.logger.warn('External Stack trace:', externalStackTrace)
|
|
39
|
+
}
|
|
35
40
|
}
|
|
36
41
|
|
|
37
42
|
/**
|
|
@@ -40,6 +45,8 @@ export class ForgetPromise {
|
|
|
40
45
|
* @param config Configuration of forget settings
|
|
41
46
|
*/
|
|
42
47
|
static forget<T>(promise: Promise<T> | PromiseEx<T> | PromisableFunction<T> | T, config?: ForgetConfig<T>) {
|
|
48
|
+
const externalStackTrace = (new Error('Stack')).stack
|
|
49
|
+
|
|
43
50
|
// default | global | provided priorities for config (not deep merge)
|
|
44
51
|
const resolvedConfig = {
|
|
45
52
|
...defaultForgetConfig, ...globalThis.xy?.forget?.config, ...config,
|
|
@@ -60,6 +67,7 @@ export class ForgetPromise {
|
|
|
60
67
|
.catch((error) => {
|
|
61
68
|
this.activeForgets--
|
|
62
69
|
completed = true
|
|
70
|
+
this.logger.error(`forgotten promise excepted: ${error.message}`, error)
|
|
63
71
|
resolvedConfig?.onComplete?.([undefined, error])
|
|
64
72
|
})
|
|
65
73
|
}
|
|
@@ -91,7 +99,7 @@ export class ForgetPromise {
|
|
|
91
99
|
} catch (ex) {
|
|
92
100
|
this.exceptedForgets += 1
|
|
93
101
|
resolvedConfig?.onException?.(ex as Error)
|
|
94
|
-
this.exceptionHandler(ex as Error, resolvedConfig)
|
|
102
|
+
this.exceptionHandler(ex as Error, resolvedConfig, externalStackTrace)
|
|
95
103
|
}
|
|
96
104
|
} else {
|
|
97
105
|
// we do nothing here since if it was a non-promise, it already got invoked.
|
|
@@ -100,6 +108,6 @@ export class ForgetPromise {
|
|
|
100
108
|
}
|
|
101
109
|
|
|
102
110
|
static timeoutHandler(time: number, _config: ForgetConfig) {
|
|
103
|
-
|
|
111
|
+
this.logger.error(`forget promise timeout out after ${time}ms [Cancelling]`)
|
|
104
112
|
}
|
|
105
113
|
}
|
package/src/ForgetPromiseNode.ts
CHANGED
|
@@ -5,22 +5,12 @@ import type { Promisable } from '@xylabs/promise'
|
|
|
5
5
|
import { defaultForgetNodeConfig, type ForgetNodeConfig } from './ForgetNodeConfig.ts'
|
|
6
6
|
import { ForgetPromise } from './ForgetPromise.ts'
|
|
7
7
|
|
|
8
|
-
export const getStackTrace = () => {
|
|
9
|
-
try {
|
|
10
|
-
throw new Error('Getting stack trace')
|
|
11
|
-
} catch (ex) {
|
|
12
|
-
const error = ex as Error
|
|
13
|
-
return error.stack
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
8
|
export class ForgetPromiseNode extends ForgetPromise {
|
|
18
9
|
static override exceptionHandler(error: Error, config: ForgetNodeConfig) {
|
|
19
10
|
// default | global | provided priorities for config (not deep merge)
|
|
20
11
|
super.exceptionHandler(error, config)
|
|
21
|
-
console.warn(getStackTrace())
|
|
22
12
|
if (config?.terminateOnException === true) {
|
|
23
|
-
|
|
13
|
+
this.logger.log('Attempting to terminate process...')
|
|
24
14
|
// eslint-disable-next-line unicorn/no-process-exit
|
|
25
15
|
process.exit(1)
|
|
26
16
|
}
|
|
@@ -35,9 +25,8 @@ export class ForgetPromiseNode extends ForgetPromise {
|
|
|
35
25
|
|
|
36
26
|
static override timeoutHandler(time: number, config: ForgetNodeConfig) {
|
|
37
27
|
super.timeoutHandler(time, config)
|
|
38
|
-
console.warn(getStackTrace())
|
|
39
28
|
if (config?.terminateOnTimeout === true) {
|
|
40
|
-
|
|
29
|
+
this.logger.log('Attempting to terminate process...')
|
|
41
30
|
// eslint-disable-next-line unicorn/no-process-exit
|
|
42
31
|
process.exit(2)
|
|
43
32
|
}
|