Skip to content

Commit 57d1253

Browse files
committed
Improvement: Avoid unnecessary angular digests on changes
We'll use eval async to run in the current or next and only try to do this if the content is different from the last seen content
1 parent 30434e2 commit 57d1253

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

src/tinymce.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,24 @@ angular.module('ui.tinymce', [])
1717
if (!$window.tinymce) {
1818
return;
1919
}
20-
2120
var ngModel = ctrls[0],
2221
form = ctrls[1] || null;
2322

23+
var lastSeenContent = null;
2424
var expression, options = {
2525
debounce: true
2626
}, tinyInstance,
2727
updateView = function(editor) {
28+
// don't jump into any digest if the content is the same
2829
var content = editor.getContent({format: options.format}).trim();
29-
content = $sce.trustAsHtml(content);
30-
31-
ngModel.$setViewValue(content);
32-
if (!$rootScope.$$phase) {
33-
scope.$digest();
30+
if (lastSeenContent === content) {
31+
return;
3432
}
33+
lastSeenContent = content;
34+
content = $sce.trustAsHtml(content);
35+
scope.$evalAsync(function() {
36+
ngModel.$setViewValue(content);
37+
});
3538
};
3639

3740
function toggleDisable(disabled) {
@@ -64,10 +67,10 @@ angular.module('ui.tinymce', [])
6467
return function(ed) {
6568
$timeout.cancel(debouncedUpdateTimer);
6669
debouncedUpdateTimer = $timeout(function() {
67-
return (function(ed) {
68-
if (ed.isDirty()) {
69-
ed.save();
70-
updateView(ed);
70+
return (function(debouncedEditor) {
71+
if (debouncedEditor.isDirty()) {
72+
debouncedEditor.save();
73+
updateView(debouncedEditor);
7174
}
7275
})(ed);
7376
}, debouncedUpdateDelay);
@@ -102,11 +105,10 @@ angular.module('ui.tinymce', [])
102105
});
103106

104107
ed.on('blur', function() {
105-
element[0].blur();
106-
ngModel.$setTouched();
107-
if (!$rootScope.$$phase) {
108-
scope.$digest();
109-
}
108+
scope.$evalAsync(function() {
109+
element[0].blur();
110+
ngModel.$setTouched();
111+
});
110112
});
111113

112114
ed.on('remove', function() {

0 commit comments

Comments
 (0)