@tldraw/state-react
Version:
tldraw infinite canvas SDK (react bindings for state).
8 lines (7 loc) • 1.71 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../src/lib/useComputed.ts"],
"sourcesContent": ["/* eslint-disable prefer-rest-params */\nimport { Computed, ComputedOptions, computed } from '@tldraw/state'\nimport { useMemo } from 'react'\n\n/** @public */\nexport function useComputed<Value>(name: string, compute: () => Value, deps: any[]): Computed<Value>\n\n/**\n * Creates a new computed signal and returns it. The computed signal will be created only once.\n *\n * @example\n * ```ts\n * type GreeterProps = {\n * firstName: Signal<string>\n * lastName: Signal<string>\n * }\n *\n * const Greeter = track(function Greeter ({firstName, lastName}: GreeterProps) {\n * const fullName = useComputed('fullName', () => `${firstName.get()} ${lastName.get()}`)\n * return <div>Hello {fullName.get()}!</div>\n * })\n * ```\n *\n * @public\n */\nexport function useComputed<Value, Diff = unknown>(\n\tname: string,\n\tcompute: () => Value,\n\topts: ComputedOptions<Value, Diff>,\n\tdeps: any[]\n): Computed<Value>\n/** @public */\nexport function useComputed() {\n\tconst name = arguments[0]\n\tconst compute = arguments[1]\n\tconst opts = arguments.length === 3 ? undefined : arguments[2]\n\tconst deps = arguments.length === 3 ? arguments[2] : arguments[3]\n\t// eslint-disable-next-line react-hooks/exhaustive-deps\n\treturn useMemo(() => computed(`useComputed(${name})`, compute, opts), deps)\n}\n"],
"mappings": "AACA,SAAoC,gBAAgB;AACpD,SAAS,eAAe;AA8BjB,SAAS,cAAc;AAC7B,QAAM,OAAO,UAAU,CAAC;AACxB,QAAM,UAAU,UAAU,CAAC;AAC3B,QAAM,OAAO,UAAU,WAAW,IAAI,SAAY,UAAU,CAAC;AAC7D,QAAM,OAAO,UAAU,WAAW,IAAI,UAAU,CAAC,IAAI,UAAU,CAAC;AAEhE,SAAO,QAAQ,MAAM,SAAS,eAAe,IAAI,KAAK,SAAS,IAAI,GAAG,IAAI;AAC3E;",
"names": []
}