UNPKG

@rivetkit/react

Version:

_Lightweight Libraries for Backends_

1 lines 4.37 kB
{"version":3,"sources":["../src/mod.ts"],"sourcesContent":["import type { Client, ExtractActorsFromRegistry } from \"@rivetkit/core/client\";\nimport {\n\ttype ActorOptions,\n\ttype AnyActorRegistry,\n\ttype CreateRivetKitOptions,\n\tcreateRivetKit as createVanillaRivetKit,\n} from \"@rivetkit/framework-base\";\nimport { useStore } from \"@tanstack/react-store\";\nimport { useEffect, useRef } from \"react\";\n\nexport { createClient } from \"@rivetkit/core/client\";\n\nexport function createRivetKit<Registry extends AnyActorRegistry>(\n\tclient: Client<Registry>,\n\topts: CreateRivetKitOptions<Registry> = {},\n) {\n\tconst { getOrCreateActor } = createVanillaRivetKit<\n\t\tRegistry,\n\t\tExtractActorsFromRegistry<Registry>,\n\t\tkeyof ExtractActorsFromRegistry<Registry>\n\t>(client, opts);\n\n\t/**\n\t * Hook to connect to a actor and retrieve its state. Using this hook with the same options\n\t * will return the same actor instance. This simplifies passing around the actor state in your components.\n\t * It also provides a method to listen for events emitted by the actor.\n\t * @param opts - Options for the actor, including its name, key, and parameters.\n\t * @returns An object containing the actor's state and a method to listen for events.\n\t */\n\tfunction useActor<\n\t\tActorName extends keyof ExtractActorsFromRegistry<Registry>,\n\t>(opts: ActorOptions<Registry, ActorName>) {\n\t\tconst { mount, setState, state } = getOrCreateActor<ActorName>(opts);\n\n\t\tuseEffect(() => {\n\t\t\tsetState((prev) => {\n\t\t\t\tprev.opts = {\n\t\t\t\t\t...opts,\n\t\t\t\t\tenabled: opts.enabled ?? true,\n\t\t\t\t};\n\t\t\t\treturn prev;\n\t\t\t});\n\t\t}, [opts, setState]);\n\n\t\tuseEffect(() => {\n\t\t\treturn mount();\n\t\t}, [mount]);\n\n\t\tconst actorState = useStore(state) || {};\n\n\t\t/**\n\t\t * Hook to listen for events emitted by the actor.\n\t\t * This hook allows you to subscribe to specific events emitted by the actor and execute a handler function\n\t\t * when the event occurs.\n\t\t * It uses the `useEffect` hook to set up the event listener when the actor connection is established.\n\t\t * It cleans up the listener when the component unmounts or when the actor connection changes.\n\t\t * @param eventName The name of the event to listen for.\n\t\t * @param handler The function to call when the event is emitted.\n\t\t */\n\t\tfunction useEvent(\n\t\t\teventName: string,\n\t\t\t// biome-ignore lint/suspicious/noExplicitAny: strong typing of handler is not supported yet\n\t\t\thandler: (...args: any[]) => void,\n\t\t) {\n\t\t\tconst ref = useRef(handler);\n\t\t\tconst actorState = useStore(state) || {};\n\n\t\t\tuseEffect(() => {\n\t\t\t\tref.current = handler;\n\t\t\t}, [handler]);\n\n\t\t\t// biome-ignore lint/correctness/useExhaustiveDependencies: it's okay to not include all dependencies here\n\t\t\tuseEffect(() => {\n\t\t\t\tif (!actorState?.connection) return;\n\n\t\t\t\tfunction eventHandler(...args: any[]) {\n\t\t\t\t\tref.current(...args);\n\t\t\t\t}\n\t\t\t\treturn actorState.connection.on(eventName, eventHandler);\n\t\t\t}, [\n\t\t\t\tactorState.connection,\n\t\t\t\tactorState.isConnected,\n\t\t\t\tactorState.hash,\n\t\t\t\teventName,\n\t\t\t]);\n\t\t}\n\n\t\treturn {\n\t\t\t...actorState,\n\t\t\tuseEvent,\n\t\t};\n\t}\n\n\treturn {\n\t\tuseActor,\n\t};\n}\n"],"mappings":";AACA;AAAA,EAIC,kBAAkB;AAAA,OACZ;AACP,SAAS,gBAAgB;AACzB,SAAS,WAAW,cAAc;AAElC,SAAS,oBAAoB;AAEtB,SAAS,eACf,QACA,OAAwC,CAAC,GACxC;AACD,QAAM,EAAE,iBAAiB,IAAI,sBAI3B,QAAQ,IAAI;AASd,WAAS,SAEPA,OAAyC;AAC1C,UAAM,EAAE,OAAO,UAAU,MAAM,IAAI,iBAA4BA,KAAI;AAEnE,cAAU,MAAM;AACf,eAAS,CAAC,SAAS;AAClB,aAAK,OAAO;AAAA,UACX,GAAGA;AAAA,UACH,SAASA,MAAK,WAAW;AAAA,QAC1B;AACA,eAAO;AAAA,MACR,CAAC;AAAA,IACF,GA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