Dramatically improved content upgrade performance by caching libraries.

Added real async function to avoid using up the stack.
namespaces
Frode Petterson 2014-08-28 09:40:02 +02:00
parent 67da268c44
commit 50b00e6459
1 changed files with 22 additions and 9 deletions

View File

@ -1,7 +1,7 @@
var H5PUpgrades = H5PUpgrades || {}; var H5PUpgrades = H5PUpgrades || {};
(function ($) { (function ($) {
var info, $container; var info, $container, librariesCache = {};
// Initialize // Initialize
$(document).ready(function () { $(document).ready(function () {
@ -78,13 +78,16 @@ var H5PUpgrades = H5PUpgrades || {};
* @param {String} err * @param {String} err
*/ */
var check = function (err) { var check = function (err) {
i++; // We need to use a real async function in order for the stack to clear.
if (i === (isArray ? obj.length : ids.length) || (err !== undefined && err !== null)) { setTimeout(function () {
finished(err); i++;
} if (i === (isArray ? obj.length : ids.length) || (err !== undefined && err !== null)) {
else { finished(err);
next(); }
} else {
next();
}
}, 0);
}; };
check(); // Start check(); // Start
@ -288,12 +291,22 @@ var H5PUpgrades = H5PUpgrades || {};
ContentUpgrade.prototype.loadLibrary = function (name, version, next) { ContentUpgrade.prototype.loadLibrary = function (name, version, next) {
var self = this; var self = this;
var key = name + '/' + version.major + '/' + version.minor;
if (librariesCache[key] !== undefined) {
// Library has been loaded before. Return cache.
next(null, librariesCache[key]);
return;
}
$.ajax({ $.ajax({
dataType: 'json', dataType: 'json',
url: info.libraryBaseUrl + '/' + name + '/' + version.major + '/' + version.minor cache: true,
url: info.libraryBaseUrl + '/' + key
}).fail(function () { }).fail(function () {
next(info.errorData.replace('%lib', name + ' ' + version)); next(info.errorData.replace('%lib', name + ' ' + version));
}).done(function (library) { }).done(function (library) {
librariesCache[key] = library;
if (library.upgradesScript) { if (library.upgradesScript) {
self.loadScript(library.upgradesScript, function (err) { self.loadScript(library.upgradesScript, function (err) {
if (err) { if (err) {