From f8c7a4644818fa87e1829179d2e72edc92ab264f Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Thu, 7 Sep 2023 11:57:19 +0200 Subject: [PATCH] refactor: sync timezone state (#10857) --- .../src/common/EmulationManager.ts | 42 +++++++++++++++++-- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/packages/puppeteer-core/src/common/EmulationManager.ts b/packages/puppeteer-core/src/common/EmulationManager.ts index 29d2c483250..343f5e3e384 100644 --- a/packages/puppeteer-core/src/common/EmulationManager.ts +++ b/packages/puppeteer-core/src/common/EmulationManager.ts @@ -36,6 +36,11 @@ interface IdleOverridesState { active: boolean; } +interface TimezoneState { + timezoneId?: string; + active: boolean; +} + /** * @internal */ @@ -49,6 +54,9 @@ export class EmulationManager { #idleOverridesState: IdleOverridesState = { active: false, }; + #timezoneState: TimezoneState = { + active: false, + }; #secondaryClients = new Set(); constructor(client: CDPSession) { @@ -65,8 +73,11 @@ export class EmulationManager { client.once(CDPSessionEmittedEvents.Disconnected, () => { return this.#secondaryClients.delete(client); }); + // We don't await here because we want to register all state changes before + // the target is unpaused. void this.#syncViewport().catch(debugError); void this.#syncIdleState().catch(debugError); + void this.#syncTimezoneState().catch(debugError); } get javascriptEnabled(): boolean { @@ -170,19 +181,42 @@ export class EmulationManager { } } - async emulateTimezone(timezoneId?: string): Promise { + async #emulateTimezone( + client: CDPSession, + timezoneState: TimezoneState + ): Promise { + if (!timezoneState.active) { + return; + } try { - await this.#client.send('Emulation.setTimezoneOverride', { - timezoneId: timezoneId || '', + await client.send('Emulation.setTimezoneOverride', { + timezoneId: timezoneState.timezoneId || '', }); } catch (error) { if (isErrorLike(error) && error.message.includes('Invalid timezone')) { - throw new Error(`Invalid timezone ID: ${timezoneId}`); + throw new Error(`Invalid timezone ID: ${timezoneState.timezoneId}`); } throw error; } } + async #syncTimezoneState() { + await Promise.all([ + this.#emulateTimezone(this.#client, this.#timezoneState), + ...Array.from(this.#secondaryClients).map(client => { + return this.#emulateTimezone(client, this.#timezoneState); + }), + ]); + } + + async emulateTimezone(timezoneId?: string): Promise { + this.#timezoneState = { + timezoneId, + active: true, + }; + await this.#syncTimezoneState(); + } + async emulateVisionDeficiency( type?: Protocol.Emulation.SetEmulatedVisionDeficiencyRequest['type'] ): Promise {