Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
more flexible signatures for module.js exports
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott J. Miles committed Feb 4, 2014
1 parent b858eb4 commit 3d161da
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions src/module.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
(function(scope) {

function withDependencies(task, depends) {
return task.apply(this, depends && depends.map(marshal) || []);
depends = depends || [];
if (!depends.map) {
depends = [depends];
}
return task.apply(this, depends.map(marshal));
}

function module(name, depends, moduleFactory) {
modules[name] = withDependencies(moduleFactory, depends);
function module(name, dependsOrFactory, moduleFactory) {
var module;
switch (arguments.length) {
case 0:
return;
case 1:
module = null;
break;
case 2:
module = dependsOrFactory.apply(this);
break;
default:
module = withDependencies(moduleFactory, dependsOrFactory);
break;
}
modules[name] = module;
};

function marshal(name) {
Expand All @@ -15,20 +33,15 @@
var modules = {};

function using(depends, task) {
if (HTMLImports.ready) {
HTMLImports.whenImportsReady(function() {
withDependencies(task, depends);
} else {
addEventListener('HTMLImportsLoaded', function() {
withDependencies(task, depends);
});
}
});
};

// exports

scope.marshal = marshal;
scope.module = module;
scope.withDependencies = withDependencies;
scope.using = using;

})(window);

0 comments on commit 3d161da

Please sign in to comment.