@dr.pogodin/js-utils 0.0.17 → 0.0.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -0
- package/build/common/Barrier.js +34 -12
- package/build/common/Barrier.js.map +1 -1
- package/build/common/Emitter.js +12 -10
- package/build/common/Emitter.js.map +1 -1
- package/build/common/Semaphore.js +23 -24
- package/build/common/Semaphore.js.map +1 -1
- package/build/common/time.js +24 -9
- package/build/common/time.js.map +1 -1
- package/build/common/withRetries.js +0 -2
- package/build/common/withRetries.js.map +1 -1
- package/build/module/Barrier.js +34 -12
- package/build/module/Barrier.js.map +1 -1
- package/build/module/Emitter.js +12 -10
- package/build/module/Emitter.js.map +1 -1
- package/build/module/Semaphore.js +23 -24
- package/build/module/Semaphore.js.map +1 -1
- package/build/module/time.js +24 -9
- package/build/module/time.js.map +1 -1
- package/build/module/withRetries.js +0 -2
- package/build/module/withRetries.js.map +1 -1
- package/build/types/Barrier.d.ts +5 -5
- package/build/types/Emitter.d.ts +1 -1
- package/build/types/Semaphore.d.ts +5 -6
- package/build/types/time.d.ts +4 -4
- package/config/babel/preset.js +1 -1
- package/docs/common-sense-versioning.md +39 -0
- package/eslint.config.mjs +14 -0
- package/package.json +12 -14
package/README.md
CHANGED
|
@@ -59,6 +59,13 @@ and used in the same way):
|
|
|
59
59
|
- `Timer` — The core implementation of [timer()] functionality, allowing
|
|
60
60
|
to create further customized timer objects. _To be documented_.
|
|
61
61
|
|
|
62
|
+
---
|
|
63
|
+
Repository of this library also hosts some documents, to be referenced from
|
|
64
|
+
other projects, as needed:
|
|
65
|
+
- [The Common Sense Versioning](./docs/common-sense-versioning.md) —
|
|
66
|
+
a lax description of a lax approach to product versioning that makes more
|
|
67
|
+
sense than [SemVer](https://semver.org).
|
|
68
|
+
|
|
62
69
|
<!-- References -->
|
|
63
70
|
|
|
64
71
|
[Barrier]: https://dr.pogodin.studio/docs/react-utils/docs/api/classes/Barrier
|
package/build/common/Barrier.js
CHANGED
|
@@ -34,7 +34,7 @@ var STATE = /*#__PURE__*/function (STATE) {
|
|
|
34
34
|
* Docs: https://dr.pogodin.studio/docs/react-utils/docs/api/classes/Barrier
|
|
35
35
|
*/
|
|
36
36
|
class Barrier extends Promise {
|
|
37
|
-
|
|
37
|
+
pState = (() => STATE.PENDING)();
|
|
38
38
|
constructor(executor) {
|
|
39
39
|
let resolveRef;
|
|
40
40
|
let rejectRef;
|
|
@@ -42,7 +42,7 @@ class Barrier extends Promise {
|
|
|
42
42
|
// Note: Enforcing `void` return type because of the BEWARE note below.
|
|
43
43
|
resolveRef = value => {
|
|
44
44
|
resolve(value);
|
|
45
|
-
this.
|
|
45
|
+
this.pState = STATE.RESOLVED;
|
|
46
46
|
|
|
47
47
|
// BEWARE: Don't try to return `this` here, it will easily cause
|
|
48
48
|
// infinite loops in React Native, which are extremely difficult
|
|
@@ -54,7 +54,7 @@ class Barrier extends Promise {
|
|
|
54
54
|
// Note: Enforcing `void` return type because of the BEWARE note below.
|
|
55
55
|
rejectRef = reason => {
|
|
56
56
|
reject(reason);
|
|
57
|
-
this.
|
|
57
|
+
this.pState = STATE.REJECTED;
|
|
58
58
|
};
|
|
59
59
|
if (executor) executor(resolveRef, rejectRef);
|
|
60
60
|
});
|
|
@@ -63,40 +63,62 @@ class Barrier extends Promise {
|
|
|
63
63
|
// the Barrier is constructed by a .then() call on a "parent" barrier,
|
|
64
64
|
// and in that scenario .then() itself will replace .p_resolve by another
|
|
65
65
|
// resolver immediately after this constructor returns.
|
|
66
|
-
this.
|
|
67
|
-
this.
|
|
66
|
+
this.pResolve = resolveRef;
|
|
67
|
+
this.pReject = rejectRef;
|
|
68
68
|
}
|
|
69
69
|
get resolve() {
|
|
70
70
|
return arg => {
|
|
71
|
-
this.
|
|
71
|
+
this.pResolve(arg);
|
|
72
72
|
return this;
|
|
73
73
|
};
|
|
74
74
|
}
|
|
75
75
|
get reject() {
|
|
76
76
|
return arg => {
|
|
77
|
-
this.
|
|
77
|
+
this.pReject(arg);
|
|
78
78
|
return this;
|
|
79
79
|
};
|
|
80
80
|
}
|
|
81
81
|
get resolved() {
|
|
82
|
-
return this.
|
|
82
|
+
return this.pState === STATE.RESOLVED;
|
|
83
83
|
}
|
|
84
84
|
get rejected() {
|
|
85
|
-
return this.
|
|
85
|
+
return this.pState === STATE.REJECTED;
|
|
86
86
|
}
|
|
87
87
|
get settled() {
|
|
88
|
-
return this.
|
|
88
|
+
return this.pState !== STATE.PENDING;
|
|
89
89
|
}
|
|
90
|
+
|
|
91
|
+
// TODO: For async functions TS requires the return type to be the global
|
|
92
|
+
// Promise, thus not allowing to return our Barrier type extending it.
|
|
93
|
+
// Thus, we don't mark this method async for now, disabling the rule,
|
|
94
|
+
// and we should think more about it in future.
|
|
95
|
+
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
90
96
|
catch(onRejected) {
|
|
91
97
|
return super.catch(onRejected);
|
|
92
98
|
}
|
|
99
|
+
|
|
100
|
+
// TODO: For async functions TS requires the return type to be the global
|
|
101
|
+
// Promise, thus not allowing to return our Barrier type extending it.
|
|
102
|
+
// Thus, we don't mark this method async for now, disabling the rule,
|
|
103
|
+
// and we should think more about it in future.
|
|
104
|
+
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
93
105
|
finally(onFinally) {
|
|
94
106
|
return super.finally(onFinally);
|
|
95
107
|
}
|
|
108
|
+
|
|
109
|
+
// TODO: For async functions TS requires the return type to be the global
|
|
110
|
+
// Promise, thus not allowing to return our Barrier type extending it.
|
|
111
|
+
// Thus, we don't mark this method async for now, disabling the rule,
|
|
112
|
+
// and we should think more about it in future.
|
|
113
|
+
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
96
114
|
then(onFulfilled, onRejected) {
|
|
97
115
|
const res = super.then(onFulfilled, onRejected);
|
|
98
|
-
|
|
99
|
-
|
|
116
|
+
// TODO: Revise later.
|
|
117
|
+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
118
|
+
res.pResolve = this.resolve;
|
|
119
|
+
// TODO: Revise later.
|
|
120
|
+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
121
|
+
res.pReject = this.reject;
|
|
100
122
|
return res;
|
|
101
123
|
}
|
|
102
124
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Barrier.js","names":["STATE","Barrier","Promise","
|
|
1
|
+
{"version":3,"file":"Barrier.js","names":["STATE","Barrier","Promise","pState","PENDING","constructor","executor","resolveRef","rejectRef","resolve","reject","value","RESOLVED","reason","REJECTED","pResolve","pReject","arg","resolved","rejected","settled","catch","onRejected","finally","onFinally","then","onFulfilled","res","exports","default"],"sources":["../../src/Barrier.ts"],"sourcesContent":["export type Executor<T> = ConstructorParameters<typeof Promise<T>>[0];\n\ntype Resolver<T> = Parameters<Executor<T>>[0];\ntype Rejecter = Parameters<Executor<unknown>>[1];\n\nenum STATE {\n PENDING = 'PENDING',\n REJECTED = 'REJECTED',\n RESOLVED = 'RESOLVED',\n}\n\n/**\n * Barrier is just a Promise which has resolve and reject exposed as instance\n * methods.\n *\n * It has two generic arguments T and TR which correspond to the argument of\n * the .resolve() method, and to the value resolved by the promise (barrier).\n * For a simple barrier TR equals to T, however for barriers created via .then()\n * chain, T corresponds to the argument of the original barrier, and TR to\n * the value resolved by the latest promise in the chain. Consider this:\n *\n * const b = new Barrier<string>();\n * b.resolve('result');\n * const s = await b; // `s` has `string` type, and equals \"result\".\n *\n * const b = (new Barrier<string>()).then((s) => s.length);\n * b.resolve('result'); // Chained barrier exposes .resolve() method of\n * // the first barrier in the chain, which expects\n * // `string` arugment (T), but the chained barrier\n * // resolves to `number` (TR).\n * const n = await b; // `n` has `number` type, and equals 6.\n *\n * Docs: https://dr.pogodin.studio/docs/react-utils/docs/api/classes/Barrier\n */\nexport default class Barrier<T = unknown, TR = T> extends Promise<TR> {\n private pResolve: Resolver<T>;\n\n private pReject: Rejecter;\n\n private pState = STATE.PENDING;\n\n constructor(executor?: Executor<TR>) {\n let resolveRef: Resolver<TR>;\n let rejectRef: Rejecter;\n\n super((resolve, reject) => {\n // Note: Enforcing `void` return type because of the BEWARE note below.\n resolveRef = (value: TR | PromiseLike<TR>): void => {\n resolve(value);\n this.pState = STATE.RESOLVED;\n\n // BEWARE: Don't try to return `this` here, it will easily cause\n // infinite loops in React Native, which are extremely difficult\n // to troubleshoot (I wasn't able to figure out, are they due to\n // internal Promise implementation in RN, or because of some bad\n // patterns in the host code).\n };\n\n // Note: Enforcing `void` return type because of the BEWARE note below.\n rejectRef = (reason?: unknown): void => {\n reject(reason);\n this.pState = STATE.REJECTED;\n };\n\n if (executor) executor(resolveRef, rejectRef);\n });\n\n // NOTE: We assume, the only scenario where TR is not equal T is when\n // the Barrier is constructed by a .then() call on a \"parent\" barrier,\n // and in that scenario .then() itself will replace .p_resolve by another\n // resolver immediately after this constructor returns.\n this.pResolve = resolveRef! as Resolver<T>;\n\n this.pReject = rejectRef!;\n }\n\n get resolve() {\n return (arg: Parameters<Resolver<T>>[0]): this => {\n this.pResolve(arg);\n return this;\n };\n }\n\n get reject() {\n return (arg: Parameters<Rejecter>[0]): this => {\n this.pReject(arg);\n return this;\n };\n }\n\n get resolved(): boolean {\n return this.pState === STATE.RESOLVED;\n }\n\n get rejected(): boolean {\n return this.pState === STATE.REJECTED;\n }\n\n get settled(): boolean {\n return this.pState !== STATE.PENDING;\n }\n\n // TODO: For async functions TS requires the return type to be the global\n // Promise, thus not allowing to return our Barrier type extending it.\n // Thus, we don't mark this method async for now, disabling the rule,\n // and we should think more about it in future.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n catch<TR1>(\n onRejected?: ((reason: unknown) => TR1 | PromiseLike<TR1>) | null,\n ): Barrier<T, TR1> {\n return super.catch(onRejected) as Barrier<T, TR1>;\n }\n\n // TODO: For async functions TS requires the return type to be the global\n // Promise, thus not allowing to return our Barrier type extending it.\n // Thus, we don't mark this method async for now, disabling the rule,\n // and we should think more about it in future.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n finally(onFinally?: (() => void) | null): Barrier<TR> {\n return super.finally(onFinally) as Barrier<TR>;\n }\n\n // TODO: For async functions TS requires the return type to be the global\n // Promise, thus not allowing to return our Barrier type extending it.\n // Thus, we don't mark this method async for now, disabling the rule,\n // and we should think more about it in future.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n then<TR1, TR2>(\n onFulfilled?: ((value: TR) => TR1 | PromiseLike<TR1>) | null,\n onRejected?: ((reason: unknown) => TR2 | PromiseLike<TR2>) | null,\n ): Barrier<T, TR1 | TR2> {\n const res = super.then(onFulfilled, onRejected) as Barrier<T, TR1 | TR2>;\n // TODO: Revise later.\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n res.pResolve = this.resolve;\n // TODO: Revise later.\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n res.pReject = this.reject;\n return res;\n }\n}\n"],"mappings":";;;;;;IAKKA,KAAK,0BAALA,KAAK;EAALA,KAAK;EAALA,KAAK;EAALA,KAAK;EAAA,OAALA,KAAK;AAAA,EAALA,KAAK;AAMV;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,MAAMC,OAAO,SAA8BC,OAAO,CAAK;EAK5DC,MAAM,UAAGH,KAAK,CAACI,OAAO;EAE9BC,WAAWA,CAACC,QAAuB,EAAE;IACnC,IAAIC,UAAwB;IAC5B,IAAIC,SAAmB;IAEvB,KAAK,CAAC,CAACC,OAAO,EAAEC,MAAM,KAAK;MACzB;MACAH,UAAU,GAAII,KAA2B,IAAW;QAClDF,OAAO,CAACE,KAAK,CAAC;QACd,IAAI,CAACR,MAAM,GAAGH,KAAK,CAACY,QAAQ;;QAE5B;QACA;QACA;QACA;QACA;MACF,CAAC;;MAED;MACAJ,SAAS,GAAIK,MAAgB,IAAW;QACtCH,MAAM,CAACG,MAAM,CAAC;QACd,IAAI,CAACV,MAAM,G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C3B;IACA;IACAkB,GAAG,CAACX,OAAO,GAAG,IAAI,CAACN,MAAM;IACzB,OAAOiB,GAAG;EACZ;AACF;AAACC,OAAA,CAAAC,OAAA,GAAA5B,OAAA","ignoreList":[]}
|
package/build/common/Emitter.js
CHANGED
|
@@ -8,17 +8,17 @@ exports.Emitter = void 0;
|
|
|
8
8
|
* Simple listeneable data Emitter.
|
|
9
9
|
*/
|
|
10
10
|
class Emitter {
|
|
11
|
-
|
|
11
|
+
pListeners = [];
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* Returns "true" if any listener is connected; "false" otherwise.
|
|
15
15
|
* @return {boolean}
|
|
16
16
|
*/
|
|
17
17
|
get hasListeners() {
|
|
18
|
-
return !!this.
|
|
18
|
+
return !!this.pListeners.length;
|
|
19
19
|
}
|
|
20
20
|
get listeners() {
|
|
21
|
-
return this.
|
|
21
|
+
return this.pListeners;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
/**
|
|
@@ -27,10 +27,12 @@ class Emitter {
|
|
|
27
27
|
* @return {function} Unsubscribe function.
|
|
28
28
|
*/
|
|
29
29
|
addListener(listener) {
|
|
30
|
-
if (!this.
|
|
31
|
-
this.
|
|
30
|
+
if (!this.pListeners.includes(listener)) {
|
|
31
|
+
this.pListeners.push(listener);
|
|
32
32
|
}
|
|
33
|
-
return () =>
|
|
33
|
+
return () => {
|
|
34
|
+
this.removeListener(listener);
|
|
35
|
+
};
|
|
34
36
|
}
|
|
35
37
|
|
|
36
38
|
/**
|
|
@@ -38,7 +40,7 @@ class Emitter {
|
|
|
38
40
|
* @param args
|
|
39
41
|
*/
|
|
40
42
|
emit() {
|
|
41
|
-
const listeners = this.
|
|
43
|
+
const listeners = this.pListeners.slice();
|
|
42
44
|
for (const listener of listeners) listener(...arguments);
|
|
43
45
|
}
|
|
44
46
|
|
|
@@ -46,7 +48,7 @@ class Emitter {
|
|
|
46
48
|
* Removes all connected listeners.
|
|
47
49
|
*/
|
|
48
50
|
removeAllListeners() {
|
|
49
|
-
this.
|
|
51
|
+
this.pListeners = [];
|
|
50
52
|
}
|
|
51
53
|
|
|
52
54
|
/**
|
|
@@ -54,8 +56,8 @@ class Emitter {
|
|
|
54
56
|
* @param listener
|
|
55
57
|
*/
|
|
56
58
|
removeListener(listener) {
|
|
57
|
-
const idx = this.
|
|
58
|
-
if (idx >= 0) this.
|
|
59
|
+
const idx = this.pListeners.indexOf(listener);
|
|
60
|
+
if (idx >= 0) this.pListeners.splice(idx, 1);
|
|
59
61
|
}
|
|
60
62
|
}
|
|
61
63
|
exports.Emitter = Emitter;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Emitter.js","names":["Emitter","
|
|
1
|
+
{"version":3,"file":"Emitter.js","names":["Emitter","pListeners","hasListeners","length","listeners","addListener","listener","includes","push","removeListener","emit","slice","arguments","removeAllListeners","idx","indexOf","splice","exports"],"sources":["../../src/Emitter.ts"],"sourcesContent":["export type Listener<T extends unknown[] = unknown[]> = (...args: T) => void;\n\n/**\n * Simple listeneable data Emitter.\n */\nexport class Emitter<T extends unknown[] = unknown[]> {\n private pListeners: Array<Listener<T>> = [];\n\n /**\n * Returns \"true\" if any listener is connected; \"false\" otherwise.\n * @return {boolean}\n */\n get hasListeners(): boolean {\n return !!this.pListeners.length;\n }\n\n get listeners(): ReadonlyArray<Listener<T>> {\n return this.pListeners;\n }\n\n /**\n * Adds `listener` if it is not already connected.\n * @param {function} listener\n * @return {function} Unsubscribe function.\n */\n addListener(listener: Listener<T>): () => void {\n if (!this.pListeners.includes(listener)) {\n this.pListeners.push(listener);\n }\n return () => {\n this.removeListener(listener);\n };\n }\n\n /**\n * Calls every connected listener with the given arguments.\n * @param args\n */\n emit(...args: T): void {\n const listeners = this.pListeners.slice();\n for (const listener of listeners) listener(...args);\n }\n\n /**\n * Removes all connected listeners.\n */\n removeAllListeners(): void {\n this.pListeners = [];\n }\n\n /**\n * Removes specified `listener`, if connected.\n * @param listener\n */\n removeListener(listener: Listener<T>): void {\n const idx = this.pListeners.indexOf(listener);\n if (idx >= 0) this.pListeners.splice(idx, 1);\n }\n}\n"],"mappings":";;;;;;AAEA;AACA;AACA;AACO,MAAMA,OAAO,CAAkC;EAC5CC,UAAU,GAAuB,EAAE;;EAE3C;AACF;AACA;AACA;EACE,IAAIC,YAAYA,CAAA,EAAY;IAC1B,OAAO,CAAC,CAAC,IAAI,CAACD,UAAU,CAACE,MAAM;EACjC;EAEA,IAAIC,SAASA,CAAA,EAA+B;IAC1C,OAAO,IAAI,CAACH,UAAU;EACxB;;EAEA;AACF;AACA;AACA;AACA;EACEI,WAAWA,CAACC,QAAqB,EAAc;IAC7C,IAAI,CAAC,IAAI,CAACL,UAAU,CAACM,QAAQ,CAACD,QAAQ,CAAC,EAAE;MACvC,IAAI,CAACL,UAAU,CAACO,IAAI,CAACF,QAAQ,CAAC;IAChC;IACA,OAAO,MAAM;MACX,IAAI,CAACG,cAAc,CAACH,QAAQ,CAAC;IAC/B,CAAC;EACH;;EAEA;AACF;AACA;AACA;EACEI,IAAIA,CAAA,EAAmB;IACrB,MAAMN,SAAS,GAAG,IAAI,CAACH,UAAU,CAACU,KAAK,CAAC,CAAC;IACzC,KAAK,MAAML,QAAQ,IAAIF,SAAS,EAAEE,QAAQ,CAAC,GAAAM,SAAO,CAAC;EACrD;;EAEA;AACF;AACA;EACEC,kBAAkBA,CAAA,EAAS;IACzB,IAAI,CAACZ,UAAU,GAAG,EAAE;EACtB;;EAEA;AACF;AACA;AACA;EACEQ,cAAcA,CAACH,QAAqB,EAAQ;IAC1C,MAAMQ,GAAG,GAAG,IAAI,CAACb,UAAU,CAACc,OAAO,CAACT,QAAQ,CAAC;IAC7C,IAAIQ,GAAG,IAAI,CAAC,EAAE,IAAI,CAACb,UAAU,CAACe,MAAM,CAACF,GAAG,EAAE,CAAC,CAAC;EAC9C;AACF;AAACG,OAAA,CAAAjB,OAAA,GAAAA,OAAA","ignoreList":[]}
|
|
@@ -12,37 +12,36 @@ var _Barrier = _interopRequireDefault(require("./Barrier"));
|
|
|
12
12
|
class Semaphore {
|
|
13
13
|
constructor() {
|
|
14
14
|
let ready = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
|
|
15
|
-
this.
|
|
15
|
+
this.pReady = !!ready;
|
|
16
16
|
}
|
|
17
17
|
get ready() {
|
|
18
|
-
return this.
|
|
18
|
+
return this.pReady;
|
|
19
19
|
}
|
|
20
20
|
setReady(ready) {
|
|
21
21
|
const bool = !!ready;
|
|
22
|
-
if (this.
|
|
23
|
-
this.
|
|
24
|
-
if (bool && !this.
|
|
25
|
-
this.
|
|
22
|
+
if (this.pReady !== bool) {
|
|
23
|
+
this.pReady = bool;
|
|
24
|
+
if (bool && !this.pDraining && this.pQueue.length) {
|
|
25
|
+
void this.pDrainQueue();
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
/**
|
|
31
31
|
* Waits until the semaphore is ready, and marks it as non-ready (seizes it).
|
|
32
|
-
* @return {Promise}
|
|
33
32
|
*/
|
|
34
33
|
async seize() {
|
|
35
34
|
return this.waitReady(true);
|
|
36
35
|
}
|
|
37
36
|
async waitReady() {
|
|
38
37
|
let seize = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
|
|
39
|
-
if (!this.
|
|
38
|
+
if (!this.pReady || this.pQueue.length) {
|
|
40
39
|
const barrier = new _Barrier.default();
|
|
41
|
-
this.
|
|
40
|
+
this.pQueue.push(barrier);
|
|
42
41
|
await barrier;
|
|
43
|
-
if (seize) this.
|
|
44
|
-
this.
|
|
45
|
-
} else if (seize) this.
|
|
42
|
+
if (seize) this.pReady = false;
|
|
43
|
+
void this.pDrainLock.resolve();
|
|
44
|
+
} else if (seize) this.pReady = false;
|
|
46
45
|
}
|
|
47
46
|
|
|
48
47
|
// Private members below this point.
|
|
@@ -53,21 +52,21 @@ class Semaphore {
|
|
|
53
52
|
* Otherwise, it breaks the queue draining loop, which will be restarted
|
|
54
53
|
* the next time the semaphore is set ready.
|
|
55
54
|
*/
|
|
56
|
-
async
|
|
57
|
-
this.
|
|
58
|
-
while (this.
|
|
59
|
-
this.
|
|
60
|
-
this.
|
|
61
|
-
await this.
|
|
62
|
-
this.
|
|
55
|
+
async pDrainQueue() {
|
|
56
|
+
this.pDraining = true;
|
|
57
|
+
while (this.pReady && this.pQueue.length) {
|
|
58
|
+
this.pDrainLock = new _Barrier.default();
|
|
59
|
+
void this.pQueue[0].resolve();
|
|
60
|
+
await this.pDrainLock;
|
|
61
|
+
void this.pQueue.shift();
|
|
63
62
|
}
|
|
64
|
-
this.
|
|
65
|
-
this.
|
|
63
|
+
this.pDraining = false;
|
|
64
|
+
this.pDrainLock = null;
|
|
66
65
|
}
|
|
67
66
|
|
|
68
67
|
// "true" when the drain queue process is running (and thus no need to start
|
|
69
68
|
// a new one).
|
|
70
|
-
|
|
69
|
+
pDraining = false;
|
|
71
70
|
|
|
72
71
|
// Each time a Promise from drain queue is resolved this drainLock is set
|
|
73
72
|
// to block further queue draining until the promise resolution handler
|
|
@@ -75,11 +74,11 @@ class Semaphore {
|
|
|
75
74
|
// to continue the draining. This is specifically important for .seize(),
|
|
76
75
|
// which should have a chance to switch semaphore state to non-ready prior
|
|
77
76
|
// to next Promise in the queue being unlocked.
|
|
78
|
-
|
|
77
|
+
pDrainLock = null;
|
|
79
78
|
|
|
80
79
|
// The array of barriers set for each async code flow awaiting for
|
|
81
80
|
// the Semaphore to become ready.
|
|
82
|
-
|
|
81
|
+
pQueue = [];
|
|
83
82
|
}
|
|
84
83
|
exports.default = Semaphore;
|
|
85
84
|
//# sourceMappingURL=Semaphore.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Semaphore.js","names":["_Barrier","_interopRequireDefault","require","Semaphore","constructor","ready","arguments","length","undefined","
|
|
1
|
+
{"version":3,"file":"Semaphore.js","names":["_Barrier","_interopRequireDefault","require","Semaphore","constructor","ready","arguments","length","undefined","pReady","setReady","bool","pDraining","pQueue","pDrainQueue","seize","waitReady","barrier","Barrier","push","pDrainLock","resolve","shift","exports","default"],"sources":["../../src/Semaphore.ts"],"sourcesContent":["import Barrier from './Barrier';\n\n/**\n * Implements a simple semaphore for async code logic.\n */\nexport default class Semaphore {\n constructor(ready = false) {\n this.pReady = !!ready;\n }\n\n get ready(): boolean {\n return this.pReady;\n }\n\n setReady(ready: boolean): void {\n const bool = !!ready;\n if (this.pReady !== bool) {\n this.pReady = bool;\n if (bool && !this.pDraining && this.pQueue.length) {\n void this.pDrainQueue();\n }\n }\n }\n\n /**\n * Waits until the semaphore is ready, and marks it as non-ready (seizes it).\n */\n async seize(): Promise<void> {\n return this.waitReady(true);\n }\n\n async waitReady(seize = false): Promise<void> {\n if (!this.pReady || this.pQueue.length) {\n const barrier = new Barrier<void>();\n this.pQueue.push(barrier);\n await barrier;\n if (seize) this.pReady = false;\n void this.pDrainLock!.resolve();\n } else if (seize) this.pReady = false;\n }\n\n // Private members below this point.\n\n /**\n * If semaphore is ready, it releases the next barrier in the queue, if any,\n * and reschedules itself for a call in the next event loop iteration.\n * Otherwise, it breaks the queue draining loop, which will be restarted\n * the next time the semaphore is set ready.\n */\n async pDrainQueue(): Promise<void> {\n this.pDraining = true;\n while (this.pReady && this.pQueue.length) {\n this.pDrainLock = new Barrier();\n void this.pQueue[0]!.resolve();\n await this.pDrainLock;\n void this.pQueue.shift();\n }\n this.pDraining = false;\n this.pDrainLock = null;\n }\n\n // \"true\" when the drain queue process is running (and thus no need to start\n // a new one).\n private pDraining = false;\n\n // Each time a Promise from drain queue is resolved this drainLock is set\n // to block further queue draining until the promise resolution handler\n // (.seize() or .waitReady()) unlocks it, thus confirming it is fine\n // to continue the draining. This is specifically important for .seize(),\n // which should have a chance to switch semaphore state to non-ready prior\n // to next Promise in the queue being unlocked.\n private pDrainLock: Barrier<void> | null = null;\n\n // The array of barriers set for each async code flow awaiting for\n // the Semaphore to become ready.\n private pQueue: Array<Barrier<void>> = [];\n\n private pReady: boolean;\n}\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,MAAM,IAAIN,KAAK,EAAE,IAAI,CAACN,MAAM,GAAG,KAAK;EACvC;;EAEA;;EAEA;AACF;AACA;AACA;AACA;AACA;EACE,MAAMK,WAAWA,CAAA,EAAkB;IACjC,IAAI,CAACF,SAAS,GAAG,IAAI;IACrB,OAAO,IAAI,CAACH,MAAM,IAAI,IAAI,CAACI,MAAM,CAACN,MAAM,EAAE;MACxC,IAAI,CAACa,UAAU,GAAG,IAAIF,gBAAO,CAAC,CAAC;MAC/B,KAAK,IAAI,CAACL,MAAM,CAAC,CAAC,CAAC,CAAEQ,OAAO,CAAC,CAAC;MAC9B,MAAM,IAAI,CAACD,UAAU;MACrB,KAAK,IAAI,CAACP,MAAM,CAACS,KAAK,CAAC,CAAC;IAC1B;IACA,IAAI,CAACV,SAAS,GAAG,KAAK;IACtB,IAAI,CAACQ,UAAU,GAAG,IAAI;EACxB;;EAEA;EACA;EACQR,SAAS,GAAG,KAAK;;EAEzB;EACA;EACA;EACA;EACA;EACA;EACQQ,UAAU,GAAyB,IAAI;;EAE/C;EACA;EACQP,MAAM,GAAyB,EAAE;AAG3C;AAACU,OAAA,CAAAC,OAAA,GAAArB,SAAA","ignoreList":[]}
|
package/build/common/time.js
CHANGED
|
@@ -22,10 +22,10 @@ const YEAR_MS = exports.YEAR_MS = 31536000000; // 365 * DAY_MS
|
|
|
22
22
|
// from the library as well, and it should be documented later.
|
|
23
23
|
class Timer extends _Barrier.default {
|
|
24
24
|
get abort() {
|
|
25
|
-
return this.
|
|
25
|
+
return this.pAbort;
|
|
26
26
|
}
|
|
27
27
|
get timeout() {
|
|
28
|
-
return this.
|
|
28
|
+
return this.pTimeout;
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
/**
|
|
@@ -43,24 +43,34 @@ class Timer extends _Barrier.default {
|
|
|
43
43
|
*/
|
|
44
44
|
constructor(executor) {
|
|
45
45
|
super(executor);
|
|
46
|
-
this.
|
|
46
|
+
this.pAbort = () => undefined;
|
|
47
47
|
}
|
|
48
48
|
init(timeout) {
|
|
49
|
-
if (this.
|
|
49
|
+
if (this.pTimeout !== undefined) {
|
|
50
50
|
throw Error('This Timer is initialized already');
|
|
51
51
|
}
|
|
52
|
-
this.
|
|
52
|
+
this.pTimeout = timeout;
|
|
53
53
|
if (timeout > 0) {
|
|
54
|
-
const id = setTimeout(
|
|
55
|
-
|
|
54
|
+
const id = setTimeout(() => {
|
|
55
|
+
void super.resolve();
|
|
56
|
+
}, timeout);
|
|
57
|
+
this.pAbort = () => {
|
|
58
|
+
clearTimeout(id);
|
|
59
|
+
};
|
|
56
60
|
} else {
|
|
57
|
-
super.resolve();
|
|
61
|
+
void super.resolve();
|
|
58
62
|
}
|
|
59
63
|
return this;
|
|
60
64
|
}
|
|
65
|
+
|
|
66
|
+
// TODO: For async functions TS requires the return type to be the global
|
|
67
|
+
// Promise, thus not allowing to return our Timer type extending that via
|
|
68
|
+
// Barrier. Thus, we don't mark this method async for now, disabling the rule,
|
|
69
|
+
// and we should think more about it in future.
|
|
70
|
+
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
61
71
|
then(onFulfilled, onRejected) {
|
|
62
72
|
const res = super.then(onFulfilled, onRejected);
|
|
63
|
-
if (this.timeout !== undefined) res.init(this.timeout);
|
|
73
|
+
if (this.timeout !== undefined) void res.init(this.timeout);
|
|
64
74
|
return res;
|
|
65
75
|
}
|
|
66
76
|
}
|
|
@@ -72,6 +82,11 @@ class Timer extends _Barrier.default {
|
|
|
72
82
|
* .abort() method attached, which cancels the pending timer resolution
|
|
73
83
|
* (without resolving or rejecting the barrier).
|
|
74
84
|
*/
|
|
85
|
+
// TODO: For async functions TS requires the return type to be the global
|
|
86
|
+
// Promise, thus not allowing to return our Timer type extending that via
|
|
87
|
+
// Barrier. Thus, we don't mark this method async for now, disabling the rule,
|
|
88
|
+
// and we should think more about it in future.
|
|
89
|
+
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
75
90
|
exports.Timer = Timer;
|
|
76
91
|
function timer(timeout) {
|
|
77
92
|
const t = new Timer();
|
package/build/common/time.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"time.js","names":["_Barrier","_interopRequireDefault","require","SEC_MS","exports","MIN_MS","HOUR_MS","DAY_MS","YEAR_MS","Timer","Barrier","abort","
|
|
1
|
+
{"version":3,"file":"time.js","names":["_Barrier","_interopRequireDefault","require","SEC_MS","exports","MIN_MS","HOUR_MS","DAY_MS","YEAR_MS","Timer","Barrier","abort","pAbort","timeout","pTimeout","constructor","executor","undefined","init","Error","id","setTimeout","resolve","clearTimeout","then","onFulfilled","onRejected","res","timer","t"],"sources":["../../src/time.ts"],"sourcesContent":["import Barrier, { type Executor } from './Barrier';\n\n// This is not very elegant, but as of now TypeScript does not support type\n// arithmetic, thus we can't have constants assigned like `MIN_MS = 60 * SEC_MS`\n// and have the result type to be 60000 (number literal), it would be just\n// the generic number type.\nexport const SEC_MS = 1000;\nexport const MIN_MS = 60000; // 60 * SEC_MS\nexport const HOUR_MS = 3600000; // 60 * MIN_MS\nexport const DAY_MS = 86400000; // 24 * HOUR_MS\nexport const YEAR_MS = 31536000000; // 365 * DAY_MS\n\n// TODO: Ok, as we have ended up with a Timer class, mostly to achieve a good\n// TypeScript typing for timer() function, it makes sense to expose the class\n// from the library as well, and it should be documented later.\nexport class Timer<T> extends Barrier<void, T> {\n private pAbort: () => void;\n\n private pTimeout?: number;\n\n get abort(): () => void {\n return this.pAbort;\n }\n\n get timeout(): number | undefined {\n return this.pTimeout;\n }\n\n /**\n * Creates a new, non-initialized instance of Timer. Call .init() method\n * to actually initialize and launch the timer.\n *\n * NOTE: Although it might be tempting to accept `timeout` value as\n * a constructor's argument, it won't work well, because Timer is an\n * extension of Promise (via Barrier), and the way Promises works (in\n * particular their .then() method, which internally calls constructor()\n * with special executor) does not play along with initalization depending\n * on custom parameters done in constructor().\n *\n * @param executor\n */\n constructor(executor?: Executor<T>) {\n super(executor);\n this.pAbort = () => undefined;\n }\n\n init(timeout: number): this {\n if (this.pTimeout !== undefined) {\n throw Error('This Timer is initialized already');\n }\n this.pTimeout = timeout;\n if (timeout > 0) {\n const id = setTimeout(() => {\n void super.resolve();\n }, timeout);\n this.pAbort = () => {\n clearTimeout(id);\n };\n } else {\n void super.resolve();\n }\n return this;\n }\n\n // TODO: For async functions TS requires the return type to be the global\n // Promise, thus not allowing to return our Timer type extending that via\n // Barrier. Thus, we don't mark this method async for now, disabling the rule,\n // and we should think more about it in future.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n then<TR1, TR2>(\n onFulfilled?: ((value: T) => TR1 | PromiseLike<TR1>) | null,\n onRejected?: ((reason: unknown) => TR2 | PromiseLike<TR2>) | null,\n ): Timer<TR1 | TR2> {\n const res = super.then(onFulfilled, onRejected) as Timer<TR1 | TR2>;\n if (this.timeout !== undefined) void res.init(this.timeout);\n return res;\n }\n}\n\n/**\n * Creates a Promise, which resolves after the given timeout.\n * @param {number} timeout Timeout [ms].\n * @return {Barrier} Resolves after the timeout. It has additional\n * .abort() method attached, which cancels the pending timer resolution\n * (without resolving or rejecting the barrier).\n */\n// TODO: For async functions TS requires the return type to be the global\n// Promise, thus not allowing to return our Timer type extending that via\n// Barrier. Thus, we don't mark this method async for now, disabling the rule,\n// and we should think more about it in future.\n// eslint-disable-next-line @typescript-eslint/promise-function-async\nexport function timer(timeout: number): Timer<void> {\n const t = new Timer<void>();\n return t.init(timeout);\n}\n"],"mappings":";;;;;;;;AAAA,IAAAA,QAAA,GAAAC,sBAAA,CAAAC,OAAA;AAEA;AACA;AACA;AACA;AACO,MAAMC,MAAM,GAAAC,OAAA,CAAAD,MAAA,GAAG,IAAI;AACnB,MAAME,MAAM,GAAAD,OAAA,CAAAC,MAAA,GAAG,KAAK,CAAC,CAAC;AACtB,MAAMC,OAAO,GAAAF,OAAA,CAAAE,OAAA,GAAG,OAAO,CAAC,CAAC;AACzB,MAAMC,MAAM,GAAAH,OAAA,CAAAG,MAAA,GAAG,QAAQ,CAAC,CAAC;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;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAAAvB,OAAA,CAAAK,KAAA,GAAAA,KAAA;AACO,SAASmB,KAAKA,CAACf,OAAe,EAAe;EAClD,MAAMgB,CAAC,GAAG,IAAIpB,KAAK,CAAO,CAAC;EAC3B,OAAOoB,CAAC,CAACX,IAAI,CAACL,OAAO,CAAC;AACxB","ignoreList":[]}
|
|
@@ -19,7 +19,6 @@ var _time = require("./time");
|
|
|
19
19
|
async function withRetries(action) {
|
|
20
20
|
let maxRetries = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 3;
|
|
21
21
|
let interval = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 300;
|
|
22
|
-
/* eslint-disable no-await-in-loop */
|
|
23
22
|
for (let n = 1;; ++n) {
|
|
24
23
|
try {
|
|
25
24
|
const res = action();
|
|
@@ -28,6 +27,5 @@ async function withRetries(action) {
|
|
|
28
27
|
if (n < maxRetries) await (0, _time.timer)(interval);else throw error;
|
|
29
28
|
}
|
|
30
29
|
}
|
|
31
|
-
/* eslint-enable no-await-in-loop */
|
|
32
30
|
}
|
|
33
31
|
//# sourceMappingURL=withRetries.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"withRetries.js","names":["_time","require","withRetries","action","maxRetries","arguments","length","undefined","interval","n","res","Promise","error","timer"],"sources":["../../src/withRetries.ts"],"sourcesContent":["import { timer } from './time';\n\n/**\n * Attempts to perform the given async `action` up to `maxRetries` times with\n * the specified `interval`, stopping at the first successful (non-throwing)\n * execution.\n * @param action\n * @param maxRetries Optional. The maximum number of re-tries. Defaults 3.\n * @param interval Optional. The interval between re-tries (in milliseconds).\n * Defaults to 300ms.\n * @returns Resolves to the result of the successful `action` execution;\n * or rejects with the error from the last faileda attempt.\n */\nexport default async function withRetries<T>(\n action: () => T,\n maxRetries = 3,\n interval = 300,\n): Promise<Awaited<T>> {\n
|
|
1
|
+
{"version":3,"file":"withRetries.js","names":["_time","require","withRetries","action","maxRetries","arguments","length","undefined","interval","n","res","Promise","error","timer"],"sources":["../../src/withRetries.ts"],"sourcesContent":["import { timer } from './time';\n\n/**\n * Attempts to perform the given async `action` up to `maxRetries` times with\n * the specified `interval`, stopping at the first successful (non-throwing)\n * execution.\n * @param action\n * @param maxRetries Optional. The maximum number of re-tries. Defaults 3.\n * @param interval Optional. The interval between re-tries (in milliseconds).\n * Defaults to 300ms.\n * @returns Resolves to the result of the successful `action` execution;\n * or rejects with the error from the last faileda attempt.\n */\nexport default async function withRetries<T>(\n action: () => T,\n maxRetries = 3,\n interval = 300,\n): Promise<Awaited<T>> {\n for (let n = 1; ; ++n) {\n try {\n const res = action();\n return res instanceof Promise ? await res : (res as Awaited<T>);\n } catch (error) {\n if (n < maxRetries) await timer(interval);\n else throw error;\n }\n }\n}\n"],"mappings":";;;;;;AAAA,IAAAA,KAAA,GAAAC,OAAA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,eAAeC,WAAWA,CACvCC,MAAe,EAGM;EAAA,IAFrBC,UAAU,GAAAC,SAAA,CAAAC,MAAA,QAAAD,SAAA,QAAAE,SAAA,GAAAF,SAAA,MAAG,CAAC;EAAA,IACdG,QAAQ,GAAAH,SAAA,CAAAC,MAAA,QAAAD,SAAA,QAAAE,SAAA,GAAAF,SAAA,MAAG,GAAG;EAEd,KAAK,IAAII,CAAC,GAAG,CAAC,GAAI,EAAEA,CAAC,EAAE;IACrB,IAAI;MACF,MAAMC,GAAG,GAAGP,MAAM,CAAC,CAAC;MACpB,OAAOO,GAAG,YAAYC,OAAO,GAAG,MAAMD,GAAG,GAAIA,GAAkB;IACjE,CAAC,CAAC,OAAOE,KAAK,EAAE;MACd,IAAIH,CAAC,GAAGL,UAAU,EAAE,MAAM,IAAAS,WAAK,EAACL,QAAQ,CAAC,CAAC,KACrC,MAAMI,KAAK;IAClB;EACF;AACF","ignoreList":[]}
|
package/build/module/Barrier.js
CHANGED
|
@@ -28,7 +28,7 @@ var STATE = /*#__PURE__*/function (STATE) {
|
|
|
28
28
|
* Docs: https://dr.pogodin.studio/docs/react-utils/docs/api/classes/Barrier
|
|
29
29
|
*/
|
|
30
30
|
export default class Barrier extends Promise {
|
|
31
|
-
|
|
31
|
+
pState = (() => STATE.PENDING)();
|
|
32
32
|
constructor(executor) {
|
|
33
33
|
let resolveRef;
|
|
34
34
|
let rejectRef;
|
|
@@ -36,7 +36,7 @@ export default class Barrier extends Promise {
|
|
|
36
36
|
// Note: Enforcing `void` return type because of the BEWARE note below.
|
|
37
37
|
resolveRef = value => {
|
|
38
38
|
resolve(value);
|
|
39
|
-
this.
|
|
39
|
+
this.pState = STATE.RESOLVED;
|
|
40
40
|
|
|
41
41
|
// BEWARE: Don't try to return `this` here, it will easily cause
|
|
42
42
|
// infinite loops in React Native, which are extremely difficult
|
|
@@ -48,7 +48,7 @@ export default class Barrier extends Promise {
|
|
|
48
48
|
// Note: Enforcing `void` return type because of the BEWARE note below.
|
|
49
49
|
rejectRef = reason => {
|
|
50
50
|
reject(reason);
|
|
51
|
-
this.
|
|
51
|
+
this.pState = STATE.REJECTED;
|
|
52
52
|
};
|
|
53
53
|
if (executor) executor(resolveRef, rejectRef);
|
|
54
54
|
});
|
|
@@ -57,40 +57,62 @@ export default class Barrier extends Promise {
|
|
|
57
57
|
// the Barrier is constructed by a .then() call on a "parent" barrier,
|
|
58
58
|
// and in that scenario .then() itself will replace .p_resolve by another
|
|
59
59
|
// resolver immediately after this constructor returns.
|
|
60
|
-
this.
|
|
61
|
-
this.
|
|
60
|
+
this.pResolve = resolveRef;
|
|
61
|
+
this.pReject = rejectRef;
|
|
62
62
|
}
|
|
63
63
|
get resolve() {
|
|
64
64
|
return arg => {
|
|
65
|
-
this.
|
|
65
|
+
this.pResolve(arg);
|
|
66
66
|
return this;
|
|
67
67
|
};
|
|
68
68
|
}
|
|
69
69
|
get reject() {
|
|
70
70
|
return arg => {
|
|
71
|
-
this.
|
|
71
|
+
this.pReject(arg);
|
|
72
72
|
return this;
|
|
73
73
|
};
|
|
74
74
|
}
|
|
75
75
|
get resolved() {
|
|
76
|
-
return this.
|
|
76
|
+
return this.pState === STATE.RESOLVED;
|
|
77
77
|
}
|
|
78
78
|
get rejected() {
|
|
79
|
-
return this.
|
|
79
|
+
return this.pState === STATE.REJECTED;
|
|
80
80
|
}
|
|
81
81
|
get settled() {
|
|
82
|
-
return this.
|
|
82
|
+
return this.pState !== STATE.PENDING;
|
|
83
83
|
}
|
|
84
|
+
|
|
85
|
+
// TODO: For async functions TS requires the return type to be the global
|
|
86
|
+
// Promise, thus not allowing to return our Barrier type extending it.
|
|
87
|
+
// Thus, we don't mark this method async for now, disabling the rule,
|
|
88
|
+
// and we should think more about it in future.
|
|
89
|
+
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
84
90
|
catch(onRejected) {
|
|
85
91
|
return super.catch(onRejected);
|
|
86
92
|
}
|
|
93
|
+
|
|
94
|
+
// TODO: For async functions TS requires the return type to be the global
|
|
95
|
+
// Promise, thus not allowing to return our Barrier type extending it.
|
|
96
|
+
// Thus, we don't mark this method async for now, disabling the rule,
|
|
97
|
+
// and we should think more about it in future.
|
|
98
|
+
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
87
99
|
finally(onFinally) {
|
|
88
100
|
return super.finally(onFinally);
|
|
89
101
|
}
|
|
102
|
+
|
|
103
|
+
// TODO: For async functions TS requires the return type to be the global
|
|
104
|
+
// Promise, thus not allowing to return our Barrier type extending it.
|
|
105
|
+
// Thus, we don't mark this method async for now, disabling the rule,
|
|
106
|
+
// and we should think more about it in future.
|
|
107
|
+
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
90
108
|
then(onFulfilled, onRejected) {
|
|
91
109
|
const res = super.then(onFulfilled, onRejected);
|
|
92
|
-
|
|
93
|
-
|
|
110
|
+
// TODO: Revise later.
|
|
111
|
+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
112
|
+
res.pResolve = this.resolve;
|
|
113
|
+
// TODO: Revise later.
|
|
114
|
+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
115
|
+
res.pReject = this.reject;
|
|
94
116
|
return res;
|
|
95
117
|
}
|
|
96
118
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Barrier.js","names":["STATE","Barrier","Promise","
|
|
1
|
+
{"version":3,"file":"Barrier.js","names":["STATE","Barrier","Promise","pState","PENDING","constructor","executor","resolveRef","rejectRef","resolve","reject","value","RESOLVED","reason","REJECTED","pResolve","pReject","arg","resolved","rejected","settled","catch","onRejected","finally","onFinally","then","onFulfilled","res"],"sources":["../../src/Barrier.ts"],"sourcesContent":["export type Executor<T> = ConstructorParameters<typeof Promise<T>>[0];\n\ntype Resolver<T> = Parameters<Executor<T>>[0];\ntype Rejecter = Parameters<Executor<unknown>>[1];\n\nenum STATE {\n PENDING = 'PENDING',\n REJECTED = 'REJECTED',\n RESOLVED = 'RESOLVED',\n}\n\n/**\n * Barrier is just a Promise which has resolve and reject exposed as instance\n * methods.\n *\n * It has two generic arguments T and TR which correspond to the argument of\n * the .resolve() method, and to the value resolved by the promise (barrier).\n * For a simple barrier TR equals to T, however for barriers created via .then()\n * chain, T corresponds to the argument of the original barrier, and TR to\n * the value resolved by the latest promise in the chain. Consider this:\n *\n * const b = new Barrier<string>();\n * b.resolve('result');\n * const s = await b; // `s` has `string` type, and equals \"result\".\n *\n * const b = (new Barrier<string>()).then((s) => s.length);\n * b.resolve('result'); // Chained barrier exposes .resolve() method of\n * // the first barrier in the chain, which expects\n * // `string` arugment (T), but the chained barrier\n * // resolves to `number` (TR).\n * const n = await b; // `n` has `number` type, and equals 6.\n *\n * Docs: https://dr.pogodin.studio/docs/react-utils/docs/api/classes/Barrier\n */\nexport default class Barrier<T = unknown, TR = T> extends Promise<TR> {\n private pResolve: Resolver<T>;\n\n private pReject: Rejecter;\n\n private pState = STATE.PENDING;\n\n constructor(executor?: Executor<TR>) {\n let resolveRef: Resolver<TR>;\n let rejectRef: Rejecter;\n\n super((resolve, reject) => {\n // Note: Enforcing `void` return type because of the BEWARE note below.\n resolveRef = (value: TR | PromiseLike<TR>): void => {\n resolve(value);\n this.pState = STATE.RESOLVED;\n\n // BEWARE: Don't try to return `this` here, it will easily cause\n // infinite loops in React Native, which are extremely difficult\n // to troubleshoot (I wasn't able to figure out, are they due to\n // internal Promise implementation in RN, or because of some bad\n // patterns in the host code).\n };\n\n // Note: Enforcing `void` return type because of the BEWARE note below.\n rejectRef = (reason?: unknown): void => {\n reject(reason);\n this.pState = STATE.REJECTED;\n };\n\n if (executor) executor(resolveRef, rejectRef);\n });\n\n // NOTE: We assume, the only scenario where TR is not equal T is when\n // the Barrier is constructed by a .then() call on a \"parent\" barrier,\n // and in that scenario .then() itself will replace .p_resolve by another\n // resolver immediately after this constructor returns.\n this.pResolve = resolveRef! as Resolver<T>;\n\n this.pReject = rejectRef!;\n }\n\n get resolve() {\n return (arg: Parameters<Resolver<T>>[0]): this => {\n this.pResolve(arg);\n return this;\n };\n }\n\n get reject() {\n return (arg: Parameters<Rejecter>[0]): this => {\n this.pReject(arg);\n return this;\n };\n }\n\n get resolved(): boolean {\n return this.pState === STATE.RESOLVED;\n }\n\n get rejected(): boolean {\n return this.pState === STATE.REJECTED;\n }\n\n get settled(): boolean {\n return this.pState !== STATE.PENDING;\n }\n\n // TODO: For async functions TS requires the return type to be the global\n // Promise, thus not allowing to return our Barrier type extending it.\n // Thus, we don't mark this method async for now, disabling the rule,\n // and we should think more about it in future.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n catch<TR1>(\n onRejected?: ((reason: unknown) => TR1 | PromiseLike<TR1>) | null,\n ): Barrier<T, TR1> {\n return super.catch(onRejected) as Barrier<T, TR1>;\n }\n\n // TODO: For async functions TS requires the return type to be the global\n // Promise, thus not allowing to return our Barrier type extending it.\n // Thus, we don't mark this method async for now, disabling the rule,\n // and we should think more about it in future.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n finally(onFinally?: (() => void) | null): Barrier<TR> {\n return super.finally(onFinally) as Barrier<TR>;\n }\n\n // TODO: For async functions TS requires the return type to be the global\n // Promise, thus not allowing to return our Barrier type extending it.\n // Thus, we don't mark this method async for now, disabling the rule,\n // and we should think more about it in future.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n then<TR1, TR2>(\n onFulfilled?: ((value: TR) => TR1 | PromiseLike<TR1>) | null,\n onRejected?: ((reason: unknown) => TR2 | PromiseLike<TR2>) | null,\n ): Barrier<T, TR1 | TR2> {\n const res = super.then(onFulfilled, onRejected) as Barrier<T, TR1 | TR2>;\n // TODO: Revise later.\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n res.pResolve = this.resolve;\n // TODO: Revise later.\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n res.pReject = this.reject;\n return res;\n }\n}\n"],"mappings":"IAKKA,KAAK,0BAALA,KAAK;EAALA,KAAK;EAALA,KAAK;EAALA,KAAK;EAAA,OAALA,KAAK;AAAA,EAALA,KAAK;AAMV;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,MAAMC,OAAO,SAA8BC,OAAO,CAAK;EAK5DC,MAAM,UAAGH,KAAK,CAACI,OAAO;EAE9BC,WAAWA,CAACC,QAAuB,EAAE;IACnC,IAAIC,UAAwB;IAC5B,IAAIC,SAAmB;IAEvB,KAAK,CAAC,CAACC,OAAO,EAAEC,MAAM,KAAK;MACzB;MACAH,UAAU,GAAII,KAA2B,IAAW;QAClDF,OAAO,CAACE,KAAK,CAAC;QACd,IAAI,CAACR,MAAM,GAAGH,KAAK,CAACY,QAAQ;;QAE5B;QACA;QACA;QACA;QACA;MACF,CAAC;;MAED;MACAJ,SAAS,GAAIK,MAAgB,IAAW;QACtCH,MAAM,CAACG,MAAM,CAAC;QACd,IAAI,CAACV,MAAM,GAAGH,KAAK,CAACc,QAAQ;MA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,OAAO,GAAG,IAAI,CAACN,MAAM;IACzB,OAAOiB,GAAG;EACZ;AACF","ignoreList":[]}
|
package/build/module/Emitter.js
CHANGED
|
@@ -2,17 +2,17 @@
|
|
|
2
2
|
* Simple listeneable data Emitter.
|
|
3
3
|
*/
|
|
4
4
|
export class Emitter {
|
|
5
|
-
|
|
5
|
+
pListeners = [];
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Returns "true" if any listener is connected; "false" otherwise.
|
|
9
9
|
* @return {boolean}
|
|
10
10
|
*/
|
|
11
11
|
get hasListeners() {
|
|
12
|
-
return !!this.
|
|
12
|
+
return !!this.pListeners.length;
|
|
13
13
|
}
|
|
14
14
|
get listeners() {
|
|
15
|
-
return this.
|
|
15
|
+
return this.pListeners;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
/**
|
|
@@ -21,10 +21,12 @@ export class Emitter {
|
|
|
21
21
|
* @return {function} Unsubscribe function.
|
|
22
22
|
*/
|
|
23
23
|
addListener(listener) {
|
|
24
|
-
if (!this.
|
|
25
|
-
this.
|
|
24
|
+
if (!this.pListeners.includes(listener)) {
|
|
25
|
+
this.pListeners.push(listener);
|
|
26
26
|
}
|
|
27
|
-
return () =>
|
|
27
|
+
return () => {
|
|
28
|
+
this.removeListener(listener);
|
|
29
|
+
};
|
|
28
30
|
}
|
|
29
31
|
|
|
30
32
|
/**
|
|
@@ -32,7 +34,7 @@ export class Emitter {
|
|
|
32
34
|
* @param args
|
|
33
35
|
*/
|
|
34
36
|
emit() {
|
|
35
|
-
const listeners = this.
|
|
37
|
+
const listeners = this.pListeners.slice();
|
|
36
38
|
for (const listener of listeners) listener(...arguments);
|
|
37
39
|
}
|
|
38
40
|
|
|
@@ -40,7 +42,7 @@ export class Emitter {
|
|
|
40
42
|
* Removes all connected listeners.
|
|
41
43
|
*/
|
|
42
44
|
removeAllListeners() {
|
|
43
|
-
this.
|
|
45
|
+
this.pListeners = [];
|
|
44
46
|
}
|
|
45
47
|
|
|
46
48
|
/**
|
|
@@ -48,8 +50,8 @@ export class Emitter {
|
|
|
48
50
|
* @param listener
|
|
49
51
|
*/
|
|
50
52
|
removeListener(listener) {
|
|
51
|
-
const idx = this.
|
|
52
|
-
if (idx >= 0) this.
|
|
53
|
+
const idx = this.pListeners.indexOf(listener);
|
|
54
|
+
if (idx >= 0) this.pListeners.splice(idx, 1);
|
|
53
55
|
}
|
|
54
56
|
}
|
|
55
57
|
//# sourceMappingURL=Emitter.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Emitter.js","names":["Emitter","
|
|
1
|
+
{"version":3,"file":"Emitter.js","names":["Emitter","pListeners","hasListeners","length","listeners","addListener","listener","includes","push","removeListener","emit","slice","arguments","removeAllListeners","idx","indexOf","splice"],"sources":["../../src/Emitter.ts"],"sourcesContent":["export type Listener<T extends unknown[] = unknown[]> = (...args: T) => void;\n\n/**\n * Simple listeneable data Emitter.\n */\nexport class Emitter<T extends unknown[] = unknown[]> {\n private pListeners: Array<Listener<T>> = [];\n\n /**\n * Returns \"true\" if any listener is connected; \"false\" otherwise.\n * @return {boolean}\n */\n get hasListeners(): boolean {\n return !!this.pListeners.length;\n }\n\n get listeners(): ReadonlyArray<Listener<T>> {\n return this.pListeners;\n }\n\n /**\n * Adds `listener` if it is not already connected.\n * @param {function} listener\n * @return {function} Unsubscribe function.\n */\n addListener(listener: Listener<T>): () => void {\n if (!this.pListeners.includes(listener)) {\n this.pListeners.push(listener);\n }\n return () => {\n this.removeListener(listener);\n };\n }\n\n /**\n * Calls every connected listener with the given arguments.\n * @param args\n */\n emit(...args: T): void {\n const listeners = this.pListeners.slice();\n for (const listener of listeners) listener(...args);\n }\n\n /**\n * Removes all connected listeners.\n */\n removeAllListeners(): void {\n this.pListeners = [];\n }\n\n /**\n * Removes specified `listener`, if connected.\n * @param listener\n */\n removeListener(listener: Listener<T>): void {\n const idx = this.pListeners.indexOf(listener);\n if (idx >= 0) this.pListeners.splice(idx, 1);\n }\n}\n"],"mappings":"AAEA;AACA;AACA;AACA,OAAO,MAAMA,OAAO,CAAkC;EAC5CC,UAAU,GAAuB,EAAE;;EAE3C;AACF;AACA;AACA;EACE,IAAIC,YAAYA,CAAA,EAAY;IAC1B,OAAO,CAAC,CAAC,IAAI,CAACD,UAAU,CAACE,MAAM;EACjC;EAEA,IAAIC,SAASA,CAAA,EAA+B;IAC1C,OAAO,IAAI,CAACH,UAAU;EACxB;;EAEA;AACF;AACA;AACA;AACA;EACEI,WAAWA,CAACC,QAAqB,EAAc;IAC7C,IAAI,CAAC,IAAI,CAACL,UAAU,CAACM,QAAQ,CAACD,QAAQ,CAAC,EAAE;MACvC,IAAI,CAACL,UAAU,CAACO,IAAI,CAACF,QAAQ,CAAC;IAChC;IACA,OAAO,MAAM;MACX,IAAI,CAACG,cAAc,CAACH,QAAQ,CAAC;IAC/B,CAAC;EACH;;EAEA;AACF;AACA;AACA;EACEI,IAAIA,CAAA,EAAmB;IACrB,MAAMN,SAAS,GAAG,IAAI,CAACH,UAAU,CAACU,KAAK,CAAC,CAAC;IACzC,KAAK,MAAML,QAAQ,IAAIF,SAAS,EAAEE,QAAQ,CAAC,GAAAM,SAAO,CAAC;EACrD;;EAEA;AACF;AACA;EACEC,kBAAkBA,CAAA,EAAS;IACzB,IAAI,CAACZ,UAAU,GAAG,EAAE;EACtB;;EAEA;AACF;AACA;AACA;EACEQ,cAAcA,CAACH,QAAqB,EAAQ;IAC1C,MAAMQ,GAAG,GAAG,IAAI,CAACb,UAAU,CAACc,OAAO,CAACT,QAAQ,CAAC;IAC7C,IAAIQ,GAAG,IAAI,CAAC,EAAE,IAAI,CAACb,UAAU,CAACe,MAAM,CAACF,GAAG,EAAE,CAAC,CAAC;EAC9C;AACF","ignoreList":[]}
|
|
@@ -6,37 +6,36 @@ import Barrier from './Barrier';
|
|
|
6
6
|
export default class Semaphore {
|
|
7
7
|
constructor() {
|
|
8
8
|
let ready = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
|
|
9
|
-
this.
|
|
9
|
+
this.pReady = !!ready;
|
|
10
10
|
}
|
|
11
11
|
get ready() {
|
|
12
|
-
return this.
|
|
12
|
+
return this.pReady;
|
|
13
13
|
}
|
|
14
14
|
setReady(ready) {
|
|
15
15
|
const bool = !!ready;
|
|
16
|
-
if (this.
|
|
17
|
-
this.
|
|
18
|
-
if (bool && !this.
|
|
19
|
-
this.
|
|
16
|
+
if (this.pReady !== bool) {
|
|
17
|
+
this.pReady = bool;
|
|
18
|
+
if (bool && !this.pDraining && this.pQueue.length) {
|
|
19
|
+
void this.pDrainQueue();
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
/**
|
|
25
25
|
* Waits until the semaphore is ready, and marks it as non-ready (seizes it).
|
|
26
|
-
* @return {Promise}
|
|
27
26
|
*/
|
|
28
27
|
async seize() {
|
|
29
28
|
return this.waitReady(true);
|
|
30
29
|
}
|
|
31
30
|
async waitReady() {
|
|
32
31
|
let seize = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
|
|
33
|
-
if (!this.
|
|
32
|
+
if (!this.pReady || this.pQueue.length) {
|
|
34
33
|
const barrier = new Barrier();
|
|
35
|
-
this.
|
|
34
|
+
this.pQueue.push(barrier);
|
|
36
35
|
await barrier;
|
|
37
|
-
if (seize) this.
|
|
38
|
-
this.
|
|
39
|
-
} else if (seize) this.
|
|
36
|
+
if (seize) this.pReady = false;
|
|
37
|
+
void this.pDrainLock.resolve();
|
|
38
|
+
} else if (seize) this.pReady = false;
|
|
40
39
|
}
|
|
41
40
|
|
|
42
41
|
// Private members below this point.
|
|
@@ -47,21 +46,21 @@ export default class Semaphore {
|
|
|
47
46
|
* Otherwise, it breaks the queue draining loop, which will be restarted
|
|
48
47
|
* the next time the semaphore is set ready.
|
|
49
48
|
*/
|
|
50
|
-
async
|
|
51
|
-
this.
|
|
52
|
-
while (this.
|
|
53
|
-
this.
|
|
54
|
-
this.
|
|
55
|
-
await this.
|
|
56
|
-
this.
|
|
49
|
+
async pDrainQueue() {
|
|
50
|
+
this.pDraining = true;
|
|
51
|
+
while (this.pReady && this.pQueue.length) {
|
|
52
|
+
this.pDrainLock = new Barrier();
|
|
53
|
+
void this.pQueue[0].resolve();
|
|
54
|
+
await this.pDrainLock;
|
|
55
|
+
void this.pQueue.shift();
|
|
57
56
|
}
|
|
58
|
-
this.
|
|
59
|
-
this.
|
|
57
|
+
this.pDraining = false;
|
|
58
|
+
this.pDrainLock = null;
|
|
60
59
|
}
|
|
61
60
|
|
|
62
61
|
// "true" when the drain queue process is running (and thus no need to start
|
|
63
62
|
// a new one).
|
|
64
|
-
|
|
63
|
+
pDraining = false;
|
|
65
64
|
|
|
66
65
|
// Each time a Promise from drain queue is resolved this drainLock is set
|
|
67
66
|
// to block further queue draining until the promise resolution handler
|
|
@@ -69,10 +68,10 @@ export default class Semaphore {
|
|
|
69
68
|
// to continue the draining. This is specifically important for .seize(),
|
|
70
69
|
// which should have a chance to switch semaphore state to non-ready prior
|
|
71
70
|
// to next Promise in the queue being unlocked.
|
|
72
|
-
|
|
71
|
+
pDrainLock = null;
|
|
73
72
|
|
|
74
73
|
// The array of barriers set for each async code flow awaiting for
|
|
75
74
|
// the Semaphore to become ready.
|
|
76
|
-
|
|
75
|
+
pQueue = [];
|
|
77
76
|
}
|
|
78
77
|
//# sourceMappingURL=Semaphore.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Semaphore.js","names":["Barrier","Semaphore","constructor","ready","arguments","length","undefined","
|
|
1
|
+
{"version":3,"file":"Semaphore.js","names":["Barrier","Semaphore","constructor","ready","arguments","length","undefined","pReady","setReady","bool","pDraining","pQueue","pDrainQueue","seize","waitReady","barrier","push","pDrainLock","resolve","shift"],"sources":["../../src/Semaphore.ts"],"sourcesContent":["import Barrier from './Barrier';\n\n/**\n * Implements a simple semaphore for async code logic.\n */\nexport default class Semaphore {\n constructor(ready = false) {\n this.pReady = !!ready;\n }\n\n get ready(): boolean {\n return this.pReady;\n }\n\n setReady(ready: boolean): void {\n const bool = !!ready;\n if (this.pReady !== bool) {\n this.pReady = bool;\n if (bool && !this.pDraining && this.pQueue.length) {\n void this.pDrainQueue();\n }\n }\n }\n\n /**\n * Waits until the semaphore is ready, and marks it as non-ready (seizes it).\n */\n async seize(): Promise<void> {\n return this.waitReady(true);\n }\n\n async waitReady(seize = false): Promise<void> {\n if (!this.pReady || this.pQueue.length) {\n const barrier = new Barrier<void>();\n this.pQueue.push(barrier);\n await barrier;\n if (seize) this.pReady = false;\n void this.pDrainLock!.resolve();\n } else if (seize) this.pReady = false;\n }\n\n // Private members below this point.\n\n /**\n * If semaphore is ready, it releases the next barrier in the queue, if any,\n * and reschedules itself for a call in the next event loop iteration.\n * Otherwise, it breaks the queue draining loop, which will be restarted\n * the next time the semaphore is set ready.\n */\n async pDrainQueue(): Promise<void> {\n this.pDraining = true;\n while (this.pReady && this.pQueue.length) {\n this.pDrainLock = new Barrier();\n void this.pQueue[0]!.resolve();\n await this.pDrainLock;\n void this.pQueue.shift();\n }\n this.pDraining = false;\n this.pDrainLock = null;\n }\n\n // \"true\" when the drain queue process is running (and thus no need to start\n // a new one).\n private pDraining = false;\n\n // Each time a Promise from drain queue is resolved this drainLock is set\n // to block further queue draining until the promise resolution handler\n // (.seize() or .waitReady()) unlocks it, thus confirming it is fine\n // to continue the draining. This is specifically important for .seize(),\n // which should have a chance to switch semaphore state to non-ready prior\n // to next Promise in the queue being unlocked.\n private pDrainLock: Barrier<void> | null = null;\n\n // The array of barriers set for each async code flow awaiting for\n // the Semaphore to become ready.\n private pQueue: Array<Barrier<void>> = [];\n\n private pReady: boolean;\n}\n"],"mappings":"AAAA,OAAOA,OAAO,MAAM,WAAW;;AAE/B;AACA;AACA;AACA,eAAe,MAAMC,SAAS,CAAC;EAC7BC,WAAWA,CAAA,EAAgB;IAAA,IAAfC,KAAK,GAAAC,SAAA,CAAAC,MAAA,QAAAD,SAAA,QAAAE,SAAA,GAAAF,S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I,MAAM,CAACN,MAAM,EAAE;MACxC,IAAI,CAACY,UAAU,GAAG,IAAIjB,OAAO,CAAC,CAAC;MAC/B,KAAK,IAAI,CAACW,MAAM,CAAC,CAAC,CAAC,CAAEO,OAAO,CAAC,CAAC;MAC9B,MAAM,IAAI,CAACD,UAAU;MACrB,KAAK,IAAI,CAACN,MAAM,CAACQ,KAAK,CAAC,CAAC;IAC1B;IACA,IAAI,CAACT,SAAS,GAAG,KAAK;IACtB,IAAI,CAACO,UAAU,GAAG,IAAI;EACxB;;EAEA;EACA;EACQP,SAAS,GAAG,KAAK;;EAEzB;EACA;EACA;EACA;EACA;EACA;EACQO,UAAU,GAAyB,IAAI;;EAE/C;EACA;EACQN,MAAM,GAAyB,EAAE;AAG3C","ignoreList":[]}
|
package/build/module/time.js
CHANGED
|
@@ -15,10 +15,10 @@ export const YEAR_MS = 31536000000; // 365 * DAY_MS
|
|
|
15
15
|
// from the library as well, and it should be documented later.
|
|
16
16
|
export class Timer extends Barrier {
|
|
17
17
|
get abort() {
|
|
18
|
-
return this.
|
|
18
|
+
return this.pAbort;
|
|
19
19
|
}
|
|
20
20
|
get timeout() {
|
|
21
|
-
return this.
|
|
21
|
+
return this.pTimeout;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
/**
|
|
@@ -36,24 +36,34 @@ export class Timer extends Barrier {
|
|
|
36
36
|
*/
|
|
37
37
|
constructor(executor) {
|
|
38
38
|
super(executor);
|
|
39
|
-
this.
|
|
39
|
+
this.pAbort = () => undefined;
|
|
40
40
|
}
|
|
41
41
|
init(timeout) {
|
|
42
|
-
if (this.
|
|
42
|
+
if (this.pTimeout !== undefined) {
|
|
43
43
|
throw Error('This Timer is initialized already');
|
|
44
44
|
}
|
|
45
|
-
this.
|
|
45
|
+
this.pTimeout = timeout;
|
|
46
46
|
if (timeout > 0) {
|
|
47
|
-
const id = setTimeout(
|
|
48
|
-
|
|
47
|
+
const id = setTimeout(() => {
|
|
48
|
+
void super.resolve();
|
|
49
|
+
}, timeout);
|
|
50
|
+
this.pAbort = () => {
|
|
51
|
+
clearTimeout(id);
|
|
52
|
+
};
|
|
49
53
|
} else {
|
|
50
|
-
super.resolve();
|
|
54
|
+
void super.resolve();
|
|
51
55
|
}
|
|
52
56
|
return this;
|
|
53
57
|
}
|
|
58
|
+
|
|
59
|
+
// TODO: For async functions TS requires the return type to be the global
|
|
60
|
+
// Promise, thus not allowing to return our Timer type extending that via
|
|
61
|
+
// Barrier. Thus, we don't mark this method async for now, disabling the rule,
|
|
62
|
+
// and we should think more about it in future.
|
|
63
|
+
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
54
64
|
then(onFulfilled, onRejected) {
|
|
55
65
|
const res = super.then(onFulfilled, onRejected);
|
|
56
|
-
if (this.timeout !== undefined) res.init(this.timeout);
|
|
66
|
+
if (this.timeout !== undefined) void res.init(this.timeout);
|
|
57
67
|
return res;
|
|
58
68
|
}
|
|
59
69
|
}
|
|
@@ -65,6 +75,11 @@ export class Timer extends Barrier {
|
|
|
65
75
|
* .abort() method attached, which cancels the pending timer resolution
|
|
66
76
|
* (without resolving or rejecting the barrier).
|
|
67
77
|
*/
|
|
78
|
+
// TODO: For async functions TS requires the return type to be the global
|
|
79
|
+
// Promise, thus not allowing to return our Timer type extending that via
|
|
80
|
+
// Barrier. Thus, we don't mark this method async for now, disabling the rule,
|
|
81
|
+
// and we should think more about it in future.
|
|
82
|
+
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
68
83
|
export function timer(timeout) {
|
|
69
84
|
const t = new Timer();
|
|
70
85
|
return t.init(timeout);
|
package/build/module/time.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"time.js","names":["Barrier","SEC_MS","MIN_MS","HOUR_MS","DAY_MS","YEAR_MS","Timer","abort","
|
|
1
|
+
{"version":3,"file":"time.js","names":["Barrier","SEC_MS","MIN_MS","HOUR_MS","DAY_MS","YEAR_MS","Timer","abort","pAbort","timeout","pTimeout","constructor","executor","undefined","init","Error","id","setTimeout","resolve","clearTimeout","then","onFulfilled","onRejected","res","timer","t"],"sources":["../../src/time.ts"],"sourcesContent":["import Barrier, { type Executor } from './Barrier';\n\n// This is not very elegant, but as of now TypeScript does not support type\n// arithmetic, thus we can't have constants assigned like `MIN_MS = 60 * SEC_MS`\n// and have the result type to be 60000 (number literal), it would be just\n// the generic number type.\nexport const SEC_MS = 1000;\nexport const MIN_MS = 60000; // 60 * SEC_MS\nexport const HOUR_MS = 3600000; // 60 * MIN_MS\nexport const DAY_MS = 86400000; // 24 * HOUR_MS\nexport const YEAR_MS = 31536000000; // 365 * DAY_MS\n\n// TODO: Ok, as we have ended up with a Timer class, mostly to achieve a good\n// TypeScript typing for timer() function, it makes sense to expose the class\n// from the library as well, and it should be documented later.\nexport class Timer<T> extends Barrier<void, T> {\n private pAbort: () => void;\n\n private pTimeout?: number;\n\n get abort(): () => void {\n return this.pAbort;\n }\n\n get timeout(): number | undefined {\n return this.pTimeout;\n }\n\n /**\n * Creates a new, non-initialized instance of Timer. Call .init() method\n * to actually initialize and launch the timer.\n *\n * NOTE: Although it might be tempting to accept `timeout` value as\n * a constructor's argument, it won't work well, because Timer is an\n * extension of Promise (via Barrier), and the way Promises works (in\n * particular their .then() method, which internally calls constructor()\n * with special executor) does not play along with initalization depending\n * on custom parameters done in constructor().\n *\n * @param executor\n */\n constructor(executor?: Executor<T>) {\n super(executor);\n this.pAbort = () => undefined;\n }\n\n init(timeout: number): this {\n if (this.pTimeout !== undefined) {\n throw Error('This Timer is initialized already');\n }\n this.pTimeout = timeout;\n if (timeout > 0) {\n const id = setTimeout(() => {\n void super.resolve();\n }, timeout);\n this.pAbort = () => {\n clearTimeout(id);\n };\n } else {\n void super.resolve();\n }\n return this;\n }\n\n // TODO: For async functions TS requires the return type to be the global\n // Promise, thus not allowing to return our Timer type extending that via\n // Barrier. Thus, we don't mark this method async for now, disabling the rule,\n // and we should think more about it in future.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n then<TR1, TR2>(\n onFulfilled?: ((value: T) => TR1 | PromiseLike<TR1>) | null,\n onRejected?: ((reason: unknown) => TR2 | PromiseLike<TR2>) | null,\n ): Timer<TR1 | TR2> {\n const res = super.then(onFulfilled, onRejected) as Timer<TR1 | TR2>;\n if (this.timeout !== undefined) void res.init(this.timeout);\n return res;\n }\n}\n\n/**\n * Creates a Promise, which resolves after the given timeout.\n * @param {number} timeout Timeout [ms].\n * @return {Barrier} Resolves after the timeout. It has additional\n * .abort() method attached, which cancels the pending timer resolution\n * (without resolving or rejecting the barrier).\n */\n// TODO: For async functions TS requires the return type to be the global\n// Promise, thus not allowing to return our Timer type extending that via\n// Barrier. Thus, we don't mark this method async for now, disabling the rule,\n// and we should think more about it in future.\n// eslint-disable-next-line @typescript-eslint/promise-function-async\nexport function timer(timeout: number): Timer<void> {\n const t = new Timer<void>();\n return t.init(timeout);\n}\n"],"mappings":"AAAA,OAAOA,OAAO,MAAyB,WAAW;;AAElD;AACA;AACA;AACA;AACA,OAAO,MAAMC,MAAM,GAAG,IAAI;AAC1B,OAAO,MAAMC,MAAM,GAAG,KAAK,CAAC,CAAC;AAC7B,OAAO,MAAMC,OAAO,GAAG,OAAO,CAAC,CAAC;AAChC,OAAO,MAAMC,MAAM,GAAG,QAAQ,CAAC,CAAC;AAChC,OAAO,MAAMC,OAAO,GAAG,WAAW,CAAC,CAAC;;AAEpC;AACA;AACA;AACA,OAAO,MAAMC,KAAK,SAAYN,OAAO,CAAU;EAK7C,IAAIO,KAAKA,CAAA,EAAe;IACtB,OAAO,IAAI,CAACC,MAAM;EACpB;EAEA,IAAIC,OAAOA,CAAA,EAAuB;IAChC,OAAO,IAAI,CAACC,QAAQ;EACtB;;EAEA;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACEC,WAAWA,CAACC,QAAsB,EAAE;IAClC,KAAK,CAACA,QAAQ,CAAC;IACf,IAAI,CAACJ,MAAM,GAAG,MAAMK,SAAS;EAC/B;EAEAC,IAAIA,CAACL,OAAe,EAAQ;IAC1B,IAAI,IAAI,CAACC,QAAQ,KAAKG,SAAS,EAAE;MAC/B,MAAME,KAAK,CAAC,mCAAmC,CAAC;IAClD;IACA,IAAI,CAACL,QAAQ,GAAGD,OAAO;IACvB,IAAIA,OAAO,GAAG,CAAC,EAAE;MACf,MAAMO,EAAE,GAAGC,UAAU,CAAC,MAAM;QAC1B,KAAK,KAAK,CAACC,OAAO,CAAC,CAAC;MACtB,CAAC,EAAET,OAAO,CAAC;MACX,IAAI,CAACD,MAAM,GAAG,MAAM;QAClBW,YAAY,CAACH,EAAE,CAAC;MAClB,CAAC;IACH,CAAC,MAAM;MACL,KAAK,KAAK,CAACE,OAAO,CAAC,CAAC;IACtB;IACA,OAAO,IAAI;EACb;;EAEA;EACA;EACA;EACA;EACA;EACAE,IAAIA,CACFC,WAA2D,EAC3DC,UAAiE,EAC/C;IAClB,MAAMC,GAAG,GAAG,KAAK,CAACH,IAAI,CAACC,WAAW,EAAEC,UAAU,CAAqB;IACnE,IAAI,IAAI,CAACb,OAAO,KAAKI,SAAS,EAAE,KAAKU,GAAG,CAACT,IAAI,CAAC,IAAI,CAACL,OAAO,CAAC;IAC3D,OAAOc,GAAG;EACZ;AACF;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,KAAKA,CAACf,OAAe,EAAe;EAClD,MAAMgB,CAAC,GAAG,IAAInB,KAAK,CAAO,CAAC;EAC3B,OAAOmB,CAAC,CAACX,IAAI,CAACL,OAAO,CAAC;AACxB","ignoreList":[]}
|
|
@@ -14,7 +14,6 @@ import { timer } from './time';
|
|
|
14
14
|
export default async function withRetries(action) {
|
|
15
15
|
let maxRetries = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 3;
|
|
16
16
|
let interval = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 300;
|
|
17
|
-
/* eslint-disable no-await-in-loop */
|
|
18
17
|
for (let n = 1;; ++n) {
|
|
19
18
|
try {
|
|
20
19
|
const res = action();
|
|
@@ -23,6 +22,5 @@ export default async function withRetries(action) {
|
|
|
23
22
|
if (n < maxRetries) await timer(interval);else throw error;
|
|
24
23
|
}
|
|
25
24
|
}
|
|
26
|
-
/* eslint-enable no-await-in-loop */
|
|
27
25
|
}
|
|
28
26
|
//# sourceMappingURL=withRetries.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"withRetries.js","names":["timer","withRetries","action","maxRetries","arguments","length","undefined","interval","n","res","Promise","error"],"sources":["../../src/withRetries.ts"],"sourcesContent":["import { timer } from './time';\n\n/**\n * Attempts to perform the given async `action` up to `maxRetries` times with\n * the specified `interval`, stopping at the first successful (non-throwing)\n * execution.\n * @param action\n * @param maxRetries Optional. The maximum number of re-tries. Defaults 3.\n * @param interval Optional. The interval between re-tries (in milliseconds).\n * Defaults to 300ms.\n * @returns Resolves to the result of the successful `action` execution;\n * or rejects with the error from the last faileda attempt.\n */\nexport default async function withRetries<T>(\n action: () => T,\n maxRetries = 3,\n interval = 300,\n): Promise<Awaited<T>> {\n
|
|
1
|
+
{"version":3,"file":"withRetries.js","names":["timer","withRetries","action","maxRetries","arguments","length","undefined","interval","n","res","Promise","error"],"sources":["../../src/withRetries.ts"],"sourcesContent":["import { timer } from './time';\n\n/**\n * Attempts to perform the given async `action` up to `maxRetries` times with\n * the specified `interval`, stopping at the first successful (non-throwing)\n * execution.\n * @param action\n * @param maxRetries Optional. The maximum number of re-tries. Defaults 3.\n * @param interval Optional. The interval between re-tries (in milliseconds).\n * Defaults to 300ms.\n * @returns Resolves to the result of the successful `action` execution;\n * or rejects with the error from the last faileda attempt.\n */\nexport default async function withRetries<T>(\n action: () => T,\n maxRetries = 3,\n interval = 300,\n): Promise<Awaited<T>> {\n for (let n = 1; ; ++n) {\n try {\n const res = action();\n return res instanceof Promise ? await res : (res as Awaited<T>);\n } catch (error) {\n if (n < maxRetries) await timer(interval);\n else throw error;\n }\n }\n}\n"],"mappings":"AAAA,SAASA,KAAK,QAAQ,QAAQ;;AAE9B;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,eAAeC,WAAWA,CACvCC,MAAe,EAGM;EAAA,IAFrBC,UAAU,GAAAC,SAAA,CAAAC,MAAA,QAAAD,SAAA,QAAAE,SAAA,GAAAF,SAAA,MAAG,CAAC;EAAA,IACdG,QAAQ,GAAAH,SAAA,CAAAC,MAAA,QAAAD,SAAA,QAAAE,SAAA,GAAAF,SAAA,MAAG,GAAG;EAEd,KAAK,IAAII,CAAC,GAAG,CAAC,GAAI,EAAEA,CAAC,EAAE;IACrB,IAAI;MACF,MAAMC,GAAG,GAAGP,MAAM,CAAC,CAAC;MACpB,OAAOO,GAAG,YAAYC,OAAO,GAAG,MAAMD,GAAG,GAAIA,GAAkB;IACjE,CAAC,CAAC,OAAOE,KAAK,EAAE;MACd,IAAIH,CAAC,GAAGL,UAAU,EAAE,MAAMH,KAAK,CAACO,QAAQ,CAAC,CAAC,KACrC,MAAMI,KAAK;IAClB;EACF;AACF","ignoreList":[]}
|
package/build/types/Barrier.d.ts
CHANGED
|
@@ -25,17 +25,17 @@ type Rejecter = Parameters<Executor<unknown>>[1];
|
|
|
25
25
|
* Docs: https://dr.pogodin.studio/docs/react-utils/docs/api/classes/Barrier
|
|
26
26
|
*/
|
|
27
27
|
export default class Barrier<T = unknown, TR = T> extends Promise<TR> {
|
|
28
|
-
private
|
|
29
|
-
private
|
|
30
|
-
private
|
|
28
|
+
private pResolve;
|
|
29
|
+
private pReject;
|
|
30
|
+
private pState;
|
|
31
31
|
constructor(executor?: Executor<TR>);
|
|
32
32
|
get resolve(): (arg: Parameters<Resolver<T>>[0]) => this;
|
|
33
33
|
get reject(): (arg: Parameters<Rejecter>[0]) => this;
|
|
34
34
|
get resolved(): boolean;
|
|
35
35
|
get rejected(): boolean;
|
|
36
36
|
get settled(): boolean;
|
|
37
|
-
catch<TR1>(onRejected?: ((reason:
|
|
37
|
+
catch<TR1>(onRejected?: ((reason: unknown) => TR1 | PromiseLike<TR1>) | null): Barrier<T, TR1>;
|
|
38
38
|
finally(onFinally?: (() => void) | null): Barrier<TR>;
|
|
39
|
-
then<TR1, TR2>(onFulfilled?: ((value: TR) => TR1 | PromiseLike<TR1>) | null, onRejected?: ((reason:
|
|
39
|
+
then<TR1, TR2>(onFulfilled?: ((value: TR) => TR1 | PromiseLike<TR1>) | null, onRejected?: ((reason: unknown) => TR2 | PromiseLike<TR2>) | null): Barrier<T, TR1 | TR2>;
|
|
40
40
|
}
|
|
41
41
|
export {};
|
package/build/types/Emitter.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export type Listener<T extends unknown[] = unknown[]> = (...args: T) => void;
|
|
|
3
3
|
* Simple listeneable data Emitter.
|
|
4
4
|
*/
|
|
5
5
|
export declare class Emitter<T extends unknown[] = unknown[]> {
|
|
6
|
-
private
|
|
6
|
+
private pListeners;
|
|
7
7
|
/**
|
|
8
8
|
* Returns "true" if any listener is connected; "false" otherwise.
|
|
9
9
|
* @return {boolean}
|
|
@@ -7,7 +7,6 @@ export default class Semaphore {
|
|
|
7
7
|
setReady(ready: boolean): void;
|
|
8
8
|
/**
|
|
9
9
|
* Waits until the semaphore is ready, and marks it as non-ready (seizes it).
|
|
10
|
-
* @return {Promise}
|
|
11
10
|
*/
|
|
12
11
|
seize(): Promise<void>;
|
|
13
12
|
waitReady(seize?: boolean): Promise<void>;
|
|
@@ -17,9 +16,9 @@ export default class Semaphore {
|
|
|
17
16
|
* Otherwise, it breaks the queue draining loop, which will be restarted
|
|
18
17
|
* the next time the semaphore is set ready.
|
|
19
18
|
*/
|
|
20
|
-
|
|
21
|
-
private
|
|
22
|
-
private
|
|
23
|
-
private
|
|
24
|
-
private
|
|
19
|
+
pDrainQueue(): Promise<void>;
|
|
20
|
+
private pDraining;
|
|
21
|
+
private pDrainLock;
|
|
22
|
+
private pQueue;
|
|
23
|
+
private pReady;
|
|
25
24
|
}
|
package/build/types/time.d.ts
CHANGED
|
@@ -5,8 +5,8 @@ export declare const HOUR_MS = 3600000;
|
|
|
5
5
|
export declare const DAY_MS = 86400000;
|
|
6
6
|
export declare const YEAR_MS = 31536000000;
|
|
7
7
|
export declare class Timer<T> extends Barrier<void, T> {
|
|
8
|
-
private
|
|
9
|
-
private
|
|
8
|
+
private pAbort;
|
|
9
|
+
private pTimeout?;
|
|
10
10
|
get abort(): () => void;
|
|
11
11
|
get timeout(): number | undefined;
|
|
12
12
|
/**
|
|
@@ -23,8 +23,8 @@ export declare class Timer<T> extends Barrier<void, T> {
|
|
|
23
23
|
* @param executor
|
|
24
24
|
*/
|
|
25
25
|
constructor(executor?: Executor<T>);
|
|
26
|
-
init(timeout: number):
|
|
27
|
-
then<TR1, TR2>(onFulfilled?: ((value: T) => TR1 | PromiseLike<TR1>) | null, onRejected?: ((reason:
|
|
26
|
+
init(timeout: number): this;
|
|
27
|
+
then<TR1, TR2>(onFulfilled?: ((value: T) => TR1 | PromiseLike<TR1>) | null, onRejected?: ((reason: unknown) => TR2 | PromiseLike<TR2>) | null): Timer<TR1 | TR2>;
|
|
28
28
|
}
|
|
29
29
|
/**
|
|
30
30
|
* Creates a Promise, which resolves after the given timeout.
|
package/config/babel/preset.js
CHANGED
|
@@ -2,9 +2,9 @@ export default function preset(api, options) {
|
|
|
2
2
|
let envPreset = '@babel/env';
|
|
3
3
|
if (options) envPreset = [envPreset, options];
|
|
4
4
|
return {
|
|
5
|
-
presets: [envPreset, '@babel/typescript'],
|
|
6
5
|
plugins: [
|
|
7
6
|
'@babel/plugin-transform-runtime',
|
|
8
7
|
],
|
|
8
|
+
presets: [envPreset, '@babel/typescript'],
|
|
9
9
|
};
|
|
10
10
|
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# The Common Sense Versioning
|
|
2
|
+
|
|
3
|
+
**Not quite [Semantic Versioning (SemVer)][SemVer]**
|
|
4
|
+
|
|
5
|
+
> It is sad when instructions for using (mostly) three-number-segment version
|
|
6
|
+
> signatures are six pages long.
|
|
7
|
+
|
|
8
|
+
0. The primary purpose of **MAJOR**.**MINOR**.**PATCH** version number is
|
|
9
|
+
to hint the product consumer on time, efforts, and risks necessary to adopt
|
|
10
|
+
the new product release. It is a hint, not a guarantee — no matter
|
|
11
|
+
the rules, mistakes will happen, patch releases will break stuff, and
|
|
12
|
+
thus following versioning rules does not cancel the need for adequate
|
|
13
|
+
Quality Assurance (QA), locking-down, and tracking of the exact versions of
|
|
14
|
+
tested dependencies in consumer's products.
|
|
15
|
+
|
|
16
|
+
1. **PATCH** increments mean changes safe to adopt without any modifications in
|
|
17
|
+
consumer's products — both backward-compatible internal changes, and
|
|
18
|
+
backward-compatible additions of new functionality (_i.e._ both [SemVer]'s
|
|
19
|
+
PATCH and MINOR updates).
|
|
20
|
+
|
|
21
|
+
2. **MINOR** increments mean changes that require minor updates in consumer's
|
|
22
|
+
products — simple, mechanical, local changes, that should not take
|
|
23
|
+
much efforts and time (_i.e._ less severe of [SemVer]'s MAJOR updates).
|
|
24
|
+
|
|
25
|
+
3. **MAJOR** increments mean (rare) changes that require major updates in
|
|
26
|
+
consumer's products — global revisions of how the versioned product is
|
|
27
|
+
consumed, and global re-works of how it should be consumed starting with
|
|
28
|
+
the new major release (_i.e._ severe of [SemVer]'s MAJOR updates).
|
|
29
|
+
|
|
30
|
+
4. Otherwise version numbers follow [SemVer] semantics, for compatibility with
|
|
31
|
+
existing tools — they are compared starting with their left-most
|
|
32
|
+
segment to the right-most one; increments of a segment are accompanied
|
|
33
|
+
with reset to zero of all segments to the right of it; additional suffixes
|
|
34
|
+
as `-alpha.0`, or `+1`, _etc._ are allowed and understood, in general, as
|
|
35
|
+
special «risky» releases, which should be adopted at the risk of
|
|
36
|
+
consumer, if he has consulted corresponding release notes, and knows what
|
|
37
|
+
he is doing.
|
|
38
|
+
|
|
39
|
+
[SemVer]: https://semver.org
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/* eslint-disable import/no-extraneous-dependencies */
|
|
2
|
+
|
|
3
|
+
import { defineConfig } from 'eslint/config';
|
|
4
|
+
import eslintConfigs from '@dr.pogodin/eslint-configs';
|
|
5
|
+
|
|
6
|
+
export default defineConfig([
|
|
7
|
+
{ ignores: ['build/'] },
|
|
8
|
+
eslintConfigs.configs.javascript,
|
|
9
|
+
eslintConfigs.configs.typescript,
|
|
10
|
+
{
|
|
11
|
+
extends: [eslintConfigs.configs.jest],
|
|
12
|
+
files: ['__tests__/**'],
|
|
13
|
+
},
|
|
14
|
+
]);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dr.pogodin/js-utils",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.18",
|
|
4
4
|
"description": "Collection of JavaScript (TypeScript) utilities.",
|
|
5
5
|
"main": "build/common/index.js",
|
|
6
6
|
"module": "build/module/index.js",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"jest": "npm run jest:types && npm run jest:logic",
|
|
19
19
|
"jest:logic": "jest --config jest.config.js",
|
|
20
20
|
"jest:types": "tstyche",
|
|
21
|
-
"lint": "eslint
|
|
21
|
+
"lint": "eslint",
|
|
22
22
|
"test": "npm run lint && npm run typecheck && npm run jest",
|
|
23
23
|
"typecheck": "tsc && tsc --project __tests__/tsconfig.json"
|
|
24
24
|
},
|
|
@@ -38,23 +38,21 @@
|
|
|
38
38
|
},
|
|
39
39
|
"homepage": "https://github.com/birdofpreyru/js-utils#readme",
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"@babel/cli": "^7.
|
|
42
|
-
"@babel/core": "^7.
|
|
43
|
-
"@babel/plugin-transform-runtime": "^7.
|
|
44
|
-
"@babel/preset-env": "^7.
|
|
45
|
-
"@babel/preset-typescript": "^7.
|
|
41
|
+
"@babel/cli": "^7.27.1",
|
|
42
|
+
"@babel/core": "^7.27.1",
|
|
43
|
+
"@babel/plugin-transform-runtime": "^7.27.1",
|
|
44
|
+
"@babel/preset-env": "^7.27.1",
|
|
45
|
+
"@babel/preset-typescript": "^7.27.1",
|
|
46
|
+
"@dr.pogodin/eslint-configs": "^0.0.5",
|
|
46
47
|
"@tsconfig/recommended": "^1.0.8",
|
|
47
48
|
"@types/jest": "^29.5.14",
|
|
48
49
|
"babel-jest": "^29.7.0",
|
|
49
|
-
"eslint": "^8.57.1",
|
|
50
|
-
"eslint-config-airbnb-base": "^15.0.0",
|
|
51
|
-
"eslint-config-airbnb-typescript": "^18.0.0",
|
|
52
|
-
"eslint-import-resolver-typescript": "^3.8.3",
|
|
53
|
-
"eslint-plugin-import": "^2.31.0",
|
|
54
50
|
"jest": "^29.7.0",
|
|
55
51
|
"rimraf": "^6.0.1",
|
|
56
52
|
"tstyche": "^3.5.0",
|
|
57
|
-
"typescript": "^5.
|
|
58
|
-
|
|
53
|
+
"typescript": "^5.8.3"
|
|
54
|
+
},
|
|
55
|
+
"dependencies": {
|
|
56
|
+
"@babel/runtime": "^7.27.1"
|
|
59
57
|
}
|
|
60
58
|
}
|