Bug 1314861: Lazily initialize l10n code. r?rpl draft
authorKris Maglione <maglione.k@gmail.com>
Sun, 30 Oct 2016 16:53:37 -0700
changeset 558817 1d3a0298575073fd057fc019a75b9c84a1a5a67b
parent 558816 97908447c131f76890dc88be522acf7a710cf7a6
child 558818 e744f8b7716b7ff682e99fc981220b4592052b9c
push id52953
push usermaglione.k@gmail.com
push dateSat, 08 Apr 2017 01:32:07 +0000
reviewersrpl
bugs1314861
milestone55.0a1
Bug 1314861: Lazily initialize l10n code. r?rpl MozReview-Commit-ID: DgmrEMQGrtw
addon-sdk/source/lib/sdk/content/l10n-html.js
addon-sdk/source/lib/sdk/l10n.js
addon-sdk/source/lib/sdk/l10n/core.js
addon-sdk/source/lib/sdk/l10n/loader.js
addon-sdk/source/lib/sdk/l10n/prefs.js
addon-sdk/source/lib/sdk/l10n/properties/core.js
--- a/addon-sdk/source/lib/sdk/content/l10n-html.js
+++ b/addon-sdk/source/lib/sdk/content/l10n-html.js
@@ -3,19 +3,20 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 "use strict";
 
 module.metadata = {
   "stability": "unstable"
 };
 
 const { Ci, Cc, Cu } = require("chrome");
-const core = require("../l10n/core");
-const { loadSheet, removeSheet } = require("../stylesheet/utils");
+lazyRequireModule(this, "../l10n/core", "core");
+lazyRequire(this, "../stylesheet/utils", "loadSheet", "removeSheet");
 const { process, frames } = require("../remote/child");
+
 var observerService = Cc["@mozilla.org/observer-service;1"]
                       .getService(Ci.nsIObserverService);
 const { ShimWaiver } = Cu.import("resource://gre/modules/ShimWaiver.jsm");
 const addObserver = ShimWaiver.getProperty(observerService, "addObserver");
 const removeObserver = ShimWaiver.getProperty(observerService, "removeObserver");
 
 const assetsURI = require('../self').data.url();
 
--- a/addon-sdk/source/lib/sdk/l10n.js
+++ b/addon-sdk/source/lib/sdk/l10n.js
@@ -2,24 +2,27 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 "use strict";
 
 module.metadata = {
   "stability": "stable"
 };
 
-const json = require("./l10n/json/core");
-const { get: getKey } = require("./l10n/core");
-const properties = require("./l10n/properties/core");
-const { getRulesForLocale } = require("./l10n/plural-rules");
+lazyRequireModule(this, "./l10n/json/core", "json");
+lazyRequire(this, "./l10n/core", {"get": "getKey"});
+lazyRequireModule(this, "./l10n/properties/core", "properties");
+lazyRequire(this, "./l10n/plural-rules", "getRulesForLocale");
+
+const { XPCOMUtils } = require("resource://gre/modules/XPCOMUtils.jsm");
 
 // Retrieve the plural mapping function
-var pluralMappingFunction = getRulesForLocale(json.language()) ||
-                            getRulesForLocale("en");
+XPCOMUtils.defineLazyGetter(this, "pluralMappingFunction",
+                            () => getRulesForLocale(json.language()) ||
+                                  getRulesForLocale("en"));
 
 exports.get = function get(k) {
   // For now, we only accept a "string" as first argument
   // TODO: handle plural forms in gettext pattern
   if (typeof k !== "string")
     throw new Error("First argument of localization method should be a string");
   let n = arguments[1];
 
--- a/addon-sdk/source/lib/sdk/l10n/core.js
+++ b/addon-sdk/source/lib/sdk/l10n/core.js
@@ -1,9 +1,15 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 "use strict";
 
-const json = require("./json/core");
-const properties = require("./properties/core");
+lazyRequireModule(this, "./json/core", "json");
+lazyRequireModule(this, "./properties/core", "properties");
 
-exports.get = json.usingJSON ? json.get : properties.get;
+const { XPCOMUtils } = require("resource://gre/modules/XPCOMUtils.jsm");
+XPCOMUtils.defineLazyGetter(this, "get",
+                            () => json.usingJSON ? json.get : properties.get);
+
+module.exports = Object.freeze({
+  get get() { return get; }, // ... yeah.
+});
--- a/addon-sdk/source/lib/sdk/l10n/loader.js
+++ b/addon-sdk/source/lib/sdk/l10n/loader.js
@@ -3,19 +3,19 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 "use strict";
 
 module.metadata = {
   "stability": "unstable"
 };
 
 const { Cc, Ci } = require("chrome");
-const { getPreferedLocales, findClosestLocale } = require("./locale");
-const { readURI } = require("../net/url");
-const { resolve } = require("../core/promise");
+lazyRequire(this, "./locale", "getPreferedLocales", "findClosestLocale");
+lazyRequire(this, "../net/url", "readURI");
+lazyRequire(this, "../core/promise", "resolve");
 
 function parseJsonURI(uri) {
   return readURI(uri).
     then(JSON.parse).
     then(null, function (error) {
       throw Error("Failed to parse locale file:\n" + uri + "\n" + error);
     });
 }
--- a/addon-sdk/source/lib/sdk/l10n/prefs.js
+++ b/addon-sdk/source/lib/sdk/l10n/prefs.js
@@ -1,15 +1,15 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 "use strict";
 
-const { on } = require("../system/events");
-const core = require("./core");
+lazyRequire(this, "../system/events", "on");
+lazyRequireModule(this, "./core", "core");
 const { id: jetpackId } = require('../self');
 
 const OPTIONS_DISPLAYED = "addon-options-displayed";
 
 function enable() {
   on(OPTIONS_DISPLAYED, onOptionsDisplayed);  
 }
 exports.enable = enable;
--- a/addon-sdk/source/lib/sdk/l10n/properties/core.js
+++ b/addon-sdk/source/lib/sdk/l10n/properties/core.js
@@ -1,22 +1,24 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 "use strict";
 
 const { Cu } = require("chrome");
-const { newURI } = require('../../url/utils')
-const { getRulesForLocale } = require("../plural-rules");
-const { getPreferedLocales } = require('../locale');
+lazyRequire(this, '../../url/utils', 'newURI');
+lazyRequire(this, "../plural-rules", 'getRulesForLocale');
+lazyRequire(this, '../locale', 'getPreferedLocales');
 const { rootURI } = require("@loader/options");
-const { Services } = Cu.import("resource://gre/modules/Services.jsm", {});
+const { Services } = require("resource://gre/modules/Services.jsm");
+const { XPCOMUtils } = require("resource://gre/modules/XPCOMUtils.jsm");
 
 const baseURI = rootURI + "locale/";
-const preferedLocales = getPreferedLocales(true);
+
+XPCOMUtils.defineLazyGetter(this, "preferedLocales", () => getPreferedLocales(true));
 
 // Make sure we don't get stale data after an update
 // (See Bug 1300735 for rationale).
 Services.strings.flushBundles();
 
 function getLocaleURL(locale) {
   // if the locale is a valid chrome URI, return it
   try {