Adding async processing of commits
This commit is contained in:
39
dist/scorm-again.js
vendored
39
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
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user