@fbltd/async 1.0.38 → 1.0.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bin/dependency/dep.factory.js +4 -0
- package/dist/bin/dependency/dependency.js +20 -19
- package/dist/bin/dependency/dependency.stream.js +21 -8
- package/dist/bin/dependency/vanilla/reaction/reaction.js +1 -1
- package/dist/types/dependency/dep.factory.d.ts +1 -0
- package/dist/types/dependency/dependency.d.ts +1 -1
- package/dist/types/dependency/dependency.stream.d.ts +3 -1
- package/package.json +1 -1
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Dependency } from "./dependency.js";
|
|
2
2
|
import { reaction } from "./vanilla/index.js";
|
|
3
|
+
import { getStream } from "./vanilla/get.stream.js";
|
|
3
4
|
export class DepFactory {
|
|
4
5
|
static ofValue(value, config) {
|
|
5
6
|
return new Dependency(value, config);
|
|
@@ -7,4 +8,7 @@ export class DepFactory {
|
|
|
7
8
|
static ofReaction(fn, config) {
|
|
8
9
|
return reaction(fn, config);
|
|
9
10
|
}
|
|
11
|
+
static ofDependency(dep) {
|
|
12
|
+
return getStream(dep);
|
|
13
|
+
}
|
|
10
14
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { PromiseConfiguration } from "../promise-configuration.js";
|
|
2
2
|
import { baseComparer } from "./utils.js";
|
|
3
|
-
import { observationState } from "./observe.state.js";
|
|
4
3
|
import { symAI } from "../constants.js";
|
|
4
|
+
import { observationState } from "./observe.state.js";
|
|
5
5
|
export class Dependency {
|
|
6
6
|
_value;
|
|
7
7
|
reactionPromise;
|
|
@@ -38,38 +38,35 @@ export class Dependency {
|
|
|
38
38
|
return this._value;
|
|
39
39
|
}
|
|
40
40
|
get done() {
|
|
41
|
-
return this.abortPromise.isFulfilled;
|
|
41
|
+
return !this.abortPromise || this.abortPromise.isFulfilled;
|
|
42
42
|
}
|
|
43
43
|
[symAI](thisStreamConfig = {}) {
|
|
44
44
|
const externalPromises = [];
|
|
45
45
|
let firstPromise;
|
|
46
46
|
const withReactionOnSubscribe = this.config.withReactionOnSubscribe || thisStreamConfig.withReactionOnSubscribe;
|
|
47
47
|
if (withReactionOnSubscribe) {
|
|
48
|
-
firstPromise =
|
|
49
|
-
|
|
50
|
-
externalPromises.push(firstPromise.promise);
|
|
48
|
+
firstPromise = Promise.resolve();
|
|
49
|
+
externalPromises.push(firstPromise);
|
|
51
50
|
}
|
|
52
51
|
const owner = this;
|
|
53
|
-
let done = false;
|
|
54
52
|
return {
|
|
55
53
|
owner,
|
|
56
54
|
dispose: owner.dispose.bind(owner),
|
|
57
55
|
get isDisposed() {
|
|
58
|
-
return
|
|
56
|
+
return owner.done;
|
|
59
57
|
},
|
|
60
58
|
next: async () => {
|
|
61
59
|
await Promise.race([
|
|
62
|
-
...externalPromises,
|
|
63
60
|
owner.next(),
|
|
61
|
+
...externalPromises,
|
|
64
62
|
]);
|
|
65
|
-
if (this.done) {
|
|
66
|
-
done = true;
|
|
67
|
-
return { done: true };
|
|
68
|
-
}
|
|
69
63
|
if (firstPromise) {
|
|
70
64
|
firstPromise = undefined;
|
|
71
65
|
externalPromises.pop();
|
|
72
66
|
}
|
|
67
|
+
if (this.done) {
|
|
68
|
+
return { done: true };
|
|
69
|
+
}
|
|
73
70
|
return {
|
|
74
71
|
done: false,
|
|
75
72
|
get value() {
|
|
@@ -98,23 +95,27 @@ export class Dependency {
|
|
|
98
95
|
* Another subscribe for current race
|
|
99
96
|
*/
|
|
100
97
|
async next() {
|
|
98
|
+
const done = { done: true };
|
|
99
|
+
const owner = this;
|
|
100
|
+
if (this.done)
|
|
101
|
+
return done;
|
|
101
102
|
await this.getOrCreateRace();
|
|
102
103
|
this._race = undefined;
|
|
103
|
-
if (this.done)
|
|
104
|
-
return
|
|
105
|
-
|
|
104
|
+
if (this.done)
|
|
105
|
+
return done;
|
|
106
|
+
this.abortPromise = new PromiseConfiguration();
|
|
106
107
|
return {
|
|
107
108
|
done: false,
|
|
108
109
|
get value() {
|
|
109
|
-
return
|
|
110
|
+
return owner.value;
|
|
110
111
|
}
|
|
111
112
|
};
|
|
112
113
|
}
|
|
113
114
|
get disposePromise() {
|
|
114
|
-
return this.abortPromise.
|
|
115
|
+
return this.abortPromise?.promise ?? Promise.resolve();
|
|
115
116
|
}
|
|
116
117
|
dispose() {
|
|
117
|
-
this.abortPromise
|
|
118
|
-
this.reactionPromise = undefined;
|
|
118
|
+
this.abortPromise?.resolve();
|
|
119
|
+
this.abortPromise = this.reactionPromise = undefined;
|
|
119
120
|
}
|
|
120
121
|
}
|
|
@@ -2,37 +2,50 @@ import { PromiseConfiguration } from "../promise-configuration.js";
|
|
|
2
2
|
import { symAI } from "../constants.js";
|
|
3
3
|
export class DependencyStream {
|
|
4
4
|
owner;
|
|
5
|
-
|
|
5
|
+
abortPromise = new PromiseConfiguration();
|
|
6
6
|
iterator;
|
|
7
7
|
get isDisposed() {
|
|
8
8
|
return this.iterator.isDisposed;
|
|
9
9
|
}
|
|
10
|
+
get done() {
|
|
11
|
+
return this.owner.done || !this.abortPromise?.isPending;
|
|
12
|
+
}
|
|
10
13
|
constructor(owner) {
|
|
11
14
|
this.owner = owner;
|
|
12
|
-
this.
|
|
15
|
+
this.abortPromise = new PromiseConfiguration();
|
|
13
16
|
this.iterator = owner[symAI]();
|
|
14
17
|
}
|
|
15
18
|
[symAI]() {
|
|
16
19
|
const done = { done: true };
|
|
17
20
|
return {
|
|
18
21
|
next: async () => {
|
|
19
|
-
if (this.
|
|
22
|
+
if (this.done) {
|
|
23
|
+
this.abort();
|
|
20
24
|
return done;
|
|
25
|
+
}
|
|
21
26
|
const nextRes = this.iterator.next();
|
|
22
27
|
await Promise.race([
|
|
23
|
-
this.
|
|
28
|
+
this.abortPromise.promise,
|
|
24
29
|
nextRes,
|
|
25
30
|
]);
|
|
26
|
-
if (this.
|
|
31
|
+
if (this.done) {
|
|
32
|
+
this.abort();
|
|
27
33
|
return done;
|
|
28
|
-
|
|
34
|
+
}
|
|
35
|
+
this.abortPromise = new PromiseConfiguration();
|
|
29
36
|
return nextRes;
|
|
30
37
|
}
|
|
31
38
|
};
|
|
32
39
|
}
|
|
40
|
+
abort() {
|
|
41
|
+
if (this.abortPromise?.isPending) {
|
|
42
|
+
this.abortPromise.resolve();
|
|
43
|
+
this.abortPromise = undefined;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
33
46
|
dispose() {
|
|
34
|
-
if (this.
|
|
47
|
+
if (this.done)
|
|
35
48
|
return;
|
|
36
|
-
this.
|
|
49
|
+
this.abort();
|
|
37
50
|
}
|
|
38
51
|
}
|
|
@@ -14,7 +14,7 @@ export function reaction(fn, config) {
|
|
|
14
14
|
beforeValues = depsArray.map(dep => dep.value_unsafe);
|
|
15
15
|
const promises = depsArray.map(dep => dep.next());
|
|
16
16
|
promises.push(dep.disposePromise);
|
|
17
|
-
await Promise.race(
|
|
17
|
+
await Promise.race(promises);
|
|
18
18
|
if (dep.done)
|
|
19
19
|
return { done: true };
|
|
20
20
|
let shouldRun = depsArray.some((dep, i) => dep.value_unsafe !== beforeValues[i]);
|
|
@@ -3,4 +3,5 @@ import { IAllStreamConfig } from "./contracts.ts";
|
|
|
3
3
|
export declare abstract class DepFactory {
|
|
4
4
|
static ofValue<T>(value: T, config?: Partial<IAllStreamConfig<T>>): Dependency<T>;
|
|
5
5
|
static ofReaction<T>(fn: () => T, config?: Partial<IAllStreamConfig<T>>): Dependency<T>;
|
|
6
|
+
static ofDependency<T>(dep: Dependency<T>): import("./dependency.stream.ts").DependencyStream<T>;
|
|
6
7
|
}
|
|
@@ -3,12 +3,14 @@ import { symAI } from "../constants.ts";
|
|
|
3
3
|
import { Dependency } from "./dependency.ts";
|
|
4
4
|
export declare class DependencyStream<T = any> implements IDependencyStream {
|
|
5
5
|
readonly owner: Dependency;
|
|
6
|
-
private
|
|
6
|
+
private abortPromise;
|
|
7
7
|
private readonly iterator;
|
|
8
8
|
get isDisposed(): boolean;
|
|
9
|
+
get done(): boolean;
|
|
9
10
|
constructor(owner: Dependency);
|
|
10
11
|
[symAI](): {
|
|
11
12
|
next: () => Promise<IteratorResult<T, void>>;
|
|
12
13
|
};
|
|
14
|
+
protected abort(): void;
|
|
13
15
|
dispose(): void;
|
|
14
16
|
}
|