Bug 1437427 - Workaround promise/microtask bug with a callback in Fluent runtime. r=pike draft
authorZibi Braniecki <zbraniecki@mozilla.com>
Tue, 13 Feb 2018 12:26:31 -0800
changeset 755217 1a8541808a7a6e629d399b5f0f148d464a5e8d1c
parent 755207 d9cab430b4b29192e6c7fe99092c0d5435aeee66
push id99121
push userbmo:gandalf@aviary.pl
push dateWed, 14 Feb 2018 23:51:23 +0000
reviewerspike
bugs1437427
milestone60.0a1
Bug 1437427 - Workaround promise/microtask bug with a callback in Fluent runtime. r=pike MozReview-Commit-ID: 9uJpEHHBv8w
intl/l10n/l10n.js
--- a/intl/l10n/l10n.js
+++ b/intl/l10n/l10n.js
@@ -1,36 +1,45 @@
 {
   const { DOMLocalization } =
     ChromeUtils.import("resource://gre/modules/DOMLocalization.jsm");
 
   /**
    * Polyfill for document.ready polyfill.
    * See: https://github.com/whatwg/html/issues/127 for details.
    *
+   * XXX: The callback is a temporary workaround for bug 1193394. Once Promises in Gecko
+   *      start beeing a microtask and stop pushing translation post-layout, we can
+   *      remove it and start using the returned Promise again.
+   *
+   * @param {Function} callback - function to be called when the document is ready.
    * @returns {Promise}
    */
-  function documentReady() {
+  function documentReady(callback) {
     if (document.contentType === 'application/vnd.mozilla.xul+xml') {
       // XUL
       return new Promise(
         resolve => document.addEventListener(
-          'MozBeforeInitialXULLayout', resolve, { once: true }
+          'MozBeforeInitialXULLayout', () => {
+            resolve(callback());
+          }, { once: true }
         )
       );
     }
 
     // HTML
     const rs = document.readyState;
     if (rs === 'interactive' || rs === 'completed') {
-      return Promise.resolve();
+      return Promise.resolve(callback());
     }
     return new Promise(
       resolve => document.addEventListener(
-        'readystatechange', resolve, { once: true }
+        'readystatechange', () => {
+          resolve(callback());
+        }, { once: true }
       )
     );
   }
 
   /**
    * Scans the `elem` for links with localization resources.
    *
    * @param {Element} elem
@@ -44,17 +53,17 @@
 
   const resourceIds = getResourceLinks(document.head || document);
 
   document.l10n = new DOMLocalization(window, resourceIds);
 
   // trigger first context to be fetched eagerly
   document.l10n.ctxs.touchNext();
 
-  document.l10n.ready = documentReady().then(() => {
+  document.l10n.ready = documentReady(() => {
     document.l10n.registerObservers();
     window.addEventListener('unload', () => {
       document.l10n.unregisterObservers();
     });
     document.l10n.connectRoot(document.documentElement);
     return document.l10n.translateRoots();
   });
 }