mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
chore: refactor mutex (#10816)
This commit is contained in:
parent
65188dd2e2
commit
8233f61a45
@ -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<void> {
|
||||
if (!this.#locked) {
|
||||
this.#locked = true;
|
||||
return Promise.resolve();
|
||||
}
|
||||
const deferred = Deferred.create<void>();
|
||||
this.#acquirers.push(deferred.resolve.bind(deferred));
|
||||
return deferred.valueOrThrow();
|
||||
}
|
||||
|
||||
release(): void {
|
||||
const resolve = this.#acquirers.shift();
|
||||
if (!resolve) {
|
||||
this.#locked = false;
|
||||
return;
|
||||
}
|
||||
resolve();
|
||||
}
|
||||
}
|
||||
|
@ -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<InstanceType<typeof Mutex.Guard>> {
|
||||
if (!this.#locked) {
|
||||
this.#locked = true;
|
||||
return new Mutex.Guard(this);
|
||||
}
|
||||
const deferred = Deferred.create<void>();
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user