Skip to content

Commit

Permalink
[INTERNAL] Infra: implement library preload with script tags
Browse files Browse the repository at this point in the history
 - implement preload with scripts + fallback to older json
 - registerPreloadedModules no longer handles dependencies. 
   Only preloadModules does so as only the preload.json files contain 
   dependencies
 - deprecate jQuery.sap.preloadModules, only consumer outside
   sap.ui.core is sap.ushell
 - core bootstrap process migrated to loadLibraries(preloadOnly:true)
 - tweak one test in Models.qunit.html (which checked for a specific, 
   no longer applicable error message)
 - add more tests based on test libraries
 - add node.js script to generate these libs out of meta info
 
Change-Id: I9ebd12545bedb766f79c92a404521e5bc0e54de6
  • Loading branch information
codeworrior committed Jun 7, 2016
1 parent 742ba81 commit 9b5b98c
Show file tree
Hide file tree
Showing 62 changed files with 1,332 additions and 63 deletions.
52 changes: 43 additions & 9 deletions src/sap.ui.core/src/jquery.sap.global.js
Original file line number Diff line number Diff line change
Expand Up @@ -2977,6 +2977,7 @@
oModule.state = FAILED;
oModule.errorMessage = xhr ? xhr.status + " - " + xhr.statusText : textStatus;
oModule.errorStack = error && error.stack;
oModule.loadError = true;
}
});
/*eslint-enable no-loop-func */
Expand All @@ -2992,6 +2993,7 @@
if ( oModule.state !== READY ) {
var oError = new Error("failed to load '" + sModuleName + "' from " + oModule.url + ": " + oModule.errorMessage);
enhanceStacktrace(oError, oModule.errorStack);
oError.loadError = oModule.loadError;
throw oError;
}

Expand Down Expand Up @@ -3388,6 +3390,17 @@
return mModules[sModuleName] && (bIncludePreloaded || mModules[sModuleName].state !== PRELOADED);
};

/**
* Whether the given resource has been loaded (or preloaded).
* @param {string} sResourceName Name of the resource to check, in unified resource name format
* @returns {boolean} Whether the resource has been loaded already
* @private
* @sap-restricted sap.ui.core
*/
jQuery.sap.isResourceLoaded = function isResourceLoaded(sResourceName) {
return mModules[sResourceName];
};

/**
* Returns the names of all declared modules.
* @return {string[]} the names of all declared modules
Expand Down Expand Up @@ -3935,10 +3948,16 @@
return requireModule(sModuleName + ".js", true);
};

/**
* @private
* @deprecated
*/
jQuery.sap.preloadModules = function(sPreloadModule, bAsync, oSyncPoint) {

var sURL, iTask, sMsg;

jQuery.sap.log.error("[Deprecated] jQuery.sap.preloadModules was never a public API and will be removed soon. Migrate to Core.loadLibraries()!");

jQuery.sap.assert(!bAsync || oSyncPoint, "if mode is async, a syncpoint object must be given");

if ( !bAsync && syncCallBehavior ) {
Expand All @@ -3960,15 +3979,21 @@

log.debug("preload file " + sPreloadModule);
iTask = oSyncPoint && oSyncPoint.startTask("load " + sPreloadModule);

jQuery.ajax({
dataType : "json",
async : bAsync,
url : sURL,
success : function(data) {
if ( data ) {
data.url = sURL;
jQuery.sap.registerPreloadedModules(data, sURL);
// also preload dependencies
if ( Array.isArray(data.dependencies) ) {
data.dependencies.forEach(function(sDependency) {
jQuery.sap.preloadModules(sDependency, bAsync, oSyncPoint);
});
}
}
jQuery.sap.registerPreloadedModules(data, bAsync, oSyncPoint);
oSyncPoint && oSyncPoint.finishTask(iTask);
},
error : function(xhr, textStatus, error) {
Expand All @@ -3979,7 +4004,21 @@

};

jQuery.sap.registerPreloadedModules = function(oData, bAsync, oSyncPoint) {
/**
* Adds all resources from a preload bundle to the preload cache.
*
* When a resource exists already in the cache, the new content is ignored.
*
* @param {object} oData Preload bundle
* @param {string} [oData.url] URL from which the bundle has been loaded
* @param {string} [oData.name] Unique name of the bundle
* @param {string} [oData.version='1.0'] Format version of the preload bundle
* @param {object} oData.modules Map of resources keyed by their resource name; each resource must be a string or a function
*
* @private
* @sap-restricted sap.ui.core,preloadfiles
*/
jQuery.sap.registerPreloadedModules = function(oData) {

var bOldSyntax = Version(oData.version || "1.0").compareTo("2.0") < 0;

Expand All @@ -3991,7 +4030,7 @@
mPreloadModules[oData.name] = true;
}

jQuery.each(oData.modules, function(sName,sContent) {
jQuery.each(oData.modules, function(sName, sContent) {
sName = bOldSyntax ? ui5ToRJS(sName) + ".js" : sName;
Module.get(sName).preload(oData.url + "/" + sName, sContent, oData.name);
// when a library file is preloaded, also mark its preload file as loaded
Expand All @@ -4002,11 +4041,6 @@
}
});

if ( oData.dependencies ) {
jQuery.each(oData.dependencies, function(idx,sModuleName) {
jQuery.sap.preloadModules(sModuleName, bAsync, oSyncPoint);
});
}
};

/**
Expand Down
Loading

0 comments on commit 9b5b98c

Please sign in to comment.