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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
import {
createDeferredPromise,
DeferredPromise,
} from '../util/DeferredPromise.js';
import {assert} from '../util/assert.js';
interface Poller<T> {
start(): Promise<T>;
stop(): Promise<void>;
result(): Promise<T>;
}
export class MutationPoller<T> implements Poller<T> {
#fn: () => Promise<T>;
#root: Node;
#observer?: MutationObserver;
#promise?: DeferredPromise<T>;
constructor(fn: () => Promise<T>, root: Node) {
this.#fn = fn;
this.#root = root;
}
async start(): Promise<T> {
const promise = (this.#promise = createDeferredPromise<T>());
const result = await this.#fn();
if (result) {
promise.resolve(result);
return result;
}
this.#observer = new MutationObserver(async () => {
const result = await this.#fn();
if (!result) {
return;
}
promise.resolve(result);
await this.stop();
});
this.#observer.observe(this.#root, {
childList: true,
subtree: true,
attributes: true,
});
return this.#promise;
}
async stop(): Promise<void> {
assert(this.#promise, 'Polling never started.');
if (!this.#promise.finished()) {
this.#promise.reject(new Error('Polling stopped'));
}
if (this.#observer) {
this.#observer.disconnect();
}
}
result(): Promise<T> {
assert(this.#promise, 'Polling never started.');
return this.#promise;
}
}
export class RAFPoller<T> implements Poller<T> {
#fn: () => Promise<T>;
#promise?: DeferredPromise<T>;
constructor(fn: () => Promise<T>) {
this.#fn = fn;
}
async start(): Promise<T> {
const promise = (this.#promise = createDeferredPromise<T>());
const result = await this.#fn();
if (result) {
promise.resolve(result);
return result;
}
const poll = async () => {
if (promise.finished()) {
return;
}
const result = await this.#fn();
if (!result) {
window.requestAnimationFrame(poll);
return;
}
promise.resolve(result);
await this.stop();
};
window.requestAnimationFrame(poll);
return this.#promise;
}
async stop(): Promise<void> {
assert(this.#promise, 'Polling never started.');
if (!this.#promise.finished()) {
this.#promise.reject(new Error('Polling stopped'));
}
}
result(): Promise<T> {
assert(this.#promise, 'Polling never started.');
return this.#promise;
}
}
export class IntervalPoller<T> implements Poller<T> {
#fn: () => Promise<T>;
#ms: number;
#interval?: NodeJS.Timer;
#promise?: DeferredPromise<T>;
constructor(fn: () => Promise<T>, ms: number) {
this.#fn = fn;
this.#ms = ms;
}
async start(): Promise<T> {
const promise = (this.#promise = createDeferredPromise<T>());
const result = await this.#fn();
if (result) {
promise.resolve(result);
return result;
}
this.#interval = setInterval(async () => {
const result = await this.#fn();
if (!result) {
return;
}
promise.resolve(result);
await this.stop();
}, this.#ms);
return this.#promise;
}
async stop(): Promise<void> {
assert(this.#promise, 'Polling never started.');
if (!this.#promise.finished()) {
this.#promise.reject(new Error('Polling stopped'));
}
if (this.#interval) {
clearInterval(this.#interval);
}
}
result(): Promise<T> {
assert(this.#promise, 'Polling never started.');
return this.#promise;
}
}
|