Skip to content

Commit cda8130

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 7547818 commit cda8130

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

app/assets/javascripts/editor/editor.js

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

424448
if (jsTree) {
425449
const newColorScheme = event.detail.currentTheme;
426450
// Update the JStree theme
427451
jsTree?.set_theme(newColorScheme === "dark" ? "default-dark" : "default");
428452
}
429-
});
453+
}
430454
},
431455

432456
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)