@xylabs/forget 4.0.3 → 4.0.5
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.
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export interface ForgetTimeoutConfig {
|
|
2
|
+
cancel: () => void;
|
|
3
|
+
delay: number;
|
|
4
|
+
}
|
|
5
|
+
export declare const ForgetPromise: {
|
|
6
|
+
readonly active: boolean;
|
|
7
|
+
activeForgets: number;
|
|
8
|
+
awaitInactive(interval?: number, timeout?: number): Promise<number>;
|
|
9
|
+
forget<T>(promise: Promise<T>, onComplete?: (result: [T | undefined, Error | undefined]) => void, config?: ForgetTimeoutConfig): void;
|
|
10
|
+
};
|
|
11
|
+
export declare const forget: <T>(promise: Promise<T>, onComplete?: (result: [T | undefined, Error | undefined]) => void, timeout?: ForgetTimeoutConfig) => void;
|
|
12
|
+
//# sourceMappingURL=forget.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"forget.d.ts","sourceRoot":"","sources":["../../src/forget.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,mBAAmB;IAClC,MAAM,EAAE,MAAM,IAAI,CAAA;IAClB,KAAK,EAAE,MAAM,CAAA;CACd;AAED,eAAO,MAAM,aAAa;;;+CAOsB,MAAM;WAmB7C,CAAC,WAAW,OAAO,CAAC,CAAC,CAAC,eAAe,CAAC,MAAM,EAAE,CAAC,CAAC,GAAG,SAAS,EAAE,KAAK,GAAG,SAAS,CAAC,KAAK,IAAI,WAAW,mBAAmB;CA0C/H,CAAA;AAGD,eAAO,MAAM,MAAM,GAAI,CAAC,WAAW,OAAO,CAAC,CAAC,CAAC,eAAe,CAAC,MAAM,EAAE,CAAC,CAAC,GAAG,SAAS,EAAE,KAAK,GAAG,SAAS,CAAC,KAAK,IAAI,YAAY,mBAAmB,SAE9I,CAAA"}
|
package/dist/neutral/index.d.ts
CHANGED
|
@@ -1,13 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
delay: number;
|
|
4
|
-
}
|
|
5
|
-
declare const ForgetPromise: {
|
|
6
|
-
readonly active: boolean;
|
|
7
|
-
activeForgets: number;
|
|
8
|
-
awaitInactive(interval?: number, timeout?: number): Promise<number>;
|
|
9
|
-
forget<T>(promise: Promise<T>, config?: ForgetTimeoutConfig): void;
|
|
10
|
-
};
|
|
11
|
-
declare const forget: (promise: Promise<unknown>, timeout?: ForgetTimeoutConfig) => void;
|
|
12
|
-
|
|
13
|
-
export { ForgetPromise, forget };
|
|
1
|
+
export { forget, ForgetPromise } from './forget.ts';
|
|
2
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA"}
|
package/dist/neutral/index.mjs
CHANGED
|
@@ -23,16 +23,18 @@ var ForgetPromise = {
|
|
|
23
23
|
* @param promise The promise to forget
|
|
24
24
|
* @param config Configuration of forget settings
|
|
25
25
|
*/
|
|
26
|
-
forget(promise, config) {
|
|
26
|
+
forget(promise, onComplete, config) {
|
|
27
27
|
let completed = false;
|
|
28
28
|
this.activeForgets++;
|
|
29
29
|
const promiseWrapper = async () => {
|
|
30
|
-
await promise.then(() => {
|
|
30
|
+
await promise.then((result) => {
|
|
31
31
|
this.activeForgets--;
|
|
32
32
|
completed = true;
|
|
33
|
-
|
|
33
|
+
onComplete?.([result, void 0]);
|
|
34
|
+
}).catch((error) => {
|
|
34
35
|
this.activeForgets--;
|
|
35
36
|
completed = true;
|
|
37
|
+
onComplete?.([void 0, error]);
|
|
36
38
|
});
|
|
37
39
|
};
|
|
38
40
|
const promises = [promiseWrapper()];
|
|
@@ -54,8 +56,8 @@ var ForgetPromise = {
|
|
|
54
56
|
});
|
|
55
57
|
}
|
|
56
58
|
};
|
|
57
|
-
var forget = (promise, timeout) => {
|
|
58
|
-
ForgetPromise.forget(promise, timeout);
|
|
59
|
+
var forget = (promise, onComplete, timeout) => {
|
|
60
|
+
ForgetPromise.forget(promise, onComplete, timeout);
|
|
59
61
|
};
|
|
60
62
|
export {
|
|
61
63
|
ForgetPromise,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/forget.ts"],"sourcesContent":["import { delay } from '@xylabs/delay'\n\nexport interface ForgetTimeoutConfig {\n cancel: () => void\n delay: number\n}\n\nexport const ForgetPromise = {\n get active() {\n return this.activeForgets > 0\n },\n\n activeForgets: 0,\n\n 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 /**\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 forget<T>(promise: Promise<T>, config?: ForgetTimeoutConfig) {\n let completed = false\n this.activeForgets++\n\n const promiseWrapper = async () => {\n await promise\n .then(() => {\n this.activeForgets--\n completed = true\n })\n .catch(() => {\n this.activeForgets--\n completed = true\n })\n }\n\n const promises = [promiseWrapper()]\n\n // if there is a timeout, add it to the race\n if (config) {\n const timeoutFunc = async () => {\n await delay(config.delay)\n if (!completed) {\n console.log(`forget promise timeout out after ${config.delay}ms [Cancelling]`)\n config.cancel?.()\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 },\n}\n\n// used to explicitly launch an async function (or Promise) with awaiting it\nexport const forget = (promise: Promise<
|
|
1
|
+
{"version":3,"sources":["../../src/forget.ts"],"sourcesContent":["import { delay } from '@xylabs/delay'\n\nexport interface ForgetTimeoutConfig {\n cancel: () => void\n delay: number\n}\n\nexport const ForgetPromise = {\n get active() {\n return this.activeForgets > 0\n },\n\n activeForgets: 0,\n\n 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 /**\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 forget<T>(promise: Promise<T>, onComplete?: (result: [T | undefined, Error | undefined]) => void, config?: ForgetTimeoutConfig) {\n let completed = false\n this.activeForgets++\n\n const promiseWrapper = async () => {\n await promise\n .then((result: T) => {\n this.activeForgets--\n completed = true\n onComplete?.([result, undefined])\n })\n .catch((error) => {\n this.activeForgets--\n completed = true\n onComplete?.([undefined, error])\n })\n }\n\n const promises = [promiseWrapper()]\n\n // if there is a timeout, add it to the race\n if (config) {\n const timeoutFunc = async () => {\n await delay(config.delay)\n if (!completed) {\n console.log(`forget promise timeout out after ${config.delay}ms [Cancelling]`)\n config.cancel?.()\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 },\n}\n\n// used to explicitly launch an async function (or Promise) with awaiting it\nexport const forget = <T>(promise: Promise<T>, onComplete?: (result: [T | undefined, Error | undefined]) => void, timeout?: ForgetTimeoutConfig) => {\n ForgetPromise.forget<T>(promise, onComplete, timeout)\n}\n"],"mappings":";AAAA,SAAS,aAAa;AAOf,IAAM,gBAAgB;AAAA,EAC3B,IAAI,SAAS;AACX,WAAO,KAAK,gBAAgB;AAAA,EAC9B;AAAA,EAEA,eAAe;AAAA,EAEf,MAAM,cAAc,WAAW,KAAK,SAAkB;AACpD,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;AAAA;AAAA;AAAA;AAAA,EAOA,OAAU,SAAqB,YAAmE,QAA8B;AAC9H,QAAI,YAAY;AAChB,SAAK;AAEL,UAAM,iBAAiB,YAAY;AACjC,YAAM,QACH,KAAK,CAAC,WAAc;AACnB,aAAK;AACL,oBAAY;AACZ,qBAAa,CAAC,QAAQ,MAAS,CAAC;AAAA,MAClC,CAAC,EACA,MAAM,CAAC,UAAU;AAChB,aAAK;AACL,oBAAY;AACZ,qBAAa,CAAC,QAAW,KAAK,CAAC;AAAA,MACjC,CAAC;AAAA,IACL;AAEA,UAAM,WAAW,CAAC,eAAe,CAAC;AAGlC,QAAI,QAAQ;AACV,YAAM,cAAc,YAAY;AAC9B,cAAM,MAAM,OAAO,KAAK;AACxB,YAAI,CAAC,WAAW;AACd,kBAAQ,IAAI,oCAAoC,OAAO,KAAK,iBAAiB;AAC7E,iBAAO,SAAS;AAAA,QAClB;AAAA,MACF;AACA,eAAS,KAAK,YAAY,CAAC;AAAA,IAC7B;AAEA,UAAM,MAAM,QAAQ,KAAK,QAAQ;AAEjC,QACG,KAAK,MAAM;AACV;AAAA,IACF,CAAC,EACA,MAAM,MAAM;AACX;AAAA,IACF,CAAC;AAAA,EACL;AACF;AAGO,IAAM,SAAS,CAAI,SAAqB,YAAmE,YAAkC;AAClJ,gBAAc,OAAU,SAAS,YAAY,OAAO;AACtD;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xylabs/forget",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.5",
|
|
4
4
|
"description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"forget",
|
|
@@ -36,11 +36,11 @@
|
|
|
36
36
|
"module": "./dist/neutral/index.mjs",
|
|
37
37
|
"types": "./dist/neutral/index.d.ts",
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@xylabs/delay": "^4.0.
|
|
39
|
+
"@xylabs/delay": "^4.0.5"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"@xylabs/ts-scripts-yarn3": "^4.0.
|
|
43
|
-
"@xylabs/tsconfig": "^4.0.
|
|
42
|
+
"@xylabs/ts-scripts-yarn3": "^4.0.7",
|
|
43
|
+
"@xylabs/tsconfig": "^4.0.7",
|
|
44
44
|
"typescript": "^5.5.4"
|
|
45
45
|
},
|
|
46
46
|
"engines": {
|
package/src/forget.ts
CHANGED
|
@@ -31,19 +31,21 @@ export const ForgetPromise = {
|
|
|
31
31
|
* @param promise The promise to forget
|
|
32
32
|
* @param config Configuration of forget settings
|
|
33
33
|
*/
|
|
34
|
-
forget<T>(promise: Promise<T>, config?: ForgetTimeoutConfig) {
|
|
34
|
+
forget<T>(promise: Promise<T>, onComplete?: (result: [T | undefined, Error | undefined]) => void, config?: ForgetTimeoutConfig) {
|
|
35
35
|
let completed = false
|
|
36
36
|
this.activeForgets++
|
|
37
37
|
|
|
38
38
|
const promiseWrapper = async () => {
|
|
39
39
|
await promise
|
|
40
|
-
.then(() => {
|
|
40
|
+
.then((result: T) => {
|
|
41
41
|
this.activeForgets--
|
|
42
42
|
completed = true
|
|
43
|
+
onComplete?.([result, undefined])
|
|
43
44
|
})
|
|
44
|
-
.catch(() => {
|
|
45
|
+
.catch((error) => {
|
|
45
46
|
this.activeForgets--
|
|
46
47
|
completed = true
|
|
48
|
+
onComplete?.([undefined, error])
|
|
47
49
|
})
|
|
48
50
|
}
|
|
49
51
|
|
|
@@ -74,6 +76,6 @@ export const ForgetPromise = {
|
|
|
74
76
|
}
|
|
75
77
|
|
|
76
78
|
// used to explicitly launch an async function (or Promise) with awaiting it
|
|
77
|
-
export const forget = (promise: Promise<
|
|
78
|
-
ForgetPromise.forget(promise, timeout)
|
|
79
|
+
export const forget = <T>(promise: Promise<T>, onComplete?: (result: [T | undefined, Error | undefined]) => void, timeout?: ForgetTimeoutConfig) => {
|
|
80
|
+
ForgetPromise.forget<T>(promise, onComplete, timeout)
|
|
79
81
|
}
|