From 8233f61a451556dcc4787a5c9f1655c25bd4b6d6 Mon Sep 17 00:00:00 2001 From: jrandolf <101637635+jrandolf@users.noreply.github.com> Date: Wed, 30 Aug 2023 15:06:19 +0200 Subject: [PATCH] chore: refactor mutex (#10816) --- .../src/common/IsolatedWorld.ts | 31 ++------------- packages/puppeteer-core/src/common/util.ts | 39 +++++++++++++++++++ 2 files changed, 42 insertions(+), 28 deletions(-) diff --git a/packages/puppeteer-core/src/common/IsolatedWorld.ts b/packages/puppeteer-core/src/common/IsolatedWorld.ts index df5c612690e..5a4341b18c2 100644 --- a/packages/puppeteer-core/src/common/IsolatedWorld.ts +++ b/packages/puppeteer-core/src/common/IsolatedWorld.ts @@ -30,6 +30,7 @@ import {CDPJSHandle} from './JSHandle.js'; import {LifecycleWatcher, PuppeteerLifeCycleEvent} from './LifecycleWatcher.js'; import {BindingPayload, EvaluateFunc, HandleFor} from './types.js'; import { + Mutex, addPageBinding, createJSHandle, debugError, @@ -222,7 +223,8 @@ export class IsolatedWorld extends Realm { return; } - await this.#mutex.acquire(); + // eslint-disable-next-line @typescript-eslint/no-unused-vars + using _ = await this.#mutex.acquire(); try { await context._client.send( 'Runtime.addBinding', @@ -256,8 +258,6 @@ export class IsolatedWorld extends Realm { } debugError(error); - } finally { - this.#mutex.release(); } } @@ -344,28 +344,3 @@ export class IsolatedWorld extends Realm { this.#client.off('Runtime.bindingCalled', this.#onBindingCalled); } } - -class Mutex { - #locked = false; - #acquirers: Array<() => void> = []; - - // This is FIFO. - acquire(): Promise { - if (!this.#locked) { - this.#locked = true; - return Promise.resolve(); - } - const deferred = Deferred.create(); - this.#acquirers.push(deferred.resolve.bind(deferred)); - return deferred.valueOrThrow(); - } - - release(): void { - const resolve = this.#acquirers.shift(); - if (!resolve) { - this.#locked = false; - return; - } - resolve(); - } -} diff --git a/packages/puppeteer-core/src/common/util.ts b/packages/puppeteer-core/src/common/util.ts index 4b00700f52b..a9d4708460c 100644 --- a/packages/puppeteer-core/src/common/util.ts +++ b/packages/puppeteer-core/src/common/util.ts @@ -669,3 +669,42 @@ export function validateDialogType( assert(dialogType, `Unknown javascript dialog type: ${type}`); return dialogType as 'alert' | 'confirm' | 'prompt' | 'beforeunload'; } + +/** + * @internal + */ +export class Mutex { + static Guard = class Guard { + #mutex: Mutex; + constructor(mutex: Mutex) { + this.#mutex = mutex; + } + [Symbol.dispose](): void { + return this.#mutex.release(); + } + }; + + #locked = false; + #acquirers: Array<() => void> = []; + + // This is FIFO. + async acquire(): Promise> { + if (!this.#locked) { + this.#locked = true; + return new Mutex.Guard(this); + } + const deferred = Deferred.create(); + this.#acquirers.push(deferred.resolve.bind(deferred)); + await deferred.valueOrThrow(); + return new Mutex.Guard(this); + } + + release(): void { + const resolve = this.#acquirers.shift(); + if (!resolve) { + this.#locked = false; + return; + } + resolve(); + } +}