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

Wait to avoid overlapping state saves #56

Merged
merged 1 commit into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
110 changes: 26 additions & 84 deletions packages/doenetml-worker/src/Core.js
Original file line number Diff line number Diff line change
Expand Up @@ -12800,20 +12800,28 @@ export default class Core {
this.saveChangesToDatabase();
}, 60000);

// TODO: find out how to test if not online
// and send this alert if not online:

// postMessage({
// messageType: "sendAlert",
// coreId: this.coreId,
// args: {
// message: "You're not connected to the internet. Changes are not saved. ",
// alertType: "error"
// }
// })
let pause100 = function () {
return new Promise((resolve, reject) => {
setTimeout(resolve, 100);
});
};

if (this.savingPageState) {
for (let i = 0; i < 100; i++) {
await pause100();

if (!this.savingPageState) {
break;
}
}
}

this.pageStateToBeSavedToDatabase.serverSaveId = this.serverSaveId;

let resp;

this.savingPageState = true;

try {
resp = await axios.post(
this.apiURLs.savePageState,
Expand All @@ -12824,26 +12832,26 @@ export default class Core {
messageType: "sendAlert",
coreId: this.coreId,
args: {
message:
"Error synchronizing data. Changes not saved to the server.",
message: `Error. Latest changes not saved. ${resp.data.message}`,
alertType: "error",
id: "dataError",
},
});

this.failedToSavePageState = true;
this.savingPageState = false;

return;
}

console.log("result from saving to database:", resp.data);
this.savingPageState = false;

if (resp.status === null) {
postMessage({
messageType: "sendAlert",
coreId: this.coreId,
args: {
message: `Error synchronizing data. Changes not saved to the server. Are you connected to the internet?`,
message: `Error. Latest changes not saved. Are you connected to the internet?`,
alertType: "error",
id: "dataError",
},
Expand All @@ -12861,7 +12869,7 @@ export default class Core {
messageType: "sendAlert",
coreId: this.coreId,
args: {
message: data.message,
message: `Error. Latest changes not saved. ${data.message}`,
alertType: "error",
id: "dataError",
},
Expand Down Expand Up @@ -12949,8 +12957,6 @@ export default class Core {
itemNumber: this.itemNumber,
};

// console.log("payload for save credit for item", payload);

axios
.post(this.apiURLs.saveCreditForItem, payload)
.then((resp) => {
Expand All @@ -12961,7 +12967,7 @@ export default class Core {
messageType: "sendAlert",
coreId: this.coreId,
args: {
message: `Credit not saved due to error. Are you connected to the internet?`,
message: `Credit not saved due to error. Are you connected to the internet?`,
alertType: "error",
id: "creditDataError",
},
Expand All @@ -12973,7 +12979,7 @@ export default class Core {
messageType: "sendAlert",
coreId: this.coreId,
args: {
message: `Credit not saved due to error: ${resp.data.message}`,
message: resp.data.message,
alertType: "error",
id: "creditDataError",
},
Expand All @@ -13000,70 +13006,6 @@ export default class Core {
});

this.failedToSaveCreditForItem = false;

//TODO: need type warning (red but doesn't hang around)
if (data.viewedSolution) {
postMessage({
messageType: "sendAlert",
coreId: this.coreId,
args: {
message:
"No credit awarded since solution was viewed.",
alertType: "info",
id: "solutionViewed",
},
});
}
if (data.timeExpired) {
postMessage({
messageType: "sendAlert",
coreId: this.coreId,
args: {
message:
"No credit awarded since the time allowed has expired.",
alertType: "info",
id: "timeExpired",
},
});
}
if (data.pastDueDate) {
postMessage({
messageType: "sendAlert",
coreId: this.coreId,
args: {
message:
"No credit awarded since the due date has passed.",
alertType: "info",
id: "pastDue",
},
});
}
if (data.exceededAttemptsAllowed) {
postMessage({
messageType: "sendAlert",
coreId: this.coreId,
args: {
message:
"No credit awarded since no more attempts are allowed.",
alertType: "info",
id: "noMoreAttempts",
},
});
}
if (data.databaseError) {
postMessage({
messageType: "sendAlert",
coreId: this.coreId,
args: {
message:
"Credit not saved due to database error.",
alertType: "error",
id: "creditDataError",
},
});

this.failedToSaveCreditForItem = true;
}
}
})
.catch((e) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/doenetml/src/DoenetML.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export function DoenetML({
forceShowSolution,
forceUnsuppressCheckwork,
addVirtualKeyboard = true,
addBottomPadding = true,
addBottomPadding = false,
location,
navigate,
updateDataOnContentChange = false,
Expand Down
33 changes: 24 additions & 9 deletions packages/doenetml/src/Viewer/ActivityViewer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ export function ActivityViewer({
const currentPageRef = useRef(currentPage); // so that event listener can get new current page
currentPageRef.current = currentPage; // so updates on every refresh

const savingActivityState = useRef(false);

const [activityAttemptNumberSetUp, setActivityAttemptNumberSetUp] =
useState(0);

Expand Down Expand Up @@ -717,10 +719,6 @@ export function ActivityViewer({
let resp;

try {
console.log(
"first one saveActivityState activityStateToBeSavedToDatabase",
activityStateToBeSavedToDatabase,
);
resp = await axios.post(
apiURLs.saveActivityState,
activityStateToBeSavedToDatabase,
Expand Down Expand Up @@ -868,13 +866,28 @@ export function ActivityViewer({
// TODO: find out how to test if not online
// and send this sendAlert if not online:

let pause100 = function () {
return new Promise((resolve, reject) => {
setTimeout(resolve, 100);
});
};

if (savingActivityState.current) {
for (let i = 0; i < 100; i++) {
await pause100();

if (!savingActivityState.current) {
break;
}
}
}

activityStateToBeSavedToDatabase.current.serverSaveId =
serverSaveId.current;

let resp;

try {
console.log(
"activity state params",
activityStateToBeSavedToDatabase.current,
);
resp = await axios.post(
apiURLs.saveActivityState,
activityStateToBeSavedToDatabase.current,
Expand All @@ -887,10 +900,12 @@ export function ActivityViewer({
"Error synchronizing data. Changes not saved to the server.",
"error",
);

savingActivityState.current = false;
return;
}

console.log("result from saving activity to database:", resp.data);
savingActivityState.current = false;

if (resp.status === null) {
console.log(
Expand Down