Adding async processing of commits
This commit is contained in:
71
dist/scorm-again.js
vendored
71
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 = {
|
#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,33 +924,50 @@ export default class BaseAPI {
|
|||||||
'errorCode': this.#error_codes.GENERAL,
|
'errorCode': this.#error_codes.GENERAL,
|
||||||
};
|
};
|
||||||
|
|
||||||
const httpReq = new XMLHttpRequest();
|
|
||||||
httpReq.open('POST', url, false);
|
|
||||||
try {
|
|
||||||
if (params instanceof Array) {
|
|
||||||
httpReq.setRequestHeader('Content-Type',
|
|
||||||
'application/x-www-form-urlencoded');
|
|
||||||
httpReq.send(params.join('&'));
|
|
||||||
} else {
|
|
||||||
httpReq.setRequestHeader('Content-Type',
|
|
||||||
this.settings.commitRequestDataType);
|
|
||||||
httpReq.send(JSON.stringify(params));
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e);
|
|
||||||
return genericError;
|
|
||||||
}
|
|
||||||
|
|
||||||
let result;
|
let result;
|
||||||
try {
|
if (!this.#settings.sendBeaconCommit) {
|
||||||
if (typeof this.settings.responseHandler === 'function') {
|
const httpReq = new XMLHttpRequest();
|
||||||
result = this.settings.responseHandler(httpReq);
|
httpReq.open('POST', url, this.#settings.asyncCommit);
|
||||||
} else {
|
try {
|
||||||
result = JSON.parse(httpReq.responseText);
|
if (params instanceof Array) {
|
||||||
|
httpReq.setRequestHeader('Content-Type',
|
||||||
|
'application/x-www-form-urlencoded');
|
||||||
|
httpReq.send(params.join('&'));
|
||||||
|
} else {
|
||||||
|
httpReq.setRequestHeader('Content-Type',
|
||||||
|
this.settings.commitRequestDataType);
|
||||||
|
httpReq.send(JSON.stringify(params));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof this.settings.responseHandler === 'function') {
|
||||||
|
result = this.settings.responseHandler(httpReq);
|
||||||
|
} else {
|
||||||
|
result = JSON.parse(httpReq.responseText);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
} catch (e) {
|
|
||||||
console.error(e);
|
|
||||||
return genericError;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof result === 'undefined') {
|
if (typeof result === 'undefined') {
|
||||||
|
|||||||
Reference in New Issue
Block a user