Bug 1438055 - Switch Fluent Localization class to use weak observers. r=mossop
authorZibi Braniecki <zbraniecki@mozilla.com>
Tue, 13 Feb 2018 16:25:41 -0800
changeset 403893 7903281904534c34e1c73781f30d55711322f13c
parent 403892 02896a1245a546f6202a2c1b01ee72b35d3daf50
child 403894 8d28b9f2d0abd7b694364ec874522102a77ff04d
push id99885
push userapavel@mozilla.com
push dateThu, 15 Feb 2018 10:38:09 +0000
treeherdermozilla-inbound@99495614cba7 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmossop
bugs1438055
milestone60.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1438055 - Switch Fluent Localization class to use weak observers. r=mossop MozReview-Commit-ID: 4R8OyziLRUo
intl/l10n/Localization.jsm
--- a/intl/l10n/Localization.jsm
+++ b/intl/l10n/Localization.jsm
@@ -16,16 +16,17 @@
  */
 
 
 /* fluent@0.6.0 */
 
 /* eslint no-console: ["error", { allow: ["warn", "error"] }] */
 /* global console */
 
+const { XPCOMUtils } = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm", {});
 const { L10nRegistry } = ChromeUtils.import("resource://gre/modules/L10nRegistry.jsm", {});
 const LocaleService = Cc["@mozilla.org/intl/localeservice;1"].getService(Ci.mozILocaleService);
 const ObserverService = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
 
 /*
  * CachedIterable caches the elements yielded by an iterable.
  *
  * It can be used to iterate over an iterable many times without depleting the
@@ -249,21 +250,21 @@ class Localization {
    * @returns {Promise<string>}
    */
   async formatValue(id, args) {
     const [val] = await this.formatValues([[id, args]]);
     return val;
   }
 
   /**
-   * Register observers on events that will trigger cache invalidation
+   * Register weak observers on events that will trigger cache invalidation
    */
   registerObservers() {
-    ObserverService.addObserver(this, 'l10n:available-locales-changed', false);
-    ObserverService.addObserver(this, 'intl:requested-locales-changed', false);
+    ObserverService.addObserver(this, 'l10n:available-locales-changed', true);
+    ObserverService.addObserver(this, 'intl:requested-locales-changed', true);
   }
 
   /**
    * Unregister observers on events that will trigger cache invalidation
    */
   unregisterObservers() {
     ObserverService.removeObserver(this, 'l10n:available-locales-changed');
     ObserverService.removeObserver(this, 'intl:requested-locales-changed');
@@ -291,16 +292,20 @@ class Localization {
    * This method should be called when there's a reason to believe
    * that language negotiation or available resources changed.
    */
   onLanguageChange() {
     this.ctxs = new CachedIterable(this.generateMessages(this.resourceIds));
   }
 }
 
+Localization.prototype.QueryInterface = XPCOMUtils.generateQI([
+  Ci.nsISupportsWeakReference
+]);
+
 /**
  * Format the value of a message into a string.
  *
  * This function is passed as a method to `keysFromContext` and resolve
  * a value of a single L10n Entity using provided `MessageContext`.
  *
  * If the function fails to retrieve the entity, it will return an ID of it.
  * If formatting fails, it will return a partially resolved entity.