Commit d1180226 authored by Julian Descottes's avatar Julian Descottes Committed by jdescottes@mozilla.com
Browse files

Bug 2003857 - [bidi] Update helper to wait for visibility to use visibilityChange event r=Sasha

The current polling approach is very likely to miss updates and face race issues.

Differential Revision: https://phabricator.services.mozilla.com/D275234
parent d6eadc17
Loading
Loading
Loading
Loading
+8 −9
Original line number Diff line number Diff line
@@ -20,7 +20,6 @@ ChromeUtils.defineESModuleGetters(lazy, {
  OriginType:
    "chrome://remote/content/webdriver-bidi/modules/root/browsingContext.sys.mjs",
  OwnershipModel: "chrome://remote/content/webdriver-bidi/RemoteValue.sys.mjs",
  PollPromise: "chrome://remote/content/shared/Sync.sys.mjs",
  pprint: "chrome://remote/content/shared/Format.sys.mjs",
});

@@ -498,15 +497,15 @@ class BrowsingContextModule extends WindowGlobalBiDiModule {
  async _awaitVisibilityState(options) {
    const { value } = options;
    const win = this.messageHandler.window;

    await lazy.PollPromise((resolve, reject) => {
      if (win.document.visibilityState === value) {
        resolve();
      } else {
        reject();
      }
    if (win.document.visibilityState !== value) {
      await new Promise(r => {
        // The visibilityState can only be hidden or visible, so if the current
        // value is not the expected one, the next visibilitychange event is
        // guaranteed to have the correct value.
        win.document.addEventListener("visibilitychange", r, { once: true });
      });
    }
  }

  _getBaseURL() {
    return this.messageHandler.window.document.baseURI;