Revert "Improved results and contentuserdata saving by queueing requests"

This reverts commit 3849f3a054.
pull/17/head
Frode Petterson 2016-02-22 11:56:52 +01:00
parent 3849f3a054
commit 256bf1b6d2
1 changed files with 32 additions and 129 deletions

131
js/h5p.js
View File

@ -1580,49 +1580,8 @@ H5P.shuffleArray = function (array) {
return array; return array;
}; };
(function ($) {
var token;
var queue = [];
/** /**
* Return unix timestamp for the given JS Date. * Post finished results for user.
*
* @private
* @param {Date} date
* @returns {Number}
*/
var toUnix = function (date) {
return Math.round(date.getTime() / 1000);
};
/**
* Post results to server
*
* @param Object result
*/
var post = function (result) {
// Add token to post
result.token = token;
token = null;
// Post the results
// TODO: Should we use a variable with the complete path?
H5P.jQuery.post(H5PIntegration.ajaxPath + 'setFinished', result).fail(function() {
// Reuse token
token = result.token;
}).done(function(data) {
token = data.token;
}).always(function() {
// Check for more requests to run
if (queue[0] !== undefined) {
post(queue.splice(0, 1));
}
});
};
/**
* Creates the finished result object for the user and schedules it for
* posting to the server.
* *
* @deprecated * @deprecated
* Do not use this function directly, trigger the finish event instead. * Do not use this function directly, trigger the finish event instead.
@ -1637,41 +1596,31 @@ H5P.shuffleArray = function (array) {
* Reported time consumption/usage * Reported time consumption/usage
*/ */
H5P.setFinished = function (contentId, score, maxScore, time) { H5P.setFinished = function (contentId, score, maxScore, time) {
if (H5PIntegration.postUserStatistics !== true) { if (H5PIntegration.postUserStatistics === true) {
return; /**
} * Return unix timestamp for the given JS Date.
*
* @private
* @param {Date} date
* @returns {Number}
*/
var toUnix = function (date) {
return Math.round(date.getTime() / 1000);
};
var result = { // Post the results
// TODO: Should we use a variable with the complete path?
H5P.jQuery.post(H5PIntegration.ajaxPath + 'setFinished', {
contentId: contentId, contentId: contentId,
score: score, score: score,
maxScore: maxScore, maxScore: maxScore,
opened: toUnix(H5P.opened[contentId]), opened: toUnix(H5P.opened[contentId]),
finished: toUnix(new Date()), finished: toUnix(new Date()),
time: time time: time
}; });
if (token === undefined) {
if (H5PIntegration.tokens === undefined ||
H5PIntegration.tokens.result === undefined) {
token = 'canHasDummyToken';
}
else {
token = H5PIntegration.tokens.result;
}
}
if (token === null) {
// Already in progress, add to queue
queue.push(result);
}
else {
post(result);
} }
}; };
})(H5P.jQuery);
// Add indexOf to browsers that lack them. (IEs) // Add indexOf to browsers that lack them. (IEs)
if (!Array.prototype.indexOf) { if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (needle) { Array.prototype.indexOf = function (needle) {
@ -1779,8 +1728,6 @@ H5P.createTitle = function (rawTitle, maxLength) {
// Wrap in privates // Wrap in privates
(function ($) { (function ($) {
var token;
var queue = [];
/** /**
* Creates ajax requests for inserting, updateing and deleteing * Creates ajax requests for inserting, updateing and deleteing
@ -1802,16 +1749,6 @@ H5P.createTitle = function (rawTitle, maxLength) {
done('Not signed in.'); done('Not signed in.');
return; return;
} }
if (token === undefined) {
// Load initial token
if (H5PIntegration.tokens === undefined ||
H5PIntegration.tokens.contentUserData === undefined) {
token = 'canHasDummyToken';
}
else {
token = H5PIntegration.tokens.contentUserData;
}
}
var options = { var options = {
url: H5PIntegration.ajax.contentUserData.replace(':contentId', contentId).replace(':dataType', dataType).replace(':subContentId', subContentId ? subContentId : 0), url: H5PIntegration.ajax.contentUserData.replace(':contentId', contentId).replace(':dataType', dataType).replace(':subContentId', subContentId ? subContentId : 0),
@ -1847,44 +1784,10 @@ H5P.createTitle = function (rawTitle, maxLength) {
done(undefined, response.data); done(undefined, response.data);
}; };
} }
if (options.type === 'POST') {
if (token === null) {
// We must queue and wait for a new token
queue.push(options);
}
else {
// Use token
options.data.token = token;
token = null;
}
}
runAjaxRequest(options); $.ajax(options);
} }
/**
*
*
* @param Object options Details for request
*/
var runAjaxRequest = function (options) {
var $req = $.ajax(options);
if (options.type === 'POST') {
$req.fail(function() {
// Reuse token
token = options.data.token;
}).done(function(data) {
// Set new token
token = data.token;
}).always(function() {
// Check for more requests to run
if (queue[0] !== undefined) {
runAjaxRequest(queue.splice(0, 1));
}
});
}
};
/** /**
* Get user data for given content. * Get user data for given content.
* *