Debouncing the LMS API calls
This commit is contained in:
56
dist/scorm-again.js
vendored
56
dist/scorm-again.js
vendored
File diff suppressed because one or more lines are too long
2
dist/scorm-again.js.map
vendored
2
dist/scorm-again.js.map
vendored
File diff suppressed because one or more lines are too long
2
dist/scorm-again.min.js
vendored
2
dist/scorm-again.min.js
vendored
File diff suppressed because one or more lines are too long
@@ -912,6 +912,31 @@ export default class BaseAPI {
|
|||||||
'The storeData method has not been implemented');
|
'The storeData method has not been implemented');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A debounce function to throttle calls to LMS commit API
|
||||||
|
*
|
||||||
|
* @param {function} func
|
||||||
|
* @param {number} wait
|
||||||
|
* @param {boolean} immediate
|
||||||
|
* @return {function(...[*]=)}
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
_debounce(func, wait, immediate = false) {
|
||||||
|
let timeout;
|
||||||
|
return function(...args) {
|
||||||
|
const later = () => {
|
||||||
|
timeout = null; // added this to set same behaviour as ES5
|
||||||
|
// eslint-disable-next-line no-invalid-this
|
||||||
|
if (!immediate) func.apply(this, args); // this is called conditionally, just like in the ES5 version
|
||||||
|
};
|
||||||
|
const callNow = immediate && !timeout;
|
||||||
|
clearTimeout(timeout);
|
||||||
|
timeout = setTimeout(later, wait);
|
||||||
|
// eslint-disable-next-line no-invalid-this
|
||||||
|
if (callNow) func.apply(this, args);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send the request to the LMS
|
* Send the request to the LMS
|
||||||
* @param {string} url
|
* @param {string} url
|
||||||
@@ -919,15 +944,17 @@ export default class BaseAPI {
|
|||||||
* @return {object}
|
* @return {object}
|
||||||
*/
|
*/
|
||||||
processHttpRequest(url: String, params) {
|
processHttpRequest(url: String, params) {
|
||||||
|
const debounced = this._debounce(
|
||||||
|
function(url, params, settings, error_codes) {
|
||||||
const genericError = {
|
const genericError = {
|
||||||
'result': global_constants.SCORM_FALSE,
|
'result': global_constants.SCORM_FALSE,
|
||||||
'errorCode': this.#error_codes.GENERAL,
|
'errorCode': error_codes.GENERAL,
|
||||||
};
|
};
|
||||||
|
|
||||||
let result;
|
let result;
|
||||||
if (!this.#settings.sendBeaconCommit) {
|
if (!settings.sendBeaconCommit) {
|
||||||
const httpReq = new XMLHttpRequest();
|
const httpReq = new XMLHttpRequest();
|
||||||
httpReq.open('POST', url, this.#settings.asyncCommit);
|
httpReq.open('POST', url, settings.asyncCommit);
|
||||||
try {
|
try {
|
||||||
if (params instanceof Array) {
|
if (params instanceof Array) {
|
||||||
httpReq.setRequestHeader('Content-Type',
|
httpReq.setRequestHeader('Content-Type',
|
||||||
@@ -935,12 +962,12 @@ export default class BaseAPI {
|
|||||||
httpReq.send(params.join('&'));
|
httpReq.send(params.join('&'));
|
||||||
} else {
|
} else {
|
||||||
httpReq.setRequestHeader('Content-Type',
|
httpReq.setRequestHeader('Content-Type',
|
||||||
this.settings.commitRequestDataType);
|
settings.commitRequestDataType);
|
||||||
httpReq.send(JSON.stringify(params));
|
httpReq.send(JSON.stringify(params));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof this.settings.responseHandler === 'function') {
|
if (typeof settings.responseHandler === 'function') {
|
||||||
result = this.settings.responseHandler(httpReq);
|
result = settings.responseHandler(httpReq);
|
||||||
} else {
|
} else {
|
||||||
result = JSON.parse(httpReq.responseText);
|
result = JSON.parse(httpReq.responseText);
|
||||||
}
|
}
|
||||||
@@ -951,7 +978,7 @@ export default class BaseAPI {
|
|||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
const headers = {
|
const headers = {
|
||||||
type: this.settings.commitRequestDataType,
|
type: settings.commitRequestDataType,
|
||||||
};
|
};
|
||||||
let blob;
|
let blob;
|
||||||
if (params instanceof Array) {
|
if (params instanceof Array) {
|
||||||
@@ -978,6 +1005,9 @@ export default class BaseAPI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
}, 5000);
|
||||||
|
|
||||||
|
return debounced(url, params, this.settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user