@rivetkit/framework-base 2.1.7 → 2.1.9
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.js +7 -1
- package/dist/mod.js.map +1 -1
- package/dist/mod.mjs +7 -1
- package/dist/mod.mjs.map +1 -1
- package/package.json +2 -2
package/dist/mod.js
CHANGED
|
@@ -8,7 +8,13 @@ function createRivetKit(client, createOpts = {}) {
|
|
|
8
8
|
});
|
|
9
9
|
const cache = /* @__PURE__ */ new Map();
|
|
10
10
|
return {
|
|
11
|
-
getOrCreateActor: (actorOpts) => getOrCreateActor(
|
|
11
|
+
getOrCreateActor: (actorOpts) => getOrCreateActor(
|
|
12
|
+
client,
|
|
13
|
+
createOpts,
|
|
14
|
+
store,
|
|
15
|
+
cache,
|
|
16
|
+
actorOpts
|
|
17
|
+
),
|
|
12
18
|
store
|
|
13
19
|
};
|
|
14
20
|
}
|
package/dist/mod.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/home/runner/work/rivet/rivet/rivetkit-typescript/packages/framework-base/dist/mod.js","../src/mod.ts"],"names":["store","cached"],"mappings":"AAAA;ACAA,wCAAuC;AACvC,8GAAkB;AAElB,2BAMO;AAqJA,SAAS,cAAA,CACf,MAAA,EACA,WAAA,EAA8C,CAAC,CAAA,EAC9C;AACD,EAAA,MAAM,MAAA,EAAQ,IAAI,iBAAA,CAA6B;AAAA,IAC9C,MAAA,EAAQ,CAAC;AAAA,EACV,CAAC,CAAA;AAED,EAAA,MAAM,MAAA,kBAAoB,IAAI,GAAA,CAAI,CAAA;AAElC,EAAA,OAAO;AAAA,IACN,gBAAA,EAAkB,CAIjB,SAAA,EAAA,GAKK,gBAAA,CAAyB,MAAA,EAAQ,UAAA,EAAY,KAAA,EAAO,KAAA,EAAO,SAAS,CAAA;AAAA,IAC1E;AAAA,EACD,CAAA;AACD;AAIA,SAAS,WAAA,CACR,KAAA,EACA,GAAA,EACA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,CAAU,EAAE,IAAA,EAAM,GAAA,EAAK,MAAA,EAAQ,SAAS,CAAC,CAAA;AACtD;AAEA,SAAS,SAAA,CAAU,CAAA,EAAoB,CAAA,EAAoB;AAC1D,EAAA,OAAO,qCAAA,CAAM,EAAG,CAAC,CAAA;AAClB;ADtQA;AACE;AACF,wCAAC","file":"/home/runner/work/rivet/rivet/rivetkit-typescript/packages/framework-base/dist/mod.js","sourcesContent":[null,"import { Derived, Effect, Store } from \"@tanstack/store\";\nimport equal from \"fast-deep-equal\";\nimport type { AnyActorDefinition, Registry } from \"rivetkit\";\nimport {\n\ttype ActorConn,\n\ttype ActorConnStatus,\n\ttype ActorHandle,\n\ttype Client,\n\ttype ExtractActorsFromRegistry,\n} from \"rivetkit/client\";\n\nexport type AnyActorRegistry = Registry<any>;\n\nexport type { ActorConnStatus };\n\ninterface ActorStateReference {\n\t/**\n\t * The unique identifier for the actor.\n\t * This is a hash generated from the actor's options.\n\t * It is used to identify the actor instance in the store.\n\t * @internal\n\t */\n\thash: string;\n\t/**\n\t * The state of the actor, derived from the store.\n\t * This includes the actor's connection and handle.\n\t */\n\thandle: ActorHandle<AnyActorDefinition> | null;\n\t/**\n\t * The connection to the actor.\n\t * This is used to communicate with the actor in realtime.\n\t */\n\tconnection: ActorConn<AnyActorDefinition> | null;\n\t/**\n\t * The connection status of the actor.\n\t */\n\tconnStatus: ActorConnStatus;\n\t/**\n\t * The error that occurred while trying to connect to the actor, if any.\n\t */\n\terror: Error | null;\n\t/**\n\t * Options for the actor, including its name, key, parameters, and whether it is enabled.\n\t */\n\topts: {\n\t\tname: string;\n\t\t/**\n\t\t * Unique key for the actor instance.\n\t\t * This can be a string or an array of strings to create multiple instances.\n\t\t * @example \"abc\" or [\"abc\", \"def\"]\n\t\t */\n\t\tkey: string | string[];\n\t\t/**\n\t\t * Parameters for the actor.\n\t\t * These are additional options that can be passed to the actor.\n\t\t */\n\t\tparams?: unknown;\n\t\t/** Region to create the actor in if it doesn't exist. */\n\t\tcreateInRegion?: string;\n\t\t/** Input data to pass to the actor. */\n\t\tcreateWithInput?: unknown;\n\t\t/**\n\t\t * Whether the actor is enabled.\n\t\t * Defaults to true.\n\t\t */\n\t\tenabled?: boolean;\n\t\t/**\n\t\t * If true, only gets the actor if it already exists. Does not create the actor.\n\t\t * Throws an error if the actor is not found.\n\t\t * Defaults to false.\n\t\t */\n\t\tnoCreate?: boolean;\n\t};\n}\n\ninterface InternalRivetKitStore {\n\tactors: Record<string, ActorStateReference>;\n}\n\n/**\n * Options for configuring a actor in RivetKit.\n */\nexport interface ActorOptions<\n\tRegistry extends AnyActorRegistry,\n\tActorName extends keyof ExtractActorsFromRegistry<Registry> & string,\n> {\n\t/**\n\t * Typesafe name of the actor.\n\t * This should match the actor's name in the app's actor definitions.\n\t * @example \"chatRoom\"\n\t */\n\tname: ActorName;\n\t/**\n\t * Unique key for the actor instance.\n\t * This can be a string or an array of strings to create multiple instances.\n\t * @example \"abc\" or [\"abc\", \"def\"]\n\t */\n\tkey: string | string[];\n\t/**\n\t * Parameters for the actor.\n\t */\n\tparams?: ExtractActorsFromRegistry<Registry>[ActorName][\"params\"];\n\t/** Region to create the actor in if it doesn't exist. */\n\tcreateInRegion?: string;\n\t/** Input data to pass to the actor. */\n\tcreateWithInput?: unknown;\n\t/**\n\t * Whether the actor is enabled.\n\t * Defaults to true.\n\t */\n\tenabled?: boolean;\n\t/**\n\t * If true, only gets the actor if it already exists. Does not create the actor.\n\t * Throws an error if the actor is not found.\n\t * Defaults to false.\n\t */\n\tnoCreate?: boolean;\n}\n\nexport type ActorsStateDerived<\n\tRegistry extends AnyActorRegistry,\n\tWorkerName extends keyof ExtractActorsFromRegistry<Registry> & string,\n> = Derived<\n\tOmit<InternalRivetKitStore[\"actors\"][string], \"handle\" | \"connection\"> & {\n\t\thandle: ActorHandle<\n\t\t\tExtractActorsFromRegistry<Registry>[WorkerName]\n\t\t> | null;\n\t\tconnection: ActorConn<\n\t\t\tExtractActorsFromRegistry<Registry>[WorkerName]\n\t\t> | null;\n\t\t/** @deprecated Use `connStatus === \"connected\"` instead */\n\t\tisConnected: boolean;\n\t}\n>;\n\nexport type AnyActorOptions = ActorOptions<AnyActorRegistry, any>;\n\nexport interface CreateRivetKitOptions<Registry extends AnyActorRegistry> {\n\thashFunction?: (opts: ActorOptions<Registry, any>) => string;\n}\n\ntype ComputedActorState = InternalRivetKitStore[\"actors\"][string] & {\n\t/** @deprecated Use `connStatus === \"connected\"` instead */\n\tisConnected: boolean;\n};\n\ntype ActorCache = Map<\n\tstring,\n\t{\n\t\tstate: Derived<ComputedActorState>;\n\t\tkey: string;\n\t\tmount: () => () => void;\n\t\tcreate: () => void;\n\t\trefCount: number;\n\t\tcleanupTimeout: ReturnType<typeof setTimeout> | null;\n\t}\n>;\n\nexport function createRivetKit<Registry extends AnyActorRegistry>(\n\tclient: Client<Registry>,\n\tcreateOpts: CreateRivetKitOptions<Registry> = {},\n) {\n\tconst store = new Store<InternalRivetKitStore>({\n\t\tactors: {},\n\t});\n\n\tconst cache: ActorCache = new Map();\n\n\treturn {\n\t\tgetOrCreateActor: <\n\t\t\tActorName extends keyof ExtractActorsFromRegistry<Registry> &\n\t\t\t\tstring,\n\t\t>(\n\t\t\tactorOpts: ActorOptions<Registry, ActorName>,\n\t\t): {\n\t\t\tmount: () => () => void;\n\t\t\tstate: ActorsStateDerived<Registry, ActorName>;\n\t\t\tkey: string;\n\t\t} => (getOrCreateActor as any)(client, createOpts, store, cache, actorOpts),\n\t\tstore,\n\t};\n}\n\ntype ActorUpdates = Partial<InternalRivetKitStore[\"actors\"][string]>;\n\nfunction updateActor(\n\tstore: Store<InternalRivetKitStore>,\n\tkey: string,\n\tupdates: ActorUpdates,\n) {\n\tstore.setState((prev) => ({\n\t\t...prev,\n\t\tactors: {\n\t\t\t...prev.actors,\n\t\t\t[key]: { ...prev.actors[key], ...updates },\n\t\t},\n\t}));\n}\n\n// See README.md for lifecycle documentation.\nfunction getOrCreateActor<\n\tRegistry extends AnyActorRegistry,\n\tActorName extends keyof ExtractActorsFromRegistry<Registry> & string,\n>(\n\tclient: Client<Registry>,\n\tcreateOpts: CreateRivetKitOptions<Registry>,\n\tstore: Store<InternalRivetKitStore>,\n\tcache: ActorCache,\n\tactorOpts: ActorOptions<Registry, ActorName>,\n) {\n\tconst hash = createOpts.hashFunction || defaultHashFunction;\n\n\tconst normalizedOpts = {\n\t\t...actorOpts,\n\t\tenabled: actorOpts.enabled ?? true,\n\t};\n\n\tconst key = hash(normalizedOpts);\n\n\t// Sync opts to store on every call (even for cached entries)\n\t// Use queueMicrotask for updates to avoid \"Cannot update a component while rendering\" React error\n\tconst existing = store.state.actors[key];\n\tif (!existing) {\n\t\tstore.setState((prev) => ({\n\t\t\t...prev,\n\t\t\tactors: {\n\t\t\t\t...prev.actors,\n\t\t\t\t[key]: {\n\t\t\t\t\thash: key,\n\t\t\t\t\tconnStatus: \"idle\",\n\t\t\t\t\tconnection: null,\n\t\t\t\t\thandle: null,\n\t\t\t\t\terror: null,\n\t\t\t\t\topts: normalizedOpts,\n\t\t\t\t},\n\t\t\t},\n\t\t}));\n\t} else if (!optsEqual(existing.opts, normalizedOpts)) {\n\t\t// Defer opts update to avoid triggering re-render during render\n\t\tqueueMicrotask(() => {\n\t\t\tupdateActor(store, key, { opts: normalizedOpts });\n\t\t});\n\t}\n\n\tconst cached = cache.get(key);\n\tif (cached) {\n\t\treturn {\n\t\t\t...cached,\n\t\t\tstate: cached.state as ActorsStateDerived<Registry, ActorName>,\n\t\t};\n\t}\n\n\tconst derived = new Derived({\n\t\tfn: ({ currDepVals: [store] }) => {\n\t\t\tconst actor = store.actors[key];\n\t\t\treturn {\n\t\t\t\t...actor,\n\t\t\t\t/** @deprecated Use `connStatus === \"connected\"` instead */\n\t\t\t\tisConnected: actor.connStatus === \"connected\",\n\t\t\t};\n\t\t},\n\t\tdeps: [store],\n\t});\n\n\t// Handle enabled/disabled state changes.\n\t// Initial connection is triggered directly in mount() since Effect\n\t// only runs on state changes, not on mount.\n\tconst effect = new Effect({\n\t\tfn: () => {\n\t\t\tconst actor = store.state.actors[key];\n\t\t\tif (!actor) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Actor with key \"${key}\" not found in store. This indicates a bug in cleanup logic.`,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\t// Dispose connection if disabled\n\t\t\tif (!actor.opts.enabled && actor.connection) {\n\t\t\t\tactor.connection.dispose();\n\n\t\t\t\t// Reset state so re-enabling will reconnect\n\t\t\t\tupdateActor(store, key, {\n\t\t\t\t\tconnection: null,\n\t\t\t\t\thandle: null,\n\t\t\t\t\tconnStatus: \"idle\",\n\t\t\t\t});\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Reconnect when re-enabled after being disabled\n\t\t\t// Defer to avoid \"Cannot update a component while rendering\" React error\n\t\t\tif (actor.connStatus === \"idle\" && actor.opts.enabled) {\n\t\t\t\tqueueMicrotask(() => {\n\t\t\t\t\t// Re-check state after microtask in case it changed\n\t\t\t\t\tconst currentActor = store.state.actors[key];\n\t\t\t\t\tif (\n\t\t\t\t\t\tcurrentActor &&\n\t\t\t\t\t\tcurrentActor.connStatus === \"idle\" &&\n\t\t\t\t\t\tcurrentActor.opts.enabled\n\t\t\t\t\t) {\n\t\t\t\t\t\t(create as Function)(client, store, key);\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\t\t},\n\t\tdeps: [derived],\n\t});\n\n\t// Track subscriptions for ref counting\n\tlet unsubscribeDerived: (() => void) | null = null;\n\tlet unsubscribeEffect: (() => void) | null = null;\n\n\tconst mount = () => {\n\t\tconst cached = cache.get(key);\n\t\tif (!cached) {\n\t\t\tthrow new Error(\n\t\t\t\t`Actor with key \"${key}\" not found in cache. This indicates a bug in cleanup logic.`,\n\t\t\t);\n\t\t}\n\n\t\t// Cancel pending cleanup\n\t\tif (cached.cleanupTimeout !== null) {\n\t\t\tclearTimeout(cached.cleanupTimeout);\n\t\t\tcached.cleanupTimeout = null;\n\t\t}\n\n\t\t// Increment ref count\n\t\tcached.refCount++;\n\n\t\t// Mount derived/effect on first reference (or re-mount after cleanup)\n\t\tif (cached.refCount === 1) {\n\t\t\tunsubscribeDerived = derived.mount();\n\t\t\tunsubscribeEffect = effect.mount();\n\n\t\t\t// Effect doesn't run immediately on mount, only on state changes.\n\t\t\t// Trigger initial connection if actor is enabled and idle.\n\t\t\tconst actor = store.state.actors[key];\n\t\t\tif (actor && actor.opts.enabled && actor.connStatus === \"idle\") {\n\t\t\t\t(create as Function)(client, store, key);\n\t\t\t}\n\t\t}\n\n\t\treturn () => {\n\t\t\t// Decrement ref count\n\t\t\tcached.refCount--;\n\n\t\t\tif (cached.refCount === 0) {\n\t\t\t\t// Deferred cleanup prevents needless reconnection when:\n\t\t\t\t// - React Strict Mode's unmount/remount cycle\n\t\t\t\t// - useActor hook moves between components in the same render cycle\n\t\t\t\tcached.cleanupTimeout = setTimeout(() => {\n\t\t\t\t\tcached.cleanupTimeout = null;\n\t\t\t\t\tif (cached.refCount > 0) return;\n\n\t\t\t\t\t// Unsubscribe from derived/effect\n\t\t\t\t\tunsubscribeDerived?.();\n\t\t\t\t\tunsubscribeEffect?.();\n\t\t\t\t\tunsubscribeDerived = null;\n\t\t\t\t\tunsubscribeEffect = null;\n\n\t\t\t\t\t// Dispose connection\n\t\t\t\t\tconst actor = store.state.actors[key];\n\t\t\t\t\tif (actor?.connection) {\n\t\t\t\t\t\tactor.connection.dispose();\n\t\t\t\t\t}\n\n\t\t\t\t\t// Remove from store and cache\n\t\t\t\t\tstore.setState((prev) => {\n\t\t\t\t\t\tconst { [key]: _, ...rest } = prev.actors;\n\t\t\t\t\t\treturn { ...prev, actors: rest };\n\t\t\t\t\t});\n\t\t\t\t\tcache.delete(key);\n\t\t\t\t}, 0);\n\t\t\t}\n\t\t};\n\t};\n\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tconst boundCreate: () => void = () => (create as any)(client, store, key);\n\tcache.set(key, {\n\t\tstate: derived,\n\t\tkey,\n\t\tmount,\n\t\tcreate: boundCreate,\n\t\trefCount: 0,\n\t\tcleanupTimeout: null,\n\t});\n\n\treturn {\n\t\tmount,\n\t\tstate: derived as ActorsStateDerived<Registry, ActorName>,\n\t\tkey,\n\t};\n}\n\nfunction create<\n\tRegistry extends AnyActorRegistry,\n\tActorName extends keyof ExtractActorsFromRegistry<Registry> & string,\n>(client: Client<Registry>, store: Store<InternalRivetKitStore>, key: string) {\n\tconst actor = store.state.actors[key];\n\tif (!actor) {\n\t\tthrow new Error(\n\t\t\t`Actor with key \"${key}\" not found in store. This indicates a bug in cleanup logic.`,\n\t\t);\n\t}\n\n\t// Save actor to map\n\tupdateActor(store, key, {\n\t\tconnStatus: \"connecting\",\n\t\terror: null,\n\t});\n\n\ttry {\n\t\tconst handle = actor.opts.noCreate\n\t\t\t? client.get(actor.opts.name as string, actor.opts.key, {\n\t\t\t\t\tparams: actor.opts.params,\n\t\t\t\t})\n\t\t\t: client.getOrCreate(actor.opts.name as string, actor.opts.key, {\n\t\t\t\t\tparams: actor.opts.params,\n\t\t\t\t\tcreateInRegion: actor.opts.createInRegion,\n\t\t\t\t\tcreateWithInput: actor.opts.createWithInput,\n\t\t\t\t});\n\n\t\tconst connection = handle.connect();\n\n\t\t// Store connection BEFORE registering callbacks to avoid race condition\n\t\t// where status change fires before connection is stored\n\t\tupdateActor(store, key, {\n\t\t\thandle: handle as ActorHandle<\n\t\t\t\tExtractActorsFromRegistry<Registry>[ActorName]\n\t\t\t>,\n\t\t\tconnection: connection as ActorConn<\n\t\t\t\tExtractActorsFromRegistry<Registry>[ActorName]\n\t\t\t>,\n\t\t});\n\n\t\t// Subscribe to connection state changes\n\t\tconnection.onStatusChange((status) => {\n\t\t\tstore.setState((prev) => {\n\t\t\t\t// Only update if this is still the active connection\n\t\t\t\tconst isActiveConnection =\n\t\t\t\t\tprev.actors[key]?.connection === connection;\n\t\t\t\tif (!isActiveConnection) return prev;\n\t\t\t\treturn {\n\t\t\t\t\t...prev,\n\t\t\t\t\tactors: {\n\t\t\t\t\t\t...prev.actors,\n\t\t\t\t\t\t[key]: {\n\t\t\t\t\t\t\t...prev.actors[key],\n\t\t\t\t\t\t\tconnStatus: status,\n\t\t\t\t\t\t\t// Only clear error when successfully connected\n\t\t\t\t\t\t\t...(status === \"connected\" ? { error: null } : {}),\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t});\n\t\t});\n\n\t\t// onError is followed by onClose which will set connStatus to Disconnected\n\t\tconnection.onError((error) => {\n\t\t\tstore.setState((prev) => {\n\t\t\t\t// Only update if this is still the active connection\n\t\t\t\tif (prev.actors[key]?.connection !== connection) return prev;\n\t\t\t\treturn {\n\t\t\t\t\t...prev,\n\t\t\t\t\tactors: {\n\t\t\t\t\t\t...prev.actors,\n\t\t\t\t\t\t[key]: {\n\t\t\t\t\t\t\t...prev.actors[key],\n\t\t\t\t\t\t\terror,\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t});\n\t\t});\n\t} catch (error) {\n\t\tconsole.error(\"Failed to create actor connection\", error);\n\t\t// Use Disconnected so Effect won't auto-retry\n\t\t// User must re-enable or take action to retry\n\t\tupdateActor(store, key, {\n\t\t\tconnStatus: \"disconnected\",\n\t\t\terror: error as Error,\n\t\t});\n\t}\n}\n\nfunction defaultHashFunction({ name, key, params, noCreate }: AnyActorOptions) {\n\treturn JSON.stringify({ name, key, params, noCreate });\n}\n\nfunction optsEqual(a: AnyActorOptions, b: AnyActorOptions) {\n\treturn equal(a, b);\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["/home/runner/work/rivet/rivet/rivetkit-typescript/packages/framework-base/dist/mod.js","../src/mod.ts"],"names":["store","cached"],"mappings":"AAAA;ACAA,wCAAuC;AACvC,8GAAkB;AAElB,2BAMO;AAqJA,SAAS,cAAA,CACf,MAAA,EACA,WAAA,EAA8C,CAAC,CAAA,EAC9C;AACD,EAAA,MAAM,MAAA,EAAQ,IAAI,iB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AAC,CAAA;AAAA,EACF,EAAA,MAAA,CAAS,KAAA,EAAO;AACf,IAAA,OAAA,CAAQ,KAAA,CAAM,mCAAA,EAAqC,KAAK,CAAA;AAGxD,IAAA,WAAA,CAAY,KAAA,EAAO,GAAA,EAAK;AAAA,MACvB,UAAA,EAAY,cAAA;AAAA,MACZ;AAAA,IACD,CAAC,CAAA;AAAA,EACF;AACD;AAEA,SAAS,mBAAA,CAAoB,EAAE,IAAA,EAAM,GAAA,EAAK,MAAA,EAAQ,SAAS,CAAA,EAAoB;AAC9E,EAAA,OAAO,IAAA,CAAK,SAAA,CAAU,EAAE,IAAA,EAAM,GAAA,EAAK,MAAA,EAAQ,SAAS,CAAC,CAAA;AACtD;AAEA,SAAS,SAAA,CAAU,CAAA,EAAoB,CAAA,EAAoB;AAC1D,EAAA,OAAO,qCAAA,CAAM,EAAG,CAAC,CAAA;AAClB;ADvQA;AACE;AACF,wCAAC","file":"/home/runner/work/rivet/rivet/rivetkit-typescript/packages/framework-base/dist/mod.js","sourcesContent":[null,"import { Derived, Effect, Store } from \"@tanstack/store\";\nimport equal from \"fast-deep-equal\";\nimport type { AnyActorDefinition, Registry } from \"rivetkit\";\nimport {\n\ttype ActorConn,\n\ttype ActorConnStatus,\n\ttype ActorHandle,\n\ttype Client,\n\ttype ExtractActorsFromRegistry,\n} from \"rivetkit/client\";\n\nexport type AnyActorRegistry = Registry<any>;\n\nexport type { ActorConnStatus };\n\ninterface ActorStateReference {\n\t/**\n\t * The unique identifier for the actor.\n\t * This is a hash generated from the actor's options.\n\t * It is used to identify the actor instance in the store.\n\t * @internal\n\t */\n\thash: string;\n\t/**\n\t * The state of the actor, derived from the store.\n\t * This includes the actor's connection and handle.\n\t */\n\thandle: ActorHandle<AnyActorDefinition> | null;\n\t/**\n\t * The connection to the actor.\n\t * This is used to communicate with the actor in realtime.\n\t */\n\tconnection: ActorConn<AnyActorDefinition> | null;\n\t/**\n\t * The connection status of the actor.\n\t */\n\tconnStatus: ActorConnStatus;\n\t/**\n\t * The error that occurred while trying to connect to the actor, if any.\n\t */\n\terror: Error | null;\n\t/**\n\t * Options for the actor, including its name, key, parameters, and whether it is enabled.\n\t */\n\topts: {\n\t\tname: string;\n\t\t/**\n\t\t * Unique key for the actor instance.\n\t\t * This can be a string or an array of strings to create multiple instances.\n\t\t * @example \"abc\" or [\"abc\", \"def\"]\n\t\t */\n\t\tkey: string | string[];\n\t\t/**\n\t\t * Parameters for the actor.\n\t\t * These are additional options that can be passed to the actor.\n\t\t */\n\t\tparams?: unknown;\n\t\t/** Region to create the actor in if it doesn't exist. */\n\t\tcreateInRegion?: string;\n\t\t/** Input data to pass to the actor. */\n\t\tcreateWithInput?: unknown;\n\t\t/**\n\t\t * Whether the actor is enabled.\n\t\t * Defaults to true.\n\t\t */\n\t\tenabled?: boolean;\n\t\t/**\n\t\t * If true, only gets the actor if it already exists. Does not create the actor.\n\t\t * Throws an error if the actor is not found.\n\t\t * Defaults to false.\n\t\t */\n\t\tnoCreate?: boolean;\n\t};\n}\n\ninterface InternalRivetKitStore {\n\tactors: Record<string, ActorStateReference>;\n}\n\n/**\n * Options for configuring a actor in RivetKit.\n */\nexport interface ActorOptions<\n\tRegistry extends AnyActorRegistry,\n\tActorName extends keyof ExtractActorsFromRegistry<Registry> & string,\n> {\n\t/**\n\t * Typesafe name of the actor.\n\t * This should match the actor's name in the app's actor definitions.\n\t * @example \"chatRoom\"\n\t */\n\tname: ActorName;\n\t/**\n\t * Unique key for the actor instance.\n\t * This can be a string or an array of strings to create multiple instances.\n\t * @example \"abc\" or [\"abc\", \"def\"]\n\t */\n\tkey: string | string[];\n\t/**\n\t * Parameters for the actor.\n\t */\n\tparams?: ExtractActorsFromRegistry<Registry>[ActorName][\"params\"];\n\t/** Region to create the actor in if it doesn't exist. */\n\tcreateInRegion?: string;\n\t/** Input data to pass to the actor. */\n\tcreateWithInput?: unknown;\n\t/**\n\t * Whether the actor is enabled.\n\t * Defaults to true.\n\t */\n\tenabled?: boolean;\n\t/**\n\t * If true, only gets the actor if it already exists. Does not create the actor.\n\t * Throws an error if the actor is not found.\n\t * Defaults to false.\n\t */\n\tnoCreate?: boolean;\n}\n\nexport type ActorsStateDerived<\n\tRegistry extends AnyActorRegistry,\n\tWorkerName extends keyof ExtractActorsFromRegistry<Registry> & string,\n> = Derived<\n\tOmit<InternalRivetKitStore[\"actors\"][string], \"handle\" | \"connection\"> & {\n\t\thandle: ActorHandle<\n\t\t\tExtractActorsFromRegistry<Registry>[WorkerName]\n\t\t> | null;\n\t\tconnection: ActorConn<\n\t\t\tExtractActorsFromRegistry<Registry>[WorkerName]\n\t\t> | null;\n\t\t/** @deprecated Use `connStatus === \"connected\"` instead */\n\t\tisConnected: boolean;\n\t}\n>;\n\nexport type AnyActorOptions = ActorOptions<AnyActorRegistry, any>;\n\nexport interface CreateRivetKitOptions<Registry extends AnyActorRegistry> {\n\thashFunction?: (opts: ActorOptions<Registry, any>) => string;\n}\n\ntype ComputedActorState = InternalRivetKitStore[\"actors\"][string] & {\n\t/** @deprecated Use `connStatus === \"connected\"` instead */\n\tisConnected: boolean;\n};\n\ntype ActorCache = Map<\n\tstring,\n\t{\n\t\tstate: Derived<ComputedActorState>;\n\t\tkey: string;\n\t\tmount: () => () => void;\n\t\tcreate: () => void;\n\t\trefCount: number;\n\t\tcleanupTimeout: ReturnType<typeof setTimeout> | null;\n\t}\n>;\n\nexport function createRivetKit<Registry extends AnyActorRegistry>(\n\tclient: Client<Registry>,\n\tcreateOpts: CreateRivetKitOptions<Registry> = {},\n) {\n\tconst store = new Store<InternalRivetKitStore>({\n\t\tactors: {},\n\t});\n\n\tconst cache: ActorCache = new Map();\n\n\treturn {\n\t\tgetOrCreateActor: <\n\t\t\tActorName extends keyof ExtractActorsFromRegistry<Registry> &\n\t\t\t\tstring,\n\t\t>(\n\t\t\tactorOpts: ActorOptions<Registry, ActorName>,\n\t\t): {\n\t\t\tmount: () => () => void;\n\t\t\tstate: ActorsStateDerived<Registry, ActorName>;\n\t\t\tkey: string;\n\t\t} =>\n\t\t\t(getOrCreateActor as any)(\n\t\t\t\tclient,\n\t\t\t\tcreateOpts,\n\t\t\t\tstore,\n\t\t\t\tcache,\n\t\t\t\tactorOpts,\n\t\t\t),\n\t\tstore,\n\t};\n}\n\ntype ActorUpdates = Partial<InternalRivetKitStore[\"actors\"][string]>;\n\nfunction updateActor(\n\tstore: Store<InternalRivetKitStore>,\n\tkey: string,\n\tupdates: ActorUpdates,\n) {\n\tstore.setState((prev) => ({\n\t\t...prev,\n\t\tactors: {\n\t\t\t...prev.actors,\n\t\t\t[key]: { ...prev.actors[key], ...updates },\n\t\t},\n\t}));\n}\n\n// See README.md for lifecycle documentation.\nfunction getOrCreateActor<\n\tRegistry extends AnyActorRegistry,\n\tActorName extends keyof ExtractActorsFromRegistry<Registry> & string,\n>(\n\tclient: Client<Registry>,\n\tcreateOpts: CreateRivetKitOptions<Registry>,\n\tstore: Store<InternalRivetKitStore>,\n\tcache: ActorCache,\n\tactorOpts: ActorOptions<Registry, ActorName>,\n) {\n\tconst hash = createOpts.hashFunction || defaultHashFunction;\n\n\tconst normalizedOpts = {\n\t\t...actorOpts,\n\t\tenabled: actorOpts.enabled ?? true,\n\t};\n\n\tconst key = hash(normalizedOpts);\n\n\t// Sync opts to store on every call (even for cached entries)\n\t// Use queueMicrotask for updates to avoid \"Cannot update a component while rendering\" React error\n\tconst existing = store.state.actors[key];\n\tif (!existing) {\n\t\tstore.setState((prev) => ({\n\t\t\t...prev,\n\t\t\tactors: {\n\t\t\t\t...prev.actors,\n\t\t\t\t[key]: {\n\t\t\t\t\thash: key,\n\t\t\t\t\tconnStatus: \"idle\",\n\t\t\t\t\tconnection: null,\n\t\t\t\t\thandle: null,\n\t\t\t\t\terror: null,\n\t\t\t\t\topts: normalizedOpts,\n\t\t\t\t},\n\t\t\t},\n\t\t}));\n\t} else if (!optsEqual(existing.opts, normalizedOpts)) {\n\t\t// Defer opts update to avoid triggering re-render during render\n\t\tqueueMicrotask(() => {\n\t\t\tupdateActor(store, key, { opts: normalizedOpts });\n\t\t});\n\t}\n\n\tconst cached = cache.get(key);\n\tif (cached) {\n\t\treturn {\n\t\t\t...cached,\n\t\t\tstate: cached.state as ActorsStateDerived<Registry, ActorName>,\n\t\t};\n\t}\n\n\tconst derived = new Derived({\n\t\tfn: ({ currDepVals: [store] }) => {\n\t\t\tconst actor = store.actors[key];\n\t\t\treturn {\n\t\t\t\t...actor,\n\t\t\t\t/** @deprecated Use `connStatus === \"connected\"` instead */\n\t\t\t\tisConnected: actor.connStatus === \"connected\",\n\t\t\t};\n\t\t},\n\t\tdeps: [store],\n\t});\n\n\t// Handle enabled/disabled state changes.\n\t// Initial connection is triggered directly in mount() since Effect\n\t// only runs on state changes, not on mount.\n\tconst effect = new Effect({\n\t\tfn: () => {\n\t\t\tconst actor = store.state.actors[key];\n\t\t\tif (!actor) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Actor with key \"${key}\" not found in store. This indicates a bug in cleanup logic.`,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\t// Dispose connection if disabled\n\t\t\tif (!actor.opts.enabled && actor.connection) {\n\t\t\t\tactor.connection.dispose();\n\n\t\t\t\t// Reset state so re-enabling will reconnect\n\t\t\t\tupdateActor(store, key, {\n\t\t\t\t\tconnection: null,\n\t\t\t\t\thandle: null,\n\t\t\t\t\tconnStatus: \"idle\",\n\t\t\t\t});\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Reconnect when re-enabled after being disabled\n\t\t\t// Defer to avoid \"Cannot update a component while rendering\" React error\n\t\t\tif (actor.connStatus === \"idle\" && actor.opts.enabled) {\n\t\t\t\tqueueMicrotask(() => {\n\t\t\t\t\t// Re-check state after microtask in case it changed\n\t\t\t\t\tconst currentActor = store.state.actors[key];\n\t\t\t\t\tif (\n\t\t\t\t\t\tcurrentActor &&\n\t\t\t\t\t\tcurrentActor.connStatus === \"idle\" &&\n\t\t\t\t\t\tcurrentActor.opts.enabled\n\t\t\t\t\t) {\n\t\t\t\t\t\t(create as Function)(client, store, key);\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\t\t},\n\t\tdeps: [derived],\n\t});\n\n\t// Track subscriptions for ref counting\n\tlet unsubscribeDerived: (() => void) | null = null;\n\tlet unsubscribeEffect: (() => void) | null = null;\n\n\tconst mount = () => {\n\t\tconst cached = cache.get(key);\n\t\tif (!cached) {\n\t\t\tthrow new Error(\n\t\t\t\t`Actor with key \"${key}\" not found in cache. This indicates a bug in cleanup logic.`,\n\t\t\t);\n\t\t}\n\n\t\t// Cancel pending cleanup\n\t\tif (cached.cleanupTimeout !== null) {\n\t\t\tclearTimeout(cached.cleanupTimeout);\n\t\t\tcached.cleanupTimeout = null;\n\t\t}\n\n\t\t// Increment ref count\n\t\tcached.refCount++;\n\n\t\t// Mount derived/effect on first reference (or re-mount after cleanup)\n\t\tif (cached.refCount === 1) {\n\t\t\tunsubscribeDerived = derived.mount();\n\t\t\tunsubscribeEffect = effect.mount();\n\n\t\t\t// Effect doesn't run immediately on mount, only on state changes.\n\t\t\t// Trigger initial connection if actor is enabled and idle.\n\t\t\tconst actor = store.state.actors[key];\n\t\t\tif (actor && actor.opts.enabled && actor.connStatus === \"idle\") {\n\t\t\t\t(create as Function)(client, store, key);\n\t\t\t}\n\t\t}\n\n\t\treturn () => {\n\t\t\t// Decrement ref count\n\t\t\tcached.refCount--;\n\n\t\t\tif (cached.refCount === 0) {\n\t\t\t\t// Deferred cleanup prevents needless reconnection when:\n\t\t\t\t// - React Strict Mode's unmount/remount cycle\n\t\t\t\t// - useActor hook moves between components in the same render cycle\n\t\t\t\tcached.cleanupTimeout = setTimeout(() => {\n\t\t\t\t\tcached.cleanupTimeout = null;\n\t\t\t\t\tif (cached.refCount > 0) return;\n\n\t\t\t\t\t// Unsubscribe from derived/effect\n\t\t\t\t\tunsubscribeDerived?.();\n\t\t\t\t\tunsubscribeEffect?.();\n\t\t\t\t\tunsubscribeDerived = null;\n\t\t\t\t\tunsubscribeEffect = null;\n\n\t\t\t\t\t// Dispose connection\n\t\t\t\t\tconst actor = store.state.actors[key];\n\t\t\t\t\tif (actor?.connection) {\n\t\t\t\t\t\tactor.connection.dispose();\n\t\t\t\t\t}\n\n\t\t\t\t\t// Remove from store and cache\n\t\t\t\t\tstore.setState((prev) => {\n\t\t\t\t\t\tconst { [key]: _, ...rest } = prev.actors;\n\t\t\t\t\t\treturn { ...prev, actors: rest };\n\t\t\t\t\t});\n\t\t\t\t\tcache.delete(key);\n\t\t\t\t}, 0);\n\t\t\t}\n\t\t};\n\t};\n\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tconst boundCreate: () => void = () => (create as any)(client, store, key);\n\tcache.set(key, {\n\t\tstate: derived,\n\t\tkey,\n\t\tmount,\n\t\tcreate: boundCreate,\n\t\trefCount: 0,\n\t\tcleanupTimeout: null,\n\t});\n\n\treturn {\n\t\tmount,\n\t\tstate: derived as ActorsStateDerived<Registry, ActorName>,\n\t\tkey,\n\t};\n}\n\nfunction create<\n\tRegistry extends AnyActorRegistry,\n\tActorName extends keyof ExtractActorsFromRegistry<Registry> & string,\n>(client: Client<Registry>, store: Store<InternalRivetKitStore>, key: string) {\n\tconst actor = store.state.actors[key];\n\tif (!actor) {\n\t\tthrow new Error(\n\t\t\t`Actor with key \"${key}\" not found in store. This indicates a bug in cleanup logic.`,\n\t\t);\n\t}\n\n\t// Save actor to map\n\tupdateActor(store, key, {\n\t\tconnStatus: \"connecting\",\n\t\terror: null,\n\t});\n\n\ttry {\n\t\tconst handle = actor.opts.noCreate\n\t\t\t? client.get(actor.opts.name as string, actor.opts.key, {\n\t\t\t\t\tparams: actor.opts.params,\n\t\t\t\t})\n\t\t\t: client.getOrCreate(actor.opts.name as string, actor.opts.key, {\n\t\t\t\t\tparams: actor.opts.params,\n\t\t\t\t\tcreateInRegion: actor.opts.createInRegion,\n\t\t\t\t\tcreateWithInput: actor.opts.createWithInput,\n\t\t\t\t});\n\n\t\tconst connection = handle.connect();\n\n\t\t// Store connection BEFORE registering callbacks to avoid race condition\n\t\t// where status change fires before connection is stored\n\t\tupdateActor(store, key, {\n\t\t\thandle: handle as ActorHandle<\n\t\t\t\tExtractActorsFromRegistry<Registry>[ActorName]\n\t\t\t>,\n\t\t\tconnection: connection as ActorConn<\n\t\t\t\tExtractActorsFromRegistry<Registry>[ActorName]\n\t\t\t>,\n\t\t});\n\n\t\t// Subscribe to connection state changes\n\t\tconnection.onStatusChange((status) => {\n\t\t\tstore.setState((prev) => {\n\t\t\t\t// Only update if this is still the active connection\n\t\t\t\tconst isActiveConnection =\n\t\t\t\t\tprev.actors[key]?.connection === connection;\n\t\t\t\tif (!isActiveConnection) return prev;\n\t\t\t\treturn {\n\t\t\t\t\t...prev,\n\t\t\t\t\tactors: {\n\t\t\t\t\t\t...prev.actors,\n\t\t\t\t\t\t[key]: {\n\t\t\t\t\t\t\t...prev.actors[key],\n\t\t\t\t\t\t\tconnStatus: status,\n\t\t\t\t\t\t\t// Only clear error when successfully connected\n\t\t\t\t\t\t\t...(status === \"connected\" ? { error: null } : {}),\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t});\n\t\t});\n\n\t\t// onError is followed by onClose which will set connStatus to Disconnected\n\t\tconnection.onError((error) => {\n\t\t\tstore.setState((prev) => {\n\t\t\t\t// Only update if this is still the active connection\n\t\t\t\tif (prev.actors[key]?.connection !== connection) return prev;\n\t\t\t\treturn {\n\t\t\t\t\t...prev,\n\t\t\t\t\tactors: {\n\t\t\t\t\t\t...prev.actors,\n\t\t\t\t\t\t[key]: {\n\t\t\t\t\t\t\t...prev.actors[key],\n\t\t\t\t\t\t\terror,\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t});\n\t\t});\n\t} catch (error) {\n\t\tconsole.error(\"Failed to create actor connection\", error);\n\t\t// Use Disconnected so Effect won't auto-retry\n\t\t// User must re-enable or take action to retry\n\t\tupdateActor(store, key, {\n\t\t\tconnStatus: \"disconnected\",\n\t\t\terror: error as Error,\n\t\t});\n\t}\n}\n\nfunction defaultHashFunction({ name, key, params, noCreate }: AnyActorOptions) {\n\treturn JSON.stringify({ name, key, params, noCreate });\n}\n\nfunction optsEqual(a: AnyActorOptions, b: AnyActorOptions) {\n\treturn equal(a, b);\n}\n"]}
|
package/dist/mod.mjs
CHANGED
|
@@ -8,7 +8,13 @@ function createRivetKit(client, createOpts = {}) {
|
|
|
8
8
|
});
|
|
9
9
|
const cache = /* @__PURE__ */ new Map();
|
|
10
10
|
return {
|
|
11
|
-
getOrCreateActor: (actorOpts) => getOrCreateActor(
|
|
11
|
+
getOrCreateActor: (actorOpts) => getOrCreateActor(
|
|
12
|
+
client,
|
|
13
|
+
createOpts,
|
|
14
|
+
store,
|
|
15
|
+
cache,
|
|
16
|
+
actorOpts
|
|
17
|
+
),
|
|
12
18
|
store
|
|
13
19
|
};
|
|
14
20
|
}
|
package/dist/mod.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/mod.ts"],"sourcesContent":["import { Derived, Effect, Store } from \"@tanstack/store\";\nimport equal from \"fast-deep-equal\";\nimport type { AnyActorDefinition, Registry } from \"rivetkit\";\nimport {\n\ttype ActorConn,\n\ttype ActorConnStatus,\n\ttype ActorHandle,\n\ttype Client,\n\ttype ExtractActorsFromRegistry,\n} from \"rivetkit/client\";\n\nexport type AnyActorRegistry = Registry<any>;\n\nexport type { ActorConnStatus };\n\ninterface ActorStateReference {\n\t/**\n\t * The unique identifier for the actor.\n\t * This is a hash generated from the actor's options.\n\t * It is used to identify the actor instance in the store.\n\t * @internal\n\t */\n\thash: string;\n\t/**\n\t * The state of the actor, derived from the store.\n\t * This includes the actor's connection and handle.\n\t */\n\thandle: ActorHandle<AnyActorDefinition> | null;\n\t/**\n\t * The connection to the actor.\n\t * This is used to communicate with the actor in realtime.\n\t */\n\tconnection: ActorConn<AnyActorDefinition> | null;\n\t/**\n\t * The connection status of the actor.\n\t */\n\tconnStatus: ActorConnStatus;\n\t/**\n\t * The error that occurred while trying to connect to the actor, if any.\n\t */\n\terror: Error | null;\n\t/**\n\t * Options for the actor, including its name, key, parameters, and whether it is enabled.\n\t */\n\topts: {\n\t\tname: string;\n\t\t/**\n\t\t * Unique key for the actor instance.\n\t\t * This can be a string or an array of strings to create multiple instances.\n\t\t * @example \"abc\" or [\"abc\", \"def\"]\n\t\t */\n\t\tkey: string | string[];\n\t\t/**\n\t\t * Parameters for the actor.\n\t\t * These are additional options that can be passed to the actor.\n\t\t */\n\t\tparams?: unknown;\n\t\t/** Region to create the actor in if it doesn't exist. */\n\t\tcreateInRegion?: string;\n\t\t/** Input data to pass to the actor. */\n\t\tcreateWithInput?: unknown;\n\t\t/**\n\t\t * Whether the actor is enabled.\n\t\t * Defaults to true.\n\t\t */\n\t\tenabled?: boolean;\n\t\t/**\n\t\t * If true, only gets the actor if it already exists. Does not create the actor.\n\t\t * Throws an error if the actor is not found.\n\t\t * Defaults to false.\n\t\t */\n\t\tnoCreate?: boolean;\n\t};\n}\n\ninterface InternalRivetKitStore {\n\tactors: Record<string, ActorStateReference>;\n}\n\n/**\n * Options for configuring a actor in RivetKit.\n */\nexport interface ActorOptions<\n\tRegistry extends AnyActorRegistry,\n\tActorName extends keyof ExtractActorsFromRegistry<Registry> & string,\n> {\n\t/**\n\t * Typesafe name of the actor.\n\t * This should match the actor's name in the app's actor definitions.\n\t * @example \"chatRoom\"\n\t */\n\tname: ActorName;\n\t/**\n\t * Unique key for the actor instance.\n\t * This can be a string or an array of strings to create multiple instances.\n\t * @example \"abc\" or [\"abc\", \"def\"]\n\t */\n\tkey: string | string[];\n\t/**\n\t * Parameters for the actor.\n\t */\n\tparams?: ExtractActorsFromRegistry<Registry>[ActorName][\"params\"];\n\t/** Region to create the actor in if it doesn't exist. */\n\tcreateInRegion?: string;\n\t/** Input data to pass to the actor. */\n\tcreateWithInput?: unknown;\n\t/**\n\t * Whether the actor is enabled.\n\t * Defaults to true.\n\t */\n\tenabled?: boolean;\n\t/**\n\t * If true, only gets the actor if it already exists. Does not create the actor.\n\t * Throws an error if the actor is not found.\n\t * Defaults to false.\n\t */\n\tnoCreate?: boolean;\n}\n\nexport type ActorsStateDerived<\n\tRegistry extends AnyActorRegistry,\n\tWorkerName extends keyof ExtractActorsFromRegistry<Registry> & string,\n> = Derived<\n\tOmit<InternalRivetKitStore[\"actors\"][string], \"handle\" | \"connection\"> & {\n\t\thandle: ActorHandle<\n\t\t\tExtractActorsFromRegistry<Registry>[WorkerName]\n\t\t> | null;\n\t\tconnection: ActorConn<\n\t\t\tExtractActorsFromRegistry<Registry>[WorkerName]\n\t\t> | null;\n\t\t/** @deprecated Use `connStatus === \"connected\"` instead */\n\t\tisConnected: boolean;\n\t}\n>;\n\nexport type AnyActorOptions = ActorOptions<AnyActorRegistry, any>;\n\nexport interface CreateRivetKitOptions<Registry extends AnyActorRegistry> {\n\thashFunction?: (opts: ActorOptions<Registry, any>) => string;\n}\n\ntype ComputedActorState = InternalRivetKitStore[\"actors\"][string] & {\n\t/** @deprecated Use `connStatus === \"connected\"` instead */\n\tisConnected: boolean;\n};\n\ntype ActorCache = Map<\n\tstring,\n\t{\n\t\tstate: Derived<ComputedActorState>;\n\t\tkey: string;\n\t\tmount: () => () => void;\n\t\tcreate: () => void;\n\t\trefCount: number;\n\t\tcleanupTimeout: ReturnType<typeof setTimeout> | null;\n\t}\n>;\n\nexport function createRivetKit<Registry extends AnyActorRegistry>(\n\tclient: Client<Registry>,\n\tcreateOpts: CreateRivetKitOptions<Registry> = {},\n) {\n\tconst store = new Store<InternalRivetKitStore>({\n\t\tactors: {},\n\t});\n\n\tconst cache: ActorCache = new Map();\n\n\treturn {\n\t\tgetOrCreateActor: <\n\t\t\tActorName extends keyof ExtractActorsFromRegistry<Registry> &\n\t\t\t\tstring,\n\t\t>(\n\t\t\tactorOpts: ActorOptions<Registry, ActorName>,\n\t\t): {\n\t\t\tmount: () => () => void;\n\t\t\tstate: ActorsStateDerived<Registry, ActorName>;\n\t\t\tkey: string;\n\t\t} => (getOrCreateActor as any)(client, createOpts, store, cache, actorOpts),\n\t\tstore,\n\t};\n}\n\ntype ActorUpdates = Partial<InternalRivetKitStore[\"actors\"][string]>;\n\nfunction updateActor(\n\tstore: Store<InternalRivetKitStore>,\n\tkey: string,\n\tupdates: ActorUpdates,\n) {\n\tstore.setState((prev) => ({\n\t\t...prev,\n\t\tactors: {\n\t\t\t...prev.actors,\n\t\t\t[key]: { ...prev.actors[key], ...updates },\n\t\t},\n\t}));\n}\n\n// See README.md for lifecycle documentation.\nfunction getOrCreateActor<\n\tRegistry extends AnyActorRegistry,\n\tActorName extends keyof ExtractActorsFromRegistry<Registry> & string,\n>(\n\tclient: Client<Registry>,\n\tcreateOpts: CreateRivetKitOptions<Registry>,\n\tstore: Store<InternalRivetKitStore>,\n\tcache: ActorCache,\n\tactorOpts: ActorOptions<Registry, ActorName>,\n) {\n\tconst hash = createOpts.hashFunction || defaultHashFunction;\n\n\tconst normalizedOpts = {\n\t\t...actorOpts,\n\t\tenabled: actorOpts.enabled ?? true,\n\t};\n\n\tconst key = hash(normalizedOpts);\n\n\t// Sync opts to store on every call (even for cached entries)\n\t// Use queueMicrotask for updates to avoid \"Cannot update a component while rendering\" React error\n\tconst existing = store.state.actors[key];\n\tif (!existing) {\n\t\tstore.setState((prev) => ({\n\t\t\t...prev,\n\t\t\tactors: {\n\t\t\t\t...prev.actors,\n\t\t\t\t[key]: {\n\t\t\t\t\thash: key,\n\t\t\t\t\tconnStatus: \"idle\",\n\t\t\t\t\tconnection: null,\n\t\t\t\t\thandle: null,\n\t\t\t\t\terror: null,\n\t\t\t\t\topts: normalizedOpts,\n\t\t\t\t},\n\t\t\t},\n\t\t}));\n\t} else if (!optsEqual(existing.opts, normalizedOpts)) {\n\t\t// Defer opts update to avoid triggering re-render during render\n\t\tqueueMicrotask(() => {\n\t\t\tupdateActor(store, key, { opts: normalizedOpts });\n\t\t});\n\t}\n\n\tconst cached = cache.get(key);\n\tif (cached) {\n\t\treturn {\n\t\t\t...cached,\n\t\t\tstate: cached.state as ActorsStateDerived<Registry, ActorName>,\n\t\t};\n\t}\n\n\tconst derived = new Derived({\n\t\tfn: ({ currDepVals: [store] }) => {\n\t\t\tconst actor = store.actors[key];\n\t\t\treturn {\n\t\t\t\t...actor,\n\t\t\t\t/** @deprecated Use `connStatus === \"connected\"` instead */\n\t\t\t\tisConnected: actor.connStatus === \"connected\",\n\t\t\t};\n\t\t},\n\t\tdeps: [store],\n\t});\n\n\t// Handle enabled/disabled state changes.\n\t// Initial connection is triggered directly in mount() since Effect\n\t// only runs on state changes, not on mount.\n\tconst effect = new Effect({\n\t\tfn: () => {\n\t\t\tconst actor = store.state.actors[key];\n\t\t\tif (!actor) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Actor with key \"${key}\" not found in store. This indicates a bug in cleanup logic.`,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\t// Dispose connection if disabled\n\t\t\tif (!actor.opts.enabled && actor.connection) {\n\t\t\t\tactor.connection.dispose();\n\n\t\t\t\t// Reset state so re-enabling will reconnect\n\t\t\t\tupdateActor(store, key, {\n\t\t\t\t\tconnection: null,\n\t\t\t\t\thandle: null,\n\t\t\t\t\tconnStatus: \"idle\",\n\t\t\t\t});\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Reconnect when re-enabled after being disabled\n\t\t\t// Defer to avoid \"Cannot update a component while rendering\" React error\n\t\t\tif (actor.connStatus === \"idle\" && actor.opts.enabled) {\n\t\t\t\tqueueMicrotask(() => {\n\t\t\t\t\t// Re-check state after microtask in case it changed\n\t\t\t\t\tconst currentActor = store.state.actors[key];\n\t\t\t\t\tif (\n\t\t\t\t\t\tcurrentActor &&\n\t\t\t\t\t\tcurrentActor.connStatus === \"idle\" &&\n\t\t\t\t\t\tcurrentActor.opts.enabled\n\t\t\t\t\t) {\n\t\t\t\t\t\t(create as Function)(client, store, key);\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\t\t},\n\t\tdeps: [derived],\n\t});\n\n\t// Track subscriptions for ref counting\n\tlet unsubscribeDerived: (() => void) | null = null;\n\tlet unsubscribeEffect: (() => void) | null = null;\n\n\tconst mount = () => {\n\t\tconst cached = cache.get(key);\n\t\tif (!cached) {\n\t\t\tthrow new Error(\n\t\t\t\t`Actor with key \"${key}\" not found in cache. This indicates a bug in cleanup logic.`,\n\t\t\t);\n\t\t}\n\n\t\t// Cancel pending cleanup\n\t\tif (cached.cleanupTimeout !== null) {\n\t\t\tclearTimeout(cached.cleanupTimeout);\n\t\t\tcached.cleanupTimeout = null;\n\t\t}\n\n\t\t// Increment ref count\n\t\tcached.refCount++;\n\n\t\t// Mount derived/effect on first reference (or re-mount after cleanup)\n\t\tif (cached.refCount === 1) {\n\t\t\tunsubscribeDerived = derived.mount();\n\t\t\tunsubscribeEffect = effect.mount();\n\n\t\t\t// Effect doesn't run immediately on mount, only on state changes.\n\t\t\t// Trigger initial connection if actor is enabled and idle.\n\t\t\tconst actor = store.state.actors[key];\n\t\t\tif (actor && actor.opts.enabled && actor.connStatus === \"idle\") {\n\t\t\t\t(create as Function)(client, store, key);\n\t\t\t}\n\t\t}\n\n\t\treturn () => {\n\t\t\t// Decrement ref count\n\t\t\tcached.refCount--;\n\n\t\t\tif (cached.refCount === 0) {\n\t\t\t\t// Deferred cleanup prevents needless reconnection when:\n\t\t\t\t// - React Strict Mode's unmount/remount cycle\n\t\t\t\t// - useActor hook moves between components in the same render cycle\n\t\t\t\tcached.cleanupTimeout = setTimeout(() => {\n\t\t\t\t\tcached.cleanupTimeout = null;\n\t\t\t\t\tif (cached.refCount > 0) return;\n\n\t\t\t\t\t// Unsubscribe from derived/effect\n\t\t\t\t\tunsubscribeDerived?.();\n\t\t\t\t\tunsubscribeEffect?.();\n\t\t\t\t\tunsubscribeDerived = null;\n\t\t\t\t\tunsubscribeEffect = null;\n\n\t\t\t\t\t// Dispose connection\n\t\t\t\t\tconst actor = store.state.actors[key];\n\t\t\t\t\tif (actor?.connection) {\n\t\t\t\t\t\tactor.connection.dispose();\n\t\t\t\t\t}\n\n\t\t\t\t\t// Remove from store and cache\n\t\t\t\t\tstore.setState((prev) => {\n\t\t\t\t\t\tconst { [key]: _, ...rest } = prev.actors;\n\t\t\t\t\t\treturn { ...prev, actors: rest };\n\t\t\t\t\t});\n\t\t\t\t\tcache.delete(key);\n\t\t\t\t}, 0);\n\t\t\t}\n\t\t};\n\t};\n\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tconst boundCreate: () => void = () => (create as any)(client, store, key);\n\tcache.set(key, {\n\t\tstate: derived,\n\t\tkey,\n\t\tmount,\n\t\tcreate: boundCreate,\n\t\trefCount: 0,\n\t\tcleanupTimeout: null,\n\t});\n\n\treturn {\n\t\tmount,\n\t\tstate: derived as ActorsStateDerived<Registry, ActorName>,\n\t\tkey,\n\t};\n}\n\nfunction create<\n\tRegistry extends AnyActorRegistry,\n\tActorName extends keyof ExtractActorsFromRegistry<Registry> & string,\n>(client: Client<Registry>, store: Store<InternalRivetKitStore>, key: string) {\n\tconst actor = store.state.actors[key];\n\tif (!actor) {\n\t\tthrow new Error(\n\t\t\t`Actor with key \"${key}\" not found in store. This indicates a bug in cleanup logic.`,\n\t\t);\n\t}\n\n\t// Save actor to map\n\tupdateActor(store, key, {\n\t\tconnStatus: \"connecting\",\n\t\terror: null,\n\t});\n\n\ttry {\n\t\tconst handle = actor.opts.noCreate\n\t\t\t? client.get(actor.opts.name as string, actor.opts.key, {\n\t\t\t\t\tparams: actor.opts.params,\n\t\t\t\t})\n\t\t\t: client.getOrCreate(actor.opts.name as string, actor.opts.key, {\n\t\t\t\t\tparams: actor.opts.params,\n\t\t\t\t\tcreateInRegion: actor.opts.createInRegion,\n\t\t\t\t\tcreateWithInput: actor.opts.createWithInput,\n\t\t\t\t});\n\n\t\tconst connection = handle.connect();\n\n\t\t// Store connection BEFORE registering callbacks to avoid race condition\n\t\t// where status change fires before connection is stored\n\t\tupdateActor(store, key, {\n\t\t\thandle: handle as ActorHandle<\n\t\t\t\tExtractActorsFromRegistry<Registry>[ActorName]\n\t\t\t>,\n\t\t\tconnection: connection as ActorConn<\n\t\t\t\tExtractActorsFromRegistry<Registry>[ActorName]\n\t\t\t>,\n\t\t});\n\n\t\t// Subscribe to connection state changes\n\t\tconnection.onStatusChange((status) => {\n\t\t\tstore.setState((prev) => {\n\t\t\t\t// Only update if this is still the active connection\n\t\t\t\tconst isActiveConnection =\n\t\t\t\t\tprev.actors[key]?.connection === connection;\n\t\t\t\tif (!isActiveConnection) return prev;\n\t\t\t\treturn {\n\t\t\t\t\t...prev,\n\t\t\t\t\tactors: {\n\t\t\t\t\t\t...prev.actors,\n\t\t\t\t\t\t[key]: {\n\t\t\t\t\t\t\t...prev.actors[key],\n\t\t\t\t\t\t\tconnStatus: status,\n\t\t\t\t\t\t\t// Only clear error when successfully connected\n\t\t\t\t\t\t\t...(status === \"connected\" ? { error: null } : {}),\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t});\n\t\t});\n\n\t\t// onError is followed by onClose which will set connStatus to Disconnected\n\t\tconnection.onError((error) => {\n\t\t\tstore.setState((prev) => {\n\t\t\t\t// Only update if this is still the active connection\n\t\t\t\tif (prev.actors[key]?.connection !== connection) return prev;\n\t\t\t\treturn {\n\t\t\t\t\t...prev,\n\t\t\t\t\tactors: {\n\t\t\t\t\t\t...prev.actors,\n\t\t\t\t\t\t[key]: {\n\t\t\t\t\t\t\t...prev.actors[key],\n\t\t\t\t\t\t\terror,\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t});\n\t\t});\n\t} catch (error) {\n\t\tconsole.error(\"Failed to create actor connection\", error);\n\t\t// Use Disconnected so Effect won't auto-retry\n\t\t// User must re-enable or take action to retry\n\t\tupdateActor(store, key, {\n\t\t\tconnStatus: \"disconnected\",\n\t\t\terror: error as Error,\n\t\t});\n\t}\n}\n\nfunction defaultHashFunction({ name, key, params, noCreate }: AnyActorOptions) {\n\treturn JSON.stringify({ name, key, params, noCreate });\n}\n\nfunction optsEqual(a: AnyActorOptions, b: AnyActorOptions) {\n\treturn equal(a, b);\n}\n"],"mappings":";AAAA,SAAS,SAAS,QAAQ,aAAa;AACvC,OAAO,WAAW;AAElB,OAMO;AAqJA,SAAS,eACf,QACA,aAA8C,CAAC,GAC9C;AACD,QAAM,QAAQ,IAAI,MAA6B;AAAA,IAC9C,QAAQ,CAAC;AAAA,EACV,CAAC;AAED,QAAM,QAAoB,oBAAI,IAAI;AAElC,SAAO;AAAA,IACN,kBAAkB,CAIjB,cAKK,iBAAyB,QAAQ,YAAY,OAAO,OAAO,SAAS;AAAA,IAC1E;AAAA,EACD;AACD;AAIA,SAAS,YACR,OACA,KACA,SACC;AACD,QAAM,SAAS,CAAC,UAAU;AAAA,IACzB,GAAG;AAAA,IACH,QAAQ;AAAA,MACP,GAAG,KAAK;AAAA,MACR,CAAC,GAAG,GAAG,EAAE,GAAG,KAAK,OAAO,GAAG,GAAG,GAAG,QAAQ;AAAA,IAC1C;AAAA,EACD,EAAE;AACH;AAGA,SAAS,iBAIR,QACA,YACA,OACA,OACA,WACC;AACD,QAAM,OAAO,WAAW,gBAAgB;AAExC,QAAM,iBAAiB;AAAA,IACtB,GAAG;AAAA,IACH,SAAS,UAAU,WAAW;AAAA,EAC/B;AAEA,QAAM,MAAM,KAAK,cAAc;AAI/B,QAAM,WAAW,MAAM,MAAM,OAAO,GAAG;AACvC,MAAI,CAAC,UAAU;AACd,UAAM,SAAS,CAAC,UAAU;AAAA,MACzB,GAAG;AAAA,MACH,QAAQ;AAAA,QACP,GAAG,KAAK;AAAA,QACR,CAAC,GAAG,GAAG;AAAA,UACN,MAAM;AAAA,UACN,YAAY;AAAA,UACZ,YAAY;AAAA,UACZ,QAAQ;AAAA,UACR,OAAO;AAAA,UACP,MAAM;AAAA,QACP;AAAA,MACD;AAAA,IACD,EAAE;AAAA,EACH,WAAW,CAAC,UAAU,SAA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AAC,UAAU;AAC7B,YAAM,SAAS,CAAC,SAAS;AA5c5B;AA8cI,cAAI,UAAK,OAAO,GAAG,MAAf,mBAAkB,gBAAe,WAAY,QAAO;AACxD,eAAO;AAAA,UACN,GAAG;AAAA,UACH,QAAQ;AAAA,YACP,GAAG,KAAK;AAAA,YACR,CAAC,GAAG,GAAG;AAAA,cACN,GAAG,KAAK,OAAO,GAAG;AAAA,cAClB;AAAA,YACD;AAAA,UACD;AAAA,QACD;AAAA,MACD,CAAC;AAAA,IACF,CAAC;AAAA,EACF,SAAS,OAAO;AACf,YAAQ,MAAM,qCAAqC,KAAK;AAGxD,gBAAY,OAAO,KAAK;AAAA,MACvB,YAAY;AAAA,MACZ;AAAA,IACD,CAAC;AAAA,EACF;AACD;AAEA,SAAS,oBAAoB,EAAE,MAAM,KAAK,QAAQ,SAAS,GAAoB;AAC9E,SAAO,KAAK,UAAU,EAAE,MAAM,KAAK,QAAQ,SAAS,CAAC;AACtD;AAEA,SAAS,UAAU,GAAoB,GAAoB;AAC1D,SAAO,MAAM,GAAG,CAAC;AAClB;","names":["store","cached"]}
|
|
1
|
+
{"version":3,"sources":["../src/mod.ts"],"sourcesContent":["import { Derived, Effect, Store } from \"@tanstack/store\";\nimport equal from \"fast-deep-equal\";\nimport type { AnyActorDefinition, Registry } from \"rivetkit\";\nimport {\n\ttype ActorConn,\n\ttype ActorConnStatus,\n\ttype ActorHandle,\n\ttype Client,\n\ttype ExtractActorsFromRegistry,\n} from \"rivetkit/client\";\n\nexport type AnyActorRegistry = Registry<any>;\n\nexport type { ActorConnStatus };\n\ninterface ActorStateReference {\n\t/**\n\t * The unique identifier for the actor.\n\t * This is a hash generated from the actor's options.\n\t * It is used to identify the actor instance in the store.\n\t * @internal\n\t */\n\thash: string;\n\t/**\n\t * The state of the actor, derived from the store.\n\t * This includes the actor's connection and handle.\n\t */\n\thandle: ActorHandle<AnyActorDefinition> | null;\n\t/**\n\t * The connection to the actor.\n\t * This is used to communicate with the actor in realtime.\n\t */\n\tconnection: ActorConn<AnyActorDefinition> | null;\n\t/**\n\t * The connection status of the actor.\n\t */\n\tconnStatus: ActorConnStatus;\n\t/**\n\t * The error that occurred while trying to connect to the actor, if any.\n\t */\n\terror: Error | null;\n\t/**\n\t * Options for the actor, including its name, key, parameters, and whether it is enabled.\n\t */\n\topts: {\n\t\tname: string;\n\t\t/**\n\t\t * Unique key for the actor instance.\n\t\t * This can be a string or an array of strings to create multiple instances.\n\t\t * @example \"abc\" or [\"abc\", \"def\"]\n\t\t */\n\t\tkey: string | string[];\n\t\t/**\n\t\t * Parameters for the actor.\n\t\t * These are additional options that can be passed to the actor.\n\t\t */\n\t\tparams?: unknown;\n\t\t/** Region to create the actor in if it doesn't exist. */\n\t\tcreateInRegion?: string;\n\t\t/** Input data to pass to the actor. */\n\t\tcreateWithInput?: unknown;\n\t\t/**\n\t\t * Whether the actor is enabled.\n\t\t * Defaults to true.\n\t\t */\n\t\tenabled?: boolean;\n\t\t/**\n\t\t * If true, only gets the actor if it already exists. Does not create the actor.\n\t\t * Throws an error if the actor is not found.\n\t\t * Defaults to false.\n\t\t */\n\t\tnoCreate?: boolean;\n\t};\n}\n\ninterface InternalRivetKitStore {\n\tactors: Record<string, ActorStateReference>;\n}\n\n/**\n * Options for configuring a actor in RivetKit.\n */\nexport interface ActorOptions<\n\tRegistry extends AnyActorRegistry,\n\tActorName extends keyof ExtractActorsFromRegistry<Registry> & string,\n> {\n\t/**\n\t * Typesafe name of the actor.\n\t * This should match the actor's name in the app's actor definitions.\n\t * @example \"chatRoom\"\n\t */\n\tname: ActorName;\n\t/**\n\t * Unique key for the actor instance.\n\t * This can be a string or an array of strings to create multiple instances.\n\t * @example \"abc\" or [\"abc\", \"def\"]\n\t */\n\tkey: string | string[];\n\t/**\n\t * Parameters for the actor.\n\t */\n\tparams?: ExtractActorsFromRegistry<Registry>[ActorName][\"params\"];\n\t/** Region to create the actor in if it doesn't exist. */\n\tcreateInRegion?: string;\n\t/** Input data to pass to the actor. */\n\tcreateWithInput?: unknown;\n\t/**\n\t * Whether the actor is enabled.\n\t * Defaults to true.\n\t */\n\tenabled?: boolean;\n\t/**\n\t * If true, only gets the actor if it already exists. Does not create the actor.\n\t * Throws an error if the actor is not found.\n\t * Defaults to false.\n\t */\n\tnoCreate?: boolean;\n}\n\nexport type ActorsStateDerived<\n\tRegistry extends AnyActorRegistry,\n\tWorkerName extends keyof ExtractActorsFromRegistry<Registry> & string,\n> = Derived<\n\tOmit<InternalRivetKitStore[\"actors\"][string], \"handle\" | \"connection\"> & {\n\t\thandle: ActorHandle<\n\t\t\tExtractActorsFromRegistry<Registry>[WorkerName]\n\t\t> | null;\n\t\tconnection: ActorConn<\n\t\t\tExtractActorsFromRegistry<Registry>[WorkerName]\n\t\t> | null;\n\t\t/** @deprecated Use `connStatus === \"connected\"` instead */\n\t\tisConnected: boolean;\n\t}\n>;\n\nexport type AnyActorOptions = ActorOptions<AnyActorRegistry, any>;\n\nexport interface CreateRivetKitOptions<Registry extends AnyActorRegistry> {\n\thashFunction?: (opts: ActorOptions<Registry, any>) => string;\n}\n\ntype ComputedActorState = InternalRivetKitStore[\"actors\"][string] & {\n\t/** @deprecated Use `connStatus === \"connected\"` instead */\n\tisConnected: boolean;\n};\n\ntype ActorCache = Map<\n\tstring,\n\t{\n\t\tstate: Derived<ComputedActorState>;\n\t\tkey: string;\n\t\tmount: () => () => void;\n\t\tcreate: () => void;\n\t\trefCount: number;\n\t\tcleanupTimeout: ReturnType<typeof setTimeout> | null;\n\t}\n>;\n\nexport function createRivetKit<Registry extends AnyActorRegistry>(\n\tclient: Client<Registry>,\n\tcreateOpts: CreateRivetKitOptions<Registry> = {},\n) {\n\tconst store = new Store<InternalRivetKitStore>({\n\t\tactors: {},\n\t});\n\n\tconst cache: ActorCache = new Map();\n\n\treturn {\n\t\tgetOrCreateActor: <\n\t\t\tActorName extends keyof ExtractActorsFromRegistry<Registry> &\n\t\t\t\tstring,\n\t\t>(\n\t\t\tactorOpts: ActorOptions<Registry, ActorName>,\n\t\t): {\n\t\t\tmount: () => () => void;\n\t\t\tstate: ActorsStateDerived<Registry, ActorName>;\n\t\t\tkey: string;\n\t\t} =>\n\t\t\t(getOrCreateActor as any)(\n\t\t\t\tclient,\n\t\t\t\tcreateOpts,\n\t\t\t\tstore,\n\t\t\t\tcache,\n\t\t\t\tactorOpts,\n\t\t\t),\n\t\tstore,\n\t};\n}\n\ntype ActorUpdates = Partial<InternalRivetKitStore[\"actors\"][string]>;\n\nfunction updateActor(\n\tstore: Store<InternalRivetKitStore>,\n\tkey: string,\n\tupdates: ActorUpdates,\n) {\n\tstore.setState((prev) => ({\n\t\t...prev,\n\t\tactors: {\n\t\t\t...prev.actors,\n\t\t\t[key]: { ...prev.actors[key], ...updates },\n\t\t},\n\t}));\n}\n\n// See README.md for lifecycle documentation.\nfunction getOrCreateActor<\n\tRegistry extends AnyActorRegistry,\n\tActorName extends keyof ExtractActorsFromRegistry<Registry> & string,\n>(\n\tclient: Client<Registry>,\n\tcreateOpts: CreateRivetKitOptions<Registry>,\n\tstore: Store<InternalRivetKitStore>,\n\tcache: ActorCache,\n\tactorOpts: ActorOptions<Registry, ActorName>,\n) {\n\tconst hash = createOpts.hashFunction || defaultHashFunction;\n\n\tconst normalizedOpts = {\n\t\t...actorOpts,\n\t\tenabled: actorOpts.enabled ?? true,\n\t};\n\n\tconst key = hash(normalizedOpts);\n\n\t// Sync opts to store on every call (even for cached entries)\n\t// Use queueMicrotask for updates to avoid \"Cannot update a component while rendering\" React error\n\tconst existing = store.state.actors[key];\n\tif (!existing) {\n\t\tstore.setState((prev) => ({\n\t\t\t...prev,\n\t\t\tactors: {\n\t\t\t\t...prev.actors,\n\t\t\t\t[key]: {\n\t\t\t\t\thash: key,\n\t\t\t\t\tconnStatus: \"idle\",\n\t\t\t\t\tconnection: null,\n\t\t\t\t\thandle: null,\n\t\t\t\t\terror: null,\n\t\t\t\t\topts: normalizedOpts,\n\t\t\t\t},\n\t\t\t},\n\t\t}));\n\t} else if (!optsEqual(existing.opts, normalizedOpts)) {\n\t\t// Defer opts update to avoid triggering re-render during render\n\t\tqueueMicrotask(() => {\n\t\t\tupdateActor(store, key, { opts: normalizedOpts });\n\t\t});\n\t}\n\n\tconst cached = cache.get(key);\n\tif (cached) {\n\t\treturn {\n\t\t\t...cached,\n\t\t\tstate: cached.state as ActorsStateDerived<Registry, ActorName>,\n\t\t};\n\t}\n\n\tconst derived = new Derived({\n\t\tfn: ({ currDepVals: [store] }) => {\n\t\t\tconst actor = store.actors[key];\n\t\t\treturn {\n\t\t\t\t...actor,\n\t\t\t\t/** @deprecated Use `connStatus === \"connected\"` instead */\n\t\t\t\tisConnected: actor.connStatus === \"connected\",\n\t\t\t};\n\t\t},\n\t\tdeps: [store],\n\t});\n\n\t// Handle enabled/disabled state changes.\n\t// Initial connection is triggered directly in mount() since Effect\n\t// only runs on state changes, not on mount.\n\tconst effect = new Effect({\n\t\tfn: () => {\n\t\t\tconst actor = store.state.actors[key];\n\t\t\tif (!actor) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Actor with key \"${key}\" not found in store. This indicates a bug in cleanup logic.`,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\t// Dispose connection if disabled\n\t\t\tif (!actor.opts.enabled && actor.connection) {\n\t\t\t\tactor.connection.dispose();\n\n\t\t\t\t// Reset state so re-enabling will reconnect\n\t\t\t\tupdateActor(store, key, {\n\t\t\t\t\tconnection: null,\n\t\t\t\t\thandle: null,\n\t\t\t\t\tconnStatus: \"idle\",\n\t\t\t\t});\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Reconnect when re-enabled after being disabled\n\t\t\t// Defer to avoid \"Cannot update a component while rendering\" React error\n\t\t\tif (actor.connStatus === \"idle\" && actor.opts.enabled) {\n\t\t\t\tqueueMicrotask(() => {\n\t\t\t\t\t// Re-check state after microtask in case it changed\n\t\t\t\t\tconst currentActor = store.state.actors[key];\n\t\t\t\t\tif (\n\t\t\t\t\t\tcurrentActor &&\n\t\t\t\t\t\tcurrentActor.connStatus === \"idle\" &&\n\t\t\t\t\t\tcurrentActor.opts.enabled\n\t\t\t\t\t) {\n\t\t\t\t\t\t(create as Function)(client, store, key);\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\t\t},\n\t\tdeps: [derived],\n\t});\n\n\t// Track subscriptions for ref counting\n\tlet unsubscribeDerived: (() => void) | null = null;\n\tlet unsubscribeEffect: (() => void) | null = null;\n\n\tconst mount = () => {\n\t\tconst cached = cache.get(key);\n\t\tif (!cached) {\n\t\t\tthrow new Error(\n\t\t\t\t`Actor with key \"${key}\" not found in cache. This indicates a bug in cleanup logic.`,\n\t\t\t);\n\t\t}\n\n\t\t// Cancel pending cleanup\n\t\tif (cached.cleanupTimeout !== null) {\n\t\t\tclearTimeout(cached.cleanupTimeout);\n\t\t\tcached.cleanupTimeout = null;\n\t\t}\n\n\t\t// Increment ref count\n\t\tcached.refCount++;\n\n\t\t// Mount derived/effect on first reference (or re-mount after cleanup)\n\t\tif (cached.refCount === 1) {\n\t\t\tunsubscribeDerived = derived.mount();\n\t\t\tunsubscribeEffect = effect.mount();\n\n\t\t\t// Effect doesn't run immediately on mount, only on state changes.\n\t\t\t// Trigger initial connection if actor is enabled and idle.\n\t\t\tconst actor = store.state.actors[key];\n\t\t\tif (actor && actor.opts.enabled && actor.connStatus === \"idle\") {\n\t\t\t\t(create as Function)(client, store, key);\n\t\t\t}\n\t\t}\n\n\t\treturn () => {\n\t\t\t// Decrement ref count\n\t\t\tcached.refCount--;\n\n\t\t\tif (cached.refCount === 0) {\n\t\t\t\t// Deferred cleanup prevents needless reconnection when:\n\t\t\t\t// - React Strict Mode's unmount/remount cycle\n\t\t\t\t// - useActor hook moves between components in the same render cycle\n\t\t\t\tcached.cleanupTimeout = setTimeout(() => {\n\t\t\t\t\tcached.cleanupTimeout = null;\n\t\t\t\t\tif (cached.refCount > 0) return;\n\n\t\t\t\t\t// Unsubscribe from derived/effect\n\t\t\t\t\tunsubscribeDerived?.();\n\t\t\t\t\tunsubscribeEffect?.();\n\t\t\t\t\tunsubscribeDerived = null;\n\t\t\t\t\tunsubscribeEffect = null;\n\n\t\t\t\t\t// Dispose connection\n\t\t\t\t\tconst actor = store.state.actors[key];\n\t\t\t\t\tif (actor?.connection) {\n\t\t\t\t\t\tactor.connection.dispose();\n\t\t\t\t\t}\n\n\t\t\t\t\t// Remove from store and cache\n\t\t\t\t\tstore.setState((prev) => {\n\t\t\t\t\t\tconst { [key]: _, ...rest } = prev.actors;\n\t\t\t\t\t\treturn { ...prev, actors: rest };\n\t\t\t\t\t});\n\t\t\t\t\tcache.delete(key);\n\t\t\t\t}, 0);\n\t\t\t}\n\t\t};\n\t};\n\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tconst boundCreate: () => void = () => (create as any)(client, store, key);\n\tcache.set(key, {\n\t\tstate: derived,\n\t\tkey,\n\t\tmount,\n\t\tcreate: boundCreate,\n\t\trefCount: 0,\n\t\tcleanupTimeout: null,\n\t});\n\n\treturn {\n\t\tmount,\n\t\tstate: derived as ActorsStateDerived<Registry, ActorName>,\n\t\tkey,\n\t};\n}\n\nfunction create<\n\tRegistry extends AnyActorRegistry,\n\tActorName extends keyof ExtractActorsFromRegistry<Registry> & string,\n>(client: Client<Registry>, store: Store<InternalRivetKitStore>, key: string) {\n\tconst actor = store.state.actors[key];\n\tif (!actor) {\n\t\tthrow new Error(\n\t\t\t`Actor with key \"${key}\" not found in store. This indicates a bug in cleanup logic.`,\n\t\t);\n\t}\n\n\t// Save actor to map\n\tupdateActor(store, key, {\n\t\tconnStatus: \"connecting\",\n\t\terror: null,\n\t});\n\n\ttry {\n\t\tconst handle = actor.opts.noCreate\n\t\t\t? client.get(actor.opts.name as string, actor.opts.key, {\n\t\t\t\t\tparams: actor.opts.params,\n\t\t\t\t})\n\t\t\t: client.getOrCreate(actor.opts.name as string, actor.opts.key, {\n\t\t\t\t\tparams: actor.opts.params,\n\t\t\t\t\tcreateInRegion: actor.opts.createInRegion,\n\t\t\t\t\tcreateWithInput: actor.opts.createWithInput,\n\t\t\t\t});\n\n\t\tconst connection = handle.connect();\n\n\t\t// Store connection BEFORE registering callbacks to avoid race condition\n\t\t// where status change fires before connection is stored\n\t\tupdateActor(store, key, {\n\t\t\thandle: handle as ActorHandle<\n\t\t\t\tExtractActorsFromRegistry<Registry>[ActorName]\n\t\t\t>,\n\t\t\tconnection: connection as ActorConn<\n\t\t\t\tExtractActorsFromRegistry<Registry>[ActorName]\n\t\t\t>,\n\t\t});\n\n\t\t// Subscribe to connection state changes\n\t\tconnection.onStatusChange((status) => {\n\t\t\tstore.setState((prev) => {\n\t\t\t\t// Only update if this is still the active connection\n\t\t\t\tconst isActiveConnection =\n\t\t\t\t\tprev.actors[key]?.connection === connection;\n\t\t\t\tif (!isActiveConnection) return prev;\n\t\t\t\treturn {\n\t\t\t\t\t...prev,\n\t\t\t\t\tactors: {\n\t\t\t\t\t\t...prev.actors,\n\t\t\t\t\t\t[key]: {\n\t\t\t\t\t\t\t...prev.actors[key],\n\t\t\t\t\t\t\tconnStatus: status,\n\t\t\t\t\t\t\t// Only clear error when successfully connected\n\t\t\t\t\t\t\t...(status === \"connected\" ? { error: null } : {}),\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t});\n\t\t});\n\n\t\t// onError is followed by onClose which will set connStatus to Disconnected\n\t\tconnection.onError((error) => {\n\t\t\tstore.setState((prev) => {\n\t\t\t\t// Only update if this is still the active connection\n\t\t\t\tif (prev.actors[key]?.connection !== connection) return prev;\n\t\t\t\treturn {\n\t\t\t\t\t...prev,\n\t\t\t\t\tactors: {\n\t\t\t\t\t\t...prev.actors,\n\t\t\t\t\t\t[key]: {\n\t\t\t\t\t\t\t...prev.actors[key],\n\t\t\t\t\t\t\terror,\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t});\n\t\t});\n\t} catch (error) {\n\t\tconsole.error(\"Failed to create actor connection\", error);\n\t\t// Use Disconnected so Effect won't auto-retry\n\t\t// User must re-enable or take action to retry\n\t\tupdateActor(store, key, {\n\t\t\tconnStatus: \"disconnected\",\n\t\t\terror: error as Error,\n\t\t});\n\t}\n}\n\nfunction defaultHashFunction({ name, key, params, noCreate }: AnyActorOptions) {\n\treturn JSON.stringify({ name, key, params, noCreate });\n}\n\nfunction optsEqual(a: AnyActorOptions, b: AnyActorOptions) {\n\treturn equal(a, b);\n}\n"],"mappings":";AAAA,SAAS,SAAS,QAAQ,aAAa;AACvC,OAAO,WAAW;AAElB,OAMO;AAqJA,SAAS,eACf,QACA,aAA8C,CAAC,GAC9C;AACD,QAAM,QAAQ,IAAI,MAA6B;AAAA,IAC9C,QAAQ,CAAC;AAAA,EACV,CAAC;AAED,QAAM,QAAoB,oBAAI,IAAI;AAElC,SAAO;AAAA,IACN,kBAAkB,CAIjB,cAMC;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACD;AAAA,IACD;AAAA,EACD;AACD;AAIA,SAAS,YACR,OACA,KACA,SACC;AACD,QAAM,SAAS,CAAC,UAAU;AAAA,IACzB,GAAG;AAAA,IACH,QAAQ;AAAA,MACP,GAAG,KAAK;AAAA,MACR,CAAC,GAAG,GAAG,EAAE,GAAG,KAAK,OAAO,GAAG,GAAG,GAAG,QAAQ;AAAA,IAC1C;AAAA,EACD,EAAE;AACH;AAGA,SAAS,iBAIR,QACA,YACA,OACA,OACA,WACC;AACD,QAAM,OAAO,WAAW,gBAAgB;AAExC,QAAM,iBAAiB;AAAA,IACtB,GAAG;AAAA,IACH,SAAS,UAAU,WAAW;AAAA,EAC/B;AAEA,QAAM,MAAM,KAAK,cAAc;AAI/B,QAAM,WAAW,MAAM,MAAM,OAAO,GAAG;AACvC,MAAI,CAAC,UAAU;AACd,UAAM,SAAS,CAAC,UAAU;AAAA,MACzB,GAAG;AAAA,MACH,QAAQ;AAAA,QACP,GAAG,KAAK;AAAA,QACR,CAAC,GAAG,G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,GAAG;AAAA,cAClB,YAAY;AAAA;AAAA,cAEZ,GAAI,WAAW,cAAc,EAAE,OAAO,KAAK,IAAI,CAAC;AAAA,YACjD;AAAA,UACD;AAAA,QACD;AAAA,MACD,CAAC;AAAA,IACF,CAAC;AAGD,eAAW,QAAQ,CAAC,UAAU;AAC7B,YAAM,SAAS,CAAC,SAAS;AAnd5B;AAqdI,cAAI,UAAK,OAAO,GAAG,MAAf,mBAAkB,gBAAe,WAAY,QAAO;AACxD,eAAO;AAAA,UACN,GAAG;AAAA,UACH,QAAQ;AAAA,YACP,GAAG,KAAK;AAAA,YACR,CAAC,GAAG,GAAG;AAAA,cACN,GAAG,KAAK,OAAO,GAAG;AAAA,cAClB;AAAA,YACD;AAAA,UACD;AAAA,QACD;AAAA,MACD,CAAC;AAAA,IACF,CAAC;AAAA,EACF,SAAS,OAAO;AACf,YAAQ,MAAM,qCAAqC,KAAK;AAGxD,gBAAY,OAAO,KAAK;AAAA,MACvB,YAAY;AAAA,MACZ;AAAA,IACD,CAAC;AAAA,EACF;AACD;AAEA,SAAS,oBAAoB,EAAE,MAAM,KAAK,QAAQ,SAAS,GAAoB;AAC9E,SAAO,KAAK,UAAU,EAAE,MAAM,KAAK,QAAQ,SAAS,CAAC;AACtD;AAEA,SAAS,UAAU,GAAoB,GAAoB;AAC1D,SAAO,MAAM,GAAG,CAAC;AAClB;","names":["store","cached"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rivetkit/framework-base",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.9",
|
|
4
4
|
"description": "Base framework utilities for RivetKit client integrations",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"keywords": [
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"@tanstack/store": "^0.7.1",
|
|
40
40
|
"fast-deep-equal": "^3.1.3",
|
|
41
|
-
"rivetkit": "2.1.
|
|
41
|
+
"rivetkit": "2.1.9"
|
|
42
42
|
},
|
|
43
43
|
"scripts": {
|
|
44
44
|
"build": "tsup src/mod.ts",
|