JI-1059 Add offline request queue only if frame contains H5P

Add logic for queueing requests that failed outside the H5P frame
Fix showing throbber when retrying because connection was reestablished
pull/61/head
Thomas Marstrander 2019-04-03 13:09:18 +02:00
parent 39d27ab9bb
commit 687f886e3d
2 changed files with 45 additions and 13 deletions

View File

@ -66,8 +66,6 @@ H5P.init = function (target) {
H5P.$body = H5P.jQuery(document.body); H5P.$body = H5P.jQuery(document.body);
} }
H5P.offlineRequestQueue = new H5P.OfflineRequestQueue();
// Determine if we can use full screen // Determine if we can use full screen
if (H5P.fullscreenSupported === undefined) { if (H5P.fullscreenSupported === undefined) {
/** /**
@ -106,6 +104,8 @@ H5P.init = function (target) {
metadata: contentData.metadata metadata: contentData.metadata
}; };
H5P.offlineRequestQueue = new H5P.OfflineRequestQueue();
H5P.getUserData(contentId, 'state', function (err, previousState) { H5P.getUserData(contentId, 'state', function (err, previousState) {
if (previousState) { if (previousState) {
library.userDatas = { library.userDatas = {
@ -2077,7 +2077,9 @@ H5P.setFinished = function (contentId, score, maxScore, time) {
}; };
H5P.jQuery.post(H5PIntegration.ajax.setFinished, data) H5P.jQuery.post(H5PIntegration.ajax.setFinished, data)
.fail(function () { .fail(function () {
H5P.offlineRequestQueue.add(H5PIntegration.ajax.setFinished, data); if (H5P.offlineRequestQueue) {
H5P.offlineRequestQueue.add(H5PIntegration.ajax.setFinished, data);
}
}); });
} }
}; };

View File

@ -21,6 +21,10 @@ H5P.RequestQueue = (function ($, EventDispatcher) {
// Initialize listener for when requests are added to queue // Initialize listener for when requests are added to queue
window.addEventListener('offline', this.updateOnlineStatus.bind(this)); window.addEventListener('offline', this.updateOnlineStatus.bind(this));
window.addEventListener('online', this.updateOnlineStatus.bind(this)); window.addEventListener('online', this.updateOnlineStatus.bind(this));
this.on('connectionReestablished', function () {
this.resumeQueue();
}.bind(this));
}; };
/** /**
@ -213,7 +217,7 @@ H5P.RequestQueue = (function ($, EventDispatcher) {
const requestQueue = this.getStoredRequests(); const requestQueue = this.getStoredRequests();
if (requestQueue.length) { if (requestQueue.length) {
message += ' ' + H5P.t('resubmitScores'); message += ' ' + H5P.t('resubmitScores');
this.resumeQueue(); this.trigger('connectionReestablished');
} }
this.displayToastMessage(message); this.displayToastMessage(message);
@ -228,7 +232,7 @@ H5P.RequestQueue = (function ($, EventDispatcher) {
* @type {offlineRequestQueue} * @type {offlineRequestQueue}
*/ */
H5P.OfflineRequestQueue = (function (RequestQueue, Dialog) { H5P.OfflineRequestQueue = (function (RequestQueue, Dialog) {
return function offlineRequestQueue() { const offlineRequestQueue = function () {
const requestQueue = new RequestQueue(); const requestQueue = new RequestQueue();
// We could handle requests from previous pages here, but instead we throw them away // We could handle requests from previous pages here, but instead we throw them away
@ -251,7 +255,6 @@ H5P.OfflineRequestQueue = (function (RequestQueue, Dialog) {
classes: ['offline'], classes: ['offline'],
}); });
const dialog = offlineDialog.getElement(); const dialog = offlineDialog.getElement();
// Add retry text to body // Add retry text to body
@ -308,6 +311,11 @@ H5P.OfflineRequestQueue = (function (RequestQueue, Dialog) {
}.bind(this)); }.bind(this));
requestQueue.on('connectionReestablished', function () {
// Skip resuming queue since request queue already does this
retryRequests(true);
}.bind(this));
offlineDialog.on('confirmed', function () { offlineDialog.on('confirmed', function () {
// Show dialog on next render in case it is being hidden by the 'confirm' button // Show dialog on next render in case it is being hidden by the 'confirm' button
isShowing = false; isShowing = false;
@ -316,6 +324,19 @@ H5P.OfflineRequestQueue = (function (RequestQueue, Dialog) {
}, 100); }, 100);
}.bind(this)); }.bind(this));
// Listen for queued requests outside the iframe
window.addEventListener('message', function (event) {
const isValidQueueEvent = window.parent === event.source
&& event.data.context === 'h5p'
&& event.data.action === 'queueRequest';
if (!isValidQueueEvent) {
return;
}
this.add(event.data.url, event.data.data);
}.bind(this));
/** /**
* Toggle throbber visibility * Toggle throbber visibility
* *
@ -334,19 +355,21 @@ H5P.OfflineRequestQueue = (function (RequestQueue, Dialog) {
if (isLoading) { if (isLoading) {
throbberWrapper.classList.add('show'); throbberWrapper.classList.add('show');
} } else {
else {
throbberWrapper.classList.remove('show'); throbberWrapper.classList.remove('show');
} }
}; };
/** /**
* Retries the failed requests * Retries the failed requests
*
* @param {boolean} [skipResumeQueue] Skip resuming queue (just do the visuals)
*/ */
const retryRequests = function () { const retryRequests = function (skipResumeQueue) {
clearInterval(currentInterval); clearInterval(currentInterval);
toggleThrobber(true); toggleThrobber(true);
requestQueue.resumeQueue(); if (!skipResumeQueue) {
requestQueue.resumeQueue();
}
}; };
/** /**
@ -365,6 +388,11 @@ H5P.OfflineRequestQueue = (function (RequestQueue, Dialog) {
* @param forceDelayedShow * @param forceDelayedShow
*/ */
const startCountDown = function (forceDelayedShow) { const startCountDown = function (forceDelayedShow) {
// Already showing, wait for retry
if (isShowing) {
return;
}
toggleThrobber(false); toggleThrobber(false);
if (!isShowing) { if (!isShowing) {
if (forceDelayedShow) { if (forceDelayedShow) {
@ -373,14 +401,14 @@ H5P.OfflineRequestQueue = (function (RequestQueue, Dialog) {
setTimeout(function () { setTimeout(function () {
offlineDialog.show(); offlineDialog.show();
}, 100); }, 100);
} } else {
else {
offlineDialog.show(); offlineDialog.show();
} }
} }
isShowing = true; isShowing = true;
startTime = new Date().getTime(); startTime = new Date().getTime();
incrementRetryInterval(); incrementRetryInterval();
clearInterval(currentInterval);
currentInterval = setInterval(updateCountDown, 100); currentInterval = setInterval(updateCountDown, 100);
}; };
@ -409,4 +437,6 @@ H5P.OfflineRequestQueue = (function (RequestQueue, Dialog) {
requestQueue.add(url, data); requestQueue.add(url, data);
}; };
}; };
return offlineRequestQueue;
})(H5P.RequestQueue, H5P.ConfirmationDialog); })(H5P.RequestQueue, H5P.ConfirmationDialog);