@tldraw/state-react
Version:
tldraw infinite canvas SDK (react bindings for state).
8 lines (7 loc) • 2.31 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../src/lib/useQuickReactor.ts"],
"sourcesContent": ["import { EMPTY_ARRAY, EffectScheduler } from '@tldraw/state'\nimport { useEffect } from 'react'\n\n/**\n * A React hook that runs side effects immediately in response to signal changes, without throttling.\n * Unlike useReactor which batches updates to animation frames, useQuickReactor executes the effect\n * function immediately when dependencies change, making it ideal for critical updates that cannot wait.\n *\n * The effect runs immediately when the component mounts and whenever tracked signals change.\n * Updates are not throttled, so the effect executes synchronously on every change.\n *\n * @example\n * ```ts\n * function DataSynchronizer() {\n * const criticalData = useAtom('criticalData', null)\n *\n * useQuickReactor('sync-data', () => {\n * const data = criticalData.get()\n * if (data) {\n * // Send immediately - don't wait for next frame\n * sendToServer(data)\n * }\n * }, [criticalData])\n *\n * return <div>Sync status updated</div>\n * }\n * ```\n *\n * @example\n * ```ts\n * function CursorUpdater({ editor }) {\n * useQuickReactor('update-cursor', () => {\n * const cursor = editor.getInstanceState().cursor\n * document.body.style.cursor = cursor.type\n * }, [])\n * }\n * ```\n *\n * @param name - A descriptive name for the reactor, used for debugging and performance profiling\n * @param reactFn - The effect function to execute when signals change. Should not return a value.\n * @param deps - Optional dependency array that controls when the reactor is recreated. Works like useEffect deps.\n * @public\n */\nexport function useQuickReactor(name: string, reactFn: () => void, deps: any[] = EMPTY_ARRAY) {\n\tuseEffect(() => {\n\t\tconst scheduler = new EffectScheduler(name, reactFn)\n\t\tscheduler.attach()\n\t\tscheduler.execute()\n\t\treturn () => {\n\t\t\tscheduler.detach()\n\t\t}\n\t\t// eslint-disable-next-line react-hooks/exhaustive-deps\n\t}, deps)\n}\n"],
"mappings": "AAAA,SAAS,aAAa,uBAAuB;AAC7C,SAAS,iBAAiB;AA0CnB,SAAS,gBAAgB,MAAc,SAAqB,OAAc,aAAa;AAC7F,YAAU,MAAM;AACf,UAAM,YAAY,IAAI,gBAAgB,MAAM,OAAO;AACnD,cAAU,OAAO;AACjB,cAAU,QAAQ;AAClB,WAAO,MAAM;AACZ,gBAAU,OAAO;AAAA,IAClB;AAAA,EAED,GAAG,IAAI;AACR;",
"names": []
}