2022-09-19 10:49:13 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2022 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {assert} from '../util/assert.js';
|
2023-05-26 09:42:22 +00:00
|
|
|
import {createDeferred, Deferred} from '../util/Deferred.js';
|
2022-08-17 12:39:41 +00:00
|
|
|
|
2022-09-15 06:22:20 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
export interface Poller<T> {
|
|
|
|
start(): Promise<void>;
|
2022-08-17 12:39:41 +00:00
|
|
|
stop(): Promise<void>;
|
|
|
|
result(): Promise<T>;
|
|
|
|
}
|
|
|
|
|
2022-09-15 06:22:20 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-08-17 12:39:41 +00:00
|
|
|
export class MutationPoller<T> implements Poller<T> {
|
|
|
|
#fn: () => Promise<T>;
|
|
|
|
|
|
|
|
#root: Node;
|
|
|
|
|
|
|
|
#observer?: MutationObserver;
|
2023-05-26 09:42:22 +00:00
|
|
|
#deferred?: Deferred<T>;
|
2022-08-17 12:39:41 +00:00
|
|
|
constructor(fn: () => Promise<T>, root: Node) {
|
|
|
|
this.#fn = fn;
|
|
|
|
this.#root = root;
|
|
|
|
}
|
|
|
|
|
2022-09-15 06:22:20 +00:00
|
|
|
async start(): Promise<void> {
|
2023-05-26 09:42:22 +00:00
|
|
|
const deferred = (this.#deferred = createDeferred<T>());
|
2022-08-17 12:39:41 +00:00
|
|
|
const result = await this.#fn();
|
|
|
|
if (result) {
|
2023-05-26 09:42:22 +00:00
|
|
|
deferred.resolve(result);
|
2022-09-15 06:22:20 +00:00
|
|
|
return;
|
2022-08-17 12:39:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.#observer = new MutationObserver(async () => {
|
|
|
|
const result = await this.#fn();
|
|
|
|
if (!result) {
|
|
|
|
return;
|
|
|
|
}
|
2023-05-26 09:42:22 +00:00
|
|
|
deferred.resolve(result);
|
2022-08-17 12:39:41 +00:00
|
|
|
await this.stop();
|
|
|
|
});
|
|
|
|
this.#observer.observe(this.#root, {
|
|
|
|
childList: true,
|
|
|
|
subtree: true,
|
|
|
|
attributes: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async stop(): Promise<void> {
|
2023-05-26 09:42:22 +00:00
|
|
|
assert(this.#deferred, 'Polling never started.');
|
|
|
|
if (!this.#deferred.finished()) {
|
|
|
|
this.#deferred.reject(new Error('Polling stopped'));
|
2022-08-17 12:39:41 +00:00
|
|
|
}
|
|
|
|
if (this.#observer) {
|
|
|
|
this.#observer.disconnect();
|
2022-09-15 06:22:20 +00:00
|
|
|
this.#observer = undefined;
|
2022-08-17 12:39:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result(): Promise<T> {
|
2023-05-26 09:42:22 +00:00
|
|
|
assert(this.#deferred, 'Polling never started.');
|
|
|
|
return this.#deferred.valueOrThrow();
|
2022-08-17 12:39:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-19 14:28:18 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-08-17 12:39:41 +00:00
|
|
|
export class RAFPoller<T> implements Poller<T> {
|
|
|
|
#fn: () => Promise<T>;
|
2023-05-26 09:42:22 +00:00
|
|
|
#deferred?: Deferred<T>;
|
2022-08-17 12:39:41 +00:00
|
|
|
constructor(fn: () => Promise<T>) {
|
|
|
|
this.#fn = fn;
|
|
|
|
}
|
|
|
|
|
2022-09-15 06:22:20 +00:00
|
|
|
async start(): Promise<void> {
|
2023-05-26 09:42:22 +00:00
|
|
|
const deferred = (this.#deferred = createDeferred<T>());
|
2022-08-17 12:39:41 +00:00
|
|
|
const result = await this.#fn();
|
|
|
|
if (result) {
|
2023-05-26 09:42:22 +00:00
|
|
|
deferred.resolve(result);
|
2022-09-15 06:22:20 +00:00
|
|
|
return;
|
2022-08-17 12:39:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const poll = async () => {
|
2023-05-26 09:42:22 +00:00
|
|
|
if (deferred.finished()) {
|
2022-08-17 12:39:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const result = await this.#fn();
|
|
|
|
if (!result) {
|
|
|
|
window.requestAnimationFrame(poll);
|
|
|
|
return;
|
|
|
|
}
|
2023-05-26 09:42:22 +00:00
|
|
|
deferred.resolve(result);
|
2022-08-17 12:39:41 +00:00
|
|
|
await this.stop();
|
|
|
|
};
|
|
|
|
window.requestAnimationFrame(poll);
|
|
|
|
}
|
|
|
|
|
|
|
|
async stop(): Promise<void> {
|
2023-05-26 09:42:22 +00:00
|
|
|
assert(this.#deferred, 'Polling never started.');
|
|
|
|
if (!this.#deferred.finished()) {
|
|
|
|
this.#deferred.reject(new Error('Polling stopped'));
|
2022-08-17 12:39:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result(): Promise<T> {
|
2023-05-26 09:42:22 +00:00
|
|
|
assert(this.#deferred, 'Polling never started.');
|
|
|
|
return this.#deferred.valueOrThrow();
|
2022-08-17 12:39:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-19 14:28:18 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
|
2022-08-17 12:39:41 +00:00
|
|
|
export class IntervalPoller<T> implements Poller<T> {
|
|
|
|
#fn: () => Promise<T>;
|
|
|
|
#ms: number;
|
|
|
|
|
|
|
|
#interval?: NodeJS.Timer;
|
2023-05-26 09:42:22 +00:00
|
|
|
#deferred?: Deferred<T>;
|
2022-08-17 12:39:41 +00:00
|
|
|
constructor(fn: () => Promise<T>, ms: number) {
|
|
|
|
this.#fn = fn;
|
|
|
|
this.#ms = ms;
|
|
|
|
}
|
|
|
|
|
2022-09-15 06:22:20 +00:00
|
|
|
async start(): Promise<void> {
|
2023-05-26 09:42:22 +00:00
|
|
|
const deferred = (this.#deferred = createDeferred<T>());
|
2022-08-17 12:39:41 +00:00
|
|
|
const result = await this.#fn();
|
|
|
|
if (result) {
|
2023-05-26 09:42:22 +00:00
|
|
|
deferred.resolve(result);
|
2022-09-15 06:22:20 +00:00
|
|
|
return;
|
2022-08-17 12:39:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.#interval = setInterval(async () => {
|
|
|
|
const result = await this.#fn();
|
|
|
|
if (!result) {
|
|
|
|
return;
|
|
|
|
}
|
2023-05-26 09:42:22 +00:00
|
|
|
deferred.resolve(result);
|
2022-08-17 12:39:41 +00:00
|
|
|
await this.stop();
|
|
|
|
}, this.#ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
async stop(): Promise<void> {
|
2023-05-26 09:42:22 +00:00
|
|
|
assert(this.#deferred, 'Polling never started.');
|
|
|
|
if (!this.#deferred.finished()) {
|
|
|
|
this.#deferred.reject(new Error('Polling stopped'));
|
2022-08-17 12:39:41 +00:00
|
|
|
}
|
|
|
|
if (this.#interval) {
|
|
|
|
clearInterval(this.#interval);
|
2022-09-15 06:22:20 +00:00
|
|
|
this.#interval = undefined;
|
2022-08-17 12:39:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result(): Promise<T> {
|
2023-05-26 09:42:22 +00:00
|
|
|
assert(this.#deferred, 'Polling never started.');
|
|
|
|
return this.#deferred.valueOrThrow();
|
2022-08-17 12:39:41 +00:00
|
|
|
}
|
|
|
|
}
|