| 1 |
// Copyright 2008 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview A default implementation for managing Javascript code modules. |
| 17 |
* |
| 18 |
*/ |
| 19 |
|
| 20 |
goog.provide('goog.module.ModuleManager'); |
| 21 |
goog.provide('goog.module.ModuleManager.CallbackType'); |
| 22 |
goog.provide('goog.module.ModuleManager.FailureType'); |
| 23 |
|
| 24 |
goog.require('goog.array'); |
| 25 |
goog.require('goog.asserts'); |
| 26 |
goog.require('goog.async.Deferred'); |
| 27 |
goog.require('goog.debug.Trace'); |
| 28 |
goog.require('goog.disposeAll'); |
| 29 |
goog.require('goog.loader.AbstractModuleManager'); |
| 30 |
goog.require('goog.log'); |
| 31 |
/** @suppress {extraRequire} */ |
| 32 |
goog.require('goog.module'); |
| 33 |
goog.require('goog.module.ModuleInfo'); |
| 34 |
goog.require('goog.module.ModuleLoadCallback'); |
| 35 |
goog.require('goog.object'); |
| 36 |
|
| 37 |
|
| 38 |
/** |
| 39 |
* The ModuleManager keeps track of all modules in the environment. |
| 40 |
* Since modules may not have their code loaded, we must keep track of them. |
| 41 |
* @constructor |
| 42 |
* @extends {goog.loader.AbstractModuleManager} |
| 43 |
* @struct |
| 44 |
*/ |
| 45 |
goog.module.ModuleManager = function() { |
| 46 |
goog.module.ModuleManager.base(this, 'constructor'); |
| 47 |
|
| 48 |
/** |
| 49 |
* A mapping from module id to ModuleInfo object. |
| 50 |
* @private {!Object<string, !goog.module.ModuleInfo>} |
| 51 |
*/ |
| 52 |
this.moduleInfoMap_ = {}; |
| 53 |
|
| 54 |
// TODO (malteubl): Switch this to a reentrant design. |
| 55 |
/** |
| 56 |
* The ids of the currently loading modules. If batch mode is disabled, then |
| 57 |
* this array will never contain more than one element at a time. |
| 58 |
* @type {!Array<string>} |
| 59 |
* @private |
| 60 |
*/ |
| 61 |
this.loadingModuleIds_ = []; |
| 62 |
|
| 63 |
/** |
| 64 |
* The requested ids of the currently loading modules. This does not include |
| 65 |
* module dependencies that may also be loading. |
| 66 |
* @type {!Array<string>} |
| 67 |
* @private |
| 68 |
*/ |
| 69 |
this.requestedLoadingModuleIds_ = []; |
| 70 |
|
| 71 |
// TODO(user): Make these and other arrays that are used as sets be |
| 72 |
// actual sets. |
| 73 |
/** |
| 74 |
* All module ids that have ever been requested. In concurrent loading these |
| 75 |
* are the ones to subtract from future requests. |
| 76 |
* @type {!Array<string>} |
| 77 |
* @private |
| 78 |
*/ |
| 79 |
this.requestedModuleIds_ = []; |
| 80 |
|
| 81 |
/** |
| 82 |
* A queue of the ids of requested but not-yet-loaded modules. The zero |
| 83 |
* position is the front of the queue. This is a 2-D array to group modules |
| 84 |
* together with other modules that should be batch loaded with them, if |
| 85 |
* batch loading is enabled. |
| 86 |
* @type {!Array<!Array<string>>} |
| 87 |
* @private |
| 88 |
*/ |
| 89 |
this.requestedModuleIdsQueue_ = []; |
| 90 |
|
| 91 |
/** |
| 92 |
* The ids of the currently loading modules which have been initiated by user |
| 93 |
* actions. |
| 94 |
* @type {!Array<string>} |
| 95 |
* @private |
| 96 |
*/ |
| 97 |
this.userInitiatedLoadingModuleIds_ = []; |
| 98 |
|
| 99 |
/** |
| 100 |
* A map of callback types to the functions to call for the specified |
| 101 |
* callback type. |
| 102 |
* @type {!Object<!goog.loader.AbstractModuleManager.CallbackType, |
| 103 |
* !Array<!Function>>} |
| 104 |
* @private |
| 105 |
*/ |
| 106 |
this.callbackMap_ = {}; |
| 107 |
|
| 108 |
/** |
| 109 |
* Module info for the base module (the one that contains the module |
| 110 |
* manager code), which we set as the loading module so one can |
| 111 |
* register initialization callbacks in the base module. |
| 112 |
* |
| 113 |
* The base module is considered loaded when #setAllModuleInfo is called or |
| 114 |
* #setModuleContext is called, whichever comes first. |
| 115 |
* |
| 116 |
* @type {!goog.module.ModuleInfo} |
| 117 |
* @private |
| 118 |
*/ |
| 119 |
this.baseModuleInfo_ = new goog.module.ModuleInfo([], ''); |
| 120 |
|
| 121 |
/** |
| 122 |
* The module that is currently loading, or null if not loading anything. |
| 123 |
* @type {?goog.module.ModuleInfo} |
| 124 |
* @private |
| 125 |
*/ |
| 126 |
this.currentlyLoadingModule_ = this.baseModuleInfo_; |
| 127 |
|
| 128 |
/** |
| 129 |
* The id of the last requested initial module. When it loaded |
| 130 |
* the deferred in {@code this.initialModulesLoaded_} resolves. |
| 131 |
* @private {?string} |
| 132 |
*/ |
| 133 |
this.lastInitialModuleId_ = null; |
| 134 |
|
| 135 |
/** |
| 136 |
* Deferred for when all initial modules have loaded. We currently block |
| 137 |
* sending additional module requests until this deferred resolves. In a |
| 138 |
* future optimization it may be possible to use the initial modules as |
| 139 |
* seeds for the module loader "requested module ids" and start making new |
| 140 |
* requests even sooner. |
| 141 |
* @private {!goog.async.Deferred} |
| 142 |
*/ |
| 143 |
this.initialModulesLoaded_ = new goog.async.Deferred(); |
| 144 |
|
| 145 |
/** |
| 146 |
* A logger. |
| 147 |
* @private {?goog.log.Logger} |
| 148 |
*/ |
| 149 |
this.logger_ = goog.log.getLogger('goog.module.ModuleManager'); |
| 150 |
|
| 151 |
/** |
| 152 |
* Whether the batch mode (i.e. the loading of multiple modules with just one |
| 153 |
* request) has been enabled. |
| 154 |
* @private {boolean} |
| 155 |
*/ |
| 156 |
this.batchModeEnabled_ = false; |
| 157 |
|
| 158 |
/** |
| 159 |
* Whether the module requests may be sent out of order. |
| 160 |
* @private {boolean} |
| 161 |
*/ |
| 162 |
this.concurrentLoadingEnabled_ = false; |
| 163 |
|
| 164 |
// TODO(user): Remove tracer. |
| 165 |
/** |
| 166 |
* Tracer that measures how long it takes to load a module. |
| 167 |
* @private {?number} |
| 168 |
*/ |
| 169 |
this.loadTracer_ = null; |
| 170 |
|
| 171 |
/** |
| 172 |
* The number of consecutive failures that have happened upon module load |
| 173 |
* requests. |
| 174 |
* @private {number} |
| 175 |
*/ |
| 176 |
this.consecutiveFailures_ = 0; |
| 177 |
|
| 178 |
/** |
| 179 |
* Determines if the module manager was just active before the processing of |
| 180 |
* the last data. |
| 181 |
* @private {boolean} |
| 182 |
*/ |
| 183 |
this.lastActive_ = false; |
| 184 |
|
| 185 |
/** |
| 186 |
* Determines if the module manager was just user active before the processing |
| 187 |
* of the last data. The module manager is user active if any of the |
| 188 |
* user-initiated modules are loading or queued up to load. |
| 189 |
* @private {boolean} |
| 190 |
*/ |
| 191 |
this.userLastActive_ = false; |
| 192 |
}; |
| 193 |
goog.inherits(goog.module.ModuleManager, goog.loader.AbstractModuleManager); |
| 194 |
goog.addSingletonGetter(goog.module.ModuleManager); |
| 195 |
|
| 196 |
|
| 197 |
/** |
| 198 |
* The type of callbacks that can be registered with the module manager,. |
| 199 |
* @enum {string} |
| 200 |
*/ |
| 201 |
goog.module.ModuleManager.CallbackType = |
| 202 |
goog.loader.AbstractModuleManager.CallbackType; |
| 203 |
|
| 204 |
|
| 205 |
/** |
| 206 |
* The possible reasons for a module load failure callback being fired. |
| 207 |
* @enum {number} |
| 208 |
*/ |
| 209 |
goog.module.ModuleManager.FailureType = |
| 210 |
goog.loader.AbstractModuleManager.FailureType; |
| 211 |
|
| 212 |
|
| 213 |
/** |
| 214 |
* A non-HTTP status code indicating a corruption in loaded module. |
| 215 |
* This should be used by a ModuleLoader as a replacement for the HTTP code |
| 216 |
* given to the error handler function to indicated that the module was |
| 217 |
* corrupted. |
| 218 |
* This will set the forceReload flag on the loadModules method when retrying |
| 219 |
* module loading. |
| 220 |
* @type {number} |
| 221 |
*/ |
| 222 |
goog.module.ModuleManager.CORRUPT_RESPONSE_STATUS_CODE = |
| 223 |
goog.loader.AbstractModuleManager.CORRUPT_RESPONSE_STATUS_CODE; |
| 224 |
|
| 225 |
|
| 226 |
/** @override */ |
| 227 |
goog.module.ModuleManager.prototype.setBatchModeEnabled = function(enabled) { |
| 228 |
this.batchModeEnabled_ = enabled; |
| 229 |
}; |
| 230 |
|
| 231 |
|
| 232 |
/** @override */ |
| 233 |
goog.module.ModuleManager.prototype.setConcurrentLoadingEnabled = function( |
| 234 |
enabled) { |
| 235 |
this.concurrentLoadingEnabled_ = enabled; |
| 236 |
}; |
| 237 |
|
| 238 |
|
| 239 |
/** @override */ |
| 240 |
goog.module.ModuleManager.prototype.setAllModuleInfo = function(infoMap) { |
| 241 |
for (var id in infoMap) { |
| 242 |
this.moduleInfoMap_[id] = new goog.module.ModuleInfo(infoMap[id], id); |
| 243 |
} |
| 244 |
if (!this.initialModulesLoaded_.hasFired()) { |
| 245 |
this.initialModulesLoaded_.callback(); |
| 246 |
} |
| 247 |
this.maybeFinishBaseLoad_(); |
| 248 |
}; |
| 249 |
|
| 250 |
|
| 251 |
/** @override */ |
| 252 |
goog.module.ModuleManager.prototype.setAllModuleInfoString = function( |
| 253 |
opt_info, opt_loadingModuleIds) { |
| 254 |
// Check for legacy direct-from-prototype usage. |
| 255 |
if (!(this instanceof goog.module.ModuleManager)) { |
| 256 |
this.setAllModuleInfoString(opt_info, opt_loadingModuleIds); |
| 257 |
return; |
| 258 |
} |
| 259 |
if (!goog.isString(opt_info)) { |
| 260 |
// The call to this method is generated in two steps, the argument is added |
| 261 |
// after some of the compilation passes. This means that the initial code |
| 262 |
// doesn't have any arguments and causes compiler errors. We make it |
| 263 |
// optional to satisfy this constraint. |
| 264 |
return; |
| 265 |
} |
| 266 |
|
| 267 |
var modules = opt_info.split('/'); |
| 268 |
var moduleIds = []; |
| 269 |
|
| 270 |
// Split the string into the infoMap of id->deps |
| 271 |
for (var i = 0; i < modules.length; i++) { |
| 272 |
var parts = modules[i].split(':'); |
| 273 |
var id = parts[0]; |
| 274 |
var deps; |
| 275 |
if (parts[1]) { |
| 276 |
deps = parts[1].split(','); |
| 277 |
for (var j = 0; j < deps.length; j++) { |
| 278 |
var index = parseInt(deps[j], 36); |
| 279 |
goog.asserts.assert( |
| 280 |
moduleIds[index], 'No module @ %s, dep of %s @ %s', index, id, i); |
| 281 |
deps[j] = moduleIds[index]; |
| 282 |
} |
| 283 |
} else { |
| 284 |
deps = []; |
| 285 |
} |
| 286 |
moduleIds.push(id); |
| 287 |
this.moduleInfoMap_[id] = new goog.module.ModuleInfo(deps, id); |
| 288 |
} |
| 289 |
if (opt_loadingModuleIds && opt_loadingModuleIds.length) { |
| 290 |
goog.array.extend(this.loadingModuleIds_, opt_loadingModuleIds); |
| 291 |
// The last module in the list of initial modules. When it has loaded all |
| 292 |
// initial modules have loaded. |
| 293 |
this.lastInitialModuleId_ = |
| 294 |
/** @type {?string} */ (goog.array.peek(opt_loadingModuleIds)); |
| 295 |
} else { |
| 296 |
if (!this.initialModulesLoaded_.hasFired()) { |
| 297 |
this.initialModulesLoaded_.callback(); |
| 298 |
} |
| 299 |
} |
| 300 |
this.maybeFinishBaseLoad_(); |
| 301 |
}; |
| 302 |
|
| 303 |
|
| 304 |
/** @override */ |
| 305 |
goog.module.ModuleManager.prototype.getModuleInfo = function(id) { |
| 306 |
return this.moduleInfoMap_[id]; |
| 307 |
}; |
| 308 |
|
| 309 |
|
| 310 |
/** @override */ |
| 311 |
goog.module.ModuleManager.prototype.setModuleTrustedUris = function( |
| 312 |
moduleUriMap) { |
| 313 |
for (var id in moduleUriMap) { |
| 314 |
this.moduleInfoMap_[id].setTrustedUris(moduleUriMap[id]); |
| 315 |
} |
| 316 |
}; |
| 317 |
|
| 318 |
|
| 319 |
/** @override */ |
| 320 |
goog.module.ModuleManager.prototype.setModuleContext = function(context) { |
| 321 |
goog.module.ModuleManager.base(this, 'setModuleContext', context); |
| 322 |
this.maybeFinishBaseLoad_(); |
| 323 |
}; |
| 324 |
|
| 325 |
|
| 326 |
/** @override */ |
| 327 |
goog.module.ModuleManager.prototype.isActive = function() { |
| 328 |
return this.loadingModuleIds_.length > 0; |
| 329 |
}; |
| 330 |
|
| 331 |
|
| 332 |
/** @override */ |
| 333 |
goog.module.ModuleManager.prototype.isUserActive = function() { |
| 334 |
return this.userInitiatedLoadingModuleIds_.length > 0; |
| 335 |
}; |
| 336 |
|
| 337 |
|
| 338 |
/** |
| 339 |
* Dispatches an ACTIVE or IDLE event if necessary. |
| 340 |
* @private |
| 341 |
*/ |
| 342 |
goog.module.ModuleManager.prototype.dispatchActiveIdleChangeIfNeeded_ = |
| 343 |
function() { |
| 344 |
var lastActive = this.lastActive_; |
| 345 |
var active = this.isActive(); |
| 346 |
if (active != lastActive) { |
| 347 |
this.executeCallbacks_( |
| 348 |
active ? goog.loader.AbstractModuleManager.CallbackType.ACTIVE : |
| 349 |
goog.loader.AbstractModuleManager.CallbackType.IDLE); |
| 350 |
|
| 351 |
// Flip the last active value. |
| 352 |
this.lastActive_ = active; |
| 353 |
} |
| 354 |
|
| 355 |
// Check if the module manager is user active i.e., there are user initiated |
| 356 |
// modules being loaded or queued up to be loaded. |
| 357 |
var userLastActive = this.userLastActive_; |
| 358 |
var userActive = this.isUserActive(); |
| 359 |
if (userActive != userLastActive) { |
| 360 |
this.executeCallbacks_( |
| 361 |
userActive ? |
| 362 |
goog.loader.AbstractModuleManager.CallbackType.USER_ACTIVE : |
| 363 |
goog.loader.AbstractModuleManager.CallbackType.USER_IDLE); |
| 364 |
|
| 365 |
// Flip the last user active value. |
| 366 |
this.userLastActive_ = userActive; |
| 367 |
} |
| 368 |
}; |
| 369 |
|
| 370 |
|
| 371 |
/** @override */ |
| 372 |
goog.module.ModuleManager.prototype.preloadModule = function(id, opt_timeout) { |
| 373 |
var d = new goog.async.Deferred(); |
| 374 |
window.setTimeout( |
| 375 |
goog.bind(this.addLoadModule_, this, id, d), opt_timeout || 0); |
| 376 |
return d; |
| 377 |
}; |
| 378 |
|
| 379 |
|
| 380 |
/** @override */ |
| 381 |
goog.module.ModuleManager.prototype.prefetchModule = function(id) { |
| 382 |
var moduleInfo = this.getModuleInfo(id); |
| 383 |
if (moduleInfo.isLoaded() || this.isModuleLoading(id)) { |
| 384 |
throw new Error('Module load already requested: ' + id); |
| 385 |
} else if (this.batchModeEnabled_) { |
| 386 |
throw new Error('Modules prefetching is not supported in batch mode'); |
| 387 |
} else { |
| 388 |
var idWithDeps = this.getNotYetLoadedTransitiveDepIds_(id); |
| 389 |
for (var i = 0; i < idWithDeps.length; i++) { |
| 390 |
this.getLoader().prefetchModule( |
| 391 |
idWithDeps[i], this.moduleInfoMap_[idWithDeps[i]]); |
| 392 |
} |
| 393 |
} |
| 394 |
}; |
| 395 |
|
| 396 |
|
| 397 |
/** |
| 398 |
* Loads a single module for use with a given deferred. |
| 399 |
* |
| 400 |
* @param {string} id The id of the module to load. |
| 401 |
* @param {!goog.async.Deferred} d A deferred object. |
| 402 |
* @private |
| 403 |
*/ |
| 404 |
goog.module.ModuleManager.prototype.addLoadModule_ = function(id, d) { |
| 405 |
var moduleInfo = this.getModuleInfo(id); |
| 406 |
if (moduleInfo.isLoaded()) { |
| 407 |
d.callback(this.getModuleContext()); |
| 408 |
return; |
| 409 |
} |
| 410 |
|
| 411 |
this.registerModuleLoadCallbacks_(id, moduleInfo, false, d); |
| 412 |
if (!this.isModuleLoading(id)) { |
| 413 |
this.loadModulesOrEnqueue_([id]); |
| 414 |
} |
| 415 |
}; |
| 416 |
|
| 417 |
|
| 418 |
/** |
| 419 |
* Loads a list of modules or, if some other module is currently being loaded, |
| 420 |
* appends the ids to the queue of requested module ids. Registers callbacks a |
| 421 |
* module that is currently loading and returns a fired deferred for a module |
| 422 |
* that is already loaded. |
| 423 |
* |
| 424 |
* @param {!Array<string>} ids The id of the module to load. |
| 425 |
* @param {boolean=} opt_userInitiated If the load is a result of a user action. |
| 426 |
* @return {!Object<string, !goog.async.Deferred>} A mapping from id (String) |
| 427 |
* to deferred objects that will callback or errback when the load for that |
| 428 |
* id is finished. |
| 429 |
* @private |
| 430 |
*/ |
| 431 |
goog.module.ModuleManager.prototype.loadModulesOrEnqueueIfNotLoadedOrLoading_ = |
| 432 |
function(ids, opt_userInitiated) { |
| 433 |
var uniqueIds = []; |
| 434 |
goog.array.removeDuplicates(ids, uniqueIds); |
| 435 |
var idsToLoad = []; |
| 436 |
var deferredMap = {}; |
| 437 |
for (var i = 0; i < uniqueIds.length; i++) { |
| 438 |
var id = uniqueIds[i]; |
| 439 |
var moduleInfo = this.getModuleInfo(id); |
| 440 |
if (!moduleInfo) { |
| 441 |
throw new Error('Unknown module: ' + id); |
| 442 |
} |
| 443 |
var d = new goog.async.Deferred(); |
| 444 |
deferredMap[id] = d; |
| 445 |
if (moduleInfo.isLoaded()) { |
| 446 |
d.callback(this.getModuleContext()); |
| 447 |
} else { |
| 448 |
this.registerModuleLoadCallbacks_(id, moduleInfo, !!opt_userInitiated, d); |
| 449 |
if (!this.isModuleLoading(id)) { |
| 450 |
idsToLoad.push(id); |
| 451 |
} |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
// If there are ids to load, load them, otherwise, they are all loading or |
| 456 |
// loaded. |
| 457 |
if (idsToLoad.length > 0) { |
| 458 |
this.loadModulesOrEnqueue_(idsToLoad); |
| 459 |
} |
| 460 |
return deferredMap; |
| 461 |
}; |
| 462 |
|
| 463 |
|
| 464 |
/** |
| 465 |
* Registers the callbacks and handles logic if it is a user initiated module |
| 466 |
* load. |
| 467 |
* |
| 468 |
* @param {string} id The id of the module to possibly load. |
| 469 |
* @param {!goog.module.ModuleInfo} moduleInfo The module identifier for the |
| 470 |
* given id. |
| 471 |
* @param {boolean} userInitiated If the load was user initiated. |
| 472 |
* @param {!goog.async.Deferred} d A deferred object. |
| 473 |
* @private |
| 474 |
*/ |
| 475 |
goog.module.ModuleManager.prototype.registerModuleLoadCallbacks_ = function( |
| 476 |
id, moduleInfo, userInitiated, d) { |
| 477 |
moduleInfo.registerCallback(d.callback, d); |
| 478 |
moduleInfo.registerErrback(function(err) { |
| 479 |
d.errback(Error(err)); |
| 480 |
}); |
| 481 |
// If it's already loading, we don't have to do anything besides handle |
| 482 |
// if it was user initiated |
| 483 |
if (this.isModuleLoading(id)) { |
| 484 |
if (userInitiated) { |
| 485 |
goog.log.info( |
| 486 |
this.logger_, 'User initiated module already loading: ' + id); |
| 487 |
this.addUserInitiatedLoadingModule_(id); |
| 488 |
this.dispatchActiveIdleChangeIfNeeded_(); |
| 489 |
} |
| 490 |
} else { |
| 491 |
if (userInitiated) { |
| 492 |
goog.log.info(this.logger_, 'User initiated module load: ' + id); |
| 493 |
this.addUserInitiatedLoadingModule_(id); |
| 494 |
} else { |
| 495 |
goog.log.info(this.logger_, 'Initiating module load: ' + id); |
| 496 |
} |
| 497 |
} |
| 498 |
}; |
| 499 |
|
| 500 |
|
| 501 |
/** |
| 502 |
* Initiates loading of a list of modules or, if a module is currently being |
| 503 |
* loaded, appends the modules to the queue of requested module ids. |
| 504 |
* |
| 505 |
* The caller should verify that the requested modules are not already loaded or |
| 506 |
* loading. {@link #loadModulesOrEnqueueIfNotLoadedOrLoading_} is a more lenient |
| 507 |
* alternative to this method. |
| 508 |
* |
| 509 |
* @param {!Array<string>} ids The ids of the modules to load. |
| 510 |
* @private |
| 511 |
*/ |
| 512 |
goog.module.ModuleManager.prototype.loadModulesOrEnqueue_ = function(ids) { |
| 513 |
// With concurrent loading we always just send off the request. |
| 514 |
if (this.concurrentLoadingEnabled_) { |
| 515 |
// For now we wait for initial modules to have downloaded as this puts the |
| 516 |
// loader in a good state for calculating the needed deps of additional |
| 517 |
// loads. |
| 518 |
// TODO(user): Make this wait unnecessary. |
| 519 |
this.initialModulesLoaded_.addCallback( |
| 520 |
goog.bind(this.loadModules_, this, ids)); |
| 521 |
} else { |
| 522 |
if (goog.array.isEmpty(this.loadingModuleIds_)) { |
| 523 |
this.loadModules_(ids); |
| 524 |
} else { |
| 525 |
this.requestedModuleIdsQueue_.push(ids); |
| 526 |
this.dispatchActiveIdleChangeIfNeeded_(); |
| 527 |
} |
| 528 |
} |
| 529 |
}; |
| 530 |
|
| 531 |
|
| 532 |
/** |
| 533 |
* Gets the amount of delay to wait before sending a request for more modules. |
| 534 |
* If a certain module request fails, we backoff a little bit and try again. |
| 535 |
* @return {number} Delay, in ms. |
| 536 |
* @private |
| 537 |
*/ |
| 538 |
goog.module.ModuleManager.prototype.getBackOff_ = function() { |
| 539 |
// 5 seconds after one error, 20 seconds after 2. |
| 540 |
return Math.pow(this.consecutiveFailures_, 2) * 5000; |
| 541 |
}; |
| 542 |
|
| 543 |
|
| 544 |
/** |
| 545 |
* Loads a list of modules and any of their not-yet-loaded prerequisites. |
| 546 |
* If batch mode is enabled, the prerequisites will be loaded together with the |
| 547 |
* requested modules and all requested modules will be loaded at the same time. |
| 548 |
* |
| 549 |
* The caller should verify that the requested modules are not already loaded |
| 550 |
* and that no modules are currently loading before calling this method. |
| 551 |
* |
| 552 |
* @param {!Array<string>} ids The ids of the modules to load. |
| 553 |
* @param {boolean=} opt_isRetry If the load is a retry of a previous load |
| 554 |
* attempt. |
| 555 |
* @param {boolean=} opt_forceReload Whether to bypass cache while loading the |
| 556 |
* module. |
| 557 |
* @private |
| 558 |
*/ |
| 559 |
goog.module.ModuleManager.prototype.loadModules_ = function( |
| 560 |
ids, opt_isRetry, opt_forceReload) { |
| 561 |
if (!opt_isRetry) { |
| 562 |
this.consecutiveFailures_ = 0; |
| 563 |
} |
| 564 |
|
| 565 |
// Not all modules may be loaded immediately if batch mode is not enabled. |
| 566 |
var idsToLoadImmediately = this.processModulesForLoad_(ids); |
| 567 |
|
| 568 |
goog.log.info(this.logger_, 'Loading module(s): ' + idsToLoadImmediately); |
| 569 |
this.loadingModuleIds_ = idsToLoadImmediately; |
| 570 |
|
| 571 |
if (this.batchModeEnabled_) { |
| 572 |
this.requestedLoadingModuleIds_ = ids; |
| 573 |
} else { |
| 574 |
// If batch mode is disabled, we treat each dependency load as a separate |
| 575 |
// load. |
| 576 |
this.requestedLoadingModuleIds_ = goog.array.clone(idsToLoadImmediately); |
| 577 |
} |
| 578 |
|
| 579 |
// Dispatch an active/idle change if needed. |
| 580 |
this.dispatchActiveIdleChangeIfNeeded_(); |
| 581 |
|
| 582 |
if (goog.array.isEmpty(idsToLoadImmediately)) { |
| 583 |
// All requested modules and deps have been either loaded already or have |
| 584 |
// already been requested. |
| 585 |
return; |
| 586 |
} |
| 587 |
|
| 588 |
this.requestedModuleIds_.push.apply( |
| 589 |
this.requestedModuleIds_, idsToLoadImmediately); |
| 590 |
|
| 591 |
var loadFn = goog.bind( |
| 592 |
this.getLoader().loadModules, goog.asserts.assert(this.getLoader()), |
| 593 |
goog.array.clone(idsToLoadImmediately), |
| 594 |
goog.asserts.assert(this.moduleInfoMap_), null, |
| 595 |
goog.bind( |
| 596 |
this.handleLoadError_, this, this.requestedLoadingModuleIds_, |
| 597 |
idsToLoadImmediately), |
| 598 |
goog.bind(this.handleLoadTimeout_, this), !!opt_forceReload); |
| 599 |
|
| 600 |
var delay = this.getBackOff_(); |
| 601 |
if (delay) { |
| 602 |
window.setTimeout(loadFn, delay); |
| 603 |
} else { |
| 604 |
loadFn(); |
| 605 |
} |
| 606 |
}; |
| 607 |
|
| 608 |
|
| 609 |
/** |
| 610 |
* Processes a list of module ids for loading. Checks if any of the modules are |
| 611 |
* already loaded and then gets transitive deps. Queues any necessary modules |
| 612 |
* if batch mode is not enabled. Returns the list of ids that should be loaded. |
| 613 |
* |
| 614 |
* @param {!Array<string>} ids The ids that need to be loaded. |
| 615 |
* @return {!Array<string>} The ids to load, including dependencies. |
| 616 |
* @throws {!Error} If the module is already loaded. |
| 617 |
* @private |
| 618 |
*/ |
| 619 |
goog.module.ModuleManager.prototype.processModulesForLoad_ = function(ids) { |
| 620 |
for (var i = 0; i < ids.length; i++) { |
| 621 |
var moduleInfo = this.moduleInfoMap_[ids[i]]; |
| 622 |
if (moduleInfo.isLoaded()) { |
| 623 |
throw new Error('Module already loaded: ' + ids[i]); |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
// Build a list of the ids of this module and any of its not-yet-loaded |
| 628 |
// prerequisite modules in dependency order. |
| 629 |
var idsWithDeps = []; |
| 630 |
for (var i = 0; i < ids.length; i++) { |
| 631 |
idsWithDeps = |
| 632 |
idsWithDeps.concat(this.getNotYetLoadedTransitiveDepIds_(ids[i])); |
| 633 |
} |
| 634 |
goog.array.removeDuplicates(idsWithDeps); |
| 635 |
|
| 636 |
if (!this.batchModeEnabled_ && idsWithDeps.length > 1) { |
| 637 |
var idToLoad = idsWithDeps.shift(); |
| 638 |
goog.log.info( |
| 639 |
this.logger_, 'Must load ' + idToLoad + ' module before ' + ids); |
| 640 |
|
| 641 |
// Insert the requested module id and any other not-yet-loaded prereqs |
| 642 |
// that it has at the front of the queue. |
| 643 |
var queuedModules = goog.array.map(idsWithDeps, function(id) { |
| 644 |
return [id]; |
| 645 |
}); |
| 646 |
this.requestedModuleIdsQueue_ = |
| 647 |
queuedModules.concat(this.requestedModuleIdsQueue_); |
| 648 |
return [idToLoad]; |
| 649 |
} else { |
| 650 |
return idsWithDeps; |
| 651 |
} |
| 652 |
}; |
| 653 |
|
| 654 |
|
| 655 |
/** |
| 656 |
* Builds a list of the ids of the not-yet-loaded modules that a particular |
| 657 |
* module transitively depends on, including itself. |
| 658 |
* |
| 659 |
* @param {string} id The id of a not-yet-loaded module. |
| 660 |
* @return {!Array<string>} An array of module ids in dependency order that's |
| 661 |
* guaranteed to end with the provided module id. |
| 662 |
* @private |
| 663 |
*/ |
| 664 |
goog.module.ModuleManager.prototype.getNotYetLoadedTransitiveDepIds_ = function( |
| 665 |
id) { |
| 666 |
// NOTE(user): We want the earliest occurrence of a module, not the first |
| 667 |
// dependency we find. Therefore we strip duplicates at the end rather than |
| 668 |
// during. See the tests for concrete examples. |
| 669 |
var ids = []; |
| 670 |
if (!goog.array.contains(this.requestedModuleIds_, id)) { |
| 671 |
ids.push(id); |
| 672 |
} |
| 673 |
var depIds = goog.array.clone(this.getModuleInfo(id).getDependencies()); |
| 674 |
while (depIds.length) { |
| 675 |
var depId = depIds.pop(); |
| 676 |
if (!this.getModuleInfo(depId).isLoaded() && |
| 677 |
!goog.array.contains(this.requestedModuleIds_, depId)) { |
| 678 |
ids.unshift(depId); |
| 679 |
// We need to process direct dependencies first. |
| 680 |
Array.prototype.unshift.apply( |
| 681 |
depIds, this.getModuleInfo(depId).getDependencies()); |
| 682 |
} |
| 683 |
} |
| 684 |
goog.array.removeDuplicates(ids); |
| 685 |
return ids; |
| 686 |
}; |
| 687 |
|
| 688 |
|
| 689 |
/** |
| 690 |
* If we are still loading the base module, consider the load complete. |
| 691 |
* @private |
| 692 |
*/ |
| 693 |
goog.module.ModuleManager.prototype.maybeFinishBaseLoad_ = function() { |
| 694 |
if (this.currentlyLoadingModule_ == this.baseModuleInfo_) { |
| 695 |
this.currentlyLoadingModule_ = null; |
| 696 |
var error = |
| 697 |
this.baseModuleInfo_.onLoad(goog.bind(this.getModuleContext, this)); |
| 698 |
if (error) { |
| 699 |
this.dispatchModuleLoadFailed_( |
| 700 |
goog.loader.AbstractModuleManager.FailureType.INIT_ERROR); |
| 701 |
} |
| 702 |
|
| 703 |
this.dispatchActiveIdleChangeIfNeeded_(); |
| 704 |
} |
| 705 |
}; |
| 706 |
|
| 707 |
|
| 708 |
/** @override */ |
| 709 |
goog.module.ModuleManager.prototype.setLoaded = function(id) { |
| 710 |
if (this.isDisposed()) { |
| 711 |
goog.log.warning( |
| 712 |
this.logger_, 'Module loaded after module manager was disposed: ' + id); |
| 713 |
return; |
| 714 |
} |
| 715 |
|
| 716 |
goog.log.info(this.logger_, 'Module loaded: ' + id); |
| 717 |
|
| 718 |
var error = |
| 719 |
this.moduleInfoMap_[id].onLoad(goog.bind(this.getModuleContext, this)); |
| 720 |
if (error) { |
| 721 |
this.dispatchModuleLoadFailed_( |
| 722 |
goog.loader.AbstractModuleManager.FailureType.INIT_ERROR); |
| 723 |
} |
| 724 |
|
| 725 |
// Remove the module id from the user initiated set if it existed there. |
| 726 |
goog.array.remove(this.userInitiatedLoadingModuleIds_, id); |
| 727 |
|
| 728 |
// Remove the module id from the loading modules if it exists there. |
| 729 |
goog.array.remove(this.loadingModuleIds_, id); |
| 730 |
|
| 731 |
if (goog.array.isEmpty(this.loadingModuleIds_)) { |
| 732 |
// No more modules are currently being loaded (e.g. arriving later in the |
| 733 |
// same HTTP response), so proceed to load the next module in the queue. |
| 734 |
this.loadNextModules_(); |
| 735 |
} |
| 736 |
|
| 737 |
if (this.lastInitialModuleId_ && id == this.lastInitialModuleId_) { |
| 738 |
if (!this.initialModulesLoaded_.hasFired()) { |
| 739 |
this.initialModulesLoaded_.callback(); |
| 740 |
} |
| 741 |
} |
| 742 |
|
| 743 |
// Dispatch an active/idle change if needed. |
| 744 |
this.dispatchActiveIdleChangeIfNeeded_(); |
| 745 |
}; |
| 746 |
|
| 747 |
|
| 748 |
/** @override */ |
| 749 |
goog.module.ModuleManager.prototype.isModuleLoading = function(id) { |
| 750 |
if (goog.array.contains(this.loadingModuleIds_, id)) { |
| 751 |
return true; |
| 752 |
} |
| 753 |
for (var i = 0; i < this.requestedModuleIdsQueue_.length; i++) { |
| 754 |
if (goog.array.contains(this.requestedModuleIdsQueue_[i], id)) { |
| 755 |
return true; |
| 756 |
} |
| 757 |
} |
| 758 |
return false; |
| 759 |
}; |
| 760 |
|
| 761 |
|
| 762 |
/** @override */ |
| 763 |
goog.module.ModuleManager.prototype.execOnLoad = function( |
| 764 |
moduleId, fn, opt_handler, opt_noLoad, opt_userInitiated, |
| 765 |
opt_preferSynchronous) { |
| 766 |
var moduleInfo = this.moduleInfoMap_[moduleId]; |
| 767 |
var callbackWrapper; |
| 768 |
|
| 769 |
if (moduleInfo.isLoaded()) { |
| 770 |
goog.log.info(this.logger_, moduleId + ' module already loaded'); |
| 771 |
// Call async so that code paths don't change between loaded and unloaded |
| 772 |
// cases. |
| 773 |
callbackWrapper = new goog.module.ModuleLoadCallback(fn, opt_handler); |
| 774 |
if (opt_preferSynchronous) { |
| 775 |
callbackWrapper.execute(this.getModuleContext()); |
| 776 |
} else { |
| 777 |
window.setTimeout(goog.bind(callbackWrapper.execute, callbackWrapper), 0); |
| 778 |
} |
| 779 |
} else if (this.isModuleLoading(moduleId)) { |
| 780 |
goog.log.info(this.logger_, moduleId + ' module already loading'); |
| 781 |
callbackWrapper = moduleInfo.registerCallback(fn, opt_handler); |
| 782 |
if (opt_userInitiated) { |
| 783 |
goog.log.info( |
| 784 |
this.logger_, 'User initiated module already loading: ' + moduleId); |
| 785 |
this.addUserInitiatedLoadingModule_(moduleId); |
| 786 |
this.dispatchActiveIdleChangeIfNeeded_(); |
| 787 |
} |
| 788 |
} else { |
| 789 |
goog.log.info(this.logger_, 'Registering callback for module: ' + moduleId); |
| 790 |
callbackWrapper = moduleInfo.registerCallback(fn, opt_handler); |
| 791 |
if (!opt_noLoad) { |
| 792 |
if (opt_userInitiated) { |
| 793 |
goog.log.info(this.logger_, 'User initiated module load: ' + moduleId); |
| 794 |
this.addUserInitiatedLoadingModule_(moduleId); |
| 795 |
} |
| 796 |
goog.log.info(this.logger_, 'Initiating module load: ' + moduleId); |
| 797 |
this.loadModulesOrEnqueue_([moduleId]); |
| 798 |
} |
| 799 |
} |
| 800 |
return callbackWrapper; |
| 801 |
}; |
| 802 |
|
| 803 |
|
| 804 |
/** @override */ |
| 805 |
goog.module.ModuleManager.prototype.load = function( |
| 806 |
moduleId, opt_userInitiated) { |
| 807 |
return this.loadModulesOrEnqueueIfNotLoadedOrLoading_( |
| 808 |
[moduleId], opt_userInitiated)[moduleId]; |
| 809 |
}; |
| 810 |
|
| 811 |
|
| 812 |
/** @override */ |
| 813 |
goog.module.ModuleManager.prototype.loadMultiple = function( |
| 814 |
moduleIds, opt_userInitiated) { |
| 815 |
return this.loadModulesOrEnqueueIfNotLoadedOrLoading_( |
| 816 |
moduleIds, opt_userInitiated); |
| 817 |
}; |
| 818 |
|
| 819 |
|
| 820 |
/** |
| 821 |
* Ensures that the module with the given id is listed as a user-initiated |
| 822 |
* module that is being loaded. This method guarantees that a module will never |
| 823 |
* get listed more than once. |
| 824 |
* @param {string} id Identifier of the module. |
| 825 |
* @private |
| 826 |
*/ |
| 827 |
goog.module.ModuleManager.prototype.addUserInitiatedLoadingModule_ = function( |
| 828 |
id) { |
| 829 |
if (!goog.array.contains(this.userInitiatedLoadingModuleIds_, id)) { |
| 830 |
this.userInitiatedLoadingModuleIds_.push(id); |
| 831 |
} |
| 832 |
}; |
| 833 |
|
| 834 |
|
| 835 |
/** @override */ |
| 836 |
goog.module.ModuleManager.prototype.beforeLoadModuleCode = function(id) { |
| 837 |
this.loadTracer_ = |
| 838 |
goog.debug.Trace.startTracer('Module Load: ' + id, 'Module Load'); |
| 839 |
if (this.currentlyLoadingModule_) { |
| 840 |
goog.log.error( |
| 841 |
this.logger_, |
| 842 |
'beforeLoadModuleCode called with module "' + id + '" while module "' + |
| 843 |
this.currentlyLoadingModule_.getId() + '" is loading'); |
| 844 |
} |
| 845 |
this.currentlyLoadingModule_ = this.getModuleInfo(id); |
| 846 |
}; |
| 847 |
|
| 848 |
|
| 849 |
/** @override */ |
| 850 |
goog.module.ModuleManager.prototype.afterLoadModuleCode = function(id) { |
| 851 |
if (!this.currentlyLoadingModule_ || |
| 852 |
id != this.currentlyLoadingModule_.getId()) { |
| 853 |
goog.log.error( |
| 854 |
this.logger_, |
| 855 |
'afterLoadModuleCode called with module "' + id + |
| 856 |
'" while loading module "' + |
| 857 |
(this.currentlyLoadingModule_ && |
| 858 |
this.currentlyLoadingModule_.getId()) + |
| 859 |
'"'); |
| 860 |
} |
| 861 |
this.currentlyLoadingModule_ = null; |
| 862 |
goog.debug.Trace.stopTracer(this.loadTracer_); |
| 863 |
}; |
| 864 |
|
| 865 |
|
| 866 |
/** @override */ |
| 867 |
goog.module.ModuleManager.prototype.registerInitializationCallback = function( |
| 868 |
fn, opt_handler) { |
| 869 |
if (!this.currentlyLoadingModule_) { |
| 870 |
goog.log.error(this.logger_, 'No module is currently loading'); |
| 871 |
} else { |
| 872 |
this.currentlyLoadingModule_.registerEarlyCallback(fn, opt_handler); |
| 873 |
} |
| 874 |
}; |
| 875 |
|
| 876 |
|
| 877 |
/** @override */ |
| 878 |
goog.module.ModuleManager.prototype.registerLateInitializationCallback = |
| 879 |
function(fn, opt_handler) { |
| 880 |
if (!this.currentlyLoadingModule_) { |
| 881 |
goog.log.error(this.logger_, 'No module is currently loading'); |
| 882 |
} else { |
| 883 |
this.currentlyLoadingModule_.registerCallback(fn, opt_handler); |
| 884 |
} |
| 885 |
}; |
| 886 |
|
| 887 |
|
| 888 |
/** @override */ |
| 889 |
goog.module.ModuleManager.prototype.setModuleConstructor = function(fn) { |
| 890 |
if (!this.currentlyLoadingModule_) { |
| 891 |
goog.log.error(this.logger_, 'No module is currently loading'); |
| 892 |
return; |
| 893 |
} |
| 894 |
this.currentlyLoadingModule_.setModuleConstructor(fn); |
| 895 |
}; |
| 896 |
|
| 897 |
|
| 898 |
/** |
| 899 |
* Handles a module load failure. |
| 900 |
* |
| 901 |
* @param {!Array<string>} requestedLoadingModuleIds Modules ids that were |
| 902 |
* requested in failed request. Does not included calculated dependencies. |
| 903 |
* @param {!Array<string>} requestedModuleIdsWithDeps All module ids requested |
| 904 |
* in the failed request including all dependencies. |
| 905 |
* @param {?number} status The error status. |
| 906 |
* @private |
| 907 |
*/ |
| 908 |
goog.module.ModuleManager.prototype.handleLoadError_ = function( |
| 909 |
requestedLoadingModuleIds, requestedModuleIdsWithDeps, status) { |
| 910 |
this.consecutiveFailures_++; |
| 911 |
// Module manager was not designed to be reentrant. Reinstate the instance |
| 912 |
// var with actual value when request failed (Other requests may have |
| 913 |
// started already.) |
| 914 |
this.requestedLoadingModuleIds_ = requestedLoadingModuleIds; |
| 915 |
// Pretend we never requested the failed modules. |
| 916 |
goog.array.forEach( |
| 917 |
requestedModuleIdsWithDeps, |
| 918 |
goog.partial(goog.array.remove, this.requestedModuleIds_), this); |
| 919 |
|
| 920 |
if (status == 401) { |
| 921 |
// The user is not logged in. They've cleared their cookies or logged out |
| 922 |
// from another window. |
| 923 |
goog.log.info(this.logger_, 'Module loading unauthorized'); |
| 924 |
this.dispatchModuleLoadFailed_( |
| 925 |
goog.loader.AbstractModuleManager.FailureType.UNAUTHORIZED); |
| 926 |
// Drop any additional module requests. |
| 927 |
this.requestedModuleIdsQueue_.length = 0; |
| 928 |
} else if (status == 410) { |
| 929 |
// The requested module js is old and not available. |
| 930 |
this.requeueBatchOrDispatchFailure_( |
| 931 |
goog.loader.AbstractModuleManager.FailureType.OLD_CODE_GONE); |
| 932 |
this.loadNextModules_(); |
| 933 |
} else if (this.consecutiveFailures_ >= 3) { |
| 934 |
goog.log.info( |
| 935 |
this.logger_, |
| 936 |
'Aborting after failure to load: ' + this.loadingModuleIds_); |
| 937 |
this.requeueBatchOrDispatchFailure_( |
| 938 |
goog.loader.AbstractModuleManager.FailureType.CONSECUTIVE_FAILURES); |
| 939 |
this.loadNextModules_(); |
| 940 |
} else { |
| 941 |
goog.log.info( |
| 942 |
this.logger_, |
| 943 |
'Retrying after failure to load: ' + this.loadingModuleIds_); |
| 944 |
var forceReload = status == |
| 945 |
goog.loader.AbstractModuleManager.CORRUPT_RESPONSE_STATUS_CODE; |
| 946 |
this.loadModules_(this.requestedLoadingModuleIds_, true, forceReload); |
| 947 |
} |
| 948 |
}; |
| 949 |
|
| 950 |
|
| 951 |
/** |
| 952 |
* Handles a module load timeout. |
| 953 |
* @private |
| 954 |
*/ |
| 955 |
goog.module.ModuleManager.prototype.handleLoadTimeout_ = function() { |
| 956 |
goog.log.info( |
| 957 |
this.logger_, 'Aborting after timeout: ' + this.loadingModuleIds_); |
| 958 |
this.requeueBatchOrDispatchFailure_( |
| 959 |
goog.loader.AbstractModuleManager.FailureType.TIMEOUT); |
| 960 |
this.loadNextModules_(); |
| 961 |
}; |
| 962 |
|
| 963 |
|
| 964 |
/** |
| 965 |
* Requeues batch loads that had more than one requested module |
| 966 |
* (i.e. modules that were not included as dependencies) as separate loads or |
| 967 |
* if there was only one requested module, fails that module with the received |
| 968 |
* cause. |
| 969 |
* @param {!goog.loader.AbstractModuleManager.FailureType} cause The reason for |
| 970 |
* the failure. |
| 971 |
* @private |
| 972 |
*/ |
| 973 |
goog.module.ModuleManager.prototype.requeueBatchOrDispatchFailure_ = function( |
| 974 |
cause) { |
| 975 |
// The load failed, so if there are more than one requested modules, then we |
| 976 |
// need to retry each one as a separate load. Otherwise, if there is only one |
| 977 |
// requested module, remove it and its dependencies from the queue. |
| 978 |
if (this.requestedLoadingModuleIds_.length > 1) { |
| 979 |
var queuedModules = |
| 980 |
goog.array.map(this.requestedLoadingModuleIds_, function(id) { |
| 981 |
return [id]; |
| 982 |
}); |
| 983 |
this.requestedModuleIdsQueue_ = |
| 984 |
queuedModules.concat(this.requestedModuleIdsQueue_); |
| 985 |
} else { |
| 986 |
this.dispatchModuleLoadFailed_(cause); |
| 987 |
} |
| 988 |
}; |
| 989 |
|
| 990 |
|
| 991 |
/** |
| 992 |
* Handles when a module load failed. |
| 993 |
* @param {!goog.loader.AbstractModuleManager.FailureType} cause The reason for |
| 994 |
* the failure. |
| 995 |
* @private |
| 996 |
*/ |
| 997 |
goog.module.ModuleManager.prototype.dispatchModuleLoadFailed_ = function( |
| 998 |
cause) { |
| 999 |
var failedIds = this.requestedLoadingModuleIds_; |
| 1000 |
this.loadingModuleIds_.length = 0; |
| 1001 |
// If any pending modules depend on the id that failed, |
| 1002 |
// they need to be removed from the queue. |
| 1003 |
var idsToCancel = []; |
| 1004 |
for (var i = 0; i < this.requestedModuleIdsQueue_.length; i++) { |
| 1005 |
var dependentModules = goog.array.filter( |
| 1006 |
this.requestedModuleIdsQueue_[i], |
| 1007 |
/** |
| 1008 |
* Returns true if the requestedId has dependencies on the modules that |
| 1009 |
* just failed to load. |
| 1010 |
* @param {string} requestedId The module to check for dependencies. |
| 1011 |
* @return {boolean} True if the module depends on failed modules. |
| 1012 |
*/ |
| 1013 |
function(requestedId) { |
| 1014 |
var requestedDeps = |
| 1015 |
this.getNotYetLoadedTransitiveDepIds_(requestedId); |
| 1016 |
return goog.array.some(failedIds, function(id) { |
| 1017 |
return goog.array.contains(requestedDeps, id); |
| 1018 |
}); |
| 1019 |
}, |
| 1020 |
this); |
| 1021 |
goog.array.extend(idsToCancel, dependentModules); |
| 1022 |
} |
| 1023 |
|
| 1024 |
// Also insert the ids that failed to load as ids to cancel. |
| 1025 |
for (var i = 0; i < failedIds.length; i++) { |
| 1026 |
goog.array.insert(idsToCancel, failedIds[i]); |
| 1027 |
} |
| 1028 |
|
| 1029 |
// Remove ids to cancel from the queues. |
| 1030 |
for (var i = 0; i < idsToCancel.length; i++) { |
| 1031 |
for (var j = 0; j < this.requestedModuleIdsQueue_.length; j++) { |
| 1032 |
goog.array.remove(this.requestedModuleIdsQueue_[j], idsToCancel[i]); |
| 1033 |
} |
| 1034 |
goog.array.remove(this.userInitiatedLoadingModuleIds_, idsToCancel[i]); |
| 1035 |
} |
| 1036 |
|
| 1037 |
// Call the functions for error notification. |
| 1038 |
var errorCallbacks = |
| 1039 |
this.callbackMap_[goog.loader.AbstractModuleManager.CallbackType.ERROR]; |
| 1040 |
if (errorCallbacks) { |
| 1041 |
for (var i = 0; i < errorCallbacks.length; i++) { |
| 1042 |
var callback = errorCallbacks[i]; |
| 1043 |
for (var j = 0; j < idsToCancel.length; j++) { |
| 1044 |
callback( |
| 1045 |
goog.loader.AbstractModuleManager.CallbackType.ERROR, |
| 1046 |
idsToCancel[j], cause); |
| 1047 |
} |
| 1048 |
} |
| 1049 |
} |
| 1050 |
|
| 1051 |
// Call the errbacks on the module info. |
| 1052 |
for (var i = 0; i < failedIds.length; i++) { |
| 1053 |
if (this.moduleInfoMap_[failedIds[i]]) { |
| 1054 |
this.moduleInfoMap_[failedIds[i]].onError(cause); |
| 1055 |
} |
| 1056 |
} |
| 1057 |
|
| 1058 |
// Clear the requested loading module ids. |
| 1059 |
this.requestedLoadingModuleIds_.length = 0; |
| 1060 |
|
| 1061 |
this.dispatchActiveIdleChangeIfNeeded_(); |
| 1062 |
}; |
| 1063 |
|
| 1064 |
|
| 1065 |
/** |
| 1066 |
* Loads the next modules on the queue. |
| 1067 |
* @private |
| 1068 |
*/ |
| 1069 |
goog.module.ModuleManager.prototype.loadNextModules_ = function() { |
| 1070 |
while (this.requestedModuleIdsQueue_.length) { |
| 1071 |
// Remove modules that are already loaded. |
| 1072 |
var nextIds = |
| 1073 |
goog.array.filter(this.requestedModuleIdsQueue_.shift(), function(id) { |
| 1074 |
return !this.getModuleInfo(id).isLoaded(); |
| 1075 |
}, this); |
| 1076 |
if (nextIds.length > 0) { |
| 1077 |
this.loadModules_(nextIds); |
| 1078 |
return; |
| 1079 |
} |
| 1080 |
} |
| 1081 |
|
| 1082 |
// Dispatch an active/idle change if needed. |
| 1083 |
this.dispatchActiveIdleChangeIfNeeded_(); |
| 1084 |
}; |
| 1085 |
|
| 1086 |
|
| 1087 |
/** @override */ |
| 1088 |
goog.module.ModuleManager.prototype.registerCallback = function(types, fn) { |
| 1089 |
if (!goog.isArray(types)) { |
| 1090 |
types = [types]; |
| 1091 |
} |
| 1092 |
|
| 1093 |
for (var i = 0; i < types.length; i++) { |
| 1094 |
this.registerCallback_(types[i], fn); |
| 1095 |
} |
| 1096 |
}; |
| 1097 |
|
| 1098 |
|
| 1099 |
/** |
| 1100 |
* Register a callback for the specified callback type. |
| 1101 |
* @param {!goog.loader.AbstractModuleManager.CallbackType} type The callback |
| 1102 |
* type. |
| 1103 |
* @param {!Function} fn The callback function. |
| 1104 |
* @private |
| 1105 |
*/ |
| 1106 |
goog.module.ModuleManager.prototype.registerCallback_ = function(type, fn) { |
| 1107 |
var callbackMap = this.callbackMap_; |
| 1108 |
if (!callbackMap[type]) { |
| 1109 |
callbackMap[type] = []; |
| 1110 |
} |
| 1111 |
callbackMap[type].push(fn); |
| 1112 |
}; |
| 1113 |
|
| 1114 |
|
| 1115 |
/** |
| 1116 |
* Call the callback functions of the specified type. |
| 1117 |
* @param {!goog.loader.AbstractModuleManager.CallbackType} type The callback |
| 1118 |
* type. |
| 1119 |
* @private |
| 1120 |
*/ |
| 1121 |
goog.module.ModuleManager.prototype.executeCallbacks_ = function(type) { |
| 1122 |
var callbacks = this.callbackMap_[type]; |
| 1123 |
for (var i = 0; callbacks && i < callbacks.length; i++) { |
| 1124 |
callbacks[i](type); |
| 1125 |
} |
| 1126 |
}; |
| 1127 |
|
| 1128 |
|
| 1129 |
/** @override */ |
| 1130 |
goog.module.ModuleManager.prototype.disposeInternal = function() { |
| 1131 |
goog.module.ModuleManager.base(this, 'disposeInternal'); |
| 1132 |
|
| 1133 |
// Dispose of each ModuleInfo object. |
| 1134 |
goog.disposeAll( |
| 1135 |
goog.object.getValues(this.moduleInfoMap_), this.baseModuleInfo_); |
| 1136 |
this.moduleInfoMap_ = {}; |
| 1137 |
this.loadingModuleIds_ = []; |
| 1138 |
this.requestedLoadingModuleIds_ = []; |
| 1139 |
this.userInitiatedLoadingModuleIds_ = []; |
| 1140 |
this.requestedModuleIdsQueue_ = []; |
| 1141 |
this.callbackMap_ = {}; |
| 1142 |
}; |