Skip to content

Commit 7547818

Browse files
committed
JS Performance: Unload and destroy ACE editor on navigation
With this change, each editor instance is destroyed upon changing the page. This prevents memory leaks and thus optimizes the performance. The editor will be restored upon visiting the sites again.
1 parent c48653a commit 7547818

File tree

5 files changed

+58
-9
lines changed

5 files changed

+58
-9
lines changed

app/assets/javascripts/community_solution.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ $(document).on('turbo-migration:load', function() {
2525
}
2626
});
2727

28+
$(document).one('turbo-migration:load', function() {
29+
if ($.isController('community_solutions') && $('#community-solution-editor').isPresent()) {
30+
$(document).one('turbo:visit', unloadEditorHandler);
31+
$(window).one('beforeunload', unloadEditorHandler);
32+
}
33+
});
34+
35+
function unloadEditorHandler() {
36+
CodeOceanEditor.autosaveIfChanged();
37+
CodeOceanEditor.unloadEditor();
38+
}
39+
2840
function submitCode(event) {
2941
const button = $(event.target) || $('#submit');
3042
this.newSentryTransaction(button, async () => {
@@ -35,10 +47,7 @@ function submitCode(event) {
3547
if (!submission) return;
3648
if (!submission.redirect) return;
3749

38-
this.autosaveIfChanged();
39-
await this.stopCode(event);
40-
this.editors = [];
41-
Turbo.cache.clear();
50+
unloadEditorHandler();
4251
Turbo.visit(submission.redirect);
4352
});
4453
}

app/assets/javascripts/editor/editor.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,9 +1111,14 @@ var CodeOceanEditor = {
11111111
unloadEverything: function () {
11121112
App.synchronized_editor?.disconnect();
11131113
this.autosaveIfChanged();
1114-
this.cacheEditorContent();
1114+
this.unloadEditor();
11151115
this.teardownEventHandlers();
1116-
this.destroyEditors();
1116+
},
1117+
1118+
unloadEditor: function () {
1119+
$(document).off('theme:change:ace');
1120+
CodeOceanEditor.cacheEditorContent();
1121+
CodeOceanEditor.destroyEditors();
11171122
},
11181123

11191124
cacheEditorContent: function () {

app/assets/javascripts/exercises.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ $(document).on('turbo-migration:load', function () {
3434
session.setTabSize(element_selector.data('indent-size'));
3535
session.setUseSoftTabs(true);
3636
session.setUseWrapMode(true);
37+
38+
function unloadTeacherEditor() {
39+
$(document).off('theme:change:ace');
40+
const value = editor.getValue();
41+
editor.destroy();
42+
// "Restore" the editor's content to the original element for caching.
43+
textarea.val = value;
44+
}
45+
46+
$(document).one('turbo:visit', unloadTeacherEditor);
47+
$(window).one('beforeunload', unloadTeacherEditor);
3748
}
3849

3950
const handleAceThemeChangeEvent = function(_event) {
@@ -42,12 +53,11 @@ $(document).on('turbo-migration:load', function () {
4253
}.bind(this));
4354
};
4455

45-
$(document).on('theme:change:ace', handleAceThemeChangeEvent.bind(this));
46-
4756
var initializeEditors = function () {
4857
// initialize ace editors for all code textareas in the dom except the last one. The last one is the dummy area for new files, which is cloned when needed.
4958
// this one must NOT! be initialized.
5059
$('.editor:not(:last)').each(initializeEditor)
60+
$(document).on('theme:change:ace', handleAceThemeChangeEvent.bind(this));
5161
};
5262

5363
var addFileForm = function (event) {

app/assets/javascripts/request_for_comments.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,4 +394,18 @@ $(document).on('turbo-migration:load', function () {
394394
showPermanent: response.status === 422,
395395
});
396396
}
397+
398+
function unloadRfCEditors() {
399+
$(document).off('theme:change:ace');
400+
$('.editor').each(function (_, editor) {
401+
const aceEditor = ace.edit(editor);
402+
const value = aceEditor.getValue();
403+
aceEditor.destroy();
404+
// "Restore" the editor's content to the original element for caching.
405+
editor.textContent = value;
406+
});
407+
}
408+
409+
$(document).one('turbo:visit', unloadRfCEditors);
410+
$(window).one('beforeunload', unloadRfCEditors);
397411
});

app/assets/javascripts/submission_statistics.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ $(document).on('turbo-migration:load', function(event) {
110110

111111
slider.on('change', onSliderChange);
112112

113-
stopReplay = function() {
113+
const stopReplay = function() {
114114
clearInterval(playInterval);
115115
playInterval = undefined;
116116
playButton.find('span.fa-solid').removeClass('fa-pause').addClass('fa-play')
@@ -145,6 +145,17 @@ $(document).on('turbo-migration:load', function(event) {
145145
// Start with newest submission
146146
slider.val(submissions.length - 1);
147147
onSliderChange();
148+
149+
const unloadSubmissionStatistics = function() {
150+
if (playInterval) {
151+
stopReplay();
152+
}
153+
$(document).off('theme:change:ace');
154+
editor.destroy();
155+
}
156+
157+
$(document).on('turbo:visit', unloadSubmissionStatistics);
158+
$(window).on('beforeunload', unloadSubmissionStatistics);
148159
}
149160

150161
});

0 commit comments

Comments
 (0)