@rivetkit/react
Version:
React hooks and components for RivetKit client applications
59 lines • 1.6 kB
JavaScript
// src/mod.ts
import {
createRivetKit as createVanillaRivetKit
} from "@rivetkit/framework-base";
import { useStore } from "@tanstack/react-store";
import { useEffect, useRef } from "react";
import {
createClient
} from "rivetkit/client";
import { ActorConnDisposed, createClient as createClient2 } from "rivetkit/client";
function createRivetKit(clientInput, opts = {}) {
return createRivetKitWithClient(
createClient(clientInput),
opts
);
}
function createRivetKitWithClient(client, opts = {}) {
const { getOrCreateActor } = createVanillaRivetKit(client, opts);
function useActor(opts2) {
const { mount, state } = getOrCreateActor(opts2);
useEffect(() => {
return mount();
}, [mount]);
const actorState = useStore(state) || {};
function useEvent(eventName, handler) {
const ref = useRef(handler);
const actorState2 = useStore(state) || {};
useEffect(() => {
ref.current = handler;
}, [handler]);
useEffect(() => {
if (!(actorState2 == null ? void 0 : actorState2.connection)) return;
function eventHandler(...args) {
ref.current(...args);
}
return actorState2.connection.on(eventName, eventHandler);
}, [
actorState2.connection,
actorState2.connStatus,
actorState2.hash,
eventName
]);
}
return {
...actorState,
useEvent
};
}
return {
useActor
};
}
export {
ActorConnDisposed,
createClient2 as createClient,
createRivetKit,
createRivetKitWithClient
};
//# sourceMappingURL=mod.mjs.map