mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-11 00:30:57 +00:00
Schedule layout suppress store updates on the next animation frame so tab switches and resize drags no longer trigger React setState-during-render warnings in TerminalComponent. Co-authored-by: Cursor <cursoragent@cursor.com>
51 lines
1.2 KiB
TypeScript
51 lines
1.2 KiB
TypeScript
import { useSyncExternalStore } from 'react';
|
|
|
|
type Listener = () => void;
|
|
|
|
let suppressDepth = 0;
|
|
const listeners = new Set<Listener>();
|
|
let notifyRafId: number | null = null;
|
|
|
|
const scheduleFrame =
|
|
typeof requestAnimationFrame === 'function'
|
|
? requestAnimationFrame
|
|
: (cb: () => void) => setTimeout(cb, 0) as unknown as number;
|
|
|
|
function scheduleEmit() {
|
|
if (notifyRafId !== null) return;
|
|
notifyRafId = scheduleFrame(() => {
|
|
notifyRafId = null;
|
|
listeners.forEach((listener) => listener());
|
|
});
|
|
}
|
|
|
|
export const terminalLayoutSuppressStore = {
|
|
getActive: () => suppressDepth > 0,
|
|
|
|
subscribe: (listener: Listener) => {
|
|
listeners.add(listener);
|
|
return () => listeners.delete(listener);
|
|
},
|
|
|
|
begin: () => {
|
|
suppressDepth += 1;
|
|
scheduleEmit();
|
|
},
|
|
|
|
end: () => {
|
|
const wasActive = suppressDepth > 0;
|
|
suppressDepth = Math.max(0, suppressDepth - 1);
|
|
if (wasActive) {
|
|
scheduleEmit();
|
|
}
|
|
},
|
|
};
|
|
|
|
export function useTerminalLayoutSuppressActive(): boolean {
|
|
return useSyncExternalStore(
|
|
terminalLayoutSuppressStore.subscribe,
|
|
terminalLayoutSuppressStore.getActive,
|
|
terminalLayoutSuppressStore.getActive,
|
|
);
|
|
}
|