Skip to content

Commit

Permalink
Merge pull request google#417 from bebbi/add_jsloaderreturndeferred
Browse files Browse the repository at this point in the history
in loadMany, return resulting deferred object to allow further manipulat...
  • Loading branch information
joeltine committed Aug 13, 2015
2 parents a5f9b8a + 43a7027 commit 748b324
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Name or Organization <email address>

Google Inc.
gigmade ltd.
Mohamed Mansour <hello@mohamedmansour.com>
Bjorn Tipling <bjorn.tipling@gmail.com>
SameGoal LLC <help@samegoal.com>
Expand Down
24 changes: 20 additions & 4 deletions closure/goog/net/jsloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ goog.net.jsloader.Options;
goog.net.jsloader.scriptsToLoad_ = [];


/**
* The deferred result of loading the URIs in scriptsToLoad_.
* We need to return this to a caller that wants to load URIs while
* a deferred is already working on them.
* @type {!goog.async.Deferred}
* @private
*/
goog.net.jsloader.scriptLoadingDeferred_;


/**
* Loads and evaluates the JavaScript files at the specified URIs, guaranteeing
* the order of script loads.
Expand All @@ -94,6 +104,8 @@ goog.net.jsloader.scriptsToLoad_ = [];
* @param {Array<string>} uris The URIs to load.
* @param {goog.net.jsloader.Options=} opt_options Optional parameters. See
* goog.net.jsloader.options documentation for details.
* @return {!goog.async.Deferred} The deferred result, that may be used to add
* callbacks
*/
goog.net.jsloader.loadMany = function(uris, opt_options) {
// Loading the scripts in serial introduces asynchronosity into the flow.
Expand All @@ -103,16 +115,18 @@ goog.net.jsloader.loadMany = function(uris, opt_options) {
//
// To work around this issue, all module loads share a queue.
if (!uris.length) {
return;
return goog.async.Deferred.succeed(null);
}

var isAnotherModuleLoading = goog.net.jsloader.scriptsToLoad_.length;
goog.array.extend(goog.net.jsloader.scriptsToLoad_, uris);
if (isAnotherModuleLoading) {
// jsloader is still loading some other scripts.
// In order to prevent the race condition noted above, we just add
// these URIs to the end of the scripts' queue and return.
return;
// these URIs to the end of the scripts' queue and return the deferred
// result of the ongoing script load, so the caller knows when they
// finish loading.
return goog.net.jsloader.scriptLoadingDeferred_;
}

uris = goog.net.jsloader.scriptsToLoad_;
Expand All @@ -122,8 +136,10 @@ goog.net.jsloader.loadMany = function(uris, opt_options) {
if (uris.length) {
deferred.addBoth(popAndLoadNextScript);
}
return deferred;
};
popAndLoadNextScript();
goog.net.jsloader.scriptLoadingDeferred_ = popAndLoadNextScript();
return goog.net.jsloader.scriptLoadingDeferred_;
};


Expand Down
21 changes: 20 additions & 1 deletion closure/goog/net/jsloader_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,19 @@ function testLoadMany() {

// Load test #3 and then #1.
window.test1 = null;
window.test4 = null;
var testUrls1 = ['testdata/jsloader_test3.js', 'testdata/jsloader_test1.js'];
goog.net.jsloader.loadMany(testUrls1);
var result = goog.net.jsloader.loadMany(testUrls1);

window.test3Callback = function(msg) {
testCase.continueTesting();

// Check that the 1st test was not loaded yet.
assertEquals('verification object', null, window.test1);

// check that result has not fired yet
assertFalse('deferred has fired', result.hasFired());

// Load test #4, which is supposed to wait for #1 to load.
testCase.waitForAsync('testLoadMany');
var testUrls2 = ['testdata/jsloader_test4.js'];
Expand All @@ -134,7 +138,22 @@ function testLoadMany() {

// Check that the 1st test was already loaded.
assertEquals('verification object', 'Test #1 loaded', window.test1);

// all scripts loaded - verify that result has fired
assertTrue('deferred has fired', result.hasFired());

// on last script loaded, set variable
window.test4 = msg;

testCase.waitForAsync('testLoadMany');
};

result.addCallback(function() {
testCase.continueTesting();

// verify that the last loaded script callback has executed
assertEquals('verification object', 'Test #4 loaded', window.test4);
});
}


Expand Down

0 comments on commit 748b324

Please sign in to comment.