@rivetkit/framework-base 0.0.0-pr.4600.32b0fc8
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/README.md +86 -0
- package/dist/mod.d.mts +125 -0
- package/dist/mod.d.ts +125 -0
- package/dist/mod.js +240 -0
- package/dist/mod.js.map +1 -0
- package/dist/mod.mjs +240 -0
- package/dist/mod.mjs.map +1 -0
- package/package.json +47 -0
package/README.md
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# RivetKit Framework Base
|
|
2
|
+
|
|
3
|
+
_Library to build and scale stateful workloads_
|
|
4
|
+
|
|
5
|
+
[Learn More →](https://github.com/rivet-dev/rivet)
|
|
6
|
+
|
|
7
|
+
[Discord](https://rivet.dev/discord) — [Documentation](https://rivetkit.org) — [Issues](https://github.com/rivet-dev/rivet/issues)
|
|
8
|
+
|
|
9
|
+
## Lifecycle
|
|
10
|
+
|
|
11
|
+
### Mount
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
1. useActor(opts) called in React component
|
|
15
|
+
|
|
16
|
+
2. getOrCreateActor(opts)
|
|
17
|
+
- hash opts to get key
|
|
18
|
+
- sync opts to store (create or update actor entry)
|
|
19
|
+
- if not in cache:
|
|
20
|
+
- create Derived (subscribes to store)
|
|
21
|
+
- create Effect (handles connection logic)
|
|
22
|
+
- add to cache with refCount=0
|
|
23
|
+
- return { mount, state }
|
|
24
|
+
|
|
25
|
+
3. useEffect runs mount()
|
|
26
|
+
- cancel any pending cleanup timeout
|
|
27
|
+
- refCount++
|
|
28
|
+
- if refCount == 1:
|
|
29
|
+
- mount derived and effect
|
|
30
|
+
- if enabled and idle: call create() directly
|
|
31
|
+
(Effect only runs on state changes, not on mount)
|
|
32
|
+
|
|
33
|
+
4. Effect triggers (on state changes)
|
|
34
|
+
- if disabled and connected: dispose connection, reset to idle
|
|
35
|
+
- if enabled and idle: call create()
|
|
36
|
+
|
|
37
|
+
5. create()
|
|
38
|
+
- set connStatus = Connecting
|
|
39
|
+
- handle = client.getOrCreate(name, key)
|
|
40
|
+
- connection = handle.connect()
|
|
41
|
+
- subscribe to connection status/error events
|
|
42
|
+
- store handle and connection in store
|
|
43
|
+
|
|
44
|
+
6. Connection established
|
|
45
|
+
- connStatus updates, Derived updates, React re-renders
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Unmount
|
|
49
|
+
|
|
50
|
+
```
|
|
51
|
+
1. Component unmounts
|
|
52
|
+
2. useEffect cleanup runs
|
|
53
|
+
3. refCount--
|
|
54
|
+
4. if refCount == 0: setTimeout(cleanup, 0)
|
|
55
|
+
5. When timeout fires:
|
|
56
|
+
- if refCount > 0: skip (was remounted)
|
|
57
|
+
- else: dispose connection, remove from store/cache
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### React Strict Mode
|
|
61
|
+
|
|
62
|
+
Why `setTimeout` matters:
|
|
63
|
+
|
|
64
|
+
```
|
|
65
|
+
- render
|
|
66
|
+
- mount: refCount = 1
|
|
67
|
+
- unmount: refCount = 0, schedule timeout
|
|
68
|
+
- remount: refCount = 1, cancel timeout
|
|
69
|
+
- timeout fires: refCount > 0, cleanup skipped
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Shared Actor
|
|
73
|
+
|
|
74
|
+
Two components using the same actor opts:
|
|
75
|
+
|
|
76
|
+
```
|
|
77
|
+
- Component A mounts: refCount = 1, connection created
|
|
78
|
+
- Component B mounts: refCount = 2, reuses connection
|
|
79
|
+
- Component A unmounts: refCount = 1, no cleanup
|
|
80
|
+
- Component B unmounts: refCount = 0, cleanup scheduled
|
|
81
|
+
- Timeout fires: connection disposed, removed from cache
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## License
|
|
85
|
+
|
|
86
|
+
Apache 2.0
|
package/dist/mod.d.mts
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { Derived, Store } from '@tanstack/store';
|
|
2
|
+
import { Registry, AnyActorDefinition } from 'rivetkit';
|
|
3
|
+
import { ExtractActorsFromRegistry, ActorHandle, ActorConn, ActorConnStatus, Client } from 'rivetkit/client';
|
|
4
|
+
export { ActorConnStatus } from 'rivetkit/client';
|
|
5
|
+
|
|
6
|
+
type AnyActorRegistry = Registry<any>;
|
|
7
|
+
|
|
8
|
+
interface ActorStateReference {
|
|
9
|
+
/**
|
|
10
|
+
* The unique identifier for the actor.
|
|
11
|
+
* This is a hash generated from the actor's options.
|
|
12
|
+
* It is used to identify the actor instance in the store.
|
|
13
|
+
* @internal
|
|
14
|
+
*/
|
|
15
|
+
hash: string;
|
|
16
|
+
/**
|
|
17
|
+
* The state of the actor, derived from the store.
|
|
18
|
+
* This includes the actor's connection and handle.
|
|
19
|
+
*/
|
|
20
|
+
handle: ActorHandle<AnyActorDefinition> | null;
|
|
21
|
+
/**
|
|
22
|
+
* The connection to the actor.
|
|
23
|
+
* This is used to communicate with the actor in realtime.
|
|
24
|
+
*/
|
|
25
|
+
connection: ActorConn<AnyActorDefinition> | null;
|
|
26
|
+
/**
|
|
27
|
+
* The connection status of the actor.
|
|
28
|
+
*/
|
|
29
|
+
connStatus: ActorConnStatus;
|
|
30
|
+
/**
|
|
31
|
+
* The error that occurred while trying to connect to the actor, if any.
|
|
32
|
+
*/
|
|
33
|
+
error: Error | null;
|
|
34
|
+
/**
|
|
35
|
+
* Options for the actor, including its name, key, parameters, and whether it is enabled.
|
|
36
|
+
*/
|
|
37
|
+
opts: {
|
|
38
|
+
name: string;
|
|
39
|
+
/**
|
|
40
|
+
* Unique key for the actor instance.
|
|
41
|
+
* This can be a string or an array of strings to create multiple instances.
|
|
42
|
+
* @example "abc" or ["abc", "def"]
|
|
43
|
+
*/
|
|
44
|
+
key: string | string[];
|
|
45
|
+
/**
|
|
46
|
+
* Parameters for the actor.
|
|
47
|
+
* These are additional options that can be passed to the actor.
|
|
48
|
+
*/
|
|
49
|
+
params?: unknown;
|
|
50
|
+
/** Region to create the actor in if it doesn't exist. */
|
|
51
|
+
createInRegion?: string;
|
|
52
|
+
/** Input data to pass to the actor. */
|
|
53
|
+
createWithInput?: unknown;
|
|
54
|
+
/**
|
|
55
|
+
* Whether the actor is enabled.
|
|
56
|
+
* Defaults to true.
|
|
57
|
+
*/
|
|
58
|
+
enabled?: boolean;
|
|
59
|
+
/**
|
|
60
|
+
* If true, only gets the actor if it already exists. Does not create the actor.
|
|
61
|
+
* Throws an error if the actor is not found.
|
|
62
|
+
* Defaults to false.
|
|
63
|
+
*/
|
|
64
|
+
noCreate?: boolean;
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
interface InternalRivetKitStore {
|
|
68
|
+
actors: Record<string, ActorStateReference>;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Options for configuring a actor in RivetKit.
|
|
72
|
+
*/
|
|
73
|
+
interface ActorOptions<Registry extends AnyActorRegistry, ActorName extends keyof ExtractActorsFromRegistry<Registry> & string> {
|
|
74
|
+
/**
|
|
75
|
+
* Typesafe name of the actor.
|
|
76
|
+
* This should match the actor's name in the app's actor definitions.
|
|
77
|
+
* @example "chatRoom"
|
|
78
|
+
*/
|
|
79
|
+
name: ActorName;
|
|
80
|
+
/**
|
|
81
|
+
* Unique key for the actor instance.
|
|
82
|
+
* This can be a string or an array of strings to create multiple instances.
|
|
83
|
+
* @example "abc" or ["abc", "def"]
|
|
84
|
+
*/
|
|
85
|
+
key: string | string[];
|
|
86
|
+
/**
|
|
87
|
+
* Parameters for the actor.
|
|
88
|
+
*/
|
|
89
|
+
params?: ExtractActorsFromRegistry<Registry>[ActorName]["params"];
|
|
90
|
+
/** Region to create the actor in if it doesn't exist. */
|
|
91
|
+
createInRegion?: string;
|
|
92
|
+
/** Input data to pass to the actor. */
|
|
93
|
+
createWithInput?: unknown;
|
|
94
|
+
/**
|
|
95
|
+
* Whether the actor is enabled.
|
|
96
|
+
* Defaults to true.
|
|
97
|
+
*/
|
|
98
|
+
enabled?: boolean;
|
|
99
|
+
/**
|
|
100
|
+
* If true, only gets the actor if it already exists. Does not create the actor.
|
|
101
|
+
* Throws an error if the actor is not found.
|
|
102
|
+
* Defaults to false.
|
|
103
|
+
*/
|
|
104
|
+
noCreate?: boolean;
|
|
105
|
+
}
|
|
106
|
+
type ActorsStateDerived<Registry extends AnyActorRegistry, WorkerName extends keyof ExtractActorsFromRegistry<Registry> & string> = Derived<Omit<InternalRivetKitStore["actors"][string], "handle" | "connection"> & {
|
|
107
|
+
handle: ActorHandle<ExtractActorsFromRegistry<Registry>[WorkerName]> | null;
|
|
108
|
+
connection: ActorConn<ExtractActorsFromRegistry<Registry>[WorkerName]> | null;
|
|
109
|
+
/** @deprecated Use `connStatus === "connected"` instead */
|
|
110
|
+
isConnected: boolean;
|
|
111
|
+
}>;
|
|
112
|
+
type AnyActorOptions = ActorOptions<AnyActorRegistry, any>;
|
|
113
|
+
interface CreateRivetKitOptions<Registry extends AnyActorRegistry> {
|
|
114
|
+
hashFunction?: (opts: ActorOptions<Registry, any>) => string;
|
|
115
|
+
}
|
|
116
|
+
declare function createRivetKit<Registry extends AnyActorRegistry>(client: Client<Registry>, createOpts?: CreateRivetKitOptions<Registry>): {
|
|
117
|
+
getOrCreateActor: <ActorName extends keyof ExtractActorsFromRegistry<Registry> & string>(actorOpts: ActorOptions<Registry, ActorName>) => {
|
|
118
|
+
mount: () => () => void;
|
|
119
|
+
state: ActorsStateDerived<Registry, ActorName>;
|
|
120
|
+
key: string;
|
|
121
|
+
};
|
|
122
|
+
store: Store<InternalRivetKitStore, (cb: InternalRivetKitStore) => InternalRivetKitStore>;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
export { type ActorOptions, type ActorsStateDerived, type AnyActorOptions, type AnyActorRegistry, type CreateRivetKitOptions, createRivetKit };
|
package/dist/mod.d.ts
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { Derived, Store } from '@tanstack/store';
|
|
2
|
+
import { Registry, AnyActorDefinition } from 'rivetkit';
|
|
3
|
+
import { ExtractActorsFromRegistry, ActorHandle, ActorConn, ActorConnStatus, Client } from 'rivetkit/client';
|
|
4
|
+
export { ActorConnStatus } from 'rivetkit/client';
|
|
5
|
+
|
|
6
|
+
type AnyActorRegistry = Registry<any>;
|
|
7
|
+
|
|
8
|
+
interface ActorStateReference {
|
|
9
|
+
/**
|
|
10
|
+
* The unique identifier for the actor.
|
|
11
|
+
* This is a hash generated from the actor's options.
|
|
12
|
+
* It is used to identify the actor instance in the store.
|
|
13
|
+
* @internal
|
|
14
|
+
*/
|
|
15
|
+
hash: string;
|
|
16
|
+
/**
|
|
17
|
+
* The state of the actor, derived from the store.
|
|
18
|
+
* This includes the actor's connection and handle.
|
|
19
|
+
*/
|
|
20
|
+
handle: ActorHandle<AnyActorDefinition> | null;
|
|
21
|
+
/**
|
|
22
|
+
* The connection to the actor.
|
|
23
|
+
* This is used to communicate with the actor in realtime.
|
|
24
|
+
*/
|
|
25
|
+
connection: ActorConn<AnyActorDefinition> | null;
|
|
26
|
+
/**
|
|
27
|
+
* The connection status of the actor.
|
|
28
|
+
*/
|
|
29
|
+
connStatus: ActorConnStatus;
|
|
30
|
+
/**
|
|
31
|
+
* The error that occurred while trying to connect to the actor, if any.
|
|
32
|
+
*/
|
|
33
|
+
error: Error | null;
|
|
34
|
+
/**
|
|
35
|
+
* Options for the actor, including its name, key, parameters, and whether it is enabled.
|
|
36
|
+
*/
|
|
37
|
+
opts: {
|
|
38
|
+
name: string;
|
|
39
|
+
/**
|
|
40
|
+
* Unique key for the actor instance.
|
|
41
|
+
* This can be a string or an array of strings to create multiple instances.
|
|
42
|
+
* @example "abc" or ["abc", "def"]
|
|
43
|
+
*/
|
|
44
|
+
key: string | string[];
|
|
45
|
+
/**
|
|
46
|
+
* Parameters for the actor.
|
|
47
|
+
* These are additional options that can be passed to the actor.
|
|
48
|
+
*/
|
|
49
|
+
params?: unknown;
|
|
50
|
+
/** Region to create the actor in if it doesn't exist. */
|
|
51
|
+
createInRegion?: string;
|
|
52
|
+
/** Input data to pass to the actor. */
|
|
53
|
+
createWithInput?: unknown;
|
|
54
|
+
/**
|
|
55
|
+
* Whether the actor is enabled.
|
|
56
|
+
* Defaults to true.
|
|
57
|
+
*/
|
|
58
|
+
enabled?: boolean;
|
|
59
|
+
/**
|
|
60
|
+
* If true, only gets the actor if it already exists. Does not create the actor.
|
|
61
|
+
* Throws an error if the actor is not found.
|
|
62
|
+
* Defaults to false.
|
|
63
|
+
*/
|
|
64
|
+
noCreate?: boolean;
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
interface InternalRivetKitStore {
|
|
68
|
+
actors: Record<string, ActorStateReference>;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Options for configuring a actor in RivetKit.
|
|
72
|
+
*/
|
|
73
|
+
interface ActorOptions<Registry extends AnyActorRegistry, ActorName extends keyof ExtractActorsFromRegistry<Registry> & string> {
|
|
74
|
+
/**
|
|
75
|
+
* Typesafe name of the actor.
|
|
76
|
+
* This should match the actor's name in the app's actor definitions.
|
|
77
|
+
* @example "chatRoom"
|
|
78
|
+
*/
|
|
79
|
+
name: ActorName;
|
|
80
|
+
/**
|
|
81
|
+
* Unique key for the actor instance.
|
|
82
|
+
* This can be a string or an array of strings to create multiple instances.
|
|
83
|
+
* @example "abc" or ["abc", "def"]
|
|
84
|
+
*/
|
|
85
|
+
key: string | string[];
|
|
86
|
+
/**
|
|
87
|
+
* Parameters for the actor.
|
|
88
|
+
*/
|
|
89
|
+
params?: ExtractActorsFromRegistry<Registry>[ActorName]["params"];
|
|
90
|
+
/** Region to create the actor in if it doesn't exist. */
|
|
91
|
+
createInRegion?: string;
|
|
92
|
+
/** Input data to pass to the actor. */
|
|
93
|
+
createWithInput?: unknown;
|
|
94
|
+
/**
|
|
95
|
+
* Whether the actor is enabled.
|
|
96
|
+
* Defaults to true.
|
|
97
|
+
*/
|
|
98
|
+
enabled?: boolean;
|
|
99
|
+
/**
|
|
100
|
+
* If true, only gets the actor if it already exists. Does not create the actor.
|
|
101
|
+
* Throws an error if the actor is not found.
|
|
102
|
+
* Defaults to false.
|
|
103
|
+
*/
|
|
104
|
+
noCreate?: boolean;
|
|
105
|
+
}
|
|
106
|
+
type ActorsStateDerived<Registry extends AnyActorRegistry, WorkerName extends keyof ExtractActorsFromRegistry<Registry> & string> = Derived<Omit<InternalRivetKitStore["actors"][string], "handle" | "connection"> & {
|
|
107
|
+
handle: ActorHandle<ExtractActorsFromRegistry<Registry>[WorkerName]> | null;
|
|
108
|
+
connection: ActorConn<ExtractActorsFromRegistry<Registry>[WorkerName]> | null;
|
|
109
|
+
/** @deprecated Use `connStatus === "connected"` instead */
|
|
110
|
+
isConnected: boolean;
|
|
111
|
+
}>;
|
|
112
|
+
type AnyActorOptions = ActorOptions<AnyActorRegistry, any>;
|
|
113
|
+
interface CreateRivetKitOptions<Registry extends AnyActorRegistry> {
|
|
114
|
+
hashFunction?: (opts: ActorOptions<Registry, any>) => string;
|
|
115
|
+
}
|
|
116
|
+
declare function createRivetKit<Registry extends AnyActorRegistry>(client: Client<Registry>, createOpts?: CreateRivetKitOptions<Registry>): {
|
|
117
|
+
getOrCreateActor: <ActorName extends keyof ExtractActorsFromRegistry<Registry> & string>(actorOpts: ActorOptions<Registry, ActorName>) => {
|
|
118
|
+
mount: () => () => void;
|
|
119
|
+
state: ActorsStateDerived<Registry, ActorName>;
|
|
120
|
+
key: string;
|
|
121
|
+
};
|
|
122
|
+
store: Store<InternalRivetKitStore, (cb: InternalRivetKitStore) => InternalRivetKitStore>;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
export { type ActorOptions, type ActorsStateDerived, type AnyActorOptions, type AnyActorRegistry, type CreateRivetKitOptions, createRivetKit };
|
package/dist/mod.js
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }// src/mod.ts
|
|
2
|
+
var _store = require('@tanstack/store');
|
|
3
|
+
var _fastdeepequal = require('fast-deep-equal'); var _fastdeepequal2 = _interopRequireDefault(_fastdeepequal);
|
|
4
|
+
require('rivetkit/client');
|
|
5
|
+
function createRivetKit(client, createOpts = {}) {
|
|
6
|
+
const store = new (0, _store.Store)({
|
|
7
|
+
actors: {}
|
|
8
|
+
});
|
|
9
|
+
const cache = /* @__PURE__ */ new Map();
|
|
10
|
+
return {
|
|
11
|
+
getOrCreateActor: (actorOpts) => getOrCreateActor(
|
|
12
|
+
client,
|
|
13
|
+
createOpts,
|
|
14
|
+
store,
|
|
15
|
+
cache,
|
|
16
|
+
actorOpts
|
|
17
|
+
),
|
|
18
|
+
store
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function updateActor(store, key, updates) {
|
|
22
|
+
store.setState((prev) => ({
|
|
23
|
+
...prev,
|
|
24
|
+
actors: {
|
|
25
|
+
...prev.actors,
|
|
26
|
+
[key]: { ...prev.actors[key], ...updates }
|
|
27
|
+
}
|
|
28
|
+
}));
|
|
29
|
+
}
|
|
30
|
+
function getOrCreateActor(client, createOpts, store, cache, actorOpts) {
|
|
31
|
+
const hash = createOpts.hashFunction || defaultHashFunction;
|
|
32
|
+
const normalizedOpts = {
|
|
33
|
+
...actorOpts,
|
|
34
|
+
enabled: _nullishCoalesce(actorOpts.enabled, () => ( true))
|
|
35
|
+
};
|
|
36
|
+
const key = hash(normalizedOpts);
|
|
37
|
+
const existing = store.state.actors[key];
|
|
38
|
+
if (!existing) {
|
|
39
|
+
store.setState((prev) => ({
|
|
40
|
+
...prev,
|
|
41
|
+
actors: {
|
|
42
|
+
...prev.actors,
|
|
43
|
+
[key]: {
|
|
44
|
+
hash: key,
|
|
45
|
+
connStatus: "idle",
|
|
46
|
+
connection: null,
|
|
47
|
+
handle: null,
|
|
48
|
+
error: null,
|
|
49
|
+
opts: normalizedOpts
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}));
|
|
53
|
+
} else if (!optsEqual(existing.opts, normalizedOpts)) {
|
|
54
|
+
queueMicrotask(() => {
|
|
55
|
+
updateActor(store, key, { opts: normalizedOpts });
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
const cached = cache.get(key);
|
|
59
|
+
if (cached) {
|
|
60
|
+
return {
|
|
61
|
+
...cached,
|
|
62
|
+
state: cached.state
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
const derived = new (0, _store.Derived)({
|
|
66
|
+
fn: ({ currDepVals: [store2] }) => {
|
|
67
|
+
const actor = store2.actors[key];
|
|
68
|
+
return {
|
|
69
|
+
...actor,
|
|
70
|
+
/** @deprecated Use `connStatus === "connected"` instead */
|
|
71
|
+
isConnected: actor.connStatus === "connected"
|
|
72
|
+
};
|
|
73
|
+
},
|
|
74
|
+
deps: [store]
|
|
75
|
+
});
|
|
76
|
+
const effect = new (0, _store.Effect)({
|
|
77
|
+
fn: () => {
|
|
78
|
+
const actor = store.state.actors[key];
|
|
79
|
+
if (!actor) {
|
|
80
|
+
throw new Error(
|
|
81
|
+
`Actor with key "${key}" not found in store. This indicates a bug in cleanup logic.`
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
if (!actor.opts.enabled && actor.connection) {
|
|
85
|
+
actor.connection.dispose();
|
|
86
|
+
updateActor(store, key, {
|
|
87
|
+
connection: null,
|
|
88
|
+
handle: null,
|
|
89
|
+
connStatus: "idle"
|
|
90
|
+
});
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
if (actor.connStatus === "idle" && actor.opts.enabled) {
|
|
94
|
+
queueMicrotask(() => {
|
|
95
|
+
const currentActor = store.state.actors[key];
|
|
96
|
+
if (currentActor && currentActor.connStatus === "idle" && currentActor.opts.enabled) {
|
|
97
|
+
create(client, store, key);
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
deps: [derived]
|
|
103
|
+
});
|
|
104
|
+
let unsubscribeDerived = null;
|
|
105
|
+
let unsubscribeEffect = null;
|
|
106
|
+
const mount = () => {
|
|
107
|
+
const cached2 = cache.get(key);
|
|
108
|
+
if (!cached2) {
|
|
109
|
+
throw new Error(
|
|
110
|
+
`Actor with key "${key}" not found in cache. This indicates a bug in cleanup logic.`
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
if (cached2.cleanupTimeout !== null) {
|
|
114
|
+
clearTimeout(cached2.cleanupTimeout);
|
|
115
|
+
cached2.cleanupTimeout = null;
|
|
116
|
+
}
|
|
117
|
+
cached2.refCount++;
|
|
118
|
+
if (cached2.refCount === 1) {
|
|
119
|
+
unsubscribeDerived = derived.mount();
|
|
120
|
+
unsubscribeEffect = effect.mount();
|
|
121
|
+
const actor = store.state.actors[key];
|
|
122
|
+
if (actor && actor.opts.enabled && actor.connStatus === "idle") {
|
|
123
|
+
create(client, store, key);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return () => {
|
|
127
|
+
cached2.refCount--;
|
|
128
|
+
if (cached2.refCount === 0) {
|
|
129
|
+
cached2.cleanupTimeout = setTimeout(() => {
|
|
130
|
+
cached2.cleanupTimeout = null;
|
|
131
|
+
if (cached2.refCount > 0) return;
|
|
132
|
+
unsubscribeDerived == null ? void 0 : unsubscribeDerived();
|
|
133
|
+
unsubscribeEffect == null ? void 0 : unsubscribeEffect();
|
|
134
|
+
unsubscribeDerived = null;
|
|
135
|
+
unsubscribeEffect = null;
|
|
136
|
+
const actor = store.state.actors[key];
|
|
137
|
+
if (actor == null ? void 0 : actor.connection) {
|
|
138
|
+
actor.connection.dispose();
|
|
139
|
+
}
|
|
140
|
+
store.setState((prev) => {
|
|
141
|
+
const { [key]: _, ...rest } = prev.actors;
|
|
142
|
+
return { ...prev, actors: rest };
|
|
143
|
+
});
|
|
144
|
+
cache.delete(key);
|
|
145
|
+
}, 0);
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
};
|
|
149
|
+
const boundCreate = () => create(client, store, key);
|
|
150
|
+
cache.set(key, {
|
|
151
|
+
state: derived,
|
|
152
|
+
key,
|
|
153
|
+
mount,
|
|
154
|
+
create: boundCreate,
|
|
155
|
+
refCount: 0,
|
|
156
|
+
cleanupTimeout: null
|
|
157
|
+
});
|
|
158
|
+
return {
|
|
159
|
+
mount,
|
|
160
|
+
state: derived,
|
|
161
|
+
key
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
function create(client, store, key) {
|
|
165
|
+
const actor = store.state.actors[key];
|
|
166
|
+
if (!actor) {
|
|
167
|
+
throw new Error(
|
|
168
|
+
`Actor with key "${key}" not found in store. This indicates a bug in cleanup logic.`
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
updateActor(store, key, {
|
|
172
|
+
connStatus: "connecting",
|
|
173
|
+
error: null
|
|
174
|
+
});
|
|
175
|
+
try {
|
|
176
|
+
const handle = actor.opts.noCreate ? client.get(actor.opts.name, actor.opts.key, {
|
|
177
|
+
params: actor.opts.params
|
|
178
|
+
}) : client.getOrCreate(actor.opts.name, actor.opts.key, {
|
|
179
|
+
params: actor.opts.params,
|
|
180
|
+
createInRegion: actor.opts.createInRegion,
|
|
181
|
+
createWithInput: actor.opts.createWithInput
|
|
182
|
+
});
|
|
183
|
+
const connection = handle.connect();
|
|
184
|
+
updateActor(store, key, {
|
|
185
|
+
handle,
|
|
186
|
+
connection
|
|
187
|
+
});
|
|
188
|
+
connection.onStatusChange((status) => {
|
|
189
|
+
store.setState((prev) => {
|
|
190
|
+
var _a;
|
|
191
|
+
const isActiveConnection = ((_a = prev.actors[key]) == null ? void 0 : _a.connection) === connection;
|
|
192
|
+
if (!isActiveConnection) return prev;
|
|
193
|
+
return {
|
|
194
|
+
...prev,
|
|
195
|
+
actors: {
|
|
196
|
+
...prev.actors,
|
|
197
|
+
[key]: {
|
|
198
|
+
...prev.actors[key],
|
|
199
|
+
connStatus: status,
|
|
200
|
+
// Only clear error when successfully connected
|
|
201
|
+
...status === "connected" ? { error: null } : {}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
connection.onError((error) => {
|
|
208
|
+
store.setState((prev) => {
|
|
209
|
+
var _a;
|
|
210
|
+
if (((_a = prev.actors[key]) == null ? void 0 : _a.connection) !== connection) return prev;
|
|
211
|
+
return {
|
|
212
|
+
...prev,
|
|
213
|
+
actors: {
|
|
214
|
+
...prev.actors,
|
|
215
|
+
[key]: {
|
|
216
|
+
...prev.actors[key],
|
|
217
|
+
error
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
} catch (error) {
|
|
224
|
+
console.error("Failed to create actor connection", error);
|
|
225
|
+
updateActor(store, key, {
|
|
226
|
+
connStatus: "disconnected",
|
|
227
|
+
error
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
function defaultHashFunction({ name, key, params, noCreate }) {
|
|
232
|
+
return JSON.stringify({ name, key, params, noCreate });
|
|
233
|
+
}
|
|
234
|
+
function optsEqual(a, b) {
|
|
235
|
+
return _fastdeepequal2.default.call(void 0, a, b);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
exports.createRivetKit = createRivetKit;
|
|
240
|
+
//# sourceMappingURL=mod.js.map
|
package/dist/mod.js.map
ADDED
|
@@ -0,0 +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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AAA,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
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
// src/mod.ts
|
|
2
|
+
import { Derived, Effect, Store } from "@tanstack/store";
|
|
3
|
+
import equal from "fast-deep-equal";
|
|
4
|
+
import "rivetkit/client";
|
|
5
|
+
function createRivetKit(client, createOpts = {}) {
|
|
6
|
+
const store = new Store({
|
|
7
|
+
actors: {}
|
|
8
|
+
});
|
|
9
|
+
const cache = /* @__PURE__ */ new Map();
|
|
10
|
+
return {
|
|
11
|
+
getOrCreateActor: (actorOpts) => getOrCreateActor(
|
|
12
|
+
client,
|
|
13
|
+
createOpts,
|
|
14
|
+
store,
|
|
15
|
+
cache,
|
|
16
|
+
actorOpts
|
|
17
|
+
),
|
|
18
|
+
store
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function updateActor(store, key, updates) {
|
|
22
|
+
store.setState((prev) => ({
|
|
23
|
+
...prev,
|
|
24
|
+
actors: {
|
|
25
|
+
...prev.actors,
|
|
26
|
+
[key]: { ...prev.actors[key], ...updates }
|
|
27
|
+
}
|
|
28
|
+
}));
|
|
29
|
+
}
|
|
30
|
+
function getOrCreateActor(client, createOpts, store, cache, actorOpts) {
|
|
31
|
+
const hash = createOpts.hashFunction || defaultHashFunction;
|
|
32
|
+
const normalizedOpts = {
|
|
33
|
+
...actorOpts,
|
|
34
|
+
enabled: actorOpts.enabled ?? true
|
|
35
|
+
};
|
|
36
|
+
const key = hash(normalizedOpts);
|
|
37
|
+
const existing = store.state.actors[key];
|
|
38
|
+
if (!existing) {
|
|
39
|
+
store.setState((prev) => ({
|
|
40
|
+
...prev,
|
|
41
|
+
actors: {
|
|
42
|
+
...prev.actors,
|
|
43
|
+
[key]: {
|
|
44
|
+
hash: key,
|
|
45
|
+
connStatus: "idle",
|
|
46
|
+
connection: null,
|
|
47
|
+
handle: null,
|
|
48
|
+
error: null,
|
|
49
|
+
opts: normalizedOpts
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}));
|
|
53
|
+
} else if (!optsEqual(existing.opts, normalizedOpts)) {
|
|
54
|
+
queueMicrotask(() => {
|
|
55
|
+
updateActor(store, key, { opts: normalizedOpts });
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
const cached = cache.get(key);
|
|
59
|
+
if (cached) {
|
|
60
|
+
return {
|
|
61
|
+
...cached,
|
|
62
|
+
state: cached.state
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
const derived = new Derived({
|
|
66
|
+
fn: ({ currDepVals: [store2] }) => {
|
|
67
|
+
const actor = store2.actors[key];
|
|
68
|
+
return {
|
|
69
|
+
...actor,
|
|
70
|
+
/** @deprecated Use `connStatus === "connected"` instead */
|
|
71
|
+
isConnected: actor.connStatus === "connected"
|
|
72
|
+
};
|
|
73
|
+
},
|
|
74
|
+
deps: [store]
|
|
75
|
+
});
|
|
76
|
+
const effect = new Effect({
|
|
77
|
+
fn: () => {
|
|
78
|
+
const actor = store.state.actors[key];
|
|
79
|
+
if (!actor) {
|
|
80
|
+
throw new Error(
|
|
81
|
+
`Actor with key "${key}" not found in store. This indicates a bug in cleanup logic.`
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
if (!actor.opts.enabled && actor.connection) {
|
|
85
|
+
actor.connection.dispose();
|
|
86
|
+
updateActor(store, key, {
|
|
87
|
+
connection: null,
|
|
88
|
+
handle: null,
|
|
89
|
+
connStatus: "idle"
|
|
90
|
+
});
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
if (actor.connStatus === "idle" && actor.opts.enabled) {
|
|
94
|
+
queueMicrotask(() => {
|
|
95
|
+
const currentActor = store.state.actors[key];
|
|
96
|
+
if (currentActor && currentActor.connStatus === "idle" && currentActor.opts.enabled) {
|
|
97
|
+
create(client, store, key);
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
deps: [derived]
|
|
103
|
+
});
|
|
104
|
+
let unsubscribeDerived = null;
|
|
105
|
+
let unsubscribeEffect = null;
|
|
106
|
+
const mount = () => {
|
|
107
|
+
const cached2 = cache.get(key);
|
|
108
|
+
if (!cached2) {
|
|
109
|
+
throw new Error(
|
|
110
|
+
`Actor with key "${key}" not found in cache. This indicates a bug in cleanup logic.`
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
if (cached2.cleanupTimeout !== null) {
|
|
114
|
+
clearTimeout(cached2.cleanupTimeout);
|
|
115
|
+
cached2.cleanupTimeout = null;
|
|
116
|
+
}
|
|
117
|
+
cached2.refCount++;
|
|
118
|
+
if (cached2.refCount === 1) {
|
|
119
|
+
unsubscribeDerived = derived.mount();
|
|
120
|
+
unsubscribeEffect = effect.mount();
|
|
121
|
+
const actor = store.state.actors[key];
|
|
122
|
+
if (actor && actor.opts.enabled && actor.connStatus === "idle") {
|
|
123
|
+
create(client, store, key);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return () => {
|
|
127
|
+
cached2.refCount--;
|
|
128
|
+
if (cached2.refCount === 0) {
|
|
129
|
+
cached2.cleanupTimeout = setTimeout(() => {
|
|
130
|
+
cached2.cleanupTimeout = null;
|
|
131
|
+
if (cached2.refCount > 0) return;
|
|
132
|
+
unsubscribeDerived == null ? void 0 : unsubscribeDerived();
|
|
133
|
+
unsubscribeEffect == null ? void 0 : unsubscribeEffect();
|
|
134
|
+
unsubscribeDerived = null;
|
|
135
|
+
unsubscribeEffect = null;
|
|
136
|
+
const actor = store.state.actors[key];
|
|
137
|
+
if (actor == null ? void 0 : actor.connection) {
|
|
138
|
+
actor.connection.dispose();
|
|
139
|
+
}
|
|
140
|
+
store.setState((prev) => {
|
|
141
|
+
const { [key]: _, ...rest } = prev.actors;
|
|
142
|
+
return { ...prev, actors: rest };
|
|
143
|
+
});
|
|
144
|
+
cache.delete(key);
|
|
145
|
+
}, 0);
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
};
|
|
149
|
+
const boundCreate = () => create(client, store, key);
|
|
150
|
+
cache.set(key, {
|
|
151
|
+
state: derived,
|
|
152
|
+
key,
|
|
153
|
+
mount,
|
|
154
|
+
create: boundCreate,
|
|
155
|
+
refCount: 0,
|
|
156
|
+
cleanupTimeout: null
|
|
157
|
+
});
|
|
158
|
+
return {
|
|
159
|
+
mount,
|
|
160
|
+
state: derived,
|
|
161
|
+
key
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
function create(client, store, key) {
|
|
165
|
+
const actor = store.state.actors[key];
|
|
166
|
+
if (!actor) {
|
|
167
|
+
throw new Error(
|
|
168
|
+
`Actor with key "${key}" not found in store. This indicates a bug in cleanup logic.`
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
updateActor(store, key, {
|
|
172
|
+
connStatus: "connecting",
|
|
173
|
+
error: null
|
|
174
|
+
});
|
|
175
|
+
try {
|
|
176
|
+
const handle = actor.opts.noCreate ? client.get(actor.opts.name, actor.opts.key, {
|
|
177
|
+
params: actor.opts.params
|
|
178
|
+
}) : client.getOrCreate(actor.opts.name, actor.opts.key, {
|
|
179
|
+
params: actor.opts.params,
|
|
180
|
+
createInRegion: actor.opts.createInRegion,
|
|
181
|
+
createWithInput: actor.opts.createWithInput
|
|
182
|
+
});
|
|
183
|
+
const connection = handle.connect();
|
|
184
|
+
updateActor(store, key, {
|
|
185
|
+
handle,
|
|
186
|
+
connection
|
|
187
|
+
});
|
|
188
|
+
connection.onStatusChange((status) => {
|
|
189
|
+
store.setState((prev) => {
|
|
190
|
+
var _a;
|
|
191
|
+
const isActiveConnection = ((_a = prev.actors[key]) == null ? void 0 : _a.connection) === connection;
|
|
192
|
+
if (!isActiveConnection) return prev;
|
|
193
|
+
return {
|
|
194
|
+
...prev,
|
|
195
|
+
actors: {
|
|
196
|
+
...prev.actors,
|
|
197
|
+
[key]: {
|
|
198
|
+
...prev.actors[key],
|
|
199
|
+
connStatus: status,
|
|
200
|
+
// Only clear error when successfully connected
|
|
201
|
+
...status === "connected" ? { error: null } : {}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
connection.onError((error) => {
|
|
208
|
+
store.setState((prev) => {
|
|
209
|
+
var _a;
|
|
210
|
+
if (((_a = prev.actors[key]) == null ? void 0 : _a.connection) !== connection) return prev;
|
|
211
|
+
return {
|
|
212
|
+
...prev,
|
|
213
|
+
actors: {
|
|
214
|
+
...prev.actors,
|
|
215
|
+
[key]: {
|
|
216
|
+
...prev.actors[key],
|
|
217
|
+
error
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
} catch (error) {
|
|
224
|
+
console.error("Failed to create actor connection", error);
|
|
225
|
+
updateActor(store, key, {
|
|
226
|
+
connStatus: "disconnected",
|
|
227
|
+
error
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
function defaultHashFunction({ name, key, params, noCreate }) {
|
|
232
|
+
return JSON.stringify({ name, key, params, noCreate });
|
|
233
|
+
}
|
|
234
|
+
function optsEqual(a, b) {
|
|
235
|
+
return equal(a, b);
|
|
236
|
+
}
|
|
237
|
+
export {
|
|
238
|
+
createRivetKit
|
|
239
|
+
};
|
|
240
|
+
//# sourceMappingURL=mod.mjs.map
|
package/dist/mod.mjs.map
ADDED
|
@@ -0,0 +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} =>\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,Q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
|
|
3
|
+
"version": "0.0.0-pr.4600.32b0fc8",
|
|
4
|
+
"description": "Base framework utilities for RivetKit client integrations",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"rivetkit",
|
|
8
|
+
"framework",
|
|
9
|
+
"base",
|
|
10
|
+
"utilities",
|
|
11
|
+
"integration"
|
|
12
|
+
],
|
|
13
|
+
"sideEffects": [
|
|
14
|
+
"./dist/chunk-*.js",
|
|
15
|
+
"./dist/chunk-*.cjs"
|
|
16
|
+
],
|
|
17
|
+
"files": [
|
|
18
|
+
"dist",
|
|
19
|
+
"package.json"
|
|
20
|
+
],
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"import": {
|
|
24
|
+
"types": "./dist/mod.d.mts",
|
|
25
|
+
"default": "./dist/mod.mjs"
|
|
26
|
+
},
|
|
27
|
+
"require": {
|
|
28
|
+
"types": "./dist/mod.d.ts",
|
|
29
|
+
"default": "./dist/mod.js"
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "tsup src/mod.ts",
|
|
35
|
+
"check-types": "tsc --noEmit"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"tsup": "^8.4.0",
|
|
39
|
+
"typescript": "^5.5.2"
|
|
40
|
+
},
|
|
41
|
+
"stableVersion": "0.8.0",
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@tanstack/store": "^0.7.1",
|
|
44
|
+
"fast-deep-equal": "^3.1.3",
|
|
45
|
+
"rivetkit": "0.0.0-pr.4600.32b0fc8"
|
|
46
|
+
}
|
|
47
|
+
}
|