feat(control): spawn session NPCs and resize tokens in preview
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -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 };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user