| 1 |
// Copyright 2017 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 The interface for module managers. The default implementation |
| 17 |
* is goog.module.ModuleManager. |
| 18 |
*/ |
| 19 |
|
| 20 |
goog.provide('goog.loader.AbstractModuleManager'); |
| 21 |
goog.provide('goog.loader.AbstractModuleManager.CallbackType'); |
| 22 |
goog.provide('goog.loader.AbstractModuleManager.FailureType'); |
| 23 |
|
| 24 |
goog.require('goog.Disposable'); |
| 25 |
goog.require('goog.async.Deferred'); |
| 26 |
goog.require('goog.module.AbstractModuleLoader'); |
| 27 |
goog.require('goog.module.ModuleInfo'); |
| 28 |
goog.require('goog.module.ModuleLoadCallback'); |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
/** |
| 33 |
* The ModuleManager keeps track of all modules in the environment. |
| 34 |
* Since modules may not have their code loaded, we must keep track of them. |
| 35 |
* @abstract |
| 36 |
* @constructor |
| 37 |
* @struct |
| 38 |
* @extends {goog.Disposable} |
| 39 |
*/ |
| 40 |
goog.loader.AbstractModuleManager = function() { |
| 41 |
goog.loader.AbstractModuleManager.base(this, 'constructor'); |
| 42 |
|
| 43 |
/** |
| 44 |
* The module context needed for module initialization. |
| 45 |
* @private {?Object} |
| 46 |
*/ |
| 47 |
this.moduleContext_ = null; |
| 48 |
|
| 49 |
/** |
| 50 |
* A loader for the modules that implements loadModules(ids, moduleInfoMap, |
| 51 |
* opt_successFn, opt_errorFn, opt_timeoutFn, opt_forceReload) method. |
| 52 |
* @private {?goog.module.AbstractModuleLoader} |
| 53 |
*/ |
| 54 |
this.loader_ = null; |
| 55 |
}; |
| 56 |
goog.inherits(goog.loader.AbstractModuleManager, goog.Disposable); |
| 57 |
|
| 58 |
|
| 59 |
/** |
| 60 |
* The type of callbacks that can be registered with the module manager,. |
| 61 |
* @enum {string} |
| 62 |
*/ |
| 63 |
goog.loader.AbstractModuleManager.CallbackType = { |
| 64 |
/** |
| 65 |
* Fired when an error has occurred. |
| 66 |
*/ |
| 67 |
ERROR: 'error', |
| 68 |
|
| 69 |
/** |
| 70 |
* Fired when it becomes idle and has no more module loads to process. |
| 71 |
*/ |
| 72 |
IDLE: 'idle', |
| 73 |
|
| 74 |
/** |
| 75 |
* Fired when it becomes active and has module loads to process. |
| 76 |
*/ |
| 77 |
ACTIVE: 'active', |
| 78 |
|
| 79 |
/** |
| 80 |
* Fired when it becomes idle and has no more user-initiated module loads to |
| 81 |
* process. |
| 82 |
*/ |
| 83 |
USER_IDLE: 'userIdle', |
| 84 |
|
| 85 |
/** |
| 86 |
* Fired when it becomes active and has user-initiated module loads to |
| 87 |
* process. |
| 88 |
*/ |
| 89 |
USER_ACTIVE: 'userActive' |
| 90 |
}; |
| 91 |
|
| 92 |
|
| 93 |
/** |
| 94 |
* The possible reasons for a module load failure callback being fired. |
| 95 |
* @enum {number} |
| 96 |
*/ |
| 97 |
goog.loader.AbstractModuleManager.FailureType = { |
| 98 |
/** 401 Status. */ |
| 99 |
UNAUTHORIZED: 0, |
| 100 |
|
| 101 |
/** Error status (not 401) returned multiple times. */ |
| 102 |
CONSECUTIVE_FAILURES: 1, |
| 103 |
|
| 104 |
/** Request timeout. */ |
| 105 |
TIMEOUT: 2, |
| 106 |
|
| 107 |
/** 410 status, old code gone. */ |
| 108 |
OLD_CODE_GONE: 3, |
| 109 |
|
| 110 |
/** The onLoad callbacks failed. */ |
| 111 |
INIT_ERROR: 4 |
| 112 |
}; |
| 113 |
|
| 114 |
|
| 115 |
/** |
| 116 |
* A non-HTTP status code indicating a corruption in loaded module. |
| 117 |
* This should be used by a ModuleLoader as a replacement for the HTTP code |
| 118 |
* given to the error handler function to indicated that the module was |
| 119 |
* corrupted. |
| 120 |
* This will set the forceReload flag on the loadModules method when retrying |
| 121 |
* module loading. |
| 122 |
* @type {number} |
| 123 |
*/ |
| 124 |
goog.loader.AbstractModuleManager.CORRUPT_RESPONSE_STATUS_CODE = 8001; |
| 125 |
|
| 126 |
|
| 127 |
/** |
| 128 |
* Sets the batch mode as enabled or disabled for the module manager. |
| 129 |
* @param {boolean} enabled Whether the batch mode is to be enabled or not. |
| 130 |
*/ |
| 131 |
goog.loader.AbstractModuleManager.prototype.setBatchModeEnabled = function( |
| 132 |
enabled) {}; |
| 133 |
|
| 134 |
|
| 135 |
/** |
| 136 |
* Sets the concurrent loading mode as enabled or disabled for the module |
| 137 |
* manager. Requires a moduleloader implementation that supports concurrent |
| 138 |
* loads. The default {@see goog.module.ModuleLoader} does not. |
| 139 |
* @param {boolean} enabled |
| 140 |
*/ |
| 141 |
goog.loader.AbstractModuleManager.prototype.setConcurrentLoadingEnabled = |
| 142 |
function(enabled) {}; |
| 143 |
|
| 144 |
|
| 145 |
/** |
| 146 |
* Sets the module info for all modules. Should only be called once. |
| 147 |
* |
| 148 |
* @param {!Object<!Array<string>>} infoMap An object that contains a mapping |
| 149 |
* from module id (String) to list of required module ids (Array). |
| 150 |
*/ |
| 151 |
goog.loader.AbstractModuleManager.prototype.setAllModuleInfo = function( |
| 152 |
infoMap) {}; |
| 153 |
|
| 154 |
|
| 155 |
/** |
| 156 |
* Sets the module info for all modules. Should only be called once. Also |
| 157 |
* marks modules that are currently being loaded. |
| 158 |
* |
| 159 |
* @param {string=} opt_info A string representation of the module dependency |
| 160 |
* graph, in the form: module1:dep1,dep2/module2:dep1,dep2 etc. |
| 161 |
* Where depX is the base-36 encoded position of the dep in the module list. |
| 162 |
* @param {!Array<string>=} opt_loadingModuleIds A list of moduleIds that |
| 163 |
* are currently being loaded. |
| 164 |
*/ |
| 165 |
goog.loader.AbstractModuleManager.prototype.setAllModuleInfoString = function( |
| 166 |
opt_info, opt_loadingModuleIds) {}; |
| 167 |
|
| 168 |
|
| 169 |
/** |
| 170 |
* Gets a module info object by id. |
| 171 |
* @param {string} id A module identifier. |
| 172 |
* @return {!goog.module.ModuleInfo} The module info. |
| 173 |
* @abstract |
| 174 |
*/ |
| 175 |
goog.loader.AbstractModuleManager.prototype.getModuleInfo = function(id) {}; |
| 176 |
|
| 177 |
|
| 178 |
/** |
| 179 |
* Sets the module uris. |
| 180 |
* @param {!Object<string, !Array<!goog.html.TrustedResourceUrl>>} moduleUriMap |
| 181 |
* The map of id/uris pairs for each module. |
| 182 |
*/ |
| 183 |
goog.loader.AbstractModuleManager.prototype.setModuleTrustedUris = function( |
| 184 |
moduleUriMap) {}; |
| 185 |
|
| 186 |
|
| 187 |
/** |
| 188 |
* Gets the application-specific module loader. |
| 189 |
* @return {?goog.module.AbstractModuleLoader} An object that has a |
| 190 |
* loadModules(ids, moduleInfoMap, opt_successFn, opt_errFn, |
| 191 |
* opt_timeoutFn, opt_forceReload) method. |
| 192 |
*/ |
| 193 |
goog.loader.AbstractModuleManager.prototype.getLoader = function() { |
| 194 |
return this.loader_; |
| 195 |
}; |
| 196 |
|
| 197 |
|
| 198 |
/** |
| 199 |
* Sets the application-specific module loader. |
| 200 |
* @param {!goog.module.AbstractModuleLoader} loader An object that has a |
| 201 |
* loadModules(ids, moduleInfoMap, opt_successFn, opt_errFn, |
| 202 |
* opt_timeoutFn, opt_forceReload) method. |
| 203 |
*/ |
| 204 |
goog.loader.AbstractModuleManager.prototype.setLoader = function(loader) { |
| 205 |
this.loader_ = loader; |
| 206 |
}; |
| 207 |
|
| 208 |
|
| 209 |
/** |
| 210 |
* Gets the module context to use to initialize the module. |
| 211 |
* @return {?Object} The context. |
| 212 |
*/ |
| 213 |
goog.loader.AbstractModuleManager.prototype.getModuleContext = function() { |
| 214 |
return this.moduleContext_; |
| 215 |
}; |
| 216 |
|
| 217 |
|
| 218 |
/** |
| 219 |
* Sets the module context to use to initialize the module. |
| 220 |
* @param {!Object} context The context. |
| 221 |
*/ |
| 222 |
goog.loader.AbstractModuleManager.prototype.setModuleContext = function( |
| 223 |
context) { |
| 224 |
this.moduleContext_ = context; |
| 225 |
}; |
| 226 |
|
| 227 |
|
| 228 |
/** |
| 229 |
* Determines if the ModuleManager is active |
| 230 |
* @return {boolean} TRUE iff the ModuleManager is active (i.e., not idle). |
| 231 |
*/ |
| 232 |
goog.loader.AbstractModuleManager.prototype.isActive = function() { |
| 233 |
return false; |
| 234 |
}; |
| 235 |
|
| 236 |
|
| 237 |
/** |
| 238 |
* Determines if the ModuleManager is user active |
| 239 |
* @return {boolean} TRUE iff the ModuleManager is user active (i.e., not idle). |
| 240 |
*/ |
| 241 |
goog.loader.AbstractModuleManager.prototype.isUserActive = function() { |
| 242 |
return false; |
| 243 |
}; |
| 244 |
|
| 245 |
|
| 246 |
/** |
| 247 |
* Preloads a module after a short delay. |
| 248 |
* |
| 249 |
* @param {string} id The id of the module to preload. |
| 250 |
* @param {number=} opt_timeout The number of ms to wait before adding the |
| 251 |
* module id to the loading queue (defaults to 0 ms). Note that the module |
| 252 |
* will be loaded asynchronously regardless of the value of this parameter. |
| 253 |
* @return {!goog.async.Deferred} A deferred object. |
| 254 |
* @abstract |
| 255 |
*/ |
| 256 |
goog.loader.AbstractModuleManager.prototype.preloadModule = function( |
| 257 |
id, opt_timeout) {}; |
| 258 |
|
| 259 |
|
| 260 |
/** |
| 261 |
* Prefetches a JavaScript module and its dependencies, which means that the |
| 262 |
* module will be downloaded, but not evaluated. To complete the module load, |
| 263 |
* the caller should also call load or execOnLoad after prefetching the module. |
| 264 |
* |
| 265 |
* @param {string} id The id of the module to prefetch. |
| 266 |
*/ |
| 267 |
goog.loader.AbstractModuleManager.prototype.prefetchModule = function(id) { |
| 268 |
throw new Error('prefetchModule is not implemented.'); |
| 269 |
}; |
| 270 |
|
| 271 |
|
| 272 |
/** |
| 273 |
* Records that a module was loaded. Also initiates loading the next module if |
| 274 |
* any module requests are queued. This method is called by code that is |
| 275 |
* generated and appended to each dynamic module's code at compilation time. |
| 276 |
* |
| 277 |
* @param {string} id A module id. |
| 278 |
*/ |
| 279 |
goog.loader.AbstractModuleManager.prototype.setLoaded = function(id) {}; |
| 280 |
|
| 281 |
|
| 282 |
/** |
| 283 |
* Gets whether a module is currently loading or in the queue, waiting to be |
| 284 |
* loaded. |
| 285 |
* @param {string} id A module id. |
| 286 |
* @return {boolean} TRUE iff the module is loading. |
| 287 |
* @abstract |
| 288 |
*/ |
| 289 |
goog.loader.AbstractModuleManager.prototype.isModuleLoading = function(id) {}; |
| 290 |
|
| 291 |
|
| 292 |
/** |
| 293 |
* Requests that a function be called once a particular module is loaded. |
| 294 |
* Client code can use this method to safely call into modules that may not yet |
| 295 |
* be loaded. For consistency, this method always calls the function |
| 296 |
* asynchronously -- even if the module is already loaded. Initiates loading of |
| 297 |
* the module if necessary, unless opt_noLoad is true. |
| 298 |
* |
| 299 |
* @param {string} moduleId A module id. |
| 300 |
* @param {!Function} fn Function to execute when the module has loaded. |
| 301 |
* @param {!Object=} opt_handler Optional handler under whose scope to execute |
| 302 |
* the callback. |
| 303 |
* @param {boolean=} opt_noLoad TRUE iff not to initiate loading of the module. |
| 304 |
* @param {boolean=} opt_userInitiated TRUE iff the loading of the module was |
| 305 |
* user initiated. |
| 306 |
* @param {boolean=} opt_preferSynchronous TRUE iff the function should be |
| 307 |
* executed synchronously if the module has already been loaded. |
| 308 |
* @return {!goog.module.ModuleLoadCallback} A callback wrapper that exposes |
| 309 |
* an abort and execute method. |
| 310 |
* @abstract |
| 311 |
*/ |
| 312 |
goog.loader.AbstractModuleManager.prototype.execOnLoad = function( |
| 313 |
moduleId, fn, opt_handler, opt_noLoad, opt_userInitiated, |
| 314 |
opt_preferSynchronous) {}; |
| 315 |
|
| 316 |
|
| 317 |
/** |
| 318 |
* Loads a module, returning a goog.async.Deferred for keeping track of the |
| 319 |
* result. |
| 320 |
* |
| 321 |
* @param {string} moduleId A module id. |
| 322 |
* @param {boolean=} opt_userInitiated If the load is a result of a user action. |
| 323 |
* @return {!goog.async.Deferred} A deferred object. |
| 324 |
* @abstract |
| 325 |
*/ |
| 326 |
goog.loader.AbstractModuleManager.prototype.load = function( |
| 327 |
moduleId, opt_userInitiated) {}; |
| 328 |
|
| 329 |
|
| 330 |
/** |
| 331 |
* Loads a list of modules, returning a goog.async.Deferred for keeping track of |
| 332 |
* the result. |
| 333 |
* |
| 334 |
* @param {!Array<string>} moduleIds A list of module ids. |
| 335 |
* @param {boolean=} opt_userInitiated If the load is a result of a user action. |
| 336 |
* @return {!Object<string, !goog.async.Deferred>} A mapping from id (String) |
| 337 |
* to deferred objects that will callback or errback when the load for that |
| 338 |
* id is finished. |
| 339 |
* @abstract |
| 340 |
*/ |
| 341 |
goog.loader.AbstractModuleManager.prototype.loadMultiple = function( |
| 342 |
moduleIds, opt_userInitiated) {}; |
| 343 |
|
| 344 |
|
| 345 |
/** |
| 346 |
* Method called just before a module code is loaded. |
| 347 |
* @param {string} id Identifier of the module. |
| 348 |
*/ |
| 349 |
goog.loader.AbstractModuleManager.prototype.beforeLoadModuleCode = function( |
| 350 |
id) {}; |
| 351 |
|
| 352 |
|
| 353 |
/** |
| 354 |
* Method called just after module code is loaded |
| 355 |
* @param {string} id Identifier of the module. |
| 356 |
*/ |
| 357 |
goog.loader.AbstractModuleManager.prototype.afterLoadModuleCode = function( |
| 358 |
id) {}; |
| 359 |
|
| 360 |
|
| 361 |
/** |
| 362 |
* Register an initialization callback for the currently loading module. This |
| 363 |
* should only be called by script that is executed during the evaluation of |
| 364 |
* a module's javascript. This is almost equivalent to calling the function |
| 365 |
* inline, but ensures that all the code from the currently loading module |
| 366 |
* has been loaded. This makes it cleaner and more robust than calling the |
| 367 |
* function inline. |
| 368 |
* |
| 369 |
* If this function is called from the base module (the one that contains |
| 370 |
* the module manager code), the callback is held until #setAllModuleInfo |
| 371 |
* is called, or until #setModuleContext is called, whichever happens first. |
| 372 |
* |
| 373 |
* @param {!Function} fn A callback function that takes a single argument |
| 374 |
* which is the module context. |
| 375 |
* @param {!Object=} opt_handler Optional handler under whose scope to execute |
| 376 |
* the callback. |
| 377 |
*/ |
| 378 |
goog.loader.AbstractModuleManager.prototype.registerInitializationCallback = |
| 379 |
function(fn, opt_handler) {}; |
| 380 |
|
| 381 |
|
| 382 |
/** |
| 383 |
* Register a late initialization callback for the currently loading module. |
| 384 |
* Callbacks registered via this function are executed similar to |
| 385 |
* {@see registerInitializationCallback}, but they are fired after all |
| 386 |
* initialization callbacks are called. |
| 387 |
* |
| 388 |
* @param {!Function} fn A callback function that takes a single argument |
| 389 |
* which is the module context. |
| 390 |
* @param {!Object=} opt_handler Optional handler under whose scope to execute |
| 391 |
* the callback. |
| 392 |
*/ |
| 393 |
goog.loader.AbstractModuleManager.prototype.registerLateInitializationCallback = |
| 394 |
function(fn, opt_handler) {}; |
| 395 |
|
| 396 |
|
| 397 |
/** |
| 398 |
* Sets the constructor to use for the module object for the currently |
| 399 |
* loading module. The constructor should derive from |
| 400 |
* {@see goog.module.BaseModule}. |
| 401 |
* @param {!Function} fn The constructor function. |
| 402 |
*/ |
| 403 |
goog.loader.AbstractModuleManager.prototype.setModuleConstructor = function( |
| 404 |
fn) {}; |
| 405 |
|
| 406 |
|
| 407 |
/** |
| 408 |
* The function to call if the module manager is in error. |
| 409 |
* @param {!goog.loader.AbstractModuleManager.CallbackType|!Array< |
| 410 |
* !goog.loader.AbstractModuleManager.CallbackType>} types The callback |
| 411 |
* type. |
| 412 |
* @param {!Function} fn The function to register as a callback. |
| 413 |
*/ |
| 414 |
goog.loader.AbstractModuleManager.prototype.registerCallback = function( |
| 415 |
types, fn) {}; |