Skip to content

Commit

Permalink
Drop mutation observer during rerender
Browse files Browse the repository at this point in the history
  • Loading branch information
mixonic committed Dec 14, 2015
1 parent a30897b commit 26d3d78
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions src/js/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ class Editor {
DEFAULT_TEXT_EXPANSIONS.forEach(e => this.registerExpansion(e));
DEFAULT_KEY_COMMANDS.forEach(kc => this.registerKeyCommand(kc));

this._mutationObserver = new MutationObserver(() => {
this.handleInput();
});
this._isMutationObserved = false;
this._parser = new DOMParser(this.builder);
this._renderer = new Renderer(this, this.cards, this.unknownCardHandler, this.cardOptions);

Expand Down Expand Up @@ -136,7 +140,9 @@ class Editor {
}

this.runCallbacks(CALLBACK_QUEUES.WILL_RENDER);
this.removeMutationObserver();
this._renderer.render(this._renderTree);
this.ensureMutationObserver();
this.runCallbacks(CALLBACK_QUEUES.DID_RENDER);
}

Expand Down Expand Up @@ -411,11 +417,28 @@ class Editor {
this._views = [];
}

ensureMutationObserver() {
if (!this._isMutationObserved) {
this._mutationObserver.observe(this.element, {
characterData: true,
childList: true,
subtree: true
});
this._isMutationObserved = true;
}
}

removeMutationObserver() {
if (this._isMutationObserved) {
this._mutationObserver.disconnect();
this._isMutationObserved = false;
}
}

destroy() {
this._isDestroyed = true;
if (this.mutationObserver) {
this.mutationObserver.disconnect();
}
this.removeMutationObserver();
this._mutationObserver = null;
this.removeAllEventListeners();
this.removeAllViews();
this._renderer.destroy();
Expand Down Expand Up @@ -551,15 +574,6 @@ class Editor {
}

_setupListeners() {
this.mutationObserver = new MutationObserver(() => {
this.handleInput();
});
this.mutationObserver.observe(this.element, {
characterData: true,
childList: true,
subtree: true
});

ELEMENT_EVENTS.forEach(eventName => {
this.addEventListener(this.element, eventName,
(...args) => this.handleEvent(eventName, ...args)
Expand Down

0 comments on commit 26d3d78

Please sign in to comment.