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.
This commit is contained in:
@@ -8,10 +8,15 @@ import { ICoreBrowserService } from './Services';
|
|||||||
export class CoreBrowserService implements ICoreBrowserService {
|
export class CoreBrowserService implements ICoreBrowserService {
|
||||||
public serviceBrand: undefined;
|
public serviceBrand: undefined;
|
||||||
|
|
||||||
|
private _isFocused = false;
|
||||||
|
private _cachedIsFocused: boolean | undefined = undefined;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private _textarea: HTMLTextAreaElement,
|
private _textarea: HTMLTextAreaElement,
|
||||||
public readonly window: Window & typeof globalThis
|
public readonly window: Window & typeof globalThis
|
||||||
) {
|
) {
|
||||||
|
this._textarea.addEventListener('focus', () => this._isFocused = true);
|
||||||
|
this._textarea.addEventListener('blur', () => this._isFocused = false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public get dpr(): number {
|
public get dpr(): number {
|
||||||
@@ -19,7 +24,10 @@ export class CoreBrowserService implements ICoreBrowserService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public get isFocused(): boolean {
|
public get isFocused(): boolean {
|
||||||
const docOrShadowRoot = this._textarea.getRootNode ? this._textarea.getRootNode() as Document | ShadowRoot : this._textarea.ownerDocument;
|
if (this._cachedIsFocused === undefined) {
|
||||||
return docOrShadowRoot.activeElement === this._textarea && this._textarea.ownerDocument.hasFocus();
|
this._cachedIsFocused = this._isFocused && this._textarea.ownerDocument.hasFocus();
|
||||||
|
queueMicrotask(() => this._cachedIsFocused = undefined);
|
||||||
|
}
|
||||||
|
return this._cachedIsFocused;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user