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,6 +78,8 @@ var H5PUpgrades = H5PUpgrades || {};
* @param {String} err * @param {String} err
*/ */
var check = function (err) { var check = function (err) {
// We need to use a real async function in order for the stack to clear.
setTimeout(function () {
i++; i++;
if (i === (isArray ? obj.length : ids.length) || (err !== undefined && err !== null)) { if (i === (isArray ? obj.length : ids.length) || (err !== undefined && err !== null)) {
finished(err); finished(err);
@ -85,6 +87,7 @@ var H5PUpgrades = H5PUpgrades || {};
else { else {
next(); 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) {