Skip to content
This repository has been archived by the owner on Aug 1, 2024. It is now read-only.

Commit

Permalink
Move the check that prevents batch mode+prefetch from the modulemanag…
Browse files Browse the repository at this point in the history
…er to the moduleloader.

RELNOTES: N/A
PiperOrigin-RevId: 555459134
Change-Id: I9a93fdd1200e414b2cfe0fe34cc389a1d27691b4
  • Loading branch information
Closure Team authored and copybara-github committed Aug 10, 2023
1 parent 9d8ac17 commit 92a6392
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
11 changes: 11 additions & 0 deletions closure/goog/module/moduleloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ function ModuleLoader() {
* @private
*/
this.loadingModulesStatus_ = {};

/**
* Whether the module loader has prefetched a module.
* @type {boolean}
* @private
*/
this.hasPrefetched_ = false;
}
goog.inherits(ModuleLoader, EventTarget);

Expand Down Expand Up @@ -258,6 +265,9 @@ ModuleLoader.prototype.usingSourceUrlInjection_ = function() {
/** @override */
ModuleLoader.prototype.loadModules = function(
ids, moduleInfoMap, {forceReload, onError, onSuccess, onTimeout} = {}) {
if (this.hasPrefetched_ && ids.length > 1) {
throw new Error('Modules prefetching is not supported in batch mode');
}
const loadStatus = this.loadingModulesStatus_[ids] ||
ModuleLoader.LoadStatus.createForIds_(ids, moduleInfoMap);
loadStatus.loadRequested = true;
Expand Down Expand Up @@ -362,6 +372,7 @@ ModuleLoader.prototype.handleSuccess_ = function(bulkLoader, moduleIds) {

/** @override */
ModuleLoader.prototype.prefetchModule = function(id, moduleInfo) {
this.hasPrefetched_ = true;
// Do not prefetch in debug mode
if (this.getDebugMode()) {
return;
Expand Down
4 changes: 3 additions & 1 deletion closure/goog/module/moduleloader_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,8 +528,10 @@ testSuite({

testPrefetchModuleWithBatchModeEnabled() {
moduleManager.setBatchModeEnabled(true);
moduleManager.prefetchModule('modA');

assertThrows('Modules prefetching is not supported in batch mode', () => {
moduleManager.prefetchModule('modA');
moduleManager.execOnLoad('modB', () => {});
});
},

Expand Down
12 changes: 4 additions & 8 deletions closure/goog/module/modulemanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,14 +473,10 @@ goog.module.ModuleManager.prototype.preloadModule = function(id, opt_timeout) {

/** @override */
goog.module.ModuleManager.prototype.prefetchModule = function(id) {
if (this.batchModeEnabled_) {
throw new Error('Modules prefetching is not supported in batch mode');
} else {
var idWithDeps = this.getNotYetLoadedTransitiveDepIds_(id);
for (var i = 0; i < idWithDeps.length; i++) {
const moduleInfoOfDep = this.getModuleInfo(idWithDeps[i]);
this.getLoader().prefetchModule(idWithDeps[i], moduleInfoOfDep);
}
var idWithDeps = this.getNotYetLoadedTransitiveDepIds_(id);
for (var i = 0; i < idWithDeps.length; i++) {
const moduleInfoOfDep = this.getModuleInfo(idWithDeps[i]);
this.getLoader().prefetchModule(idWithDeps[i], moduleInfoOfDep);
}
};

Expand Down

0 comments on commit 92a6392

Please sign in to comment.