feat(control): spawn session NPCs and resize tokens in preview

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Ivan Fontosh
2026-08-18 14:42:06 +08:00
parent 1ab6ffd593
commit 1a9f0f2557
28 changed files with 1446 additions and 89 deletions
@@ -46,3 +46,50 @@ export function useLiveDragBroadcast(
return { schedule, flush };
}
/** Во время resize шлёт onResize не чаще одного раза за кадр. */
export function useLiveResizeBroadcast(
onResize: ((id: string, sizeN: number) => void) | undefined,
id: string,
): {
schedule: (sizeN: number) => void;
flush: (sizeN: number) => void;
} {
const rafRef = useRef(0);
const pendingRef = useRef<number | null>(null);
const onResizeRef = useRef(onResize);
onResizeRef.current = onResize;
const idRef = useRef(id);
idRef.current = id;
useEffect(
() => () => {
if (rafRef.current) cancelAnimationFrame(rafRef.current);
},
[],
);
const schedule = useCallback((sizeN: number) => {
if (!onResizeRef.current) return;
pendingRef.current = sizeN;
if (rafRef.current) return;
rafRef.current = requestAnimationFrame(() => {
rafRef.current = 0;
const pending = pendingRef.current;
const cb = onResizeRef.current;
if (pending === null || !cb) return;
cb(idRef.current, pending);
});
}, []);
const flush = useCallback((sizeN: number) => {
if (rafRef.current) {
cancelAnimationFrame(rafRef.current);
rafRef.current = 0;
}
pendingRef.current = null;
onResizeRef.current?.(idRef.current, sizeN);
}, []);
return { schedule, flush };
}