Bug 1438055 - Switch Fluent Localization class to use weak observers. r?mossop
MozReview-Commit-ID: 4R8OyziLRUo
--- 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.