@rivetkit/react 2.1.5 → 2.1.6-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/mod.d.mts CHANGED
@@ -1,46 +1,47 @@
1
1
  import * as rivetkit_client from 'rivetkit/client';
2
- import { createClient, ExtractActorsFromRegistry, Client } from 'rivetkit/client';
2
+ import { ClientConfigInput, ExtractActorsFromRegistry, ActorConn, Client } from 'rivetkit/client';
3
3
  export { ActorConnDisposed, createClient } from 'rivetkit/client';
4
+ import * as rivetkit from 'rivetkit';
4
5
  import { AnyActorRegistry, CreateRivetKitOptions, ActorOptions } from '@rivetkit/framework-base';
5
6
  export { ActorConnStatus } from '@rivetkit/framework-base';
6
7
 
7
- declare function createRivetKit<Registry extends AnyActorRegistry>(clientInput?: Parameters<typeof createClient>[0], opts?: CreateRivetKitOptions<Registry>): {
8
- useActor: <ActorName extends keyof ExtractActorsFromRegistry<Registry>>(opts: ActorOptions<Registry, ActorName>) => {
9
- useEvent: (eventName: string, handler: (...args: any[]) => void) => void;
8
+ declare function createRivetKit<Registry extends AnyActorRegistry>(clientInput?: string | ClientConfigInput | undefined, opts?: CreateRivetKitOptions<Registry>): {
9
+ useActor: <ActorName extends keyof ExtractActorsFromRegistry<Registry> & string>(opts: ActorOptions<Registry, ActorName>) => {
10
+ useEvent: ActorConn<ExtractActorsFromRegistry<Registry>[ActorName]> | null extends ActorConn<infer AD extends rivetkit.AnyActorDefinition> | null ? ActorConn<AD>["on"] : never;
10
11
  error: Error | null;
11
- hash: string;
12
12
  connStatus: rivetkit_client.ActorConnStatus;
13
+ hash: string;
13
14
  opts: {
14
- name: keyof ExtractActorsFromRegistry<Registry>;
15
+ name: string;
15
16
  key: string | string[];
16
- params?: Record<string, string>;
17
+ params?: unknown;
17
18
  createInRegion?: string;
18
19
  createWithInput?: unknown;
19
20
  enabled?: boolean;
20
21
  noCreate?: boolean;
21
22
  };
22
23
  handle: rivetkit_client.ActorHandle<ExtractActorsFromRegistry<Registry>[ActorName]> | null;
23
- connection: rivetkit_client.ActorConn<ExtractActorsFromRegistry<Registry>[ActorName]> | null;
24
+ connection: ActorConn<ExtractActorsFromRegistry<Registry>[ActorName]> | null;
24
25
  isConnected: boolean;
25
26
  };
26
27
  };
27
28
  declare function createRivetKitWithClient<Registry extends AnyActorRegistry>(client: Client<Registry>, opts?: CreateRivetKitOptions<Registry>): {
28
- useActor: <ActorName extends keyof ExtractActorsFromRegistry<Registry>>(opts: ActorOptions<Registry, ActorName>) => {
29
- useEvent: (eventName: string, handler: (...args: any[]) => void) => void;
29
+ useActor: <ActorName extends keyof ExtractActorsFromRegistry<Registry> & string>(opts: ActorOptions<Registry, ActorName>) => {
30
+ useEvent: ActorConn<ExtractActorsFromRegistry<Registry>[ActorName]> | null extends ActorConn<infer AD extends rivetkit.AnyActorDefinition> | null ? ActorConn<AD>["on"] : never;
30
31
  error: Error | null;
31
- hash: string;
32
32
  connStatus: rivetkit_client.ActorConnStatus;
33
+ hash: string;
33
34
  opts: {
34
- name: keyof ExtractActorsFromRegistry<Registry>;
35
+ name: string;
35
36
  key: string | string[];
36
- params?: Record<string, string>;
37
+ params?: unknown;
37
38
  createInRegion?: string;
38
39
  createWithInput?: unknown;
39
40
  enabled?: boolean;
40
41
  noCreate?: boolean;
41
42
  };
42
43
  handle: rivetkit_client.ActorHandle<ExtractActorsFromRegistry<Registry>[ActorName]> | null;
43
- connection: rivetkit_client.ActorConn<ExtractActorsFromRegistry<Registry>[ActorName]> | null;
44
+ connection: ActorConn<ExtractActorsFromRegistry<Registry>[ActorName]> | null;
44
45
  isConnected: boolean;
45
46
  };
46
47
  };
package/dist/mod.d.ts CHANGED
@@ -1,46 +1,47 @@
1
1
  import * as rivetkit_client from 'rivetkit/client';
2
- import { createClient, ExtractActorsFromRegistry, Client } from 'rivetkit/client';
2
+ import { ClientConfigInput, ExtractActorsFromRegistry, ActorConn, Client } from 'rivetkit/client';
3
3
  export { ActorConnDisposed, createClient } from 'rivetkit/client';
4
+ import * as rivetkit from 'rivetkit';
4
5
  import { AnyActorRegistry, CreateRivetKitOptions, ActorOptions } from '@rivetkit/framework-base';
5
6
  export { ActorConnStatus } from '@rivetkit/framework-base';
6
7
 
7
- declare function createRivetKit<Registry extends AnyActorRegistry>(clientInput?: Parameters<typeof createClient>[0], opts?: CreateRivetKitOptions<Registry>): {
8
- useActor: <ActorName extends keyof ExtractActorsFromRegistry<Registry>>(opts: ActorOptions<Registry, ActorName>) => {
9
- useEvent: (eventName: string, handler: (...args: any[]) => void) => void;
8
+ declare function createRivetKit<Registry extends AnyActorRegistry>(clientInput?: string | ClientConfigInput | undefined, opts?: CreateRivetKitOptions<Registry>): {
9
+ useActor: <ActorName extends keyof ExtractActorsFromRegistry<Registry> & string>(opts: ActorOptions<Registry, ActorName>) => {
10
+ useEvent: ActorConn<ExtractActorsFromRegistry<Registry>[ActorName]> | null extends ActorConn<infer AD extends rivetkit.AnyActorDefinition> | null ? ActorConn<AD>["on"] : never;
10
11
  error: Error | null;
11
- hash: string;
12
12
  connStatus: rivetkit_client.ActorConnStatus;
13
+ hash: string;
13
14
  opts: {
14
- name: keyof ExtractActorsFromRegistry<Registry>;
15
+ name: string;
15
16
  key: string | string[];
16
- params?: Record<string, string>;
17
+ params?: unknown;
17
18
  createInRegion?: string;
18
19
  createWithInput?: unknown;
19
20
  enabled?: boolean;
20
21
  noCreate?: boolean;
21
22
  };
22
23
  handle: rivetkit_client.ActorHandle<ExtractActorsFromRegistry<Registry>[ActorName]> | null;
23
- connection: rivetkit_client.ActorConn<ExtractActorsFromRegistry<Registry>[ActorName]> | null;
24
+ connection: ActorConn<ExtractActorsFromRegistry<Registry>[ActorName]> | null;
24
25
  isConnected: boolean;
25
26
  };
26
27
  };
27
28
  declare function createRivetKitWithClient<Registry extends AnyActorRegistry>(client: Client<Registry>, opts?: CreateRivetKitOptions<Registry>): {
28
- useActor: <ActorName extends keyof ExtractActorsFromRegistry<Registry>>(opts: ActorOptions<Registry, ActorName>) => {
29
- useEvent: (eventName: string, handler: (...args: any[]) => void) => void;
29
+ useActor: <ActorName extends keyof ExtractActorsFromRegistry<Registry> & string>(opts: ActorOptions<Registry, ActorName>) => {
30
+ useEvent: ActorConn<ExtractActorsFromRegistry<Registry>[ActorName]> | null extends ActorConn<infer AD extends rivetkit.AnyActorDefinition> | null ? ActorConn<AD>["on"] : never;
30
31
  error: Error | null;
31
- hash: string;
32
32
  connStatus: rivetkit_client.ActorConnStatus;
33
+ hash: string;
33
34
  opts: {
34
- name: keyof ExtractActorsFromRegistry<Registry>;
35
+ name: string;
35
36
  key: string | string[];
36
- params?: Record<string, string>;
37
+ params?: unknown;
37
38
  createInRegion?: string;
38
39
  createWithInput?: unknown;
39
40
  enabled?: boolean;
40
41
  noCreate?: boolean;
41
42
  };
42
43
  handle: rivetkit_client.ActorHandle<ExtractActorsFromRegistry<Registry>[ActorName]> | null;
43
- connection: rivetkit_client.ActorConn<ExtractActorsFromRegistry<Registry>[ActorName]> | null;
44
+ connection: ActorConn<ExtractActorsFromRegistry<Registry>[ActorName]> | null;
44
45
  isConnected: boolean;
45
46
  };
46
47
  };
package/dist/mod.js CHANGED
@@ -21,26 +21,27 @@ function createRivetKitWithClient(client, opts = {}) {
21
21
  _react.useEffect.call(void 0, () => {
22
22
  return mount();
23
23
  }, [mount]);
24
- const actorState = _reactstore.useStore.call(void 0, state) || {};
25
- function useEvent(eventName, handler) {
24
+ const actorState = _reactstore.useStore.call(void 0, state);
25
+ const useEvent = ((eventName, handler) => {
26
26
  const ref = _react.useRef.call(void 0, handler);
27
- const actorState2 = _reactstore.useStore.call(void 0, state) || {};
27
+ const actorState2 = _reactstore.useStore.call(void 0, state);
28
28
  _react.useEffect.call(void 0, () => {
29
29
  ref.current = handler;
30
30
  }, [handler]);
31
31
  _react.useEffect.call(void 0, () => {
32
- if (!(actorState2 == null ? void 0 : actorState2.connection)) return;
32
+ const connection = actorState2.connection;
33
+ if (!connection) return;
33
34
  function eventHandler(...args) {
34
35
  ref.current(...args);
35
36
  }
36
- return actorState2.connection.on(eventName, eventHandler);
37
+ return connection.on(eventName, eventHandler);
37
38
  }, [
38
39
  actorState2.connection,
39
40
  actorState2.connStatus,
40
41
  actorState2.hash,
41
42
  eventName
42
43
  ]);
43
- }
44
+ });
44
45
  return {
45
46
  ...actorState,
46
47
  useEvent
package/dist/mod.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["/home/runner/work/rivet/rivet/rivetkit-typescript/packages/react/dist/mod.js","../src/mod.ts"],"names":["opts","actorState"],"mappings":"AAAA;ACAA;AAIC;AAAkB,yDACZ;AACP,mDAAyB;AACzB,8BAAkC;AAClC;AAEC;AAAA,yCAEM;AAEP;AAGO,SAAS,cAAA,CACf,YAAA,EAAkD,KAAA,CAAA,EAClD,KAAA,EAAwC,CAAC,CAAA,EACxC;AACD,EAAA,OAAO,wBAAA;AAAA,IACN,kCAAA,WAAkC,CAAA;AAAA,IAClC;AAAA,EACD,CAAA;AACD;AAEO,SAAS,wBAAA,CACf,MAAA,EACA,KAAA,EAAwC,CAAC,CAAA,EACxC;AACD,EAAA,MAAM,EAAE,iBAAiB,EAAA,EAAI,2CAAA,MAG3B,EAAQ,IAAI,CAAA;AASd,EAAA,SAAS,QAAA,CAEPA,KAAAA,EAAyC;AAE1C,IAAA,MAAM,EAAE,KAAA,EAAO,MAAM,EAAA,EAAI,gBAAA,CAA4BA,KAAI,CAAA;AAEzD,IAAA,8BAAA,CAAU,EAAA,GAAM;AACf,MAAA,OAAO,KAAA,CAAM,CAAA;AAAA,IACd,CAAA,EAAG,CAAC,KAAK,CAAC,CAAA;AAEV,IAAA,MAAM,WAAA,EAAa,kCAAA,KAAc,EAAA,GAAK,CAAC,CAAA;AAWvC,IAAA,SAAS,QAAA,CACR,SAAA,EAEA,OAAA,EACC;AACD,MAAA,MAAM,IAAA,EAAM,2BAAA,OAAc,CAAA;AAC1B,MAAA,MAAMC,YAAAA,EAAa,kCAAA,KAAc,EAAA,GAAK,CAAC,CAAA;AAEvC,MAAA,8BAAA,CAAU,EAAA,GAAM;AACf,QAAA,GAAA,CAAI,QAAA,EAAU,OAAA;AAAA,MACf,CAAA,EAAG,CAAC,OAAO,CAAC,CAAA;AAGZ,MAAA,8BAAA,CAAU,EAAA,GAAM;AACf,QAAA,GAAA,CAAI,CAAA,CAACA,YAAAA,GAAA,KAAA,EAAA,KAAA,EAAA,EAAAA,WAAAA,CAAY,UAAA,CAAA,EAAY,MAAA;AAE7B,QAAA,SAAS,YAAA,CAAA,GAAgB,IAAA,EAAa;AACrC,UAAA,GAAA,CAAI,OAAA,CAAQ,GAAG,IAAI,CAAA;AAAA,QACpB;AACA,QAAA,OAAOA,WAAAA,CAAW,UAAA,CAAW,EAAA,CAAG,SAAA,EAAW,YAAY,CAAA;AAAA,MACxD,CAAA,EAAG;AAAA,QACFA,WAAAA,CAAW,UAAA;AAAA,QACXA,WAAAA,CAAW,UAAA;AAAA,QACXA,WAAAA,CAAW,IAAA;AAAA,QACX;AAAA,MACD,CAAC,CAAA;AAAA,IACF;AAEA,IAAA,OAAO;AAAA,MACN,GAAG,UAAA;AAAA,MACH;AAAA,IACD,CAAA;AAAA,EACD;AAEA,EAAA,OAAO;AAAA,IACN;AAAA,EACD,CAAA;AACD;ADjDA;AACE;AACA;AACA;AACA;AACF,yMAAC","file":"/home/runner/work/rivet/rivet/rivetkit-typescript/packages/react/dist/mod.js","sourcesContent":[null,"import {\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\";\nimport {\n\ttype Client,\n\tcreateClient,\n\ttype ExtractActorsFromRegistry,\n} from \"rivetkit/client\";\n\nexport { ActorConnDisposed, createClient } from \"rivetkit/client\";\nexport type { ActorConnStatus } from \"@rivetkit/framework-base\";\n\nexport function createRivetKit<Registry extends AnyActorRegistry>(\n\tclientInput: Parameters<typeof createClient>[0] = undefined,\n\topts: CreateRivetKitOptions<Registry> = {},\n) {\n\treturn createRivetKitWithClient<Registry>(\n\t\tcreateClient<Registry>(clientInput),\n\t\topts,\n\t);\n}\n\nexport function createRivetKitWithClient<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>(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\t// getOrCreateActor syncs opts to store on every call\n\t\tconst { mount, state } = getOrCreateActor<ActorName>(opts);\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.connStatus,\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"]}
1
+ {"version":3,"sources":["/home/runner/work/rivet/rivet/rivetkit-typescript/packages/react/dist/mod.js","../src/mod.ts"],"names":["opts","actorState"],"mappings":"AAAA;ACAA;AAIC;AAAkB,yDACZ;AACP,mDAAyB;AACzB,8BAAkC;AAClC;AAIC;AAAA,yCAEM;AAEP;AAGO,SAAS,cAAA,CACf,YAAA,EAAsD,KAAA,CAAA,EACtD,KAAA,EAAwC,CAAC,CAAA,EACxC;AAED,EAAA,OAAO,wBAAA;AAAA,IACN,kCAAA,WAAkC,CAAA;AAAA,IAClC;AAAA,EACD,CAAA;AACD;AAEO,SAAS,wBAAA,CACf,MAAA,EACA,KAAA,EAAwC,CAAC,CAAA,EACxC;AAED,EAAA,MAAM,EAAE,iBAAiB,EAAA,EAAI,2CAAA,MAAsB,EAAQ,IAAI,CAAA;AAS/D,EAAA,SAAS,QAAA,CAEPA,KAAAA,EAAyC;AAE1C,IAAA,MAAM,EAAE,KAAA,EAAO,MAAM,EAAA,EAAI,gBAAA,CAA4BA,KAAI,CAAA;AAEzD,IAAA,8BAAA,CAAU,EAAA,GAAM;AACf,MAAA,OAAO,KAAA,CAAM,CAAA;AAAA,IACd,CAAA,EAAG,CAAC,KAAK,CAAC,CAAA;AAEV,IAAA,MAAM,WAAA,EAAa,kCAAA,KAAc,CAAA;AAgBjC,IAAA,MAAM,SAAA,EAAA,CAAY,CACjB,SAAA,EACA,OAAA,EAAA,GACI;AACJ,MAAA,MAAM,IAAA,EAAM,2BAAA,OAAc,CAAA;AAC1B,MAAA,MAAMC,YAAAA,EAAa,kCAAA,KAAc,CAAA;AAEjC,MAAA,8BAAA,CAAU,EAAA,GAAM;AACf,QAAA,GAAA,CAAI,QAAA,EAAU,OAAA;AAAA,MACf,CAAA,EAAG,CAAC,OAAO,CAAC,CAAA;AAGZ,MAAA,8BAAA,CAAU,EAAA,GAAM;AACf,QAAA,MAAM,WAAA,EAAaA,WAAAA,CAAW,UAAA;AAM9B,QAAA,GAAA,CAAI,CAAC,UAAA,EAAY,MAAA;AAEjB,QAAA,SAAS,YAAA,CAAA,GAAgB,IAAA,EAAiB;AACzC,UAAA,GAAA,CAAI,OAAA,CAAQ,GAAG,IAAI,CAAA;AAAA,QACpB;AACA,QAAA,OAAO,UAAA,CAAW,EAAA,CAAG,SAAA,EAAW,YAAY,CAAA;AAAA,MAC7C,CAAA,EAAG;AAAA,QACFA,WAAAA,CAAW,UAAA;AAAA,QACXA,WAAAA,CAAW,UAAA;AAAA,QACXA,WAAAA,CAAW,IAAA;AAAA,QACX;AAAA,MACD,CAAC,CAAA;AAAA,IACF,CAAA,CAAA;AAEA,IAAA,OAAO;AAAA,MACN,GAAG,UAAA;AAAA,MACH;AAAA,IACD,CAAA;AAAA,EACD;AAEA,EAAA,OAAO;AAAA,IACN;AAAA,EACD,CAAA;AACD;AD3DA;AACE;AACA;AACA;AACA;AACF,yMAAC","file":"/home/runner/work/rivet/rivet/rivetkit-typescript/packages/react/dist/mod.js","sourcesContent":[null,"import {\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\";\nimport {\n\ttype ActorConn,\n\ttype Client,\n\ttype ClientConfigInput,\n\tcreateClient,\n\ttype ExtractActorsFromRegistry,\n} from \"rivetkit/client\";\n\nexport { ActorConnDisposed, createClient } from \"rivetkit/client\";\nexport type { ActorConnStatus } from \"@rivetkit/framework-base\";\n\nexport function createRivetKit<Registry extends AnyActorRegistry>(\n\tclientInput: string | ClientConfigInput | undefined = undefined,\n\topts: CreateRivetKitOptions<Registry> = {},\n) {\n\t// @ts-ignore Type instantiation can be excessively deep for complex registries.\n\treturn createRivetKitWithClient<Registry>(\n\t\tcreateClient<Registry>(clientInput),\n\t\topts,\n\t);\n}\n\nexport function createRivetKitWithClient<Registry extends AnyActorRegistry>(\n\tclient: Client<Registry>,\n\topts: CreateRivetKitOptions<Registry> = {},\n) {\n\t// @ts-ignore Type instantiation can be excessively deep for complex registries.\n\tconst { getOrCreateActor } = createVanillaRivetKit(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> & string,\n\t>(opts: ActorOptions<Registry, ActorName>) {\n\t\t// getOrCreateActor syncs opts to store on every call\n\t\tconst { mount, state } = getOrCreateActor<ActorName>(opts);\n\n\t\tuseEffect(() => {\n\t\t\treturn mount();\n\t\t}, [mount]);\n\n\t\tconst actorState = useStore(state);\n\t\ttype UseEvent = (typeof actorState)[\"connection\"] extends ActorConn<\n\t\t\tinfer AD\n\t\t> | null\n\t\t\t? ActorConn<AD>[\"on\"]\n\t\t\t: never;\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\tconst useEvent = ((\n\t\t\teventName: string,\n\t\t\thandler: (...args: unknown[]) => 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\tconst connection = actorState.connection as {\n\t\t\t\t\ton: (\n\t\t\t\t\t\teventName: string,\n\t\t\t\t\t\tcallback: (...args: unknown[]) => void,\n\t\t\t\t\t) => () => void;\n\t\t\t\t} | null;\n\t\t\t\tif (!connection) return;\n\n\t\t\t\tfunction eventHandler(...args: unknown[]) {\n\t\t\t\t\tref.current(...args);\n\t\t\t\t}\n\t\t\t\treturn connection.on(eventName, eventHandler);\n\t\t\t}, [\n\t\t\t\tactorState.connection,\n\t\t\t\tactorState.connStatus,\n\t\t\t\tactorState.hash,\n\t\t\t\teventName,\n\t\t\t]);\n\t\t}) as UseEvent;\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"]}
package/dist/mod.mjs CHANGED
@@ -21,26 +21,27 @@ function createRivetKitWithClient(client, opts = {}) {
21
21
  useEffect(() => {
22
22
  return mount();
23
23
  }, [mount]);
24
- const actorState = useStore(state) || {};
25
- function useEvent(eventName, handler) {
24
+ const actorState = useStore(state);
25
+ const useEvent = ((eventName, handler) => {
26
26
  const ref = useRef(handler);
27
- const actorState2 = useStore(state) || {};
27
+ const actorState2 = useStore(state);
28
28
  useEffect(() => {
29
29
  ref.current = handler;
30
30
  }, [handler]);
31
31
  useEffect(() => {
32
- if (!(actorState2 == null ? void 0 : actorState2.connection)) return;
32
+ const connection = actorState2.connection;
33
+ if (!connection) return;
33
34
  function eventHandler(...args) {
34
35
  ref.current(...args);
35
36
  }
36
- return actorState2.connection.on(eventName, eventHandler);
37
+ return connection.on(eventName, eventHandler);
37
38
  }, [
38
39
  actorState2.connection,
39
40
  actorState2.connStatus,
40
41
  actorState2.hash,
41
42
  eventName
42
43
  ]);
43
- }
44
+ });
44
45
  return {
45
46
  ...actorState,
46
47
  useEvent
package/dist/mod.mjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/mod.ts"],"sourcesContent":["import {\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\";\nimport {\n\ttype Client,\n\tcreateClient,\n\ttype ExtractActorsFromRegistry,\n} from \"rivetkit/client\";\n\nexport { ActorConnDisposed, createClient } from \"rivetkit/client\";\nexport type { ActorConnStatus } from \"@rivetkit/framework-base\";\n\nexport function createRivetKit<Registry extends AnyActorRegistry>(\n\tclientInput: Parameters<typeof createClient>[0] = undefined,\n\topts: CreateRivetKitOptions<Registry> = {},\n) {\n\treturn createRivetKitWithClient<Registry>(\n\t\tcreateClient<Registry>(clientInput),\n\t\topts,\n\t);\n}\n\nexport function createRivetKitWithClient<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>(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\t// getOrCreateActor syncs opts to store on every call\n\t\tconst { mount, state } = getOrCreateActor<ActorName>(opts);\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.connStatus,\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":";AAAA;AAAA,EAIC,kBAAkB;AAAA,OACZ;AACP,SAAS,gBAAgB;AACzB,SAAS,WAAW,cAAc;AAClC;AAAA,EAEC;AAAA,OAEM;AAEP,SAAS,mBAAmB,gBAAAA,qBAAoB;AAGzC,SAAS,eACf,cAAkD,QAClD,OAAwC,CAAC,GACxC;AACD,SAAO;AAAA,IACN,aAAuB,WAAW;AAAA,IAClC;AAAA,EACD;AACD;AAEO,SAAS,yBACf,QACA,OAAwC,CAAC,GACxC;AACD,QAAM,EAAE,iBAAiB,IAAI,sBAG3B,QAAQ,IAAI;AASd,WAAS,SAEPC,OAAyC;AAE1C,UAAM,EAAE,OAAO,MAAM,IAAI,iBAA4BA,KAAI;AAEzD,cAAU,MAAM;AACf,aAAO,MAAM;AAAA,IACd,GAAG,CAAC,KAAK,CAAC;AAEV,UAAM,aAAa,SAAS,KAAK,KAAK,CAAC;AAWvC,aAAS,SACR,WAEA,SACC;AACD,YAAM,MAAM,OAAO,OAAO;AAC1B,YAAMC,cAAa,SAAS,KAAK,KAAK,CAAC;AAEvC,gBAAU,MAAM;AACf,YAAI,UAAU;AAAA,MACf,GAAG,CAAC,OAAO,CAAC;AAGZ,gBAAU,MAAM;AACf,YAAI,EAACA,eAAA,gBAAAA,YAAY,YAAY;AAE7B,iBAAS,gBAAgB,MAAa;AACrC,cAAI,QAAQ,GAAG,IAAI;AAAA,QACpB;AACA,eAAOA,YAAW,WAAW,GAAG,WAAW,YAAY;AAAA,MACxD,GAAG;AAAA,QACFA,YAAW;AAAA,QACXA,YAAW;AAAA,QACXA,YAAW;AAAA,QACX;AAAA,MACD,CAAC;AAAA,IACF;AAEA,WAAO;AAAA,MACN,GAAG;AAAA,MACH;AAAA,IACD;AAAA,EACD;AAEA,SAAO;AAAA,IACN;AAAA,EACD;AACD;","names":["createClient","opts","actorState"]}
1
+ {"version":3,"sources":["../src/mod.ts"],"sourcesContent":["import {\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\";\nimport {\n\ttype ActorConn,\n\ttype Client,\n\ttype ClientConfigInput,\n\tcreateClient,\n\ttype ExtractActorsFromRegistry,\n} from \"rivetkit/client\";\n\nexport { ActorConnDisposed, createClient } from \"rivetkit/client\";\nexport type { ActorConnStatus } from \"@rivetkit/framework-base\";\n\nexport function createRivetKit<Registry extends AnyActorRegistry>(\n\tclientInput: string | ClientConfigInput | undefined = undefined,\n\topts: CreateRivetKitOptions<Registry> = {},\n) {\n\t// @ts-ignore Type instantiation can be excessively deep for complex registries.\n\treturn createRivetKitWithClient<Registry>(\n\t\tcreateClient<Registry>(clientInput),\n\t\topts,\n\t);\n}\n\nexport function createRivetKitWithClient<Registry extends AnyActorRegistry>(\n\tclient: Client<Registry>,\n\topts: CreateRivetKitOptions<Registry> = {},\n) {\n\t// @ts-ignore Type instantiation can be excessively deep for complex registries.\n\tconst { getOrCreateActor } = createVanillaRivetKit(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> & string,\n\t>(opts: ActorOptions<Registry, ActorName>) {\n\t\t// getOrCreateActor syncs opts to store on every call\n\t\tconst { mount, state } = getOrCreateActor<ActorName>(opts);\n\n\t\tuseEffect(() => {\n\t\t\treturn mount();\n\t\t}, [mount]);\n\n\t\tconst actorState = useStore(state);\n\t\ttype UseEvent = (typeof actorState)[\"connection\"] extends ActorConn<\n\t\t\tinfer AD\n\t\t> | null\n\t\t\t? ActorConn<AD>[\"on\"]\n\t\t\t: never;\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\tconst useEvent = ((\n\t\t\teventName: string,\n\t\t\thandler: (...args: unknown[]) => 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\tconst connection = actorState.connection as {\n\t\t\t\t\ton: (\n\t\t\t\t\t\teventName: string,\n\t\t\t\t\t\tcallback: (...args: unknown[]) => void,\n\t\t\t\t\t) => () => void;\n\t\t\t\t} | null;\n\t\t\t\tif (!connection) return;\n\n\t\t\t\tfunction eventHandler(...args: unknown[]) {\n\t\t\t\t\tref.current(...args);\n\t\t\t\t}\n\t\t\t\treturn connection.on(eventName, eventHandler);\n\t\t\t}, [\n\t\t\t\tactorState.connection,\n\t\t\t\tactorState.connStatus,\n\t\t\t\tactorState.hash,\n\t\t\t\teventName,\n\t\t\t]);\n\t\t}) as UseEvent;\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":";AAAA;AAAA,EAIC,kBAAkB;AAAA,OACZ;AACP,SAAS,gBAAgB;AACzB,SAAS,WAAW,cAAc;AAClC;AAAA,EAIC;AAAA,OAEM;AAEP,SAAS,mBAAmB,gBAAAA,qBAAoB;AAGzC,SAAS,eACf,cAAsD,QACtD,OAAwC,CAAC,GACxC;AAED,SAAO;AAAA,IACN,aAAuB,WAAW;AAAA,IAClC;AAAA,EACD;AACD;AAEO,SAAS,yBACf,QACA,OAAwC,CAAC,GACxC;AAED,QAAM,EAAE,iBAAiB,IAAI,sBAAsB,QAAQ,IAAI;AAS/D,WAAS,SAEPC,OAAyC;AAE1C,UAAM,EAAE,OAAO,MAAM,IAAI,iBAA4BA,KAAI;AAEzD,cAAU,MAAM;AACf,aAAO,MAAM;AAAA,IACd,GAAG,CAAC,KAAK,CAAC;AAEV,UAAM,aAAa,SAAS,KAAK;AAgBjC,UAAM,YAAY,CACjB,WACA,YACI;AACJ,YAAM,MAAM,OAAO,OAAO;AAC1B,YAAMC,cAAa,SAAS,KAAK;AAEjC,gBAAU,MAAM;AACf,YAAI,UAAU;AAAA,MACf,GAAG,CAAC,OAAO,CAAC;AAGZ,gBAAU,MAAM;AACf,cAAM,aAAaA,YAAW;AAM9B,YAAI,CAAC,WAAY;AAEjB,iBAAS,gBAAgB,MAAiB;AACzC,cAAI,QAAQ,GAAG,IAAI;AAAA,QACpB;AACA,eAAO,WAAW,GAAG,WAAW,YAAY;AAAA,MAC7C,GAAG;AAAA,QACFA,YAAW;AAAA,QACXA,YAAW;AAAA,QACXA,YAAW;AAAA,QACX;AAAA,MACD,CAAC;AAAA,IACF;AAEA,WAAO;AAAA,MACN,GAAG;AAAA,MACH;AAAA,IACD;AAAA,EACD;AAEA,SAAO;AAAA,IACN;AAAA,EACD;AACD;","names":["createClient","opts","actorState"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rivetkit/react",
3
- "version": "2.1.5",
3
+ "version": "2.1.6-rc.1",
4
4
  "description": "React hooks and components for RivetKit client applications",
5
5
  "license": "Apache-2.0",
6
6
  "keywords": [
@@ -33,8 +33,8 @@
33
33
  },
34
34
  "dependencies": {
35
35
  "@tanstack/react-store": "^0.7.1",
36
- "@rivetkit/framework-base": "2.1.5",
37
- "rivetkit": "^2.1.5"
36
+ "@rivetkit/framework-base": "2.1.6-rc.1",
37
+ "rivetkit": "^2.1.6-rc.1"
38
38
  },
39
39
  "peerDependencies": {
40
40
  "react": "^18 || ^19",