mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-27 14:52:16 +00:00
Bug 1613705 - [localization] part8: Refactor SetIsSync to be SetAsync. r=platform-i18n-reviewers,dminor,emilio
Depends on D113571 Differential Revision: https://phabricator.services.mozilla.com/D113691
This commit is contained in:
parent
d22418d41e
commit
de1ee6dbf1
@ -325,7 +325,7 @@ already_AddRefed<Promise> DOMLocalization::TranslateElements(
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (mIsSync) {
|
||||
if (IsSync()) {
|
||||
nsTArray<Nullable<L10nMessage>> l10nMessages;
|
||||
|
||||
FormatMessagesSync(l10nKeys, l10nMessages, aRv);
|
||||
|
@ -296,7 +296,7 @@ void DocumentL10n::InitialTranslationCompleted(bool aL10nCached) {
|
||||
|
||||
// From now on, the state of Localization is unconditionally
|
||||
// async.
|
||||
SetIsSync(false);
|
||||
SetAsync();
|
||||
}
|
||||
|
||||
void DocumentL10n::ConnectRoot(nsINode& aNode, bool aTranslate,
|
||||
|
@ -157,7 +157,7 @@ interface Localization {
|
||||
*/
|
||||
[NewObject] Promise<sequence<L10nMessage?>> formatMessages(sequence<L10nKey> aKeys);
|
||||
|
||||
void setIsSync(boolean aIsSync);
|
||||
void setAsync();
|
||||
|
||||
[NewObject, Throws]
|
||||
UTF8String? formatValueSync(UTF8String aId, optional L10nArgs aArgs);
|
||||
|
@ -140,32 +140,31 @@ already_AddRefed<Localization> Localization::Create(
|
||||
return MakeAndAddRef<Localization>(aResourceIds, aIsSync);
|
||||
}
|
||||
|
||||
Localization::Localization(const nsTArray<nsCString>& aResIds, bool aIsSync)
|
||||
: mIsSync(aIsSync) {
|
||||
ffi::localization_new(&aResIds, mIsSync, nullptr, getter_AddRefs(mRaw));
|
||||
Localization::Localization(const nsTArray<nsCString>& aResIds, bool aIsSync) {
|
||||
ffi::localization_new(&aResIds, aIsSync, nullptr, getter_AddRefs(mRaw));
|
||||
|
||||
RegisterObservers();
|
||||
}
|
||||
|
||||
Localization::Localization(nsIGlobalObject* aGlobal,
|
||||
const nsTArray<nsCString>& aResIds, bool aIsSync)
|
||||
: mGlobal(aGlobal), mIsSync(aIsSync) {
|
||||
ffi::localization_new(&aResIds, mIsSync, nullptr, getter_AddRefs(mRaw));
|
||||
: mGlobal(aGlobal) {
|
||||
ffi::localization_new(&aResIds, aIsSync, nullptr, getter_AddRefs(mRaw));
|
||||
|
||||
RegisterObservers();
|
||||
}
|
||||
|
||||
Localization::Localization(nsIGlobalObject* aGlobal, bool aIsSync)
|
||||
: mGlobal(aGlobal), mIsSync(aIsSync) {
|
||||
: mGlobal(aGlobal) {
|
||||
nsTArray<nsCString> resIds;
|
||||
ffi::localization_new(&resIds, mIsSync, nullptr, getter_AddRefs(mRaw));
|
||||
ffi::localization_new(&resIds, aIsSync, nullptr, getter_AddRefs(mRaw));
|
||||
|
||||
RegisterObservers();
|
||||
}
|
||||
|
||||
Localization::Localization(nsIGlobalObject* aGlobal, bool aIsSync,
|
||||
const ffi::LocalizationRc* aRaw)
|
||||
: mGlobal(aGlobal), mRaw(aRaw), mIsSync(aIsSync) {
|
||||
: mGlobal(aGlobal), mRaw(aRaw) {
|
||||
RegisterObservers();
|
||||
}
|
||||
|
||||
@ -238,12 +237,6 @@ void Localization::RegisterObservers() {
|
||||
|
||||
void Localization::OnChange() { ffi::localization_on_change(mRaw.get()); }
|
||||
|
||||
void Localization::SetIsSync(bool aIsSync) {
|
||||
MOZ_ASSERT(!aIsSync, "We should only move from sync to async!");
|
||||
mIsSync = aIsSync;
|
||||
Upgrade();
|
||||
}
|
||||
|
||||
void Localization::AddResourceId(const nsAString& aResourceId) {
|
||||
NS_ConvertUTF16toUTF8 resId(aResourceId);
|
||||
ffi::localization_add_res_id(mRaw.get(), &resId);
|
||||
@ -464,7 +457,8 @@ void Localization::FormatMessagesSync(
|
||||
aRetVal = ConvertToL10nMessages(result, aRv);
|
||||
}
|
||||
|
||||
void Localization::Upgrade() { ffi::localization_upgrade(mRaw.get()); }
|
||||
void Localization::SetAsync() { ffi::localization_set_async(mRaw.get()); }
|
||||
bool Localization::IsSync() { return ffi::localization_is_sync(mRaw.get()); }
|
||||
|
||||
/**
|
||||
* PromiseResolver is a PromiseNativeHandler used
|
||||
|
@ -76,7 +76,8 @@ class Localization : public nsIObserver,
|
||||
void AddResourceIds(const nsTArray<nsString>& aResourceIds);
|
||||
uint32_t RemoveResourceIds(const nsTArray<nsString>& aResourceIds);
|
||||
|
||||
void Upgrade();
|
||||
void SetAsync();
|
||||
bool IsSync();
|
||||
|
||||
protected:
|
||||
Localization(const nsTArray<nsCString>& aResIds, bool aIsSync);
|
||||
@ -96,7 +97,6 @@ class Localization : public nsIObserver,
|
||||
|
||||
nsCOMPtr<nsIGlobalObject> mGlobal;
|
||||
RefPtr<const ffi::LocalizationRc> mRaw;
|
||||
bool mIsSync;
|
||||
};
|
||||
|
||||
} // namespace intl
|
||||
|
@ -169,8 +169,7 @@ impl LocalizationRc {
|
||||
self.inner.borrow_mut().remove_resource_ids(res_ids)
|
||||
}
|
||||
|
||||
/// Upgrade synchronous mode to asynchronous.
|
||||
pub fn upgrade(&self) {
|
||||
pub fn set_async(&self) {
|
||||
if self.is_sync() {
|
||||
self.inner.borrow_mut().set_async();
|
||||
}
|
||||
@ -555,8 +554,13 @@ pub extern "C" fn localization_format_messages(
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn localization_upgrade(loc: &LocalizationRc) {
|
||||
loc.upgrade();
|
||||
pub extern "C" fn localization_set_async(loc: &LocalizationRc) {
|
||||
loc.set_async();
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn localization_is_sync(loc: &LocalizationRc) -> bool {
|
||||
loc.is_sync()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
|
@ -167,18 +167,22 @@ add_task(async function test_switch_to_async() {
|
||||
const source = L10nFileSource.createMock("test", ["en-US"], "/localization/{locale}", fs);
|
||||
l10nReg.registerSources([source]);
|
||||
|
||||
const l10n = new Localization(["/browser/menu.ftl"], false, l10nReg, ["en-US"]);
|
||||
const l10n = new Localization(["/browser/menu.ftl"], true, l10nReg, ["en-US"]);
|
||||
|
||||
let values = await l10n.formatValues([{id: "key1"}, {id: "key2"}]);
|
||||
let values = l10n.formatValuesSync([{id: "key1"}, {id: "key2"}]);
|
||||
|
||||
strictEqual(values[0], "Value1");
|
||||
strictEqual(values[1], null);
|
||||
|
||||
l10n.setIsSync(true);
|
||||
l10n.setAsync();
|
||||
|
||||
Assert.throws(() => {
|
||||
l10n.formatValuesSync([{ id: "key1" }, { id: "key2" }]);
|
||||
}, /Can't use formatValuesSync when state is async./);
|
||||
|
||||
l10n.addResourceIds(["/toolkit/menu.ftl"]);
|
||||
|
||||
values = l10n.formatValuesSync([{id: "key1"}, {id: "key2"}]);
|
||||
values = await l10n.formatValues([{id: "key1"}, {id: "key2"}]);
|
||||
let values2 = await l10n.formatValues([{id: "key1"}, {id: "key2"}]);
|
||||
|
||||
deepEqual(values, values2);
|
||||
|
Loading…
Reference in New Issue
Block a user