Ensure to clear timeout in autosave

We are still trying to ensure autosave is not called outside the /implement route.

Relates to CODEOCEAN-G1
This commit is contained in:
Sebastian Serth
2023-02-28 12:31:22 +01:00
parent 6c1bfb6815
commit 252704b083
2 changed files with 11 additions and 10 deletions

View File

@ -988,8 +988,8 @@ var CodeOceanEditor = {
this.initializeDeadlines(); this.initializeDeadlines();
CodeOceanEditorTips.initializeEventHandlers(); CodeOceanEditorTips.initializeEventHandlers();
window.addEventListener("turbolinks:before-render", this.unloadAutoSave.bind(this)); window.addEventListener("turbolinks:before-render", this.autosaveIfChanged.bind(this));
window.addEventListener("beforeunload", this.unloadAutoSave.bind(this)); window.addEventListener("beforeunload", this.autosaveIfChanged.bind(this));
// create autosave when the editor is opened the first time // create autosave when the editor is opened the first time
this.autosave(); this.autosave();
} }

View File

@ -204,7 +204,7 @@ CodeOceanEditorSubmissions = {
const button = $(event.target) || $('#submit'); const button = $(event.target) || $('#submit');
this.createSubmission(button, null, function (response) { this.createSubmission(button, null, function (response) {
if (response.redirect) { if (response.redirect) {
this.unloadAutoSave(); this.autosaveIfChanged();
this.editors = []; this.editors = [];
Turbolinks.clearCache(); Turbolinks.clearCache();
Turbolinks.visit(response.redirect); Turbolinks.visit(response.redirect);
@ -228,13 +228,6 @@ CodeOceanEditorSubmissions = {
this.autosaveTimer = setTimeout(this.autosave.bind(this), this.AUTOSAVE_INTERVAL); this.autosaveTimer = setTimeout(this.autosave.bind(this), this.AUTOSAVE_INTERVAL);
}, },
unloadAutoSave: function() {
if(this.autosaveTimer != null){
clearTimeout(this.autosaveTimer);
this.autosave();
}
},
updateSaveStateLabel: function() { updateSaveStateLabel: function() {
var date = new Date(); var date = new Date();
var autosaveLabel = $(this.autosaveLabel); var autosaveLabel = $(this.autosaveLabel);
@ -243,7 +236,15 @@ CodeOceanEditorSubmissions = {
autosaveLabel.text(date.toLocaleTimeString()); autosaveLabel.text(date.toLocaleTimeString());
}, },
autosaveIfChanged: function() {
// Only save if the user has changed the code in the meantime (represented by an active timer)
if(this.autosaveTimer != null){
this.autosave();
}
},
autosave: function () { autosave: function () {
clearTimeout(this.autosaveTimer);
this.autosaveTimer = null; this.autosaveTimer = null;
this.createSubmission($('#autosave'), null); this.createSubmission($('#autosave'), null);
} }