diff --git a/dom/l10n/DocumentL10n.cpp b/dom/l10n/DocumentL10n.cpp index 212c5b6189d9..442729b0e14d 100644 --- a/dom/l10n/DocumentL10n.cpp +++ b/dom/l10n/DocumentL10n.cpp @@ -277,13 +277,9 @@ void DocumentL10n::InitialTranslationCompleted(bool aL10nCached) { mContentSink->InitialTranslationCompleted(); } - // If sync was true, we want to change the state of - // mozILocalization to async now. - if (mIsSync) { - mIsSync = false; - - mLocalization->SetIsSync(mIsSync); - } + // From now on, the state of Localization is unconditionally + // async. + SetIsSync(false); } void DocumentL10n::ConnectRoot(nsINode& aNode, bool aTranslate, diff --git a/intl/l10n/Localization.cpp b/intl/l10n/Localization.cpp index 573eef688023..dde048a323fc 100644 --- a/intl/l10n/Localization.cpp +++ b/intl/l10n/Localization.cpp @@ -145,7 +145,7 @@ Localization::Observe(nsISupports* aSubject, const char* aTopic, void Localization::OnChange() { if (mLocalization) { - mLocalization->OnChange(mResourceIds); + mLocalization->OnChange(mResourceIds, mIsSync); } } @@ -221,10 +221,7 @@ already_AddRefed Localization::FormatValue( return MaybeWrapPromise(promise); } -void Localization::SetIsSync(const bool aIsSync) { - mIsSync = aIsSync; - mLocalization->SetIsSync(aIsSync); -} +void Localization::SetIsSync(const bool aIsSync) { mIsSync = aIsSync; } already_AddRefed Localization::FormatValues( JSContext* aCx, const Sequence& aKeys, ErrorResult& aRv) { @@ -273,6 +270,11 @@ already_AddRefed Localization::FormatMessages( void Localization::FormatValueSync(JSContext* aCx, const nsACString& aId, const Optional& aArgs, nsACString& aRetVal, ErrorResult& aRv) { + if (!mIsSync) { + aRv.ThrowInvalidStateError( + "Can't use formatValueSync when state is async."); + return; + } JS::Rooted args(aCx); if (aArgs.WasPassed()) { @@ -291,6 +293,11 @@ void Localization::FormatValuesSync(JSContext* aCx, const Sequence& aKeys, nsTArray& aRetVal, ErrorResult& aRv) { + if (!mIsSync) { + aRv.ThrowInvalidStateError( + "Can't use formatValuesSync when state is async."); + return; + } nsTArray jsKeys; SequenceRooter rooter(aCx, &jsKeys); for (auto& key : aKeys) { @@ -309,6 +316,11 @@ void Localization::FormatMessagesSync(JSContext* aCx, const Sequence& aKeys, nsTArray>& aRetVal, ErrorResult& aRv) { + if (!mIsSync) { + aRv.ThrowInvalidStateError( + "Can't use formatMessagesSync when state is async."); + return; + } nsTArray jsKeys; SequenceRooter rooter(aCx, &jsKeys); for (auto& key : aKeys) { diff --git a/intl/l10n/Localization.jsm b/intl/l10n/Localization.jsm index b4b6a08eb6fb..0629a77b195a 100644 --- a/intl/l10n/Localization.jsm +++ b/intl/l10n/Localization.jsm @@ -219,7 +219,6 @@ class Localization { this.resourceIds = []; this.generateBundles = undefined; this.generateBundlesSync = undefined; - this.isSync = undefined; this.bundles = undefined; } @@ -228,29 +227,24 @@ class Localization { * * @param {Array} resourceIds - List of resource ids used by this * localization. - * @param {bool} sync - Whether the instance should be + * @param {bool} isSync - Whether the instance should be * synchronous. * @param {bool} eager - Whether the initial bundles should be * fetched eagerly. * @param {Function} generateBundles - Custom FluentBundle asynchronous generator. * @param {Function} generateBundlesSync - Custom FluentBundle generator. */ - activate(resourceIds, sync, eager, generateBundles = defaultGenerateBundles, generateBundlesSync = defaultGenerateBundlesSync) { + activate(resourceIds, isSync, eager, generateBundles = defaultGenerateBundles, generateBundlesSync = defaultGenerateBundlesSync) { if (this.bundles) { throw new Error("Attempt to initialize an already initialized instance."); } this.generateBundles = generateBundles; this.generateBundlesSync = generateBundlesSync; - this.isSync = sync; - this.regenerateBundles(resourceIds, eager); + this.regenerateBundles(resourceIds, isSync, eager); } - setIsSync(isSync) { - this.isSync = isSync; - } - - cached(iterable) { - if (this.isSync) { + cached(iterable, isSync) { + if (isSync) { return CachedSyncIterable.from(iterable); } else { return CachedAsyncIterable.from(iterable); @@ -309,9 +303,6 @@ class Localization { * @private */ formatWithFallbackSync(keys, method) { - if (!this.isSync) { - throw new Error("Can't use sync formatWithFallback when state is async."); - } if (!this.bundles) { throw new Error("Attempt to format on an uninitialized instance."); } @@ -464,9 +455,9 @@ class Localization { * @param {Array} resourceIds - List of resource ids used by this * localization. */ - onChange(resourceIds) { + onChange(resourceIds, isSync) { if (this.bundles) { - this.regenerateBundles(resourceIds, false); + this.regenerateBundles(resourceIds, isSync, false); } } @@ -478,11 +469,11 @@ class Localization { * localization. * @param {bool} eager - whether the I/O for new context should begin eagerly */ - regenerateBundles(resourceIds, eager = false) { + regenerateBundles(resourceIds, isSync, eager = false) { // Store for error reporting from `formatWithFallback`. this.resourceIds = resourceIds; - let generateMessages = this.isSync ? this.generateBundlesSync : this.generateBundles; - this.bundles = this.cached(generateMessages(this.resourceIds)); + let generateMessages = isSync ? this.generateBundlesSync : this.generateBundles; + this.bundles = this.cached(generateMessages(this.resourceIds), isSync); if (eager) { // If the first app locale is the same as last fallback // it means that we have all resources in this locale, and diff --git a/intl/l10n/mozILocalization.idl b/intl/l10n/mozILocalization.idl index a906208008de..3622015fc8a5 100644 --- a/intl/l10n/mozILocalization.idl +++ b/intl/l10n/mozILocalization.idl @@ -26,7 +26,7 @@ interface mozILocalization : nsISupports Array formatValuesSync(in Array aKeys); Array formatMessagesSync(in Array aKeys); - void onChange(in Array aResourceIds); + void onChange(in Array aResourceIds, in bool aIsSync); }; [scriptable, uuid(96632d26-1422-12e9-b1ce-9bb586acd241)]