UNPKG

puppeteer-core

Version:

A high-level API to control headless Chrome over the DevTools Protocol

146 lines 4.03 kB
"use strict"; /** * @license * Copyright 2022 Google Inc. * SPDX-License-Identifier: Apache-2.0 */ Object.defineProperty(exports, "__esModule", { value: true }); exports.IntervalPoller = exports.RAFPoller = exports.MutationPoller = void 0; const assert_js_1 = require("../util/assert.js"); const Deferred_js_1 = require("../util/Deferred.js"); /** * @internal */ class MutationPoller { #fn; #root; #observer; #deferred; constructor(fn, root) { this.#fn = fn; this.#root = root; } async start() { const deferred = (this.#deferred = Deferred_js_1.Deferred.create()); const result = await this.#fn(); if (result) { deferred.resolve(result); return; } this.#observer = new MutationObserver(async () => { const result = await this.#fn(); if (!result) { return; } deferred.resolve(result); await this.stop(); }); this.#observer.observe(this.#root, { childList: true, subtree: true, attributes: true, }); } async stop() { (0, assert_js_1.assert)(this.#deferred, 'Polling never started.'); if (!this.#deferred.finished()) { this.#deferred.reject(new Error('Polling stopped')); } if (this.#observer) { this.#observer.disconnect(); this.#observer = undefined; } } result() { (0, assert_js_1.assert)(this.#deferred, 'Polling never started.'); return this.#deferred.valueOrThrow(); } } exports.MutationPoller = MutationPoller; /** * @internal */ class RAFPoller { #fn; #deferred; constructor(fn) { this.#fn = fn; } async start() { const deferred = (this.#deferred = Deferred_js_1.Deferred.create()); const result = await this.#fn(); if (result) { deferred.resolve(result); return; } const poll = async () => { if (deferred.finished()) { return; } const result = await this.#fn(); if (!result) { window.requestAnimationFrame(poll); return; } deferred.resolve(result); await this.stop(); }; window.requestAnimationFrame(poll); } async stop() { (0, assert_js_1.assert)(this.#deferred, 'Polling never started.'); if (!this.#deferred.finished()) { this.#deferred.reject(new Error('Polling stopped')); } } result() { (0, assert_js_1.assert)(this.#deferred, 'Polling never started.'); return this.#deferred.valueOrThrow(); } } exports.RAFPoller = RAFPoller; /** * @internal */ class IntervalPoller { #fn; #ms; #interval; #deferred; constructor(fn, ms) { this.#fn = fn; this.#ms = ms; } async start() { const deferred = (this.#deferred = Deferred_js_1.Deferred.create()); const result = await this.#fn(); if (result) { deferred.resolve(result); return; } this.#interval = setInterval(async () => { const result = await this.#fn(); if (!result) { return; } deferred.resolve(result); await this.stop(); }, this.#ms); } async stop() { (0, assert_js_1.assert)(this.#deferred, 'Polling never started.'); if (!this.#deferred.finished()) { this.#deferred.reject(new Error('Polling stopped')); } if (this.#interval) { clearInterval(this.#interval); this.#interval = undefined; } } result() { (0, assert_js_1.assert)(this.#deferred, 'Polling never started.'); return this.#deferred.valueOrThrow(); } } exports.IntervalPoller = IntervalPoller; //# sourceMappingURL=Poller.js.map