@dr.pogodin/js-utils 0.0.11 → 0.0.13
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/build/Barrier.d.ts +22 -5
- package/build/Barrier.js +40 -2
- package/build/time.js +2 -2
- package/build/withRetries.js +1 -1
- package/package.json +13 -13
- package/src/Barrier.ts +52 -9
- package/tstyche.config.json +4 -1
package/build/Barrier.d.ts
CHANGED
|
@@ -1,10 +1,27 @@
|
|
|
1
|
-
type
|
|
2
|
-
type
|
|
3
|
-
|
|
1
|
+
export type Executor<T> = ConstructorParameters<typeof Promise<T>>[0];
|
|
2
|
+
type Resolver<T> = Parameters<Executor<T>>[0];
|
|
3
|
+
type Rejecter = Parameters<Executor<unknown>>[1];
|
|
4
4
|
/**
|
|
5
5
|
* Barrier is just a Promise which has resolve and reject exposed as instance
|
|
6
6
|
* methods.
|
|
7
7
|
*
|
|
8
|
+
* It has two generic arguments T and TR which correspond to the argument of
|
|
9
|
+
* the .resolve() method, and to the value resolved by the promise (barrier).
|
|
10
|
+
* For a simple barrier TR equals to T, however for barriers created via .then()
|
|
11
|
+
* chain, T corresponds to the argument of the original barrier, and TR to
|
|
12
|
+
* the value resolved by the latest promise in the chain. Consider this:
|
|
13
|
+
*
|
|
14
|
+
* const b = new Barrier<string>();
|
|
15
|
+
* b.resolve('result');
|
|
16
|
+
* const s = await b; // `s` has `string` type, and equals "result".
|
|
17
|
+
*
|
|
18
|
+
* const b = (new Barrier<string>()).then((s) => s.length);
|
|
19
|
+
* b.resolve('result'); // Chained barrier exposes .resolve() method of
|
|
20
|
+
* // the first barrier in the chain, which expects
|
|
21
|
+
* // `string` arugment (T), but the chained barrier
|
|
22
|
+
* // resolves to `number` (TR).
|
|
23
|
+
* const n = await b; // `n` has `number` type, and equals 6.
|
|
24
|
+
*
|
|
8
25
|
* Docs: https://dr.pogodin.studio/docs/react-utils/docs/api/classes/Barrier
|
|
9
26
|
*/
|
|
10
27
|
export default class Barrier<T = unknown, TR = T> extends Promise<TR> {
|
|
@@ -12,8 +29,8 @@ export default class Barrier<T = unknown, TR = T> extends Promise<TR> {
|
|
|
12
29
|
private p_reject;
|
|
13
30
|
private p_state;
|
|
14
31
|
constructor(executor?: Executor<TR>);
|
|
15
|
-
get resolve(): Resolver<T
|
|
16
|
-
get reject(): Rejecter;
|
|
32
|
+
get resolve(): (arg: Parameters<Resolver<T>>[0]) => this;
|
|
33
|
+
get reject(): (arg: Parameters<Rejecter>[0]) => this;
|
|
17
34
|
get resolved(): boolean;
|
|
18
35
|
get rejected(): boolean;
|
|
19
36
|
get settled(): boolean;
|
package/build/Barrier.js
CHANGED
|
@@ -10,6 +10,23 @@ var STATE;
|
|
|
10
10
|
* Barrier is just a Promise which has resolve and reject exposed as instance
|
|
11
11
|
* methods.
|
|
12
12
|
*
|
|
13
|
+
* It has two generic arguments T and TR which correspond to the argument of
|
|
14
|
+
* the .resolve() method, and to the value resolved by the promise (barrier).
|
|
15
|
+
* For a simple barrier TR equals to T, however for barriers created via .then()
|
|
16
|
+
* chain, T corresponds to the argument of the original barrier, and TR to
|
|
17
|
+
* the value resolved by the latest promise in the chain. Consider this:
|
|
18
|
+
*
|
|
19
|
+
* const b = new Barrier<string>();
|
|
20
|
+
* b.resolve('result');
|
|
21
|
+
* const s = await b; // `s` has `string` type, and equals "result".
|
|
22
|
+
*
|
|
23
|
+
* const b = (new Barrier<string>()).then((s) => s.length);
|
|
24
|
+
* b.resolve('result'); // Chained barrier exposes .resolve() method of
|
|
25
|
+
* // the first barrier in the chain, which expects
|
|
26
|
+
* // `string` arugment (T), but the chained barrier
|
|
27
|
+
* // resolves to `number` (TR).
|
|
28
|
+
* const n = await b; // `n` has `number` type, and equals 6.
|
|
29
|
+
*
|
|
13
30
|
* Docs: https://dr.pogodin.studio/docs/react-utils/docs/api/classes/Barrier
|
|
14
31
|
*/
|
|
15
32
|
class Barrier extends Promise {
|
|
@@ -17,10 +34,17 @@ class Barrier extends Promise {
|
|
|
17
34
|
let resolveRef;
|
|
18
35
|
let rejectRef;
|
|
19
36
|
super((resolve, reject) => {
|
|
37
|
+
// Note: Enforcing `void` return type because of the BEWARE note below.
|
|
20
38
|
resolveRef = (value) => {
|
|
21
39
|
resolve(value);
|
|
22
40
|
this.p_state = STATE.RESOLVED;
|
|
41
|
+
// BEWARE: Don't try to return `this` here, it will easily cause
|
|
42
|
+
// infinite loops in React Native, which are extremely difficult
|
|
43
|
+
// to troubleshoot (I wasn't able to figure out, are they due to
|
|
44
|
+
// internal Promise implementation in RN, or because of some bad
|
|
45
|
+
// patterns in the host code).
|
|
23
46
|
};
|
|
47
|
+
// Note: Enforcing `void` return type because of the BEWARE note below.
|
|
24
48
|
rejectRef = (reason) => {
|
|
25
49
|
reject(reason);
|
|
26
50
|
this.p_state = STATE.REJECTED;
|
|
@@ -29,11 +53,25 @@ class Barrier extends Promise {
|
|
|
29
53
|
executor(resolveRef, rejectRef);
|
|
30
54
|
});
|
|
31
55
|
this.p_state = STATE.PENDING;
|
|
56
|
+
// NOTE: We assume, the only scenario where TR is not equal T is when
|
|
57
|
+
// the Barrier is constructed by a .then() call on a "parent" barrier,
|
|
58
|
+
// and in that scenario .then() itself will replace .p_resolve by another
|
|
59
|
+
// resolver immediately after this constructor returns.
|
|
32
60
|
this.p_resolve = resolveRef;
|
|
33
61
|
this.p_reject = rejectRef;
|
|
34
62
|
}
|
|
35
|
-
get resolve() {
|
|
36
|
-
|
|
63
|
+
get resolve() {
|
|
64
|
+
return (arg) => {
|
|
65
|
+
this.p_resolve(arg);
|
|
66
|
+
return this;
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
get reject() {
|
|
70
|
+
return (arg) => {
|
|
71
|
+
this.p_reject(arg);
|
|
72
|
+
return this;
|
|
73
|
+
};
|
|
74
|
+
}
|
|
37
75
|
get resolved() { return this.p_state === STATE.RESOLVED; }
|
|
38
76
|
get rejected() { return this.p_state === STATE.REJECTED; }
|
|
39
77
|
get settled() { return this.p_state !== STATE.PENDING; }
|
package/build/time.js
CHANGED
|
@@ -3,7 +3,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
6
|
+
exports.Timer = exports.YEAR_MS = exports.DAY_MS = exports.HOUR_MS = exports.MIN_MS = exports.SEC_MS = void 0;
|
|
7
|
+
exports.timer = timer;
|
|
7
8
|
const Barrier_1 = __importDefault(require("./Barrier"));
|
|
8
9
|
// This is not very elegant, but as of now TypeScript does not support type
|
|
9
10
|
// arithmetic, thus we can't have constants assigned like `MIN_MS = 60 * SEC_MS`
|
|
@@ -70,4 +71,3 @@ function timer(timeout) {
|
|
|
70
71
|
const t = new Timer();
|
|
71
72
|
return t.init(timeout);
|
|
72
73
|
}
|
|
73
|
-
exports.timer = timer;
|
package/build/withRetries.js
CHANGED
|
@@ -9,6 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.default = withRetries;
|
|
12
13
|
const time_1 = require("./time");
|
|
13
14
|
/**
|
|
14
15
|
* Attempts to perform the given async `action` up to `maxRetries` times with
|
|
@@ -39,4 +40,3 @@ function withRetries(action_1) {
|
|
|
39
40
|
/* eslint-enable no-await-in-loop */
|
|
40
41
|
});
|
|
41
42
|
}
|
|
42
|
-
exports.default = withRetries;
|
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.13",
|
|
4
4
|
"description": "Collection of JavaScript (TypeScript) utilities.",
|
|
5
5
|
"main": "build/index",
|
|
6
6
|
"react-native": "src/index",
|
|
@@ -31,21 +31,21 @@
|
|
|
31
31
|
},
|
|
32
32
|
"homepage": "https://github.com/birdofpreyru/js-utils#readme",
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@babel/core": "^7.
|
|
35
|
-
"@babel/preset-env": "^7.
|
|
36
|
-
"@babel/preset-typescript": "^7.
|
|
37
|
-
"@tsconfig/recommended": "^1.0.
|
|
38
|
-
"@types/jest": "^29.5.
|
|
34
|
+
"@babel/core": "^7.26.0",
|
|
35
|
+
"@babel/preset-env": "^7.26.0",
|
|
36
|
+
"@babel/preset-typescript": "^7.26.0",
|
|
37
|
+
"@tsconfig/recommended": "^1.0.8",
|
|
38
|
+
"@types/jest": "^29.5.14",
|
|
39
39
|
"babel-jest": "^29.7.0",
|
|
40
|
-
"eslint": "^8.57.
|
|
40
|
+
"eslint": "^8.57.1",
|
|
41
41
|
"eslint-config-airbnb-base": "^15.0.0",
|
|
42
42
|
"eslint-config-airbnb-typescript": "^18.0.0",
|
|
43
|
-
"eslint-import-resolver-typescript": "^3.
|
|
44
|
-
"eslint-plugin-import": "^2.
|
|
43
|
+
"eslint-import-resolver-typescript": "^3.7.0",
|
|
44
|
+
"eslint-plugin-import": "^2.31.0",
|
|
45
45
|
"jest": "^29.7.0",
|
|
46
|
-
"rimraf": "^
|
|
47
|
-
"tstyche": "^
|
|
48
|
-
"typescript": "^5.
|
|
49
|
-
"typescript-eslint": "^
|
|
46
|
+
"rimraf": "^6.0.1",
|
|
47
|
+
"tstyche": "^3.2.0",
|
|
48
|
+
"typescript": "^5.7.2",
|
|
49
|
+
"typescript-eslint": "^8.18.2"
|
|
50
50
|
}
|
|
51
51
|
}
|
package/src/Barrier.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
type
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
export type Executor<T> = ConstructorParameters<typeof Promise<T>>[0];
|
|
2
|
+
|
|
3
|
+
type Resolver<T> = Parameters<Executor<T>>[0];
|
|
4
|
+
type Rejecter = Parameters<Executor<unknown>>[1];
|
|
4
5
|
|
|
5
6
|
enum STATE {
|
|
6
7
|
PENDING = 'PENDING',
|
|
@@ -12,10 +13,27 @@ enum STATE {
|
|
|
12
13
|
* Barrier is just a Promise which has resolve and reject exposed as instance
|
|
13
14
|
* methods.
|
|
14
15
|
*
|
|
16
|
+
* It has two generic arguments T and TR which correspond to the argument of
|
|
17
|
+
* the .resolve() method, and to the value resolved by the promise (barrier).
|
|
18
|
+
* For a simple barrier TR equals to T, however for barriers created via .then()
|
|
19
|
+
* chain, T corresponds to the argument of the original barrier, and TR to
|
|
20
|
+
* the value resolved by the latest promise in the chain. Consider this:
|
|
21
|
+
*
|
|
22
|
+
* const b = new Barrier<string>();
|
|
23
|
+
* b.resolve('result');
|
|
24
|
+
* const s = await b; // `s` has `string` type, and equals "result".
|
|
25
|
+
*
|
|
26
|
+
* const b = (new Barrier<string>()).then((s) => s.length);
|
|
27
|
+
* b.resolve('result'); // Chained barrier exposes .resolve() method of
|
|
28
|
+
* // the first barrier in the chain, which expects
|
|
29
|
+
* // `string` arugment (T), but the chained barrier
|
|
30
|
+
* // resolves to `number` (TR).
|
|
31
|
+
* const n = await b; // `n` has `number` type, and equals 6.
|
|
32
|
+
*
|
|
15
33
|
* Docs: https://dr.pogodin.studio/docs/react-utils/docs/api/classes/Barrier
|
|
16
34
|
*/
|
|
17
35
|
export default class Barrier<T = unknown, TR = T> extends Promise<TR> {
|
|
18
|
-
private p_resolve: Resolver<T
|
|
36
|
+
private p_resolve: Resolver<T>;
|
|
19
37
|
|
|
20
38
|
private p_reject: Rejecter;
|
|
21
39
|
|
|
@@ -26,24 +44,49 @@ export default class Barrier<T = unknown, TR = T> extends Promise<TR> {
|
|
|
26
44
|
let rejectRef: Rejecter;
|
|
27
45
|
|
|
28
46
|
super((resolve, reject) => {
|
|
29
|
-
|
|
47
|
+
// Note: Enforcing `void` return type because of the BEWARE note below.
|
|
48
|
+
resolveRef = (value: TR | PromiseLike<TR>): void => {
|
|
30
49
|
resolve(value);
|
|
31
50
|
this.p_state = STATE.RESOLVED;
|
|
51
|
+
|
|
52
|
+
// BEWARE: Don't try to return `this` here, it will easily cause
|
|
53
|
+
// infinite loops in React Native, which are extremely difficult
|
|
54
|
+
// to troubleshoot (I wasn't able to figure out, are they due to
|
|
55
|
+
// internal Promise implementation in RN, or because of some bad
|
|
56
|
+
// patterns in the host code).
|
|
32
57
|
};
|
|
33
|
-
|
|
58
|
+
|
|
59
|
+
// Note: Enforcing `void` return type because of the BEWARE note below.
|
|
60
|
+
rejectRef = (reason?: any): void => {
|
|
34
61
|
reject(reason);
|
|
35
62
|
this.p_state = STATE.REJECTED;
|
|
36
63
|
};
|
|
64
|
+
|
|
37
65
|
if (executor) executor(resolveRef, rejectRef);
|
|
38
66
|
});
|
|
39
67
|
|
|
40
|
-
|
|
68
|
+
// NOTE: We assume, the only scenario where TR is not equal T is when
|
|
69
|
+
// the Barrier is constructed by a .then() call on a "parent" barrier,
|
|
70
|
+
// and in that scenario .then() itself will replace .p_resolve by another
|
|
71
|
+
// resolver immediately after this constructor returns.
|
|
72
|
+
this.p_resolve = resolveRef! as Resolver<T>;
|
|
73
|
+
|
|
41
74
|
this.p_reject = rejectRef!;
|
|
42
75
|
}
|
|
43
76
|
|
|
44
|
-
get resolve() {
|
|
77
|
+
get resolve() {
|
|
78
|
+
return (arg: Parameters<Resolver<T>>[0]) => {
|
|
79
|
+
this.p_resolve(arg);
|
|
80
|
+
return this;
|
|
81
|
+
};
|
|
82
|
+
}
|
|
45
83
|
|
|
46
|
-
get reject() {
|
|
84
|
+
get reject() {
|
|
85
|
+
return (arg: Parameters<Rejecter>[0]) => {
|
|
86
|
+
this.p_reject(arg);
|
|
87
|
+
return this;
|
|
88
|
+
};
|
|
89
|
+
}
|
|
47
90
|
|
|
48
91
|
get resolved() { return this.p_state === STATE.RESOLVED; }
|
|
49
92
|
|