Bug 991904 - Create a way to load devtools modules lazily. r=dcamp

This commit is contained in:
Nick Fitzgerald 2014-04-25 11:23:00 -04:00
Родитель 565a34e98e
Коммит 54975d6a6d
6 изменённых файлов: 34 добавлений и 5 удалений

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

@ -275,6 +275,24 @@ DevToolsLoader.prototype = {
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

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

@ -4,6 +4,7 @@ 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

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

@ -1,9 +1,6 @@
/* 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

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

@ -1,8 +1,6 @@
/* 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);

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

@ -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);
}

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

@ -7,3 +7,4 @@ tail =
[test_safeErrorString.js]
[test_defineLazyPrototypeGetter.js]
[test_async-utils.js]
[test_require_lazy.js]