Adding async processing of commits

This commit is contained in:
Jonathan Putney
2020-06-29 15:43:10 -04:00
parent e56ae12d17
commit 075dff8eec
4 changed files with 92 additions and 52 deletions
+30 -9
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
+26 -7
View File
@@ -18,6 +18,8 @@ export default class BaseAPI {
#settings = { #settings = {
autocommit: false, autocommit: false,
autocommitSeconds: 10, autocommitSeconds: 10,
asyncCommit: false,
sendBeaconCommit: false,
lmsCommitUrl: false, lmsCommitUrl: false,
dataCommitFormat: 'json', // valid formats are 'json' or 'flattened', 'params' dataCommitFormat: 'json', // valid formats are 'json' or 'flattened', 'params'
commitRequestDataType: 'application/json;charset=UTF-8', commitRequestDataType: 'application/json;charset=UTF-8',
@@ -922,8 +924,10 @@ export default class BaseAPI {
'errorCode': this.#error_codes.GENERAL, 'errorCode': this.#error_codes.GENERAL,
}; };
let result;
if (!this.#settings.sendBeaconCommit) {
const httpReq = new XMLHttpRequest(); const httpReq = new XMLHttpRequest();
httpReq.open('POST', url, false); httpReq.open('POST', url, this.#settings.asyncCommit);
try { try {
if (params instanceof Array) { if (params instanceof Array) {
httpReq.setRequestHeader('Content-Type', httpReq.setRequestHeader('Content-Type',
@@ -934,13 +938,7 @@ export default class BaseAPI {
this.settings.commitRequestDataType); this.settings.commitRequestDataType);
httpReq.send(JSON.stringify(params)); httpReq.send(JSON.stringify(params));
} }
} catch (e) {
console.error(e);
return genericError;
}
let result;
try {
if (typeof this.settings.responseHandler === 'function') { if (typeof this.settings.responseHandler === 'function') {
result = this.settings.responseHandler(httpReq); result = this.settings.responseHandler(httpReq);
} else { } else {
@@ -950,6 +948,27 @@ export default class BaseAPI {
console.error(e); console.error(e);
return genericError; return genericError;
} }
} else {
try {
let beacon;
if (params instanceof Array) {
beacon = navigator.sendBeacon(url, params.join('&'));
} else {
beacon = navigator.sendBeacon(url, params);
}
result = {};
if (beacon) {
result.result = global_constants.SCORM_TRUE;
} else {
result.result = global_constants.SCORM_FALSE;
result.errorCode = 101;
}
} catch (e) {
console.error(e);
return genericError;
}
}
if (typeof result === 'undefined') { if (typeof result === 'undefined') {
return genericError; return genericError;