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

39
dist/scorm-again.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -18,6 +18,8 @@ export default class BaseAPI {
#settings = {
autocommit: false,
autocommitSeconds: 10,
asyncCommit: false,
sendBeaconCommit: false,
lmsCommitUrl: false,
dataCommitFormat: 'json', // valid formats are 'json' or 'flattened', 'params'
commitRequestDataType: 'application/json;charset=UTF-8',
@@ -922,8 +924,10 @@ export default class BaseAPI {
'errorCode': this.#error_codes.GENERAL,
};
let result;
if (!this.#settings.sendBeaconCommit) {
const httpReq = new XMLHttpRequest();
httpReq.open('POST', url, false);
httpReq.open('POST', url, this.#settings.asyncCommit);
try {
if (params instanceof Array) {
httpReq.setRequestHeader('Content-Type',
@@ -934,13 +938,7 @@ export default class BaseAPI {
this.settings.commitRequestDataType);
httpReq.send(JSON.stringify(params));
}
} catch (e) {
console.error(e);
return genericError;
}
let result;
try {
if (typeof this.settings.responseHandler === 'function') {
result = this.settings.responseHandler(httpReq);
} else {
@@ -950,6 +948,27 @@ export default class BaseAPI {
console.error(e);
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') {
return genericError;