Bug 991904 - Create a way to load devtools modules lazily. r=dcamp
authorNick Fitzgerald <fitzgen@mozilla.com>
Fri, 25 Apr 2014 11:23:00 -0400
changeset 180819 c00e60f190244ba93d491f9392be20372b9e2320
parent 180818 c376ff82cd3e04fc02077fe0545b89076949020f
child 180820 76604cbff5ab3ef8cde1ae8625324d45a195c50c
push id272
push userpvanderbeken@mozilla.com
push dateMon, 05 May 2014 16:31:18 +0000
reviewersdcamp
bugs991904
milestone31.0a1
Bug 991904 - Create a way to load devtools modules lazily. r=dcamp
toolkit/devtools/Loader.jsm
toolkit/devtools/tests/unit/head_devtools.js
toolkit/devtools/tests/unit/test_independent_loaders.js
toolkit/devtools/tests/unit/test_invisible_loader.js
toolkit/devtools/tests/unit/test_require_lazy.js
toolkit/devtools/tests/unit/xpcshell.ini
--- a/toolkit/devtools/Loader.jsm
+++ b/toolkit/devtools/Loader.jsm
@@ -271,16 +271,34 @@ DevToolsLoader.prototype = {
    * @see setProvider
    */
   require: function() {
     this._chooseProvider();
     return this.require.apply(this, arguments);
   },
 
   /**
+   * Define a getter property on the given object that requires the given
+   * module. This enables delaying importing modules until the module is
+   * actually used.
+   *
+   * @param Object obj
+   *    The object to define the property on.
+   * @param String property
+   *    The property name.
+   * @param String module
+   *    The module path.
+   */
+  lazyRequireGetter: function (obj, property, module) {
+    Object.defineProperty(obj, property, {
+      get: () => this.require(module)
+    });
+  },
+
+  /**
    * Add a URI to the loader.
    * @param string id
    *    The module id that can be used within the loader to refer to this module.
    * @param string uri
    *    The URI to load as a module.
    * @returns The module's exports.
    */
   loadURI: function(id, uri) {
--- a/toolkit/devtools/tests/unit/head_devtools.js
+++ b/toolkit/devtools/tests/unit/head_devtools.js
@@ -1,14 +1,15 @@
 "use strict";
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 const Cu = Components.utils;
 const Cr = Components.results;
 
+Cu.import("resource://gre/modules/devtools/Loader.jsm");
 Cu.import("resource://gre/modules/devtools/DevToolsUtils.jsm");
 
 // Register a console listener, so console messages don't just disappear
 // into the ether.
 let errorCount = 0;
 let listener = {
   observe: function (aMessage) {
     errorCount++;
--- a/toolkit/devtools/tests/unit/test_independent_loaders.js
+++ b/toolkit/devtools/tests/unit/test_independent_loaders.js
@@ -1,14 +1,11 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
-const { DevToolsLoader } =
-  Cu.import("resource://gre/modules/devtools/Loader.jsm", {});
-
 /**
  * Ensure that each instance of the Dev Tools loader contains its own loader
  * instance, and also returns unique objects.  This ensures there is no sharing
  * in place between loaders.
  */
 function run_test() {
   let loader1 = new DevToolsLoader();
   let loader2 = new DevToolsLoader();
--- a/toolkit/devtools/tests/unit/test_invisible_loader.js
+++ b/toolkit/devtools/tests/unit/test_invisible_loader.js
@@ -1,13 +1,11 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
-const { DevToolsLoader } =
-  Cu.import("resource://gre/modules/devtools/Loader.jsm", {});
 Cu.import("resource://gre/modules/jsdebugger.jsm");
 addDebuggerToGlobal(this);
 
 const COLOR_URI = "resource://gre/modules/devtools/css-color.js";
 
 /**
  * Ensure that sandboxes created via the Dev Tools loader respect the
  * invisibleToDebugger flag.
new file mode 100644
--- /dev/null
+++ b/toolkit/devtools/tests/unit/test_require_lazy.js
@@ -0,0 +1,14 @@
+/* -*- Mode: js; js-indent-level: 2; -*- */
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+// Test devtools.lazyRequireGetter
+
+function run_test() {
+  const o = {};
+  devtools.lazyRequireGetter(o, "asyncUtils", "devtools/async-utils");
+  const asyncUtils = devtools.require("devtools/async-utils");
+  // XXX: do_check_eq only works on primitive types, so we have this
+  // do_check_true of an equality expression.
+  do_check_true(o.asyncUtils === asyncUtils);
+}
--- a/toolkit/devtools/tests/unit/xpcshell.ini
+++ b/toolkit/devtools/tests/unit/xpcshell.ini
@@ -2,8 +2,9 @@
 head = head_devtools.js
 tail =
 
 [test_independent_loaders.js]
 [test_invisible_loader.js]
 [test_safeErrorString.js]
 [test_defineLazyPrototypeGetter.js]
 [test_async-utils.js]
+[test_require_lazy.js]
\ No newline at end of file