Permalink
Browse files

Merge remote-tracking branch 'origin/master' into remove_timeouts

  • Loading branch information...
2 parents 297dbde + 2c6d2dc commit 33540227c0bd91da949630ca9f80eb7fe66848b9 @danwrong committed May 14, 2012
Showing with 87 additions and 23 deletions.
  1. +10 −11 plugins/defer.js
  2. +62 −12 src/loadrunner.js
  3. +1 −0 test/modules/modcompiled.js
  4. +14 −0 test/test.html
View
@@ -1,31 +1,30 @@
(function(context) {
loadrunner(function(using, provide) {
var def;
-
+
function deferred(id, body) {
return new loadrunner.Definition(id, function(exports) {
exports(body());
});
}
-
+
context.deferred = deferred;
-
+
using.matchers.add(/(^script!|\.js(!?)$)/, function(path) {
var force = !!path.match(/!$/);
-
- path = path.replace(/^\$/, using.path.replace(/\/$/, '') + '/').
- replace(/^script!/,'').replace(/!$/, '');
-
-
+
+ path = path.replace(/^script!/,'').replace(/!$/, '');
+
+
if (def = loadrunner.Definition.provided[path]) {
return def;
} else {
var script = new loadrunner.Script(path, force);
-
+
if (force) script.start();
return script;
- }
+ }
});
-
+
});
}(this));
View
@@ -154,6 +154,17 @@
}
Script.autoFetch = true;
+ Script.prototype.start = function() {
+ var me = this, bundle;
+ if (bundle = findBundle(this.id)) {
+ bundle.then(function() {
+ me.start();
+ });
+ } else {
+ Dependency.prototype.start.call(this);
+ }
+ };
+
Script.xhrTransport = function() {
var xhr;
@@ -213,8 +224,9 @@
}
Script.prototype = new Dependency;
- Script.prototype.resolvePath = function(path) {
- return (whichBundle(path) != path) ? whichBundle(path) : path;
+ Script.prototype.resolvePath = function(filePath) {
+ filePath = filePath.replace(/^\$/, using.path.replace(/\/$/, '') + '/');
+ return filePath;
}
Script.prototype.key = function() {
return "script_" + this.id;
@@ -235,22 +247,26 @@
Module.exports = {};
Module.prototype = new Script;
Module.prototype.start = function() {
- var me = this, def;
+ var me = this, def, bundle;
if (def = Definition.provided[this.id]) {
def.then(function(exports) {
me.complete.call(me, exports);
});
+ } else if (bundle = findBundle(this.id)) {
+ bundle.then(function() {
+ me.start();
+ });
} else {
Script.prototype.start.call(this);
}
};
Module.prototype.key = function() {
return 'module_' + this.id;
- }
+ };
Module.prototype.resolvePath = function(id) {
- return path(using.path, (whichBundle(id) != id) ? whichBundle(id) : id + '.js');
- }
+ return path(using.path, id + '.js');
+ };
Module.prototype.loaded = function() {
var p, exports, me = this;
if (!useInteractive) {
@@ -266,7 +282,39 @@
throw new Error("Tried to load '" + this.id +"' as a module, but it didn't have a 'provide()' in it.");
}
}
- }
+ };
+ function Bundle(id, contents) {
+ this.id = id;
+ this.contents = contents;
+ this.dep = createDependency(id);
+ this.deps = [];
+ this.path = this.dep.path;
+ }
+ Bundle.prototype = new Script;
+ Bundle.prototype.start = function() {
+ var me = this, def, dep, key;
+ for (var i=0, l=this.contents.length; i<l; i++) {
+ dep = createDependency(this.contents[i]);
+ this.deps.push(dep);
+ key = dep.key();
+
+ if (!metDependencies[key] && !inProgressDependencies[key] && !pausedDependencies[key]) {
+ pausedDependencies[key] = this;
+ }
+ }
+ Script.prototype.start.call(this);
+ };
+ Bundle.prototype.loaded = function() {
+ var p, exports, me = this, dep, key;
+ for (var i=0, l=this.deps.length; i<l; i++) {
+ dep = this.deps[i];
+ key = dep.key();
+
+ delete pausedDependencies[key];
+ metDependencies[key] = this;
+ }
+ Script.prototype.loaded.call(this);
+ };
function Definition(id, body) {
var module;
@@ -426,13 +474,15 @@
}
Sequence.prototype.forceFetch = forceFetch;
+ var definedBundles = [];
function Manifest() {
this.entries = {};
}
Manifest.prototype.push = function(bundle) {
for (var file in bundle) {
+ definedBundles[file] = new Bundle(file, bundle[file]);
for (var i=0, alias; alias = bundle[file][i]; i++) {
- this.entries[alias] = file;
+ this.entries[alias] = definedBundles[file];
}
}
}
@@ -588,8 +638,8 @@
// Append your bundle manifests to this array
// using.bundles.push( { "bundlename" : ["modulename", "modulename2", "script"], "bundle2": ["script2"] });
// Loadbuilder can generate your bundles and manifests
- function whichBundle(id) {
- return using.bundles.get(id) || id;
+ function findBundle(id) {
+ return using.bundles.get(id) || undefined;
}
using.matchers = [];
@@ -598,12 +648,12 @@
}
using.matchers.add(/(^script!|\.js$)/, function(path) {
- var script = new Script(path.replace(/^\$/, using.path.replace(/\/$/, '') + '/').replace(/^script!/,''));
+ var script = new Script(path.replace(/^script!/, ''));
return script;
});
using.matchers.add(/^(lr!)?[a-zA-Z0-9_\-\/]+$/, function(id) {
- var mod = new Module(id.replace(/^lr!/, '').replace(/!$/, ''));
+ var mod = new Module(id.replace(/^lr!/, ''));
return mod;
});
@@ -1,4 +1,5 @@
provide('thing', function(exports) {
+ window.thingLoaded = true;
exports('thing');
});
View
@@ -305,6 +305,20 @@ <h2 id="qunit-userAgent"></h2>
});
});
+ QUnit.test('should only evaluate required modules from a bundle', function() {
+ expect(3);
+ stop(2000);
+ loadrunner.reset();
+ window.thingLoaded = false;
+ equals(false, window.thingLoaded);
+ using.bundles.push({ 'modules/modcompiled.js': ['thing', 'another'] });
+ using('another', function(a) {
+ equals(a, 'another');
+ equals(false, window.thingLoaded);
+ start();
+ });
+ });
+
QUnit.module('Autofetch tests', {
teardown: function() {

0 comments on commit 3354022

Please sign in to comment.