Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Make sendState call async (Issue/134) #135

Merged
merged 14 commits into from
Jan 20, 2025
Merged
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions js/XAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class XAPI extends Backbone.Model {
components: 'component',
offlineStorage: 'offlineStorage'
};

this.sendState = _.debounce(this.sendState.bind(this), 500);
oliverfoster marked this conversation as resolved.
Show resolved Hide resolved
}

/** Implementation starts here */
Expand Down Expand Up @@ -1086,10 +1088,9 @@ class XAPI extends Backbone.Model {
* Sends the state to the or the given model to the configured LRS.
* @param {AdaptModel} model - The AdaptModel whose state has changed.
*/
sendState(model, modelState) {
if (this.get('shouldTrackState') !== true || model.get('_isTrackable') === false) {
return;
}
async sendState(model, modelState) {
if (this.get('shouldTrackState') !== true) return;
if (model.get('_isTrackable') === false) return;

const activityId = this.get('activityId');
const actor = this.get('actor');
Expand All @@ -1102,7 +1103,7 @@ class XAPI extends Backbone.Model {
return (o === type || o.indexOf(type) > -1);
});
const stateCollection = Array.isArray(state[collectionName]) ? state[collectionName] : [];
let newState;
let newState = modelState;

if (collectionName !== 'course' && collectionName !== 'offlineStorage') {
const index = _.findIndex(stateCollection, { _id: model.get('_id') });
Expand All @@ -1114,8 +1115,6 @@ class XAPI extends Backbone.Model {
}

newState = stateCollection;
} else {
newState = modelState;
}

// Update the locally held state.
Expand All @@ -1124,7 +1123,7 @@ class XAPI extends Backbone.Model {
state
});

// Pass the new state to the LRS.
// Pass the new state to the LRS
this.xapiWrapper.sendState(activityId, actor, collectionName, registration, newState, null, null, (error, xhr) => {
if (error) {
Adapt.trigger('xapi:lrs:sendState:error', error);
Expand Down Expand Up @@ -1360,9 +1359,7 @@ class XAPI extends Backbone.Model {
* @param {array} [attachments] - An array of attachments to pass to the LRS.
*/
async sendStatement(statement, attachments = null) {
if (!statement) {
return;
}
if (!statement) return;

Adapt.trigger('xapi:preSendStatement', statement);

Expand Down Expand Up @@ -1516,9 +1513,7 @@ class XAPI extends Backbone.Model {
* @param {ADL.XAPIStatement[]} statements - An array of valid ADL.XAPIStatement objects.
*/
async sendStatements(statements) {
if (!statements || statements.length === 0) {
return;
}
if (!statements || statements.length === 0) return;
cahirodoherty-learningpool marked this conversation as resolved.
Show resolved Hide resolved

Adapt.trigger('xapi:preSendStatements', statements);

Expand Down