Skip to content

Commit

Permalink
Improve types & avoid callbacks when rendering the editor
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdima committed Feb 6, 2020
1 parent 18c10b1 commit 1bce32d
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions src/vs/editor/browser/config/elementSizeObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,27 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { Disposable } from 'vs/base/common/lifecycle';
import { Disposable, IDisposable } from 'vs/base/common/lifecycle';
import { IDimension } from 'vs/editor/common/editorCommon';
import * as dom from 'vs/base/browser/dom';

export class ElementSizeObserver extends Disposable {

private readonly referenceDomElement: HTMLElement | null;
private readonly changeCallback: () => void;
private width: number;
private height: number;
private resizeObserver: any;
private mutationObserver: MutationObserver | null;
private windowSizeListener: IDisposable | null;

constructor(referenceDomElement: HTMLElement | null, dimension: IDimension | undefined, changeCallback: () => void) {
super();
this.referenceDomElement = referenceDomElement;
this.changeCallback = changeCallback;
this.width = -1;
this.height = -1;
this.resizeObserver = null;
this.mutationObserver = null;
this.windowSizeListener = null;
this.measureReferenceDomElement(false, dimension);
}

Expand All @@ -38,35 +41,37 @@ export class ElementSizeObserver extends Disposable {
}

public startObserving(): void {
if (this.resizeObserver === null) {
this.resizeObserver = new MutationObserver(this.mutationObserve.bind(this));
this.resizeObserver.observe(this.referenceDomElement, {
if (!this.mutationObserver && this.referenceDomElement) {
this.mutationObserver = new MutationObserver(() => this._onDidMutate());
this.mutationObserver.observe(this.referenceDomElement, {
attributes: true,
childList: true,
characterData: true,
subtree: true
});
window.addEventListener('resize', this.windowObserve.bind(this));
}
if (!this.windowSizeListener) {
this.windowSizeListener = dom.addDisposableListener(window, 'resize', () => this._onDidResizeWindow());
}
}

public stopObserving(): void {
if (this.resizeObserver !== null) {
this.resizeObserver.disconnect();
this.resizeObserver = null;
window.removeEventListener('resize', this.windowObserve.bind(this));
if (this.mutationObserver) {
this.mutationObserver.disconnect();
this.mutationObserver = null;
}
if (this.windowSizeListener) {
this.windowSizeListener.dispose();
this.windowSizeListener = null;
}
}

public observe(dimension?: IDimension): void {
this.measureReferenceDomElement(true, dimension);
}

private mutationObserve(mutations: MutationRecord[], observer: MutationObserver): void {
private _onDidMutate(): void {
this.measureReferenceDomElement(true);
}

private windowObserve(): void {
private _onDidResizeWindow(): void {
this.measureReferenceDomElement(true);
}

Expand Down

0 comments on commit 1bce32d

Please sign in to comment.