@telperion/js-utils 3.5.0
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/LICENSE +21 -0
- package/README.md +621 -0
- package/array/arrayize.cjs +22 -0
- package/array/arrayize.d.ts +17 -0
- package/array/arrayize.js +21 -0
- package/array/arrayize.js.map +1 -0
- package/array/async-map.cjs +40 -0
- package/array/async-map.d.ts +34 -0
- package/array/async-map.js +39 -0
- package/array/async-map.js.map +1 -0
- package/array/compact.cjs +34 -0
- package/array/compact.d.ts +28 -0
- package/array/compact.js +33 -0
- package/array/compact.js.map +1 -0
- package/array/difference.cjs +16 -0
- package/array/difference.d.ts +45 -0
- package/array/difference.js +15 -0
- package/array/difference.js.map +1 -0
- package/array/find-by-key.cjs +35 -0
- package/array/find-by-key.d.ts +30 -0
- package/array/find-by-key.js +34 -0
- package/array/find-by-key.js.map +1 -0
- package/array/index.cjs +14 -0
- package/array/index.d.ts +11 -0
- package/array/index.js +13 -0
- package/array/index.js.map +1 -0
- package/array/intersection.cjs +16 -0
- package/array/intersection.d.ts +45 -0
- package/array/intersection.js +15 -0
- package/array/intersection.js.map +1 -0
- package/array/limit.cjs +37 -0
- package/array/limit.d.ts +29 -0
- package/array/limit.js +36 -0
- package/array/limit.js.map +1 -0
- package/array/proto/async-map.cjs +6 -0
- package/array/proto/async-map.d.ts +25 -0
- package/array/proto/async-map.js +6 -0
- package/array/proto/async-map.js.map +1 -0
- package/array/proto/compact.cjs +6 -0
- package/array/proto/compact.d.ts +15 -0
- package/array/proto/compact.js +6 -0
- package/array/proto/compact.js.map +1 -0
- package/array/proto/difference.cjs +6 -0
- package/array/proto/difference.d.ts +27 -0
- package/array/proto/difference.js +6 -0
- package/array/proto/difference.js.map +1 -0
- package/array/proto/find-by-key.cjs +6 -0
- package/array/proto/find-by-key.d.ts +25 -0
- package/array/proto/find-by-key.js +6 -0
- package/array/proto/find-by-key.js.map +1 -0
- package/array/proto/intersection.cjs +6 -0
- package/array/proto/intersection.d.ts +27 -0
- package/array/proto/intersection.js +6 -0
- package/array/proto/intersection.js.map +1 -0
- package/array/proto/limit.cjs +6 -0
- package/array/proto/limit.d.ts +24 -0
- package/array/proto/limit.js +6 -0
- package/array/proto/limit.js.map +1 -0
- package/array/proto/remove.cjs +6 -0
- package/array/proto/remove.d.ts +32 -0
- package/array/proto/remove.js +6 -0
- package/array/proto/remove.js.map +1 -0
- package/array/proto/replace.cjs +6 -0
- package/array/proto/replace.d.ts +72 -0
- package/array/proto/replace.js +6 -0
- package/array/proto/replace.js.map +1 -0
- package/array/proto/uniquify-by-key.cjs +6 -0
- package/array/proto/uniquify-by-key.d.ts +24 -0
- package/array/proto/uniquify-by-key.js +6 -0
- package/array/proto/uniquify-by-key.js.map +1 -0
- package/array/proto/uniquify.cjs +6 -0
- package/array/proto/uniquify.d.ts +23 -0
- package/array/proto/uniquify.js +6 -0
- package/array/proto/uniquify.js.map +1 -0
- package/array/remove.cjs +46 -0
- package/array/remove.d.ts +33 -0
- package/array/remove.js +45 -0
- package/array/remove.js.map +1 -0
- package/array/replace.cjs +69 -0
- package/array/replace.d.ts +139 -0
- package/array/replace.js +68 -0
- package/array/replace.js.map +1 -0
- package/array/static/arrayize.cjs +4 -0
- package/array/static/arrayize.d.ts +6 -0
- package/array/static/arrayize.js +4 -0
- package/array/static/arrayize.js.map +1 -0
- package/array/uniquify-by-key.cjs +41 -0
- package/array/uniquify-by-key.d.ts +29 -0
- package/array/uniquify-by-key.js +40 -0
- package/array/uniquify-by-key.js.map +1 -0
- package/array/uniquify.cjs +32 -0
- package/array/uniquify.d.ts +27 -0
- package/array/uniquify.js +31 -0
- package/array/uniquify.js.map +1 -0
- package/boolean/static/is-falsy.cjs +4 -0
- package/boolean/static/is-falsy.d.ts +23 -0
- package/boolean/static/is-falsy.js +4 -0
- package/boolean/static/is-falsy.js.map +1 -0
- package/boolean/static/is-truthy.cjs +4 -0
- package/boolean/static/is-truthy.d.ts +23 -0
- package/boolean/static/is-truthy.js +4 -0
- package/boolean/static/is-truthy.js.map +1 -0
- package/class/index.cjs +4 -0
- package/class/index.d.ts +1 -0
- package/class/index.js +3 -0
- package/class/index.js.map +1 -0
- package/class/mixin.cjs +60 -0
- package/class/mixin.d.ts +50 -0
- package/class/mixin.js +59 -0
- package/class/mixin.js.map +1 -0
- package/compact.cjs +39 -0
- package/compact.d.ts +2 -0
- package/compact.js +38 -0
- package/compact.js.map +1 -0
- package/function/call.cjs +6 -0
- package/function/call.d.ts +1 -0
- package/function/call.js +5 -0
- package/function/call.js.map +1 -0
- package/function/debounce.cjs +85 -0
- package/function/debounce.d.ts +44 -0
- package/function/debounce.js +82 -0
- package/function/debounce.js.map +1 -0
- package/function/defer.cjs +42 -0
- package/function/defer.d.ts +26 -0
- package/function/defer.js +41 -0
- package/function/defer.js.map +1 -0
- package/function/index.cjs +8 -0
- package/function/index.d.ts +5 -0
- package/function/index.js +7 -0
- package/function/index.js.map +1 -0
- package/function/noop.cjs +23 -0
- package/function/noop.d.ts +20 -0
- package/function/noop.js +22 -0
- package/function/noop.js.map +1 -0
- package/function/of.cjs +44 -0
- package/function/of.d.ts +31 -0
- package/function/of.js +43 -0
- package/function/of.js.map +1 -0
- package/function/proto/debounce.cjs +6 -0
- package/function/proto/debounce.d.ts +30 -0
- package/function/proto/debounce.js +6 -0
- package/function/proto/debounce.js.map +1 -0
- package/function/static/call.cjs +4 -0
- package/function/static/call.d.ts +6 -0
- package/function/static/call.js +4 -0
- package/function/static/call.js.map +1 -0
- package/function/static/defer.cjs +4 -0
- package/function/static/defer.d.ts +23 -0
- package/function/static/defer.js +4 -0
- package/function/static/defer.js.map +1 -0
- package/function/static/noop.cjs +4 -0
- package/function/static/noop.d.ts +18 -0
- package/function/static/noop.js +4 -0
- package/function/static/noop.js.map +1 -0
- package/function/static/of.cjs +4 -0
- package/function/static/of.d.ts +26 -0
- package/function/static/of.js +4 -0
- package/function/static/of.js.map +1 -0
- package/index.cjs +31 -0
- package/index.d.ts +14 -0
- package/index.js +17 -0
- package/index.js.map +1 -0
- package/is-falsy.cjs +33 -0
- package/is-falsy.d.ts +28 -0
- package/is-falsy.js +32 -0
- package/is-falsy.js.map +1 -0
- package/is-truthy.cjs +33 -0
- package/is-truthy.d.ts +28 -0
- package/is-truthy.js +32 -0
- package/is-truthy.js.map +1 -0
- package/limit.cjs +34 -0
- package/limit.d.ts +2 -0
- package/limit.js +33 -0
- package/limit.js.map +1 -0
- package/map/index.cjs +4 -0
- package/map/index.d.ts +1 -0
- package/map/index.js +3 -0
- package/map/index.js.map +1 -0
- package/map/merge.cjs +33 -0
- package/map/merge.d.ts +24 -0
- package/map/merge.js +32 -0
- package/map/merge.js.map +1 -0
- package/math/index.cjs +4 -0
- package/math/index.d.ts +1 -0
- package/math/index.js +3 -0
- package/math/index.js.map +1 -0
- package/math/min-max.cjs +29 -0
- package/math/min-max.d.ts +24 -0
- package/math/min-max.js +28 -0
- package/math/min-max.js.map +1 -0
- package/math/static/min-max.cjs +4 -0
- package/math/static/min-max.d.ts +20 -0
- package/math/static/min-max.js +4 -0
- package/math/static/min-max.js.map +1 -0
- package/object/clone.cjs +157 -0
- package/object/clone.d.ts +161 -0
- package/object/clone.js +156 -0
- package/object/clone.js.map +1 -0
- package/object/compact.cjs +49 -0
- package/object/compact.d.ts +37 -0
- package/object/compact.js +48 -0
- package/object/compact.js.map +1 -0
- package/object/deep-freeze.cjs +16 -0
- package/object/deep-freeze.d.ts +1 -0
- package/object/deep-freeze.js +15 -0
- package/object/deep-freeze.js.map +1 -0
- package/object/deepest.cjs +55 -0
- package/object/deepest.d.ts +47 -0
- package/object/deepest.js +54 -0
- package/object/deepest.js.map +1 -0
- package/object/index.cjs +7 -0
- package/object/index.d.ts +4 -0
- package/object/index.js +6 -0
- package/object/index.js.map +1 -0
- package/object/static/clone.cjs +4 -0
- package/object/static/clone.d.ts +43 -0
- package/object/static/clone.js +4 -0
- package/object/static/clone.js.map +1 -0
- package/object/static/compact.cjs +4 -0
- package/object/static/compact.d.ts +26 -0
- package/object/static/compact.js +4 -0
- package/object/static/compact.js.map +1 -0
- package/object/static/deep-freeze.cjs +4 -0
- package/object/static/deep-freeze.d.ts +6 -0
- package/object/static/deep-freeze.js +4 -0
- package/object/static/deep-freeze.js.map +1 -0
- package/object/static/deepest.cjs +4 -0
- package/object/static/deepest.d.ts +32 -0
- package/object/static/deepest.js +4 -0
- package/object/static/deepest.js.map +1 -0
- package/open-promise.cjs +115 -0
- package/open-promise.d.ts +80 -0
- package/open-promise.js +113 -0
- package/open-promise.js.map +1 -0
- package/package.json +667 -0
- package/promise/chain.cjs +12 -0
- package/promise/chain.d.ts +7 -0
- package/promise/chain.js +11 -0
- package/promise/chain.js.map +1 -0
- package/promise/index.cjs +10 -0
- package/promise/index.d.ts +7 -0
- package/promise/index.js +9 -0
- package/promise/index.js.map +1 -0
- package/promise/never.cjs +41 -0
- package/promise/never.d.ts +34 -0
- package/promise/never.js +39 -0
- package/promise/never.js.map +1 -0
- package/promise/promisify.cjs +12 -0
- package/promise/promisify.d.ts +7 -0
- package/promise/promisify.js +11 -0
- package/promise/promisify.js.map +1 -0
- package/promise/proto/revert.cjs +6 -0
- package/promise/proto/revert.d.ts +29 -0
- package/promise/proto/revert.js +6 -0
- package/promise/proto/revert.js.map +1 -0
- package/promise/proto/try-catch.cjs +7 -0
- package/promise/proto/try-catch.d.ts +30 -0
- package/promise/proto/try-catch.js +7 -0
- package/promise/proto/try-catch.js.map +1 -0
- package/promise/revert.cjs +43 -0
- package/promise/revert.d.ts +38 -0
- package/promise/revert.js +42 -0
- package/promise/revert.js.map +1 -0
- package/promise/static/chain.cjs +4 -0
- package/promise/static/chain.d.ts +10 -0
- package/promise/static/chain.js +4 -0
- package/promise/static/chain.js.map +1 -0
- package/promise/static/never.cjs +5 -0
- package/promise/static/never.d.ts +39 -0
- package/promise/static/never.js +5 -0
- package/promise/static/never.js.map +1 -0
- package/promise/static/timeout.cjs +4 -0
- package/promise/static/timeout.d.ts +21 -0
- package/promise/static/timeout.js +4 -0
- package/promise/static/timeout.js.map +1 -0
- package/promise/static/try-one-by-one.cjs +4 -0
- package/promise/static/try-one-by-one.d.ts +27 -0
- package/promise/static/try-one-by-one.js +4 -0
- package/promise/static/try-one-by-one.js.map +1 -0
- package/promise/timeout.cjs +104 -0
- package/promise/timeout.d.ts +109 -0
- package/promise/timeout.js +103 -0
- package/promise/timeout.js.map +1 -0
- package/promise/try-catch.cjs +12 -0
- package/promise/try-catch.d.ts +45 -0
- package/promise/try-catch.js +11 -0
- package/promise/try-catch.js.map +1 -0
- package/promise/try-one-by-one.cjs +59 -0
- package/promise/try-one-by-one.d.ts +42 -0
- package/promise/try-one-by-one.js +58 -0
- package/promise/try-one-by-one.js.map +1 -0
- package/set/proto/difference.cjs +6 -0
- package/set/proto/difference.d.ts +26 -0
- package/set/proto/difference.js +6 -0
- package/set/proto/difference.js.map +1 -0
- package/set/proto/intersection.cjs +6 -0
- package/set/proto/intersection.d.ts +26 -0
- package/set/proto/intersection.js +6 -0
- package/set/proto/intersection.js.map +1 -0
- package/smart-map.cjs +108 -0
- package/smart-map.d.ts +81 -0
- package/smart-map.js +106 -0
- package/smart-map.js.map +1 -0
- package/string/index.cjs +4 -0
- package/string/index.d.ts +1 -0
- package/string/index.js +3 -0
- package/string/index.js.map +1 -0
- package/string/limit.cjs +35 -0
- package/string/limit.d.ts +29 -0
- package/string/limit.js +34 -0
- package/string/limit.js.map +1 -0
- package/string/proto/limit.cjs +6 -0
- package/string/proto/limit.d.ts +30 -0
- package/string/proto/limit.js +6 -0
- package/string/proto/limit.js.map +1 -0
- package/time/days.cjs +21 -0
- package/time/days.d.ts +15 -0
- package/time/days.js +20 -0
- package/time/days.js.map +1 -0
- package/time/hours.cjs +21 -0
- package/time/hours.d.ts +15 -0
- package/time/hours.js +20 -0
- package/time/hours.js.map +1 -0
- package/time/index.cjs +10 -0
- package/time/index.d.ts +7 -0
- package/time/index.js +9 -0
- package/time/index.js.map +1 -0
- package/time/minutes.cjs +21 -0
- package/time/minutes.d.ts +15 -0
- package/time/minutes.js +20 -0
- package/time/minutes.js.map +1 -0
- package/time/months.cjs +21 -0
- package/time/months.d.ts +15 -0
- package/time/months.js +20 -0
- package/time/months.js.map +1 -0
- package/time/seconds.cjs +20 -0
- package/time/seconds.d.ts +15 -0
- package/time/seconds.js +19 -0
- package/time/seconds.js.map +1 -0
- package/time/weeks.cjs +21 -0
- package/time/weeks.d.ts +15 -0
- package/time/weeks.js +20 -0
- package/time/weeks.js.map +1 -0
- package/time/years.cjs +21 -0
- package/time/years.d.ts +15 -0
- package/time/years.js +20 -0
- package/time/years.js.map +1 -0
- package/unique-id.cjs +24 -0
- package/unique-id.d.ts +21 -0
- package/unique-id.js +23 -0
- package/unique-id.js.map +1 -0
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.timeout = void 0;
|
|
4
|
+
const open_promise_1 = require("@telperion/js-utils/open-promise");
|
|
5
|
+
// Defined here for documentation
|
|
6
|
+
function promiseTimeoutInitializer() {
|
|
7
|
+
const REFERANCE_CACHE = new WeakMap();
|
|
8
|
+
const KEY_CACHE = new Map();
|
|
9
|
+
const timeout = (time, value, key) => {
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
11
|
+
const openPromise = new open_promise_1.OpenPromise();
|
|
12
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
13
|
+
const id = setTimeout(() => {
|
|
14
|
+
openPromise.resolve(value);
|
|
15
|
+
}, time);
|
|
16
|
+
const cacheObject = { openPromise, id };
|
|
17
|
+
REFERANCE_CACHE.set(openPromise, cacheObject);
|
|
18
|
+
if (key) {
|
|
19
|
+
KEY_CACHE.set(key, cacheObject);
|
|
20
|
+
}
|
|
21
|
+
return openPromise;
|
|
22
|
+
};
|
|
23
|
+
Object.defineProperties(timeout, {
|
|
24
|
+
FINISHED_ALREADY: {
|
|
25
|
+
writable: false,
|
|
26
|
+
value: Symbol('Timeout finished already')
|
|
27
|
+
},
|
|
28
|
+
IDENTIFIER_NOT_FOUND: {
|
|
29
|
+
writable: false,
|
|
30
|
+
value: Symbol('Timeout not found via identifier')
|
|
31
|
+
},
|
|
32
|
+
TIMEOUT_CANCELLED: {
|
|
33
|
+
writable: false,
|
|
34
|
+
value: Symbol('Timeout cancelled')
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
timeout.cancel = async (identifier, error) => {
|
|
38
|
+
const { FINISHED_ALREADY, IDENTIFIER_NOT_FOUND, TIMEOUT_CANCELLED } = timeout;
|
|
39
|
+
if (typeof error === 'undefined') {
|
|
40
|
+
error = TIMEOUT_CANCELLED;
|
|
41
|
+
}
|
|
42
|
+
let cache;
|
|
43
|
+
if (typeof identifier === 'symbol') {
|
|
44
|
+
cache = KEY_CACHE.get(identifier);
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
cache = REFERANCE_CACHE.get(identifier);
|
|
48
|
+
}
|
|
49
|
+
if (!cache) {
|
|
50
|
+
throw IDENTIFIER_NOT_FOUND;
|
|
51
|
+
}
|
|
52
|
+
if (cache.openPromise.finished) {
|
|
53
|
+
throw FINISHED_ALREADY;
|
|
54
|
+
}
|
|
55
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
56
|
+
clearTimeout(cache.id);
|
|
57
|
+
cache.openPromise.reject(error);
|
|
58
|
+
if (typeof identifier === 'symbol') {
|
|
59
|
+
KEY_CACHE.delete(identifier);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
return timeout;
|
|
63
|
+
}
|
|
64
|
+
;
|
|
65
|
+
/**
|
|
66
|
+
* #### Promise Timeout
|
|
67
|
+
* Returns a promise which resolves after given time
|
|
68
|
+
*
|
|
69
|
+
* * * *
|
|
70
|
+
* Example:
|
|
71
|
+
* ```typescript
|
|
72
|
+
* import { timeout } from "@telperion/js-utils/promise";
|
|
73
|
+
*
|
|
74
|
+
* timeout(1000);
|
|
75
|
+
* .then(() => console.log("will be logged after a second"));
|
|
76
|
+
* ```
|
|
77
|
+
* Example with a resolve value:
|
|
78
|
+
* ```typescript
|
|
79
|
+
* import { timeout } from "@telperion/js-utils/promise";
|
|
80
|
+
*
|
|
81
|
+
* timeout(1000, "foo");
|
|
82
|
+
* .then(val => console.log("will log 'foo' after a second", val));
|
|
83
|
+
* ```
|
|
84
|
+
* Can be used in promise chaining:
|
|
85
|
+
* ```typescript
|
|
86
|
+
* import { timeout } from "@telperion/js-utils/promise";
|
|
87
|
+
*
|
|
88
|
+
* fetch("http://localhost:8080/anEndpoint") // Fetch something
|
|
89
|
+
* .then(val => timeout(1000, val)) // Wait a second after response
|
|
90
|
+
* .then(val => {
|
|
91
|
+
* ...do something else
|
|
92
|
+
* });
|
|
93
|
+
* ```
|
|
94
|
+
* Static usage example:
|
|
95
|
+
* ```typescript
|
|
96
|
+
* import "@telperion/js-utils/promise/static/timeout";
|
|
97
|
+
*
|
|
98
|
+
* Promise.timeout(1000);
|
|
99
|
+
* .then(() => console.log("will be logged after a second"));
|
|
100
|
+
* ```
|
|
101
|
+
* * * *
|
|
102
|
+
* @see PromiseTimeoutFunction#cancel for cancelling
|
|
103
|
+
*/
|
|
104
|
+
exports.timeout = promiseTimeoutInitializer();
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
export interface PromiseTimeoutFunction {
|
|
2
|
+
/**
|
|
3
|
+
* @see timeout
|
|
4
|
+
* @param time Time to resolve promise in miliseconds
|
|
5
|
+
* @param value Value to resolve promise with
|
|
6
|
+
* @param key Key to cancel the timeout if needed
|
|
7
|
+
*/
|
|
8
|
+
<T = void>(time: number, value?: T, key?: symbol): Promise<T>;
|
|
9
|
+
/**
|
|
10
|
+
* #### Promise Timeout Canceller
|
|
11
|
+
*
|
|
12
|
+
* * * *
|
|
13
|
+
* Usage via promise instance:
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import { timeout } from "@telperion/js-utils/promise";
|
|
16
|
+
*
|
|
17
|
+
* const timeout = timeout(1000);
|
|
18
|
+
*
|
|
19
|
+
* timeout
|
|
20
|
+
* .then(() => console.log("this won't be logged"))
|
|
21
|
+
* .catch(() => console.log("this will be logged, because timer has been cancelled"));
|
|
22
|
+
*
|
|
23
|
+
* timeout.cancel(timeout);
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* Usage via key
|
|
27
|
+
* ```typescript
|
|
28
|
+
* import { timeout } from "@telperion/js-utils/promise";
|
|
29
|
+
*
|
|
30
|
+
* const key = Symbol();
|
|
31
|
+
*
|
|
32
|
+
* timeout(1000, null, key)
|
|
33
|
+
* .then(() => console.log("this won't be logged"))
|
|
34
|
+
* .catch(() => console.log("this will be logged, because timer has been cancelled"));
|
|
35
|
+
*
|
|
36
|
+
* timeout.cancel(key);
|
|
37
|
+
* ```
|
|
38
|
+
* Static usage example:
|
|
39
|
+
* ```typescript
|
|
40
|
+
* import "@telperion/js-utils/promise/static/timeout";
|
|
41
|
+
*
|
|
42
|
+
* const timeout = Promise.timeout(1000);
|
|
43
|
+
*
|
|
44
|
+
* timeout
|
|
45
|
+
* .then(() => console.log("this won't be logged"))
|
|
46
|
+
* .catch(() => console.log("this will be logged, because timer has been cancelled"));
|
|
47
|
+
*
|
|
48
|
+
* Promise.timeout.cancel(timeout);
|
|
49
|
+
* ```
|
|
50
|
+
* * * *
|
|
51
|
+
* @param identifier The identifier of the promise to cancel
|
|
52
|
+
* @param error The error which will be throwed by the cancelled promise
|
|
53
|
+
* @returns A promise which resolves if cancelling process is successfull, rejects otherwise
|
|
54
|
+
*/
|
|
55
|
+
cancel<T = unknown>(identifier: Promise<T> | symbol, error?: unknown): Promise<void>;
|
|
56
|
+
/**
|
|
57
|
+
* Will be throwed in cancelling promise when the timer has already finished or cancelled
|
|
58
|
+
*/
|
|
59
|
+
readonly FINISHED_ALREADY: unique symbol;
|
|
60
|
+
/**
|
|
61
|
+
* Will be throwed in cancelling promise when the timer has not found via identifier
|
|
62
|
+
*/
|
|
63
|
+
readonly IDENTIFIER_NOT_FOUND: unique symbol;
|
|
64
|
+
/**
|
|
65
|
+
* The default timeout cancelling rejection error
|
|
66
|
+
* Will be throwed when the timer has cancelled
|
|
67
|
+
*/
|
|
68
|
+
readonly TIMEOUT_CANCELLED: unique symbol;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* #### Promise Timeout
|
|
72
|
+
* Returns a promise which resolves after given time
|
|
73
|
+
*
|
|
74
|
+
* * * *
|
|
75
|
+
* Example:
|
|
76
|
+
* ```typescript
|
|
77
|
+
* import { timeout } from "@telperion/js-utils/promise";
|
|
78
|
+
*
|
|
79
|
+
* timeout(1000);
|
|
80
|
+
* .then(() => console.log("will be logged after a second"));
|
|
81
|
+
* ```
|
|
82
|
+
* Example with a resolve value:
|
|
83
|
+
* ```typescript
|
|
84
|
+
* import { timeout } from "@telperion/js-utils/promise";
|
|
85
|
+
*
|
|
86
|
+
* timeout(1000, "foo");
|
|
87
|
+
* .then(val => console.log("will log 'foo' after a second", val));
|
|
88
|
+
* ```
|
|
89
|
+
* Can be used in promise chaining:
|
|
90
|
+
* ```typescript
|
|
91
|
+
* import { timeout } from "@telperion/js-utils/promise";
|
|
92
|
+
*
|
|
93
|
+
* fetch("http://localhost:8080/anEndpoint") // Fetch something
|
|
94
|
+
* .then(val => timeout(1000, val)) // Wait a second after response
|
|
95
|
+
* .then(val => {
|
|
96
|
+
* ...do something else
|
|
97
|
+
* });
|
|
98
|
+
* ```
|
|
99
|
+
* Static usage example:
|
|
100
|
+
* ```typescript
|
|
101
|
+
* import "@telperion/js-utils/promise/static/timeout";
|
|
102
|
+
*
|
|
103
|
+
* Promise.timeout(1000);
|
|
104
|
+
* .then(() => console.log("will be logged after a second"));
|
|
105
|
+
* ```
|
|
106
|
+
* * * *
|
|
107
|
+
* @see PromiseTimeoutFunction#cancel for cancelling
|
|
108
|
+
*/
|
|
109
|
+
export declare const timeout: PromiseTimeoutFunction;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { OpenPromise } from '@telperion/js-utils/open-promise';
|
|
2
|
+
// Defined here for documentation
|
|
3
|
+
function promiseTimeoutInitializer() {
|
|
4
|
+
const REFERANCE_CACHE = new WeakMap();
|
|
5
|
+
const KEY_CACHE = new Map();
|
|
6
|
+
const timeout = (time, value, key) => {
|
|
7
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
8
|
+
const openPromise = new OpenPromise();
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
10
|
+
const id = setTimeout(() => {
|
|
11
|
+
openPromise.resolve(value);
|
|
12
|
+
}, time);
|
|
13
|
+
const cacheObject = { openPromise, id };
|
|
14
|
+
REFERANCE_CACHE.set(openPromise, cacheObject);
|
|
15
|
+
if (key) {
|
|
16
|
+
KEY_CACHE.set(key, cacheObject);
|
|
17
|
+
}
|
|
18
|
+
return openPromise;
|
|
19
|
+
};
|
|
20
|
+
Object.defineProperties(timeout, {
|
|
21
|
+
FINISHED_ALREADY: {
|
|
22
|
+
writable: false,
|
|
23
|
+
value: Symbol('Timeout finished already')
|
|
24
|
+
},
|
|
25
|
+
IDENTIFIER_NOT_FOUND: {
|
|
26
|
+
writable: false,
|
|
27
|
+
value: Symbol('Timeout not found via identifier')
|
|
28
|
+
},
|
|
29
|
+
TIMEOUT_CANCELLED: {
|
|
30
|
+
writable: false,
|
|
31
|
+
value: Symbol('Timeout cancelled')
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
timeout.cancel = async (identifier, error) => {
|
|
35
|
+
const { FINISHED_ALREADY, IDENTIFIER_NOT_FOUND, TIMEOUT_CANCELLED } = timeout;
|
|
36
|
+
if (typeof error === 'undefined') {
|
|
37
|
+
error = TIMEOUT_CANCELLED;
|
|
38
|
+
}
|
|
39
|
+
let cache;
|
|
40
|
+
if (typeof identifier === 'symbol') {
|
|
41
|
+
cache = KEY_CACHE.get(identifier);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
cache = REFERANCE_CACHE.get(identifier);
|
|
45
|
+
}
|
|
46
|
+
if (!cache) {
|
|
47
|
+
throw IDENTIFIER_NOT_FOUND;
|
|
48
|
+
}
|
|
49
|
+
if (cache.openPromise.finished) {
|
|
50
|
+
throw FINISHED_ALREADY;
|
|
51
|
+
}
|
|
52
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
53
|
+
clearTimeout(cache.id);
|
|
54
|
+
cache.openPromise.reject(error);
|
|
55
|
+
if (typeof identifier === 'symbol') {
|
|
56
|
+
KEY_CACHE.delete(identifier);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
return timeout;
|
|
60
|
+
}
|
|
61
|
+
;
|
|
62
|
+
/**
|
|
63
|
+
* #### Promise Timeout
|
|
64
|
+
* Returns a promise which resolves after given time
|
|
65
|
+
*
|
|
66
|
+
* * * *
|
|
67
|
+
* Example:
|
|
68
|
+
* ```typescript
|
|
69
|
+
* import { timeout } from "@telperion/js-utils/promise";
|
|
70
|
+
*
|
|
71
|
+
* timeout(1000);
|
|
72
|
+
* .then(() => console.log("will be logged after a second"));
|
|
73
|
+
* ```
|
|
74
|
+
* Example with a resolve value:
|
|
75
|
+
* ```typescript
|
|
76
|
+
* import { timeout } from "@telperion/js-utils/promise";
|
|
77
|
+
*
|
|
78
|
+
* timeout(1000, "foo");
|
|
79
|
+
* .then(val => console.log("will log 'foo' after a second", val));
|
|
80
|
+
* ```
|
|
81
|
+
* Can be used in promise chaining:
|
|
82
|
+
* ```typescript
|
|
83
|
+
* import { timeout } from "@telperion/js-utils/promise";
|
|
84
|
+
*
|
|
85
|
+
* fetch("http://localhost:8080/anEndpoint") // Fetch something
|
|
86
|
+
* .then(val => timeout(1000, val)) // Wait a second after response
|
|
87
|
+
* .then(val => {
|
|
88
|
+
* ...do something else
|
|
89
|
+
* });
|
|
90
|
+
* ```
|
|
91
|
+
* Static usage example:
|
|
92
|
+
* ```typescript
|
|
93
|
+
* import "@telperion/js-utils/promise/static/timeout";
|
|
94
|
+
*
|
|
95
|
+
* Promise.timeout(1000);
|
|
96
|
+
* .then(() => console.log("will be logged after a second"));
|
|
97
|
+
* ```
|
|
98
|
+
* * * *
|
|
99
|
+
* @see PromiseTimeoutFunction#cancel for cancelling
|
|
100
|
+
*/
|
|
101
|
+
export const timeout = promiseTimeoutInitializer();
|
|
102
|
+
|
|
103
|
+
//# sourceMappingURL=timeout.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/js-utils/src/promise/timeout.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,kCAAkC,CAAC;AAiF/D,iCAAiC;AACjC,SAAS,yBAAyB;IAChC,MAAM,eAAe,GAAG,IAAI,OAAO,EAAkC,CAAC;IACtE,MAAM,SAAS,GAAG,IAAI,GAAG,EAAwB,CAAC;IAClD,MAAM,OAAO,GAAG,CAAW,IAAY,EAAE,KAAS,EAAE,GAAY,EAAc,EAAE;QAC9E,8DAA8D;QAC9D,MAAM,WAAW,GAAG,IAAI,WAAW,EAAO,CAAC;QAC3C,8DAA8D;QAC9D,MAAM,EAAE,GAAQ,UAAU,CAAC,GAAG,EAAE;YAC9B,WAAW,CAAC,OAAO,CAAC,KAAM,CAAC,CAAC;QAC9B,CAAC,EAAE,IAAI,CAAC,CAAC;QACT,MAAM,WAAW,GAAiB,EAAE,WAAW,EAAE,EAAE,EAAE,CAAC;QAEtD,eAAe,CAAC,GAAG,CAAC,WAAW,EAAE,WAAW,CAAC,CAAC;QAE9C,IAAI,GAAG,EAAE,CAAC;YACR,SAAS,CAAC,GAAG,CAAC,GAAG,EAAE,WAAW,CAAC,CAAC;QAClC,CAAC;QAED,OAAO,WAAW,CAAC;IACrB,CAAC,CAAC;IAEF,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE;QAC/B,gBAAgB,EAAE;YAChB,QAAQ,EAAE,KAAK;YACf,KAAK,EAAE,MAAM,CAAC,0BAA0B,CAAC;SAC1C;QACD,oBAAoB,EAAE;YACpB,QAAQ,EAAE,KAAK;YACf,KAAK,EAAE,MAAM,CAAC,kCAAkC,CAAC;SAClD;QACD,iBAAiB,EAAE;YACjB,QAAQ,EAAE,KAAK;YACf,KAAK,EAAE,MAAM,CAAC,mBAAmB,CAAC;SACnC;KACF,CAAC,CAAC;IAEsB,OAAQ,CAAC,MAAM,GAAG,KAAK,EAAE,UAAqC,EAAE,KAAe,EAAE,EAAE;QAC1G,MAAM,EAAE,gBAAgB,EAAE,oBAAoB,EAAE,iBAAiB,EAAE,GAA2B,OAAO,CAAC;QAEtG,IAAI,OAAO,KAAK,KAAK,WAAW,EAAE,CAAC;YACjC,KAAK,GAAG,iBAAiB,CAAC;QAC5B,CAAC;QAED,IAAI,KAAmB,CAAC;QAExB,IAAI,OAAO,UAAU,KAAK,QAAQ,EAAE,CAAC;YACnC,KAAK,GAAG,SAAS,CAAC,GAAG,CAAC,UAAU,CAAE,CAAC;QACrC,CAAC;aAAM,CAAC;YACN,KAAK,GAAG,eAAe,CAAC,GAAG,CAAC,UAAU,CAAE,CAAC;QAC3C,CAAC;QAED,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,MAAM,oBAAoB,CAAC;QAC7B,CAAC;QAED,IAAI,KAAK,CAAC,WAAW,CAAC,QAAQ,EAAE,CAAC;YAC/B,MAAM,gBAAgB,CAAC;QACzB,CAAC;QAED,8DAA8D;QAC9D,YAAY,CAAC,KAAK,CAAC,EAAS,CAAC,CAAC;QAC9B,KAAK,CAAC,WAAW,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAEhC,IAAI,OAAO,UAAU,KAAK,QAAQ,EAAE,CAAC;YACnC,SAAS,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;QAC/B,CAAC;IACH,CAAC,CAAC;IAEF,OAAO,OAAiC,CAAC;AAC3C,CAAC;AAAA,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG,yBAAyB,EAAE,CAAC","file":"timeout.js","sourcesContent":["import { OpenPromise } from '@telperion/js-utils/open-promise';\n\nexport interface PromiseTimeoutFunction {\n /**\n * @see timeout\n * @param time Time to resolve promise in miliseconds\n * @param value Value to resolve promise with\n * @param key Key to cancel the timeout if needed\n */\n <T = void>(time: number, value?: T, key?: symbol): Promise<T>;\n\n /**\n * #### Promise Timeout Canceller\n *\n * * * *\n * Usage via promise instance:\n * ```typescript\n * import { timeout } from \"@telperion/js-utils/promise\";\n *\n * const timeout = timeout(1000);\n *\n * timeout\n * .then(() => console.log(\"this won't be logged\"))\n * .catch(() => console.log(\"this will be logged, because timer has been cancelled\"));\n *\n * timeout.cancel(timeout);\n * ```\n *\n * Usage via key\n * ```typescript\n * import { timeout } from \"@telperion/js-utils/promise\";\n *\n * const key = Symbol();\n *\n * timeout(1000, null, key)\n * .then(() => console.log(\"this won't be logged\"))\n * .catch(() => console.log(\"this will be logged, because timer has been cancelled\"));\n *\n * timeout.cancel(key);\n * ```\n * Static usage example:\n * ```typescript\n * import \"@telperion/js-utils/promise/static/timeout\";\n *\n * const timeout = Promise.timeout(1000);\n *\n * timeout\n * .then(() => console.log(\"this won't be logged\"))\n * .catch(() => console.log(\"this will be logged, because timer has been cancelled\"));\n *\n * Promise.timeout.cancel(timeout);\n * ```\n * * * *\n * @param identifier The identifier of the promise to cancel\n * @param error The error which will be throwed by the cancelled promise\n * @returns A promise which resolves if cancelling process is successfull, rejects otherwise\n */\n cancel<T = unknown>(identifier: Promise<T> | symbol, error?: unknown): Promise<void>;\n\n /**\n * Will be throwed in cancelling promise when the timer has already finished or cancelled\n */\n readonly FINISHED_ALREADY: unique symbol;\n\n /**\n * Will be throwed in cancelling promise when the timer has not found via identifier\n */\n readonly IDENTIFIER_NOT_FOUND: unique symbol;\n\n /**\n * The default timeout cancelling rejection error\n * Will be throwed when the timer has cancelled\n */\n readonly TIMEOUT_CANCELLED: unique symbol;\n}\n\ninterface TimeoutCache {\n id: number;\n openPromise: OpenPromise;\n}\n\n// Defined here for documentation\nfunction promiseTimeoutInitializer() {\n const REFERANCE_CACHE = new WeakMap<Promise<unknown>, TimeoutCache>();\n const KEY_CACHE = new Map<symbol, TimeoutCache>();\n const timeout = <T = void>(time: number, value?: T, key?: symbol): Promise<T> => {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const openPromise = new OpenPromise<any>();\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const id: any = setTimeout(() => {\n openPromise.resolve(value!);\n }, time);\n const cacheObject: TimeoutCache = { openPromise, id };\n\n REFERANCE_CACHE.set(openPromise, cacheObject);\n\n if (key) {\n KEY_CACHE.set(key, cacheObject);\n }\n\n return openPromise;\n };\n\n Object.defineProperties(timeout, {\n FINISHED_ALREADY: {\n writable: false,\n value: Symbol('Timeout finished already')\n },\n IDENTIFIER_NOT_FOUND: {\n writable: false,\n value: Symbol('Timeout not found via identifier')\n },\n TIMEOUT_CANCELLED: {\n writable: false,\n value: Symbol('Timeout cancelled')\n }\n });\n\n (<PromiseTimeoutFunction>timeout).cancel = async (identifier: Promise<unknown> | symbol, error?: unknown) => {\n const { FINISHED_ALREADY, IDENTIFIER_NOT_FOUND, TIMEOUT_CANCELLED } = <PromiseTimeoutFunction>timeout;\n\n if (typeof error === 'undefined') {\n error = TIMEOUT_CANCELLED;\n }\n\n let cache: TimeoutCache;\n\n if (typeof identifier === 'symbol') {\n cache = KEY_CACHE.get(identifier)!;\n } else {\n cache = REFERANCE_CACHE.get(identifier)!;\n }\n\n if (!cache) {\n throw IDENTIFIER_NOT_FOUND;\n }\n\n if (cache.openPromise.finished) {\n throw FINISHED_ALREADY;\n }\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n clearTimeout(cache.id as any);\n cache.openPromise.reject(error);\n\n if (typeof identifier === 'symbol') {\n KEY_CACHE.delete(identifier);\n }\n };\n\n return timeout as PromiseTimeoutFunction;\n};\n\n/**\n * #### Promise Timeout\n * Returns a promise which resolves after given time\n *\n * * * *\n * Example:\n * ```typescript\n * import { timeout } from \"@telperion/js-utils/promise\";\n *\n * timeout(1000);\n * .then(() => console.log(\"will be logged after a second\"));\n * ```\n * Example with a resolve value:\n * ```typescript\n * import { timeout } from \"@telperion/js-utils/promise\";\n *\n * timeout(1000, \"foo\");\n * .then(val => console.log(\"will log 'foo' after a second\", val));\n * ```\n * Can be used in promise chaining:\n * ```typescript\n * import { timeout } from \"@telperion/js-utils/promise\";\n *\n * fetch(\"http://localhost:8080/anEndpoint\") // Fetch something\n * .then(val => timeout(1000, val)) // Wait a second after response\n * .then(val => {\n * ...do something else\n * });\n * ```\n * Static usage example:\n * ```typescript\n * import \"@telperion/js-utils/promise/static/timeout\";\n *\n * Promise.timeout(1000);\n * .then(() => console.log(\"will be logged after a second\"));\n * ```\n * * * *\n * @see PromiseTimeoutFunction#cancel for cancelling\n */\nexport const timeout = promiseTimeoutInitializer();\n"]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.tryCatch = tryCatch;
|
|
4
|
+
async function tryCatch(promise, defaultResult) {
|
|
5
|
+
try {
|
|
6
|
+
const result = await promise;
|
|
7
|
+
return [null, result];
|
|
8
|
+
}
|
|
9
|
+
catch (error) {
|
|
10
|
+
return [error, defaultResult ?? null];
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #### Try Catch
|
|
3
|
+
*
|
|
4
|
+
* Merges result and error in the same callback
|
|
5
|
+
*
|
|
6
|
+
* * * *
|
|
7
|
+
* Example:
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { tryCatch } from "@telperion/js-utils/promise";
|
|
10
|
+
*
|
|
11
|
+
* async function fooFunction() {
|
|
12
|
+
* const promise = anAsyncCall();
|
|
13
|
+
* const [error, result] = await tryCatch(promise);
|
|
14
|
+
*
|
|
15
|
+
* if (error) {
|
|
16
|
+
* // handle error
|
|
17
|
+
* }
|
|
18
|
+
*
|
|
19
|
+
* // do stuff
|
|
20
|
+
* }
|
|
21
|
+
*
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* Prototype Example:
|
|
25
|
+
* ```typescript
|
|
26
|
+
* import "@telperion/js-utils/promise/proto/try-catch";
|
|
27
|
+
*
|
|
28
|
+
* async function fooFunction() {
|
|
29
|
+
* const [error, result] = await anAsyncCall().tryCatch();
|
|
30
|
+
*
|
|
31
|
+
* if (error) {
|
|
32
|
+
* // handle error
|
|
33
|
+
* }
|
|
34
|
+
*
|
|
35
|
+
* // do stuff
|
|
36
|
+
* }
|
|
37
|
+
*
|
|
38
|
+
* ```
|
|
39
|
+
* * * *
|
|
40
|
+
* @param promise Promise to try
|
|
41
|
+
* @param defaultResult Setting this will put the value into the result field when the promise throws error
|
|
42
|
+
* @returns Error and result array
|
|
43
|
+
*/
|
|
44
|
+
export declare function tryCatch<T, E = unknown>(promise: Promise<T>): Promise<[E | null, T | null]>;
|
|
45
|
+
export declare function tryCatch<T, E = unknown>(promise: Promise<T>, defaultResult: T): Promise<[E | null, T]>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/js-utils/src/promise/try-catch.ts"],"names":[],"mappings":"AA6CA,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAiB,OAAmB,EAAE,aAAiB;IACnF,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC;QAE7B,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IACxB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,CAAC,KAAU,EAAE,aAAa,IAAI,IAAI,CAAC,CAAC;IAC7C,CAAC;AACH,CAAC","file":"try-catch.js","sourcesContent":["/**\n * #### Try Catch\n *\n * Merges result and error in the same callback\n *\n * * * *\n * Example:\n * ```typescript\n * import { tryCatch } from \"@telperion/js-utils/promise\";\n *\n * async function fooFunction() {\n * const promise = anAsyncCall();\n * const [error, result] = await tryCatch(promise);\n *\n * if (error) {\n * // handle error\n * }\n *\n * // do stuff\n * }\n *\n * ```\n *\n * Prototype Example:\n * ```typescript\n * import \"@telperion/js-utils/promise/proto/try-catch\";\n *\n * async function fooFunction() {\n * const [error, result] = await anAsyncCall().tryCatch();\n *\n * if (error) {\n * // handle error\n * }\n *\n * // do stuff\n * }\n *\n * ```\n * * * *\n * @param promise Promise to try\n * @param defaultResult Setting this will put the value into the result field when the promise throws error\n * @returns Error and result array\n */\nexport async function tryCatch<T, E = unknown>(promise: Promise<T>): Promise<[E | null, T | null]>\nexport async function tryCatch<T, E = unknown>(promise: Promise<T>, defaultResult: T): Promise<[E | null, T]>\nexport async function tryCatch<T, E = unknown>(promise: Promise<T>, defaultResult?: T): Promise<[E | null, T | null]> {\n try {\n const result = await promise;\n\n return [null, result];\n } catch (error) {\n return [error as E, defaultResult ?? null];\n }\n}\n"]}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.tryOneByOne = tryOneByOne;
|
|
4
|
+
/**
|
|
5
|
+
* #### Try One By One
|
|
6
|
+
*
|
|
7
|
+
* Tries a set of promises one by one with given order. Breaks the call when a promise resolved. Otherwise keeps trying incoming promises until the list is finished.
|
|
8
|
+
*
|
|
9
|
+
* * * *
|
|
10
|
+
* Example:
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { tryOneByOne } from "@telperion/js-utils/promise";
|
|
13
|
+
*
|
|
14
|
+
* async function fooFunction() {
|
|
15
|
+
* const foo = await tryOneByOne([
|
|
16
|
+
* () => someCall(),
|
|
17
|
+
* (err) => anotherCall(),
|
|
18
|
+
* (err) => fooPromise()
|
|
19
|
+
* ]);
|
|
20
|
+
*
|
|
21
|
+
* // do stuff
|
|
22
|
+
* }
|
|
23
|
+
*
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* Static Example:
|
|
27
|
+
* ```typescript
|
|
28
|
+
* import "@telperion/js-utils/promise/static/try-one-by-one";
|
|
29
|
+
*
|
|
30
|
+
* async function fooFunction() {
|
|
31
|
+
* const foo = await Promise.tryOneByOne([
|
|
32
|
+
* () => someCall(),
|
|
33
|
+
* (err) => anotherCall(),
|
|
34
|
+
* (err) => fooPromise()
|
|
35
|
+
* ]);
|
|
36
|
+
*
|
|
37
|
+
* // do stuff
|
|
38
|
+
* }
|
|
39
|
+
*
|
|
40
|
+
* ```
|
|
41
|
+
* * * *
|
|
42
|
+
* @param promises List of promises to try one by one with the order
|
|
43
|
+
* @returns
|
|
44
|
+
*/
|
|
45
|
+
async function tryOneByOne(promises) {
|
|
46
|
+
return await tryPromises([...promises], null);
|
|
47
|
+
}
|
|
48
|
+
async function tryPromises(promises, lastError) {
|
|
49
|
+
const promiseFn = promises.shift();
|
|
50
|
+
if (!promiseFn)
|
|
51
|
+
throw lastError;
|
|
52
|
+
try {
|
|
53
|
+
const promise = typeof promiseFn === "function" ? promiseFn(lastError) : promiseFn;
|
|
54
|
+
return await promise;
|
|
55
|
+
}
|
|
56
|
+
catch (err) {
|
|
57
|
+
return tryPromises(promises, err);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #### Try One By One
|
|
3
|
+
*
|
|
4
|
+
* Tries a set of promises one by one with given order. Breaks the call when a promise resolved. Otherwise keeps trying incoming promises until the list is finished.
|
|
5
|
+
*
|
|
6
|
+
* * * *
|
|
7
|
+
* Example:
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { tryOneByOne } from "@telperion/js-utils/promise";
|
|
10
|
+
*
|
|
11
|
+
* async function fooFunction() {
|
|
12
|
+
* const foo = await tryOneByOne([
|
|
13
|
+
* () => someCall(),
|
|
14
|
+
* (err) => anotherCall(),
|
|
15
|
+
* (err) => fooPromise()
|
|
16
|
+
* ]);
|
|
17
|
+
*
|
|
18
|
+
* // do stuff
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* Static Example:
|
|
24
|
+
* ```typescript
|
|
25
|
+
* import "@telperion/js-utils/promise/static/try-one-by-one";
|
|
26
|
+
*
|
|
27
|
+
* async function fooFunction() {
|
|
28
|
+
* const foo = await Promise.tryOneByOne([
|
|
29
|
+
* () => someCall(),
|
|
30
|
+
* (err) => anotherCall(),
|
|
31
|
+
* (err) => fooPromise()
|
|
32
|
+
* ]);
|
|
33
|
+
*
|
|
34
|
+
* // do stuff
|
|
35
|
+
* }
|
|
36
|
+
*
|
|
37
|
+
* ```
|
|
38
|
+
* * * *
|
|
39
|
+
* @param promises List of promises to try one by one with the order
|
|
40
|
+
* @returns
|
|
41
|
+
*/
|
|
42
|
+
export declare function tryOneByOne<T>(promises: Array<Promise<T> | ((lastError: unknown) => Promise<T>)>): Promise<T>;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #### Try One By One
|
|
3
|
+
*
|
|
4
|
+
* Tries a set of promises one by one with given order. Breaks the call when a promise resolved. Otherwise keeps trying incoming promises until the list is finished.
|
|
5
|
+
*
|
|
6
|
+
* * * *
|
|
7
|
+
* Example:
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { tryOneByOne } from "@telperion/js-utils/promise";
|
|
10
|
+
*
|
|
11
|
+
* async function fooFunction() {
|
|
12
|
+
* const foo = await tryOneByOne([
|
|
13
|
+
* () => someCall(),
|
|
14
|
+
* (err) => anotherCall(),
|
|
15
|
+
* (err) => fooPromise()
|
|
16
|
+
* ]);
|
|
17
|
+
*
|
|
18
|
+
* // do stuff
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* Static Example:
|
|
24
|
+
* ```typescript
|
|
25
|
+
* import "@telperion/js-utils/promise/static/try-one-by-one";
|
|
26
|
+
*
|
|
27
|
+
* async function fooFunction() {
|
|
28
|
+
* const foo = await Promise.tryOneByOne([
|
|
29
|
+
* () => someCall(),
|
|
30
|
+
* (err) => anotherCall(),
|
|
31
|
+
* (err) => fooPromise()
|
|
32
|
+
* ]);
|
|
33
|
+
*
|
|
34
|
+
* // do stuff
|
|
35
|
+
* }
|
|
36
|
+
*
|
|
37
|
+
* ```
|
|
38
|
+
* * * *
|
|
39
|
+
* @param promises List of promises to try one by one with the order
|
|
40
|
+
* @returns
|
|
41
|
+
*/
|
|
42
|
+
export async function tryOneByOne(promises) {
|
|
43
|
+
return await tryPromises([...promises], null);
|
|
44
|
+
}
|
|
45
|
+
async function tryPromises(promises, lastError) {
|
|
46
|
+
const promiseFn = promises.shift();
|
|
47
|
+
if (!promiseFn)
|
|
48
|
+
throw lastError;
|
|
49
|
+
try {
|
|
50
|
+
const promise = typeof promiseFn === "function" ? promiseFn(lastError) : promiseFn;
|
|
51
|
+
return await promise;
|
|
52
|
+
}
|
|
53
|
+
catch (err) {
|
|
54
|
+
return tryPromises(promises, err);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
//# sourceMappingURL=try-one-by-one.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/js-utils/src/promise/try-one-by-one.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAAI,QAAkE;IACpG,OAAO,MAAM,WAAW,CAAC,CAAC,GAAG,QAAQ,CAAC,EAAE,IAAI,CAAC,CAAC;AACjD,CAAC;AAED,KAAK,UAAU,WAAW,CAAI,QAAkE,EAAE,SAAkB;IAClH,MAAM,SAAS,GAAG,QAAQ,CAAC,KAAK,EAAE,CAAC;IAEnC,IAAI,CAAC,SAAS;QAAE,MAAM,SAAS,CAAC;IAEhC,IAAI,CAAC;QACH,MAAM,OAAO,GAAG,OAAO,SAAS,KAAK,UAAU,CAAC,CAAC,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;QAEnF,OAAO,MAAM,OAAO,CAAC;IACvB,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,OAAO,WAAW,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC;IACpC,CAAC;AACH,CAAC","file":"try-one-by-one.js","sourcesContent":["/**\n * #### Try One By One\n *\n * Tries a set of promises one by one with given order. Breaks the call when a promise resolved. Otherwise keeps trying incoming promises until the list is finished.\n *\n * * * *\n * Example:\n * ```typescript\n * import { tryOneByOne } from \"@telperion/js-utils/promise\";\n *\n * async function fooFunction() {\n * const foo = await tryOneByOne([\n * () => someCall(),\n * (err) => anotherCall(),\n * (err) => fooPromise()\n * ]);\n *\n * // do stuff\n * }\n *\n * ```\n *\n * Static Example:\n * ```typescript\n * import \"@telperion/js-utils/promise/static/try-one-by-one\";\n *\n * async function fooFunction() {\n * const foo = await Promise.tryOneByOne([\n * () => someCall(),\n * (err) => anotherCall(),\n * (err) => fooPromise()\n * ]);\n *\n * // do stuff\n * }\n *\n * ```\n * * * *\n * @param promises List of promises to try one by one with the order\n * @returns\n */\nexport async function tryOneByOne<T>(promises: Array<Promise<T> | ((lastError: unknown) => Promise<T>)>): Promise<T> {\n return await tryPromises([...promises], null);\n}\n\nasync function tryPromises<T>(promises: Array<Promise<T> | ((lastError: unknown) => Promise<T>)>, lastError: unknown): Promise<T> {\n const promiseFn = promises.shift();\n\n if (!promiseFn) throw lastError;\n\n try {\n const promise = typeof promiseFn === \"function\" ? promiseFn(lastError) : promiseFn;\n\n return await promise;\n } catch (err) {\n return tryPromises(promises, err);\n }\n}\n"]}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const difference_1 = require("@telperion/js-utils/array/difference");
|
|
4
|
+
Set.prototype.getDifference = function (substraction, allDiff = false) {
|
|
5
|
+
return (0, difference_1.difference)(this, substraction, allDiff);
|
|
6
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Substraction } from '@telperion/js-utils/array/difference';
|
|
2
|
+
declare global {
|
|
3
|
+
export interface Set<T> {
|
|
4
|
+
/**
|
|
5
|
+
* #### Difference
|
|
6
|
+
*
|
|
7
|
+
* Gets the difference of the sets
|
|
8
|
+
*
|
|
9
|
+
* * * *
|
|
10
|
+
* Example:
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import "@telperion/js-utils/set/proto/difference";
|
|
13
|
+
*
|
|
14
|
+
* const base = new Set(["a", "b", "c", "d"]);
|
|
15
|
+
*
|
|
16
|
+
* base.getDifference(["a", "b"]); // Set(["c", "d"])
|
|
17
|
+
* ```
|
|
18
|
+
* * * *
|
|
19
|
+
* @param base Base Set
|
|
20
|
+
* @param substraction Set or Array to remove its values from the base
|
|
21
|
+
* @param allDiff By default all the same items encountered in substraction will be removed, set this argument as true to get real difference
|
|
22
|
+
* @returns Difference of base from substraction
|
|
23
|
+
*/
|
|
24
|
+
getDifference(substraction: Substraction, allDiff?: boolean): Set<T>;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/js-utils/src/set/proto/difference.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAgB,MAAM,sCAAsC,CAAC;AA4BhF,GAAG,CAAC,SAAS,CAAC,aAAa,GAAG,UAA0B,YAA0B,EAAE,OAAO,GAAG,KAAK;IACjG,OAAO,UAAU,CAAC,IAAI,EAAE,YAAY,EAAE,OAAO,CAAC,CAAC;AACjD,CAAC,CAAC","file":"difference.js","sourcesContent":["import { difference, Substraction } from '@telperion/js-utils/array/difference';\n\ndeclare global {\n export interface Set<T> {\n /**\n * #### Difference\n *\n * Gets the difference of the sets\n *\n * * * *\n * Example:\n * ```typescript\n * import \"@telperion/js-utils/set/proto/difference\";\n *\n * const base = new Set([\"a\", \"b\", \"c\", \"d\"]);\n *\n * base.getDifference([\"a\", \"b\"]); // Set([\"c\", \"d\"])\n * ```\n * * * *\n * @param base Base Set\n * @param substraction Set or Array to remove its values from the base\n * @param allDiff By default all the same items encountered in substraction will be removed, set this argument as true to get real difference\n * @returns Difference of base from substraction\n */\n getDifference(substraction: Substraction, allDiff?: boolean): Set<T>;\n }\n}\n\nSet.prototype.getDifference = function<T>(this: Set<T>, substraction: Substraction, allDiff = false) {\n return difference(this, substraction, allDiff);\n};\n"]}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const intersection_1 = require("@telperion/js-utils/array/intersection");
|
|
4
|
+
Set.prototype.getIntersection = function (inclusion, allEquals = true) {
|
|
5
|
+
return (0, intersection_1.intersection)(this, inclusion, allEquals);
|
|
6
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Inclusion } from '@telperion/js-utils/array/intersection';
|
|
2
|
+
declare global {
|
|
3
|
+
export interface Set<T> {
|
|
4
|
+
/**
|
|
5
|
+
* #### Intersection
|
|
6
|
+
*
|
|
7
|
+
* Gets the intersection of the two arrays or sets
|
|
8
|
+
*
|
|
9
|
+
* * * *
|
|
10
|
+
* Example:
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import "@telperion/js-utils/set/proto/intersection";
|
|
13
|
+
*
|
|
14
|
+
* const base = new Set(["a", "b", "c", "d"]);
|
|
15
|
+
*
|
|
16
|
+
* base.getIntersection(["a", "b"]); // Set(["a", "b"])
|
|
17
|
+
* ```
|
|
18
|
+
* * * *
|
|
19
|
+
* @param base Base Set
|
|
20
|
+
* @param inclusion Set or Array to include its values
|
|
21
|
+
* @param allEquals By default all the same items encountered in the inclusion will be included, set this argument as false to get real intersection
|
|
22
|
+
* @returns Intersection of base and inclusion
|
|
23
|
+
*/
|
|
24
|
+
getIntersection(inclusion: Inclusion, allEquals?: boolean): Set<T>;
|
|
25
|
+
}
|
|
26
|
+
}
|