Bug 1631593 - Move aIsSync to Localization C++. r=stas,jfkthame

Differential Revision: https://phabricator.services.mozilla.com/D71679
This commit is contained in:
Zibi Braniecki 2020-05-19 16:27:58 +00:00
Родитель 1b6968ba58
Коммит f6d6adc6b3
4 изменённых файлов: 31 добавлений и 32 удалений

Просмотреть файл

@ -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,

Просмотреть файл

@ -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<Promise> 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<Promise> Localization::FormatValues(
JSContext* aCx, const Sequence<L10nKey>& aKeys, ErrorResult& aRv) {
@ -273,6 +270,11 @@ already_AddRefed<Promise> Localization::FormatMessages(
void Localization::FormatValueSync(JSContext* aCx, const nsACString& aId,
const Optional<L10nArgs>& aArgs,
nsACString& aRetVal, ErrorResult& aRv) {
if (!mIsSync) {
aRv.ThrowInvalidStateError(
"Can't use formatValueSync when state is async.");
return;
}
JS::Rooted<JS::Value> args(aCx);
if (aArgs.WasPassed()) {
@ -291,6 +293,11 @@ void Localization::FormatValuesSync(JSContext* aCx,
const Sequence<L10nKey>& aKeys,
nsTArray<nsCString>& aRetVal,
ErrorResult& aRv) {
if (!mIsSync) {
aRv.ThrowInvalidStateError(
"Can't use formatValuesSync when state is async.");
return;
}
nsTArray<JS::Value> jsKeys;
SequenceRooter<JS::Value> rooter(aCx, &jsKeys);
for (auto& key : aKeys) {
@ -309,6 +316,11 @@ void Localization::FormatMessagesSync(JSContext* aCx,
const Sequence<L10nKey>& aKeys,
nsTArray<Nullable<L10nMessage>>& aRetVal,
ErrorResult& aRv) {
if (!mIsSync) {
aRv.ThrowInvalidStateError(
"Can't use formatMessagesSync when state is async.");
return;
}
nsTArray<JS::Value> jsKeys;
SequenceRooter<JS::Value> rooter(aCx, &jsKeys);
for (auto& key : aKeys) {

Просмотреть файл

@ -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<String>} 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<String>} 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

Просмотреть файл

@ -26,7 +26,7 @@ interface mozILocalization : nsISupports
Array<AUTF8String> formatValuesSync(in Array<jsval> aKeys);
Array<jsval> formatMessagesSync(in Array<jsval> aKeys);
void onChange(in Array<AString> aResourceIds);
void onChange(in Array<AString> aResourceIds, in bool aIsSync);
};
[scriptable, uuid(96632d26-1422-12e9-b1ce-9bb586acd241)]