Skip to content

Commit 937c3ce

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 c9d0389 commit 937c3ce

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

app/assets/javascripts/editor.coffee

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ initializeAce = ->
66
$(document).on 'fields_added.nested_form_fields', ->
77
$ initializeEditors()
88

9+
unloadEditors = (event) ->
10+
$('.editor').each (_, editor) ->
11+
aceEditor = ace.edit(editor)
12+
content = aceEditor.getValue();
13+
aceEditor.destroy()
14+
editor.textContent = content;
15+
return
16+
917
initializeEditors = ->
1018
$('.editor').each ->
1119
editor = ace.edit(this)
@@ -17,6 +25,8 @@ initializeEditors = ->
1725
else
1826
editor.getSession().on 'change', (e) ->
1927
hiddenContent.val(editor.getValue()).trigger('change')
28+
$(document).one('turbo:visit', unloadEditors);
29+
$(window).one('beforeunload', unloadEditors);
2030

2131
setAceEditorValue = (editor, value) ->
2232
aceEditor = $(editor).parent().find('.editor')[0]

0 commit comments

Comments
 (0)