Skip to content

Commit ddc0a84

Browse files
committed
JS Performance: Unload JsTree on navigation
Similar to the ACE editor, we unload the JsTree to prevent memory leaks. Also, we use this opportunity to reduce code duplication.
1 parent e271898 commit ddc0a84

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

app/assets/javascripts/editor/editor.js.erb

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,15 +424,39 @@ var CodeOceanEditor = {
424424
this.showFrame(frame);
425425
this.toggleButtonStates();
426426
}.bind(this));
427-
$(document).on('theme:change', function(event) {
427+
428+
this.installFileTreeEventHandlers(filesInstance);
429+
},
430+
431+
installFileTreeEventHandlers: function (filesInstance) {
432+
// Prevent duplicate event listeners by removing them during unload.
433+
const themeListener = this.createFileTreeThemeChangeListener(filesInstance);
434+
const jsTree = filesInstance?.jstree(true);
435+
$(document).on('theme:change', themeListener);
436+
$(document).one('turbo:visit', function() {
437+
$(document).off('theme:change', themeListener);
438+
if (jsTree && jsTree.element) {
439+
jsTree.destroy(true);
440+
}
441+
});
442+
$(window).one('beforeunload', function() {
443+
$(document).off('theme:change', themeListener);
444+
if (jsTree && jsTree.element) {
445+
jsTree.destroy(true);
446+
}
447+
});
448+
},
449+
450+
createFileTreeThemeChangeListener: function (filesInstance) {
451+
return function (event) {
428452
const jsTree = filesInstance?.jstree(true);
429453

430454
if (jsTree) {
431455
const newColorScheme = event.detail.currentTheme;
432456
// Update the JStree theme
433457
jsTree?.set_theme(newColorScheme === "dark" ? "default-dark" : "default");
434458
}
435-
});
459+
}
436460
},
437461

438462
initializeFileTreeButtons: function () {

app/assets/javascripts/shell.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,7 @@ $(document).on('turbo-migration:load', function () {
133133
window.location = downloadPath;
134134
}
135135
}.bind(this));
136-
$(document).on('theme:change', function(event) {
137-
const newColorScheme = event.detail.currentTheme;
138-
// Update the JStree theme
139-
fileTree.jstree(true).set_theme(newColorScheme === "dark" ? "default-dark" : "default");
140-
});
136+
CodeOceanEditor.installFileTreeEventHandlers(fileTree);
141137
}
142138
}
143139

app/assets/javascripts/submission_statistics.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,7 @@ $(document).on('turbo-migration:load', function(event) {
4646
});
4747
showActiveFile();
4848
});
49-
$(document).on('theme:change', function(event) {
50-
const newColorScheme = event.detail.currentTheme;
51-
// Update the JStree theme
52-
fileTree.jstree(true).set_theme(newColorScheme === "dark" ? "default-dark" : "default");
53-
});
49+
CodeOceanEditor.installFileTreeEventHandlers(fileTree);
5450
fileTrees.push(fileTree);
5551
});
5652
};

0 commit comments

Comments
 (0)