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

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

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

@ -71,7 +71,7 @@ void Localization::Activate(const bool aSync, const bool aEager,
}
mIsSync = aSync;
mLocalization->Activate(aSync, aEager, generateBundlesJS,
mLocalization->Activate(mResourceIds, aSync, aEager, generateBundlesJS,
generateBundlesSyncJS);
RegisterObservers();
@ -145,26 +145,23 @@ Localization::Observe(nsISupports* aSubject, const char* aTopic,
void Localization::OnChange() {
if (mLocalization) {
mLocalization->OnChange();
mLocalization->OnChange(mResourceIds);
}
}
uint32_t Localization::AddResourceId(const nsAString& aResourceId) {
uint32_t ret = 0;
mLocalization->AddResourceId(aResourceId, &ret);
return ret;
if (!mResourceIds.Contains(aResourceId)) {
mResourceIds.AppendElement(aResourceId);
OnChange();
}
return mResourceIds.Length();
}
uint32_t Localization::RemoveResourceId(const nsAString& aResourceId) {
// We need to guard against a scenario where the
// mLocalization has been unlinked, but the elements
// are only now removed from DOM.
if (!mLocalization) {
return 0;
if (mResourceIds.RemoveElement(aResourceId)) {
OnChange();
}
uint32_t ret = 0;
mLocalization->RemoveResourceId(aResourceId, &ret);
return ret;
return mResourceIds.Length();
}
/**
@ -172,22 +169,33 @@ uint32_t Localization::RemoveResourceId(const nsAString& aResourceId) {
*/
uint32_t Localization::AddResourceIds(const nsTArray<nsString>& aResourceIds) {
uint32_t ret = 0;
mLocalization->AddResourceIds(aResourceIds, &ret);
return ret;
bool added = false;
for (const auto& resId : aResourceIds) {
if (!mResourceIds.Contains(resId)) {
mResourceIds.AppendElement(resId);
added = true;
}
}
if (added) {
OnChange();
}
return mResourceIds.Length();
}
uint32_t Localization::RemoveResourceIds(
const nsTArray<nsString>& aResourceIds) {
// We need to guard against a scenario where the
// mLocalization has been unlinked, but the elements
// are only now removed from DOM.
if (!mLocalization) {
return 0;
bool removed = false;
for (const auto& resId : aResourceIds) {
if (mResourceIds.RemoveElement(resId)) {
removed = true;
}
}
uint32_t ret = 0;
mLocalization->RemoveResourceIds(aResourceIds, &ret);
return ret;
if (removed) {
OnChange();
}
return mResourceIds.Length();
}
already_AddRefed<Promise> Localization::FormatValue(

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

@ -93,6 +93,7 @@ class Localization : public nsIObserver,
nsCOMPtr<nsIGlobalObject> mGlobal;
nsCOMPtr<mozILocalization> mLocalization;
bool mIsSync;
nsTArray<nsString> mResourceIds;
};
} // namespace intl

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

@ -226,6 +226,8 @@ class Localization {
/**
* Activate the instance of the `Localization` class.
*
* @param {Array<String>} resourceIds - List of resource ids used by this
* localization.
* @param {bool} sync - Whether the instance should be
* synchronous.
* @param {bool} eager - Whether the initial bundles should be
@ -233,14 +235,14 @@ class Localization {
* @param {Function} generateBundles - Custom FluentBundle asynchronous generator.
* @param {Function} generateBundlesSync - Custom FluentBundle generator.
*/
activate(sync, eager, generateBundles = defaultGenerateBundles, generateBundlesSync = defaultGenerateBundlesSync) {
activate(resourceIds, sync, 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(eager);
this.regenerateBundles(resourceIds, eager);
}
setIsSync(isSync) {
@ -255,42 +257,6 @@ class Localization {
}
}
/**
* @param {String} resourceId - Resource IDs
*/
addResourceId(resourceId) {
this.resourceIds.push(resourceId);
this.onChange();
return this.resourceIds.length;
}
/**
* @param {String} resourceId - Resource IDs
*/
removeResourceId(resourceId) {
this.resourceIds = this.resourceIds.filter(r => r !== resourceId);
this.onChange();
return this.resourceIds.length;
}
/**
* @param {Array<String>} resourceIds - List of resource IDs
*/
addResourceIds(resourceIds) {
this.resourceIds.push(...resourceIds);
this.onChange();
return this.resourceIds.length;
}
/**
* @param {Array<String>} resourceIds - List of resource IDs
*/
removeResourceIds(resourceIds) {
this.resourceIds = this.resourceIds.filter(r => !resourceIds.includes(r));
this.onChange();
return this.resourceIds.length;
}
/**
* Format translations and handle fallback if needed.
*
@ -494,9 +460,13 @@ class Localization {
return val;
}
onChange() {
/**
* @param {Array<String>} resourceIds - List of resource ids used by this
* localization.
*/
onChange(resourceIds) {
if (this.bundles) {
this.regenerateBundles(false);
this.regenerateBundles(resourceIds, false);
}
}
@ -504,9 +474,13 @@ class Localization {
* This method should be called when there's a reason to believe
* that language negotiation or available resources changed.
*
* @param {Array<String>} resourceIds - List of resource ids used by this
* localization.
* @param {bool} eager - whether the I/O for new context should begin eagerly
*/
regenerateBundles(eager = false) {
regenerateBundles(resourceIds, 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));
if (eager) {

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

@ -14,16 +14,10 @@
[scriptable, uuid(7d468600-551f-4fe0-98c9-92a53b63ec8d)]
interface mozILocalization : nsISupports
{
void activate(in bool aSync, in bool aEager, in jsval aGenerateBundles, in jsval aGenerateBundlesSync);
void activate(in Array<AString> aResourceIds, in bool aSync, in bool aEager, in jsval aGenerateBundles, in jsval aGenerateBundlesSync);
void setIsSync(in boolean isSync);
unsigned long addResourceId(in AString resourceId);
unsigned long removeResourceId(in AString resourceId);
unsigned long addResourceIds(in Array<AString> resourceIds);
unsigned long removeResourceIds(in Array<AString> resourceIds);
Promise formatMessages(in Array<jsval> aKeys);
Promise formatValues(in Array<jsval> aKeys);
Promise formatValue(in AUTF8String aId, [optional] in jsval aArgs);
@ -32,7 +26,7 @@ interface mozILocalization : nsISupports
Array<AUTF8String> formatValuesSync(in Array<jsval> aKeys);
Array<jsval> formatMessagesSync(in Array<jsval> aKeys);
void onChange();
void onChange(in Array<AString> aResourceIds);
};
[scriptable, uuid(96632d26-1422-12e9-b1ce-9bb586acd241)]