Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Importing Modules Lazily

8 views
Skip to first unread message

Jordan Santell

unread,
Jul 10, 2015, 1:41:09 PM7/10/15
to dev-developer-tools
On the loader, we have several lazy loading methods:

* lazyGetter
* lazyImporter
* lazyServiceGetter
* lazyRequireGetter

In bug 1181967[0], it's proposed to consolidate these into one lazy
function that handles all scenarios, whether loading a common JS module,
JSM, service, or some custom importer.

More explanation can be found in the bug; `lazyImporter` becomes woefully
redundant as our Loader can also load JSM's via resource URIs, and
`lazyServiceGetter` is seldom used, and just the lazyGetter function is
used in those cases.

Proposing a single method, (called `lazyDefine` for now, and we probably
need a better name! ideas?) that handles taking a loader path, resource
URI, or a function.

// Both commonJS and JSMs can be destructured, as currently. JSMs almost
always will
// need to be destructured.
loader.lazyDefine(this, "emit", "sdk/events/core", true)
loader.lazyDefine(this, "Task", "resource://gre/modules/Task.jsm", true);

// Can also just drop in a function that's called once on first access
// This is how you can get a service, or any custom behavior on load
lazyDefine(this, “myServiceName”, () =>
Cc[“@mozilla/service;1”].getService(Ci.nsIInterface));

So, two things: should this lazy loader function handle both commonJS/JSM
modules as well as an arbitrary function? And if so, what should it be
called? Or should we just reduce lazy functions to one that handles module
paths and one that takes a function? I like a single function that does
both (easily), but it's hard to come up with a name -- because sometimes we
just lazily populate an object that does *not* import things:

loader.lazyDefine(this, "toolboxStrings", () =>
Services.strings.createBundle(STRINGS_URL));

More background found in the bug!

[0] https://bugzilla.mozilla.org/show_bug.cgi?id=1181967
0 new messages