@dr.pogodin/js-utils 0.0.10 → 0.0.12
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 -4
- package/package.json +4 -6
- package/src/Barrier.ts +52 -11
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,25 +34,44 @@ 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;
|
|
23
|
-
return this
|
|
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).
|
|
24
46
|
};
|
|
47
|
+
// Note: Enforcing `void` return type because of the BEWARE note below.
|
|
25
48
|
rejectRef = (reason) => {
|
|
26
49
|
reject(reason);
|
|
27
50
|
this.p_state = STATE.REJECTED;
|
|
28
|
-
return this;
|
|
29
51
|
};
|
|
30
52
|
if (executor)
|
|
31
53
|
executor(resolveRef, rejectRef);
|
|
32
54
|
});
|
|
33
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.
|
|
34
60
|
this.p_resolve = resolveRef;
|
|
35
61
|
this.p_reject = rejectRef;
|
|
36
62
|
}
|
|
37
|
-
get resolve() {
|
|
38
|
-
|
|
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
|
+
}
|
|
39
75
|
get resolved() { return this.p_state === STATE.RESOLVED; }
|
|
40
76
|
get rejected() { return this.p_state === STATE.REJECTED; }
|
|
41
77
|
get settled() { return this.p_state !== STATE.PENDING; }
|
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.12",
|
|
4
4
|
"description": "Collection of JavaScript (TypeScript) utilities.",
|
|
5
5
|
"main": "build/index",
|
|
6
6
|
"react-native": "src/index",
|
|
@@ -31,11 +31,10 @@
|
|
|
31
31
|
},
|
|
32
32
|
"homepage": "https://github.com/birdofpreyru/js-utils#readme",
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@babel/core": "^7.24.
|
|
35
|
-
"@babel/preset-env": "^7.24.
|
|
36
|
-
"@babel/preset-typescript": "^7.24.
|
|
34
|
+
"@babel/core": "^7.24.6",
|
|
35
|
+
"@babel/preset-env": "^7.24.6",
|
|
36
|
+
"@babel/preset-typescript": "^7.24.6",
|
|
37
37
|
"@tsconfig/recommended": "^1.0.6",
|
|
38
|
-
"@tsd/typescript": "^5.4.5",
|
|
39
38
|
"@types/jest": "^29.5.12",
|
|
40
39
|
"babel-jest": "^29.7.0",
|
|
41
40
|
"eslint": "^8.57.0",
|
|
@@ -44,7 +43,6 @@
|
|
|
44
43
|
"eslint-import-resolver-typescript": "^3.6.1",
|
|
45
44
|
"eslint-plugin-import": "^2.29.1",
|
|
46
45
|
"jest": "^29.7.0",
|
|
47
|
-
"jest-runner-tsd": "^6.0.0",
|
|
48
46
|
"rimraf": "^5.0.7",
|
|
49
47
|
"tstyche": "^1.1.0",
|
|
50
48
|
"typescript": "^5.4.5",
|
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,26 +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;
|
|
32
|
-
|
|
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).
|
|
33
57
|
};
|
|
34
|
-
|
|
58
|
+
|
|
59
|
+
// Note: Enforcing `void` return type because of the BEWARE note below.
|
|
60
|
+
rejectRef = (reason?: any): void => {
|
|
35
61
|
reject(reason);
|
|
36
62
|
this.p_state = STATE.REJECTED;
|
|
37
|
-
return this;
|
|
38
63
|
};
|
|
64
|
+
|
|
39
65
|
if (executor) executor(resolveRef, rejectRef);
|
|
40
66
|
});
|
|
41
67
|
|
|
42
|
-
|
|
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
|
+
|
|
43
74
|
this.p_reject = rejectRef!;
|
|
44
75
|
}
|
|
45
76
|
|
|
46
|
-
get resolve() {
|
|
77
|
+
get resolve() {
|
|
78
|
+
return (arg: Parameters<Resolver<T>>[0]) => {
|
|
79
|
+
this.p_resolve(arg);
|
|
80
|
+
return this;
|
|
81
|
+
};
|
|
82
|
+
}
|
|
47
83
|
|
|
48
|
-
get reject() {
|
|
84
|
+
get reject() {
|
|
85
|
+
return (arg: Parameters<Rejecter>[0]) => {
|
|
86
|
+
this.p_reject(arg);
|
|
87
|
+
return this;
|
|
88
|
+
};
|
|
89
|
+
}
|
|
49
90
|
|
|
50
91
|
get resolved() { return this.p_state === STATE.RESOLVED; }
|
|
51
92
|
|