Switching to lodash's debounce
This commit is contained in:
465
dist/scorm-again.js
vendored
465
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
39
dist/scorm-again.min.js
vendored
39
dist/scorm-again.min.js
vendored
File diff suppressed because one or more lines are too long
8
package-lock.json
generated
8
package-lock.json
generated
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "scorm-again",
|
"name": "scorm-again",
|
||||||
"version": "1.1.4",
|
"version": "1.3.0",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
@@ -6535,6 +6535,12 @@
|
|||||||
"integrity": "sha1-euMBfpOWIqwxt9fX3LGzTbFpDTU=",
|
"integrity": "sha1-euMBfpOWIqwxt9fX3LGzTbFpDTU=",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
|
"lodash.debounce": {
|
||||||
|
"version": "4.0.8",
|
||||||
|
"resolved": "https://registry.npmjs.org/lodash.debounce/-/lodash.debounce-4.0.8.tgz",
|
||||||
|
"integrity": "sha1-gteb/zCmfEAF/9XiUVMArZyk168=",
|
||||||
|
"dev": true
|
||||||
|
},
|
||||||
"lodash.defaults": {
|
"lodash.defaults": {
|
||||||
"version": "4.2.0",
|
"version": "4.2.0",
|
||||||
"resolved": "https://registry.npmjs.org/lodash.defaults/-/lodash.defaults-4.2.0.tgz",
|
"resolved": "https://registry.npmjs.org/lodash.defaults/-/lodash.defaults-4.2.0.tgz",
|
||||||
|
|||||||
@@ -31,6 +31,7 @@
|
|||||||
"grunt-mocha-test": "^0.13.3",
|
"grunt-mocha-test": "^0.13.3",
|
||||||
"jsdoc": "^3.6.4",
|
"jsdoc": "^3.6.4",
|
||||||
"jsdoc-babel": "^0.5.0",
|
"jsdoc-babel": "^0.5.0",
|
||||||
|
"lodash.debounce": "^4.0.8",
|
||||||
"minifyify": "^7.3.5",
|
"minifyify": "^7.3.5",
|
||||||
"minimist": "^1.2.5",
|
"minimist": "^1.2.5",
|
||||||
"mocha": "^8.0.1",
|
"mocha": "^8.0.1",
|
||||||
|
|||||||
147
src/BaseAPI.js
147
src/BaseAPI.js
@@ -4,6 +4,7 @@ import {ValidationError} from './exceptions';
|
|||||||
import ErrorCodes from './constants/error_codes';
|
import ErrorCodes from './constants/error_codes';
|
||||||
import APIConstants from './constants/api_constants';
|
import APIConstants from './constants/api_constants';
|
||||||
import {unflatten} from './utilities';
|
import {unflatten} from './utilities';
|
||||||
|
import debounce from 'lodash.debounce';
|
||||||
|
|
||||||
const global_constants = APIConstants.global;
|
const global_constants = APIConstants.global;
|
||||||
const scorm12_error_codes = ErrorCodes.scorm12;
|
const scorm12_error_codes = ErrorCodes.scorm12;
|
||||||
@@ -920,31 +921,6 @@ 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
|
||||||
@@ -952,71 +928,76 @@ export default class BaseAPI {
|
|||||||
* @return {object}
|
* @return {object}
|
||||||
*/
|
*/
|
||||||
processHttpRequest(url: String, params) {
|
processHttpRequest(url: String, params) {
|
||||||
const debounced = this._debounce(
|
const process = function(url, params, settings, error_codes) {
|
||||||
function(url, params, settings, error_codes) {
|
const genericError = {
|
||||||
const genericError = {
|
'result': global_constants.SCORM_FALSE,
|
||||||
'result': global_constants.SCORM_FALSE,
|
'errorCode': error_codes.GENERAL,
|
||||||
'errorCode': error_codes.GENERAL,
|
};
|
||||||
};
|
|
||||||
|
|
||||||
let result;
|
let result;
|
||||||
if (!settings.sendBeaconCommit) {
|
if (!settings.sendBeaconCommit) {
|
||||||
const httpReq = new XMLHttpRequest();
|
const httpReq = new XMLHttpRequest();
|
||||||
httpReq.open('POST', url, 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',
|
||||||
'application/x-www-form-urlencoded');
|
'application/x-www-form-urlencoded');
|
||||||
httpReq.send(params.join('&'));
|
httpReq.send(params.join('&'));
|
||||||
} else {
|
|
||||||
httpReq.setRequestHeader('Content-Type',
|
|
||||||
settings.commitRequestDataType);
|
|
||||||
httpReq.send(JSON.stringify(params));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof settings.responseHandler === 'function') {
|
|
||||||
result = settings.responseHandler(httpReq);
|
|
||||||
} else {
|
|
||||||
result = JSON.parse(httpReq.responseText);
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e);
|
|
||||||
return genericError;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
try {
|
httpReq.setRequestHeader('Content-Type',
|
||||||
const headers = {
|
settings.commitRequestDataType);
|
||||||
type: settings.commitRequestDataType,
|
httpReq.send(JSON.stringify(params));
|
||||||
};
|
|
||||||
let blob;
|
|
||||||
if (params instanceof Array) {
|
|
||||||
blob = new Blob([params.join('&')], headers);
|
|
||||||
} else {
|
|
||||||
blob = new Blob([JSON.stringify(params)], headers);
|
|
||||||
}
|
|
||||||
|
|
||||||
result = {};
|
|
||||||
if (navigator.sendBeacon(url, blob)) {
|
|
||||||
result.result = global_constants.SCORM_TRUE;
|
|
||||||
result.errorCode = 0;
|
|
||||||
} else {
|
|
||||||
result.result = global_constants.SCORM_FALSE;
|
|
||||||
result.errorCode = 101;
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e);
|
|
||||||
return genericError;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof result === 'undefined') {
|
if (typeof settings.responseHandler === 'function') {
|
||||||
return genericError;
|
result = settings.responseHandler(httpReq);
|
||||||
|
} else {
|
||||||
|
result = JSON.parse(httpReq.responseText);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
return genericError;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
const headers = {
|
||||||
|
type: settings.commitRequestDataType,
|
||||||
|
};
|
||||||
|
let blob;
|
||||||
|
if (params instanceof Array) {
|
||||||
|
blob = new Blob([params.join('&')], headers);
|
||||||
|
} else {
|
||||||
|
blob = new Blob([JSON.stringify(params)], headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
result = {};
|
||||||
}, 5000);
|
if (navigator.sendBeacon(url, blob)) {
|
||||||
|
result.result = global_constants.SCORM_TRUE;
|
||||||
|
result.errorCode = 0;
|
||||||
|
} else {
|
||||||
|
result.result = global_constants.SCORM_FALSE;
|
||||||
|
result.errorCode = 101;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
return genericError;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return debounced(url, params, this.settings, this.error_codes);
|
if (typeof result === 'undefined') {
|
||||||
|
return genericError;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
|
if (typeof debounce !== 'undefined') {
|
||||||
|
const debounced = debounce(process, 5000);
|
||||||
|
|
||||||
|
return debounced(url, params, this.settings, this.error_codes);
|
||||||
|
} else {
|
||||||
|
return process(url, params, this.settings, this.error_codes);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user