From b5cd4f14923ac482b2fd4f8a8a63456b128303c6 Mon Sep 17 00:00:00 2001 From: Daniel Imms <2193314+Tyriar@users.noreply.github.com> Date: Thu, 27 Oct 2022 16:50:45 -0700 Subject: [PATCH] Cache ICoreBrowserService.isFocused per task This is called for every selected cell when rendering which ends up consuming a bunch of CPU due to the DOM calls. --- src/browser/services/CoreBrowserService.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/browser/services/CoreBrowserService.ts b/src/browser/services/CoreBrowserService.ts index 6504e5e4..e992f255 100644 --- a/src/browser/services/CoreBrowserService.ts +++ b/src/browser/services/CoreBrowserService.ts @@ -8,10 +8,15 @@ import { ICoreBrowserService } from './Services'; export class CoreBrowserService implements ICoreBrowserService { public serviceBrand: undefined; + private _isFocused = false; + private _cachedIsFocused: boolean | undefined = undefined; + constructor( private _textarea: HTMLTextAreaElement, public readonly window: Window & typeof globalThis ) { + this._textarea.addEventListener('focus', () => this._isFocused = true); + this._textarea.addEventListener('blur', () => this._isFocused = false); } public get dpr(): number { @@ -19,7 +24,10 @@ export class CoreBrowserService implements ICoreBrowserService { } public get isFocused(): boolean { - const docOrShadowRoot = this._textarea.getRootNode ? this._textarea.getRootNode() as Document | ShadowRoot : this._textarea.ownerDocument; - return docOrShadowRoot.activeElement === this._textarea && this._textarea.ownerDocument.hasFocus(); + if (this._cachedIsFocused === undefined) { + this._cachedIsFocused = this._isFocused && this._textarea.ownerDocument.hasFocus(); + queueMicrotask(() => this._cachedIsFocused = undefined); + } + return this._cachedIsFocused; } }