-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathasyncEffect.ts
59 lines (55 loc) · 1.42 KB
/
asyncEffect.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { Effect, nextTrackId } from '../effect.js';
import { Dependency, endTrack, link, startTrack, SubscriberFlags } from '../system.js';
import { asyncCheckDirty } from './asyncSystem.js';
export function asyncEffect<T>(fn: () => AsyncGenerator<Dependency, T>): AsyncEffect<T> {
const e = new AsyncEffect(fn);
e.run();
return e;
}
export class AsyncEffect<T = any> extends Effect {
async notify(): Promise<void> {
let flags = this.flags;
if (flags & SubscriberFlags.Dirty) {
this.run();
return;
}
if (flags & SubscriberFlags.ToCheckDirty) {
if (await asyncCheckDirty(this.deps!)) {
this.run();
return;
} else {
this.flags = flags &= ~SubscriberFlags.ToCheckDirty;
}
}
if (flags & SubscriberFlags.InnerEffectsPending) {
this.flags = flags & ~SubscriberFlags.InnerEffectsPending;
let link = this.deps!;
do {
const dep = link.dep;
if ('notify' in dep) {
dep.notify();
}
link = link.nextDep!;
} while (link !== undefined);
}
}
async run(): Promise<T> {
try {
startTrack(this);
const trackId = nextTrackId();
const generator = this.fn();
let current = await generator.next();
while (!current.done) {
const dep = current.value;
if (dep.lastTrackedId !== trackId) {
dep.lastTrackedId = trackId;
link(dep, this);
}
current = await generator.next();
}
return await current.value;
} finally {
endTrack(this);
}
}
}