@xylabs/forget 4.6.1 → 4.6.2
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 +33 -28
- package/dist/neutral/index.mjs.map +1 -1
- package/dist/types/forget.d.ts +2 -1
- package/dist/types/forget.d.ts.map +1 -1
- package/package.json +3 -2
- package/src/forget.ts +39 -34
package/dist/neutral/index.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// src/forget.ts
|
|
2
2
|
import { delay } from "@xylabs/delay";
|
|
3
|
+
import { isPromise } from "@xylabs/promise";
|
|
3
4
|
var ForgetPromise = {
|
|
4
5
|
get active() {
|
|
5
6
|
return this.activeForgets > 0;
|
|
@@ -24,36 +25,40 @@ var ForgetPromise = {
|
|
|
24
25
|
* @param config Configuration of forget settings
|
|
25
26
|
*/
|
|
26
27
|
forget(promise, onComplete, config) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const promises = [promiseWrapper()];
|
|
41
|
-
if (config) {
|
|
42
|
-
const timeoutFunc = async () => {
|
|
43
|
-
await delay(config.delay);
|
|
44
|
-
if (!completed) {
|
|
45
|
-
console.log(`forget promise timeout out after ${config.delay}ms [Cancelling]`);
|
|
46
|
-
config.cancel?.();
|
|
47
|
-
}
|
|
28
|
+
if (isPromise(promise)) {
|
|
29
|
+
let completed = false;
|
|
30
|
+
this.activeForgets++;
|
|
31
|
+
const promiseWrapper = async () => {
|
|
32
|
+
await promise.then((result) => {
|
|
33
|
+
this.activeForgets--;
|
|
34
|
+
completed = true;
|
|
35
|
+
onComplete?.([result, void 0]);
|
|
36
|
+
}).catch((error) => {
|
|
37
|
+
this.activeForgets--;
|
|
38
|
+
completed = true;
|
|
39
|
+
onComplete?.([void 0, error]);
|
|
40
|
+
});
|
|
48
41
|
};
|
|
49
|
-
promises
|
|
42
|
+
const promises = [promiseWrapper()];
|
|
43
|
+
if (config) {
|
|
44
|
+
const timeoutFunc = async () => {
|
|
45
|
+
await delay(config.delay);
|
|
46
|
+
if (!completed) {
|
|
47
|
+
console.log(`forget promise timeout out after ${config.delay}ms [Cancelling]`);
|
|
48
|
+
config.cancel?.();
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
promises.push(timeoutFunc());
|
|
52
|
+
}
|
|
53
|
+
const all = Promise.race(promises);
|
|
54
|
+
all.then(() => {
|
|
55
|
+
return;
|
|
56
|
+
}).catch(() => {
|
|
57
|
+
return;
|
|
58
|
+
});
|
|
59
|
+
} else {
|
|
60
|
+
return promise();
|
|
50
61
|
}
|
|
51
|
-
const all = Promise.race(promises);
|
|
52
|
-
all.then(() => {
|
|
53
|
-
return;
|
|
54
|
-
}).catch(() => {
|
|
55
|
-
return;
|
|
56
|
-
});
|
|
57
62
|
}
|
|
58
63
|
};
|
|
59
64
|
var forget = (promise, onComplete, timeout) => {
|
|
@@ -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:
|
|
1
|
+
{"version":3,"sources":["../../src/forget.ts"],"sourcesContent":["import { delay } from '@xylabs/delay'\nimport { isPromise, type Promisable } from '@xylabs/promise'\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: Promisable<T>, onComplete?: (result: [T | undefined, Error | undefined]) => void, config?: ForgetTimeoutConfig) {\n if (isPromise(promise)) {\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 } else {\n return (promise as () => void)()\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;AACtB,SAAS,iBAAkC;AAOpC,IAAM,gBAAgB;AAAA,EAC3B,IAAI,SAAS;AACX,WAAO,KAAK,gBAAgB;AAAA,EAC9B;AAAA,EAEA,eAAe;AAAA,EAEf,MAAM,cAAc,WAAW,KAAK,SAAkB;AACpD,QAAI,mB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
2
|
export interface ForgetTimeoutConfig {
|
|
2
3
|
cancel: () => void;
|
|
3
4
|
delay: number;
|
|
@@ -11,7 +12,7 @@ export declare const ForgetPromise: {
|
|
|
11
12
|
* @param promise The promise to forget
|
|
12
13
|
* @param config Configuration of forget settings
|
|
13
14
|
*/
|
|
14
|
-
forget<T>(promise:
|
|
15
|
+
forget<T>(promise: Promisable<T>, onComplete?: (result: [T | undefined, Error | undefined]) => void, config?: ForgetTimeoutConfig): void;
|
|
15
16
|
};
|
|
16
17
|
export declare const forget: <T>(promise: Promise<T>, onComplete?: (result: [T | undefined, Error | undefined]) => void, timeout?: ForgetTimeoutConfig) => void;
|
|
17
18
|
//# sourceMappingURL=forget.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"forget.d.ts","sourceRoot":"","sources":["../../src/forget.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"forget.d.ts","sourceRoot":"","sources":["../../src/forget.ts"],"names":[],"mappings":"AACA,OAAO,EAAa,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAE5D,MAAM,WAAW,mBAAmB;IAClC,MAAM,EAAE,MAAM,IAAI,CAAA;IAClB,KAAK,EAAE,MAAM,CAAA;CACd;AAED,eAAO,MAAM,aAAa;;;+CAOsB,MAAM;IAcpD;;;;OAIG;WACI,CAAC,WAAW,UAAU,CAAC,CAAC,CAAC,eAAe,CAAC,MAAM,EAAE,CAAC,CAAC,GAAG,SAAS,EAAE,KAAK,GAAG,SAAS,CAAC,KAAK,IAAI,WAAW,mBAAmB;CA8ClI,CAAA;AAGD,eAAO,MAAM,MAAM,GAAI,CAAC,EAAE,SAAS,OAAO,CAAC,CAAC,CAAC,EAAE,aAAa,CAAC,MAAM,EAAE,CAAC,CAAC,GAAG,SAAS,EAAE,KAAK,GAAG,SAAS,CAAC,KAAK,IAAI,EAAE,UAAU,mBAAmB,SAE9I,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xylabs/forget",
|
|
3
|
-
"version": "4.6.
|
|
3
|
+
"version": "4.6.2",
|
|
4
4
|
"description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"forget",
|
|
@@ -36,7 +36,8 @@
|
|
|
36
36
|
"module": "./dist/neutral/index.mjs",
|
|
37
37
|
"types": "./dist/types/index.d.ts",
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@xylabs/delay": "^4.6.
|
|
39
|
+
"@xylabs/delay": "^4.6.2",
|
|
40
|
+
"@xylabs/promise": "^4.6.2"
|
|
40
41
|
},
|
|
41
42
|
"devDependencies": {
|
|
42
43
|
"@xylabs/ts-scripts-yarn3": "^6.1.3",
|
package/src/forget.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { delay } from '@xylabs/delay'
|
|
2
|
+
import { isPromise, type Promisable } from '@xylabs/promise'
|
|
2
3
|
|
|
3
4
|
export interface ForgetTimeoutConfig {
|
|
4
5
|
cancel: () => void
|
|
@@ -31,47 +32,51 @@ export const ForgetPromise = {
|
|
|
31
32
|
* @param promise The promise to forget
|
|
32
33
|
* @param config Configuration of forget settings
|
|
33
34
|
*/
|
|
34
|
-
forget<T>(promise:
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
forget<T>(promise: Promisable<T>, onComplete?: (result: [T | undefined, Error | undefined]) => void, config?: ForgetTimeoutConfig) {
|
|
36
|
+
if (isPromise(promise)) {
|
|
37
|
+
let completed = false
|
|
38
|
+
this.activeForgets++
|
|
37
39
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
40
|
+
const promiseWrapper = async () => {
|
|
41
|
+
await promise
|
|
42
|
+
.then((result: T) => {
|
|
43
|
+
this.activeForgets--
|
|
44
|
+
completed = true
|
|
45
|
+
onComplete?.([result, undefined])
|
|
46
|
+
})
|
|
47
|
+
.catch((error) => {
|
|
48
|
+
this.activeForgets--
|
|
49
|
+
completed = true
|
|
50
|
+
onComplete?.([undefined, error])
|
|
51
|
+
})
|
|
52
|
+
}
|
|
51
53
|
|
|
52
|
-
|
|
54
|
+
const promises = [promiseWrapper()]
|
|
53
55
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
56
|
+
// if there is a timeout, add it to the race
|
|
57
|
+
if (config) {
|
|
58
|
+
const timeoutFunc = async () => {
|
|
59
|
+
await delay(config.delay)
|
|
60
|
+
if (!completed) {
|
|
61
|
+
console.log(`forget promise timeout out after ${config.delay}ms [Cancelling]`)
|
|
62
|
+
config.cancel?.()
|
|
63
|
+
}
|
|
61
64
|
}
|
|
65
|
+
promises.push(timeoutFunc())
|
|
62
66
|
}
|
|
63
|
-
promises.push(timeoutFunc())
|
|
64
|
-
}
|
|
65
67
|
|
|
66
|
-
|
|
68
|
+
const all = Promise.race(promises)
|
|
67
69
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
70
|
+
all
|
|
71
|
+
.then(() => {
|
|
72
|
+
return
|
|
73
|
+
})
|
|
74
|
+
.catch(() => {
|
|
75
|
+
return
|
|
76
|
+
})
|
|
77
|
+
} else {
|
|
78
|
+
return (promise as () => void)()
|
|
79
|
+
}
|
|
75
80
|
},
|
|
76
81
|
}
|
|
77
82
|
|