Bug 1314861: Lazily initialize l10n code. r?rpl draft
authorKris Maglione <maglione.k@gmail.com>
Sun, 30 Oct 2016 16:53:37 -0700
changeset 433092 531f125b08319f3b53c34b7f40c156f7640e0695
parent 433091 6da48856fad901712b7463c2ba79c9c40fc219b1
child 433093 2a3f7b7d6b3182a3272cfebf3f94e886d83da82f
push id34481
push usermaglione.k@gmail.com
push dateThu, 03 Nov 2016 03:45:04 +0000
reviewersrpl
bugs1314861
milestone52.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");
+lazyRequire({ "../l10n/core": "core" });
+lazyRequire("../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");
+lazyRequire({ "./l10n/json/core": "json" });
+lazyRequire("./l10n/core", {"get": "getKey"});
+lazyRequire({ "./l10n/properties/core": "properties" });
+lazyRequire("./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,17 @@
 /* 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");
+lazyRequire({
+  "./json/core": "json",
+  "./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("./locale", "getPreferedLocales", "findClosestLocale");
+lazyRequire("../net/url", "readURI");
+lazyRequire("../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("../system/events", "on");
+lazyRequire({ "./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('../../url/utils', 'newURI');
+lazyRequire("../plural-rules", 'getRulesForLocale');
+lazyRequire('../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 {