@rivetkit/framework-base 2.0.2 → 2.0.4-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +78 -3
- package/dist/mod.d.mts +132 -0
- package/dist/mod.d.ts +38 -24
- package/dist/mod.js +220 -277
- package/dist/mod.js.map +1 -0
- package/dist/mod.mjs +241 -0
- package/dist/mod.mjs.map +1 -0
- package/package.json +15 -14
- package/dist/mod.cjs +0 -1
package/README.md
CHANGED
|
@@ -1,10 +1,85 @@
|
|
|
1
1
|
# RivetKit Framework Base
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
_Library to build and scale stateful workloads_
|
|
4
4
|
|
|
5
|
-
[Learn More →](https://github.com/rivet-
|
|
5
|
+
[Learn More →](https://github.com/rivet-dev/rivetkit)
|
|
6
6
|
|
|
7
|
-
[Discord](https://rivet.
|
|
7
|
+
[Discord](https://rivet.dev/discord) — [Documentation](https://rivetkit.org) — [Issues](https://github.com/rivet-dev/rivetkit/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
|
+
```
|
|
8
83
|
|
|
9
84
|
## License
|
|
10
85
|
|
package/dist/mod.d.mts
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
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<AD extends AnyActorDefinition> {
|
|
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<AD> | null;
|
|
21
|
+
/**
|
|
22
|
+
* The connection to the actor.
|
|
23
|
+
* This is used to communicate with the actor in realtime.
|
|
24
|
+
*/
|
|
25
|
+
connection: ActorConn<AD> | 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: keyof AD;
|
|
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?: Record<string, string>;
|
|
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<Registry extends AnyActorRegistry, Actors extends ExtractActorsFromRegistry<Registry>> {
|
|
68
|
+
actors: Record<string, ActorStateReference<Actors>>;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Options for configuring a actor in RivetKit.
|
|
72
|
+
*/
|
|
73
|
+
interface ActorOptions<Registry extends AnyActorRegistry, ActorName extends keyof ExtractActorsFromRegistry<Registry>> {
|
|
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?: Registry[ExtractActorsFromRegistry<Registry>]["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>> = Derived<Omit<InternalRivetKitStore<Registry, ExtractActorsFromRegistry<Registry>>["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, Actors extends ExtractActorsFromRegistry<Registry>>(client: Client<Registry>, createOpts?: CreateRivetKitOptions<Registry>): {
|
|
117
|
+
getOrCreateActor: <ActorName extends keyof Actors>(actorOpts: ActorOptions<Registry, ActorName>) => {
|
|
118
|
+
state: ActorsStateDerived<Registry, ActorName>;
|
|
119
|
+
key: string;
|
|
120
|
+
mount: () => () => void;
|
|
121
|
+
create: () => void;
|
|
122
|
+
refCount: number;
|
|
123
|
+
cleanupTimeout: ReturnType<typeof setTimeout> | null;
|
|
124
|
+
} | {
|
|
125
|
+
mount: () => () => void;
|
|
126
|
+
state: ActorsStateDerived<Registry, ActorName>;
|
|
127
|
+
key: string;
|
|
128
|
+
};
|
|
129
|
+
store: Store<InternalRivetKitStore<Registry, Actors>, (cb: InternalRivetKitStore<Registry, Actors>) => InternalRivetKitStore<Registry, Actors>>;
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
export { type ActorOptions, type ActorsStateDerived, type AnyActorOptions, type AnyActorRegistry, type CreateRivetKitOptions, createRivetKit };
|
package/dist/mod.d.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
export
|
|
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
|
+
|
|
5
8
|
interface ActorStateReference<AD extends AnyActorDefinition> {
|
|
6
9
|
/**
|
|
7
10
|
* The unique identifier for the actor.
|
|
@@ -21,17 +24,9 @@ interface ActorStateReference<AD extends AnyActorDefinition> {
|
|
|
21
24
|
*/
|
|
22
25
|
connection: ActorConn<AD> | null;
|
|
23
26
|
/**
|
|
24
|
-
*
|
|
25
|
-
*/
|
|
26
|
-
isConnected?: boolean;
|
|
27
|
-
/**
|
|
28
|
-
* Whether the actor is currently connecting, indicating that a connection attempt is in progress.
|
|
27
|
+
* The connection status of the actor.
|
|
29
28
|
*/
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Whether there was an error connecting to the actor.
|
|
33
|
-
*/
|
|
34
|
-
isError?: boolean;
|
|
29
|
+
connStatus: ActorConnStatus;
|
|
35
30
|
/**
|
|
36
31
|
* The error that occurred while trying to connect to the actor, if any.
|
|
37
32
|
*/
|
|
@@ -61,6 +56,12 @@ interface ActorStateReference<AD extends AnyActorDefinition> {
|
|
|
61
56
|
* Defaults to true.
|
|
62
57
|
*/
|
|
63
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;
|
|
64
65
|
};
|
|
65
66
|
}
|
|
66
67
|
interface InternalRivetKitStore<Registry extends AnyActorRegistry, Actors extends ExtractActorsFromRegistry<Registry>> {
|
|
@@ -69,7 +70,7 @@ interface InternalRivetKitStore<Registry extends AnyActorRegistry, Actors extend
|
|
|
69
70
|
/**
|
|
70
71
|
* Options for configuring a actor in RivetKit.
|
|
71
72
|
*/
|
|
72
|
-
|
|
73
|
+
interface ActorOptions<Registry extends AnyActorRegistry, ActorName extends keyof ExtractActorsFromRegistry<Registry>> {
|
|
73
74
|
/**
|
|
74
75
|
* Typesafe name of the actor.
|
|
75
76
|
* This should match the actor's name in the app's actor definitions.
|
|
@@ -95,24 +96,37 @@ export interface ActorOptions<Registry extends AnyActorRegistry, ActorName exten
|
|
|
95
96
|
* Defaults to true.
|
|
96
97
|
*/
|
|
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;
|
|
98
105
|
}
|
|
99
|
-
|
|
106
|
+
type ActorsStateDerived<Registry extends AnyActorRegistry, WorkerName extends keyof ExtractActorsFromRegistry<Registry>> = Derived<Omit<InternalRivetKitStore<Registry, ExtractActorsFromRegistry<Registry>>["actors"][string], "handle" | "connection"> & {
|
|
100
107
|
handle: ActorHandle<ExtractActorsFromRegistry<Registry>[WorkerName]> | null;
|
|
101
108
|
connection: ActorConn<ExtractActorsFromRegistry<Registry>[WorkerName]> | null;
|
|
109
|
+
/** @deprecated Use `connStatus === "connected"` instead */
|
|
110
|
+
isConnected: boolean;
|
|
102
111
|
}>;
|
|
103
|
-
|
|
104
|
-
|
|
112
|
+
type AnyActorOptions = ActorOptions<AnyActorRegistry, any>;
|
|
113
|
+
interface CreateRivetKitOptions<Registry extends AnyActorRegistry> {
|
|
105
114
|
hashFunction?: (opts: ActorOptions<Registry, any>) => string;
|
|
106
115
|
}
|
|
107
|
-
|
|
108
|
-
getOrCreateActor: <ActorName extends
|
|
116
|
+
declare function createRivetKit<Registry extends AnyActorRegistry, Actors extends ExtractActorsFromRegistry<Registry>>(client: Client<Registry>, createOpts?: CreateRivetKitOptions<Registry>): {
|
|
117
|
+
getOrCreateActor: <ActorName extends keyof Actors>(actorOpts: ActorOptions<Registry, ActorName>) => {
|
|
109
118
|
state: ActorsStateDerived<Registry, ActorName>;
|
|
110
119
|
key: string;
|
|
111
|
-
mount: () => void;
|
|
112
|
-
setState: (set: Updater<ActorStateReference<Actors>>) => void;
|
|
120
|
+
mount: () => () => void;
|
|
113
121
|
create: () => void;
|
|
114
|
-
|
|
122
|
+
refCount: number;
|
|
123
|
+
cleanupTimeout: ReturnType<typeof setTimeout> | null;
|
|
124
|
+
} | {
|
|
125
|
+
mount: () => () => void;
|
|
126
|
+
state: ActorsStateDerived<Registry, ActorName>;
|
|
127
|
+
key: string;
|
|
115
128
|
};
|
|
116
129
|
store: Store<InternalRivetKitStore<Registry, Actors>, (cb: InternalRivetKitStore<Registry, Actors>) => InternalRivetKitStore<Registry, Actors>>;
|
|
117
130
|
};
|
|
118
|
-
|
|
131
|
+
|
|
132
|
+
export { type ActorOptions, type ActorsStateDerived, type AnyActorOptions, type AnyActorRegistry, type CreateRivetKitOptions, createRivetKit };
|
package/dist/mod.js
CHANGED
|
@@ -1,298 +1,241 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
for (const s of e) {
|
|
15
|
-
const o = p.get(s);
|
|
16
|
-
o && C(o);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
function F(i) {
|
|
21
|
-
i.listeners.forEach(
|
|
22
|
-
(n) => n({
|
|
23
|
-
prevVal: i.prevState,
|
|
24
|
-
currentVal: i.state
|
|
25
|
-
})
|
|
26
|
-
);
|
|
27
|
-
}
|
|
28
|
-
function O(i) {
|
|
29
|
-
i.listeners.forEach(
|
|
30
|
-
(n) => n({
|
|
31
|
-
prevVal: i.prevState,
|
|
32
|
-
currentVal: i.state
|
|
33
|
-
})
|
|
34
|
-
);
|
|
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(client, createOpts, store, cache, actorOpts),
|
|
12
|
+
store
|
|
13
|
+
};
|
|
35
14
|
}
|
|
36
|
-
function
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
for (const t of n) {
|
|
43
|
-
const e = k.get(t) ?? t.prevState;
|
|
44
|
-
t.prevState = e, F(t);
|
|
45
|
-
}
|
|
46
|
-
for (const t of n) {
|
|
47
|
-
const e = p.get(t);
|
|
48
|
-
e && (g.current.push(t), C(e));
|
|
49
|
-
}
|
|
50
|
-
for (const t of n) {
|
|
51
|
-
const e = p.get(t);
|
|
52
|
-
if (e)
|
|
53
|
-
for (const s of e)
|
|
54
|
-
O(s);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
} finally {
|
|
58
|
-
b = !1, g.current = [], k.clear();
|
|
15
|
+
function updateActor(store, key, updates) {
|
|
16
|
+
store.setState((prev) => ({
|
|
17
|
+
...prev,
|
|
18
|
+
actors: {
|
|
19
|
+
...prev.actors,
|
|
20
|
+
[key]: { ...prev.actors[key], ...updates }
|
|
59
21
|
}
|
|
22
|
+
}));
|
|
60
23
|
}
|
|
61
|
-
function
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
constructor(n) {
|
|
82
|
-
this.listeners = /* @__PURE__ */ new Set(), this._subscriptions = [], this.lastSeenDepValues = [], this.getDepVals = () => {
|
|
83
|
-
const t = [], e = [];
|
|
84
|
-
for (const s of this.options.deps)
|
|
85
|
-
t.push(s.prevState), e.push(s.state);
|
|
86
|
-
return this.lastSeenDepValues = e, {
|
|
87
|
-
prevDepVals: t,
|
|
88
|
-
currDepVals: e,
|
|
89
|
-
prevVal: this.prevState ?? void 0
|
|
90
|
-
};
|
|
91
|
-
}, this.recompute = () => {
|
|
92
|
-
var t, e;
|
|
93
|
-
this.prevState = this.state;
|
|
94
|
-
const { prevDepVals: s, currDepVals: o, prevVal: h } = this.getDepVals();
|
|
95
|
-
this.state = this.options.fn({
|
|
96
|
-
prevDepVals: s,
|
|
97
|
-
currDepVals: o,
|
|
98
|
-
prevVal: h
|
|
99
|
-
}), (e = (t = this.options).onUpdate) == null || e.call(t);
|
|
100
|
-
}, this.checkIfRecalculationNeededDeeply = () => {
|
|
101
|
-
for (const o of this.options.deps)
|
|
102
|
-
o instanceof f && o.checkIfRecalculationNeededDeeply();
|
|
103
|
-
let t = !1;
|
|
104
|
-
const e = this.lastSeenDepValues, { currDepVals: s } = this.getDepVals();
|
|
105
|
-
for (let o = 0; o < s.length; o++)
|
|
106
|
-
if (s[o] !== e[o]) {
|
|
107
|
-
t = !0;
|
|
108
|
-
break;
|
|
24
|
+
function getOrCreateActor(client, createOpts, store, cache, actorOpts) {
|
|
25
|
+
const hash = createOpts.hashFunction || defaultHashFunction;
|
|
26
|
+
const normalizedOpts = {
|
|
27
|
+
...actorOpts,
|
|
28
|
+
enabled: _nullishCoalesce(actorOpts.enabled, () => ( true))
|
|
29
|
+
};
|
|
30
|
+
const key = hash(normalizedOpts);
|
|
31
|
+
const existing = store.state.actors[key];
|
|
32
|
+
if (!existing) {
|
|
33
|
+
store.setState((prev) => ({
|
|
34
|
+
...prev,
|
|
35
|
+
actors: {
|
|
36
|
+
...prev.actors,
|
|
37
|
+
[key]: {
|
|
38
|
+
hash: key,
|
|
39
|
+
connStatus: "idle",
|
|
40
|
+
connection: null,
|
|
41
|
+
handle: null,
|
|
42
|
+
error: null,
|
|
43
|
+
opts: normalizedOpts
|
|
109
44
|
}
|
|
110
|
-
t && this.recompute();
|
|
111
|
-
}, this.mount = () => (this.registerOnGraph(), this.checkIfRecalculationNeededDeeply(), () => {
|
|
112
|
-
this.unregisterFromGraph();
|
|
113
|
-
for (const t of this._subscriptions)
|
|
114
|
-
t();
|
|
115
|
-
}), this.subscribe = (t) => {
|
|
116
|
-
var e, s;
|
|
117
|
-
this.listeners.add(t);
|
|
118
|
-
const o = (s = (e = this.options).onSubscribe) == null ? void 0 : s.call(e, t, this);
|
|
119
|
-
return () => {
|
|
120
|
-
this.listeners.delete(t), o == null || o();
|
|
121
|
-
};
|
|
122
|
-
}, this.options = n, this.state = n.fn({
|
|
123
|
-
prevDepVals: void 0,
|
|
124
|
-
prevVal: void 0,
|
|
125
|
-
currDepVals: this.getDepVals().currDepVals
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
registerOnGraph(n = this.options.deps) {
|
|
129
|
-
for (const t of n)
|
|
130
|
-
if (t instanceof f)
|
|
131
|
-
t.registerOnGraph(), this.registerOnGraph(t.options.deps);
|
|
132
|
-
else if (t instanceof m) {
|
|
133
|
-
let e = p.get(t);
|
|
134
|
-
e || (e = /* @__PURE__ */ new Set(), p.set(t, e)), e.add(this);
|
|
135
|
-
let s = S.get(this);
|
|
136
|
-
s || (s = /* @__PURE__ */ new Set(), S.set(this, s)), s.add(t);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
unregisterFromGraph(n = this.options.deps) {
|
|
140
|
-
for (const t of n)
|
|
141
|
-
if (t instanceof f)
|
|
142
|
-
this.unregisterFromGraph(t.options.deps);
|
|
143
|
-
else if (t instanceof m) {
|
|
144
|
-
const e = p.get(t);
|
|
145
|
-
e && e.delete(this);
|
|
146
|
-
const s = S.get(this);
|
|
147
|
-
s && s.delete(t);
|
|
148
45
|
}
|
|
46
|
+
}));
|
|
47
|
+
} else if (!optsEqual(existing.opts, normalizedOpts)) {
|
|
48
|
+
queueMicrotask(() => {
|
|
49
|
+
updateActor(store, key, { opts: normalizedOpts });
|
|
50
|
+
});
|
|
149
51
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
fn: () => {
|
|
157
|
-
},
|
|
158
|
-
onUpdate() {
|
|
159
|
-
e();
|
|
160
|
-
}
|
|
161
|
-
}), t && e();
|
|
162
|
-
}
|
|
163
|
-
mount() {
|
|
164
|
-
return this._derived.mount();
|
|
52
|
+
const cached = cache.get(key);
|
|
53
|
+
if (cached) {
|
|
54
|
+
return {
|
|
55
|
+
...cached,
|
|
56
|
+
state: cached.state
|
|
57
|
+
};
|
|
165
58
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
actors: {}
|
|
170
|
-
}), e = n.hashFunction || L, s = /* @__PURE__ */ new Map();
|
|
171
|
-
function o(h) {
|
|
172
|
-
const r = e(h), _ = s.get(r);
|
|
173
|
-
if (_)
|
|
59
|
+
const derived = new (0, _store.Derived)({
|
|
60
|
+
fn: ({ currDepVals: [store2] }) => {
|
|
61
|
+
const actor = store2.actors[key];
|
|
174
62
|
return {
|
|
175
|
-
...
|
|
176
|
-
|
|
63
|
+
...actor,
|
|
64
|
+
/** @deprecated Use `connStatus === "connected"` instead */
|
|
65
|
+
isConnected: actor.connStatus === "connected"
|
|
177
66
|
};
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
c.opts.key,
|
|
189
|
-
{
|
|
190
|
-
params: c.opts.params,
|
|
191
|
-
createInRegion: c.opts.createInRegion,
|
|
192
|
-
createWithInput: c.opts.createWithInput
|
|
193
|
-
}
|
|
194
|
-
), l = u.connect();
|
|
195
|
-
await u.resolve(
|
|
196
|
-
/*{ signal: AbortSignal.timeout(0) }*/
|
|
197
|
-
), t.setState((D) => ({
|
|
198
|
-
...D,
|
|
199
|
-
actors: {
|
|
200
|
-
...D.actors,
|
|
201
|
-
[r]: {
|
|
202
|
-
...D.actors[r],
|
|
203
|
-
isConnected: !0,
|
|
204
|
-
isConnecting: !1,
|
|
205
|
-
handle: u,
|
|
206
|
-
connection: l,
|
|
207
|
-
isError: !1,
|
|
208
|
-
error: null
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
}));
|
|
212
|
-
} catch (u) {
|
|
213
|
-
t.setState((l) => ({
|
|
214
|
-
...l,
|
|
215
|
-
actors: {
|
|
216
|
-
...l.actors,
|
|
217
|
-
[r]: {
|
|
218
|
-
...l.actors[r],
|
|
219
|
-
isError: !0,
|
|
220
|
-
isConnecting: !1,
|
|
221
|
-
error: u
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
}));
|
|
225
|
-
}
|
|
67
|
+
},
|
|
68
|
+
deps: [store]
|
|
69
|
+
});
|
|
70
|
+
const effect = new (0, _store.Effect)({
|
|
71
|
+
fn: () => {
|
|
72
|
+
const actor = store.state.actors[key];
|
|
73
|
+
if (!actor) {
|
|
74
|
+
throw new Error(
|
|
75
|
+
`Actor with key "${key}" not found in store. This indicates a bug in cleanup logic.`
|
|
76
|
+
);
|
|
226
77
|
}
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
fn: () => {
|
|
231
|
-
const a = t.state.actors[r];
|
|
232
|
-
JSON.stringify(t.prevState.actors[r].opts) === JSON.stringify(t.state.actors[r].opts) && !a.isConnected && !a.isConnecting && !a.isError && a.opts.enabled && V();
|
|
233
|
-
},
|
|
234
|
-
deps: [d]
|
|
235
|
-
});
|
|
236
|
-
t.setState((a) => a.actors[r] ? a : {
|
|
237
|
-
...a,
|
|
238
|
-
actors: {
|
|
239
|
-
...a.actors,
|
|
240
|
-
[r]: {
|
|
241
|
-
hash: r,
|
|
242
|
-
isConnected: !1,
|
|
243
|
-
isConnecting: !1,
|
|
78
|
+
if (!actor.opts.enabled && actor.connection) {
|
|
79
|
+
actor.connection.dispose();
|
|
80
|
+
updateActor(store, key, {
|
|
244
81
|
connection: null,
|
|
245
82
|
handle: null,
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
83
|
+
connStatus: "idle"
|
|
84
|
+
});
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
if (actor.connStatus === "idle" && actor.opts.enabled) {
|
|
88
|
+
queueMicrotask(() => {
|
|
89
|
+
const currentActor = store.state.actors[key];
|
|
90
|
+
if (currentActor && currentActor.connStatus === "idle" && currentActor.opts.enabled) {
|
|
91
|
+
create(client, store, key);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
deps: [derived]
|
|
97
|
+
});
|
|
98
|
+
let unsubscribeDerived = null;
|
|
99
|
+
let unsubscribeEffect = null;
|
|
100
|
+
const mount = () => {
|
|
101
|
+
const cached2 = cache.get(key);
|
|
102
|
+
if (!cached2) {
|
|
103
|
+
throw new Error(
|
|
104
|
+
`Actor with key "${key}" not found in cache. This indicates a bug in cleanup logic.`
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
if (cached2.cleanupTimeout !== null) {
|
|
108
|
+
clearTimeout(cached2.cleanupTimeout);
|
|
109
|
+
cached2.cleanupTimeout = null;
|
|
110
|
+
}
|
|
111
|
+
cached2.refCount++;
|
|
112
|
+
if (cached2.refCount === 1) {
|
|
113
|
+
unsubscribeDerived = derived.mount();
|
|
114
|
+
unsubscribeEffect = effect.mount();
|
|
115
|
+
const actor = store.state.actors[key];
|
|
116
|
+
if (actor && actor.opts.enabled && actor.connStatus === "idle") {
|
|
117
|
+
create(client, store, key);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return () => {
|
|
121
|
+
cached2.refCount--;
|
|
122
|
+
if (cached2.refCount === 0) {
|
|
123
|
+
cached2.cleanupTimeout = setTimeout(() => {
|
|
124
|
+
cached2.cleanupTimeout = null;
|
|
125
|
+
if (cached2.refCount > 0) return;
|
|
126
|
+
unsubscribeDerived == null ? void 0 : unsubscribeDerived();
|
|
127
|
+
unsubscribeEffect == null ? void 0 : unsubscribeEffect();
|
|
128
|
+
unsubscribeDerived = null;
|
|
129
|
+
unsubscribeEffect = null;
|
|
130
|
+
const actor = store.state.actors[key];
|
|
131
|
+
if (actor == null ? void 0 : actor.connection) {
|
|
132
|
+
actor.connection.dispose();
|
|
133
|
+
}
|
|
134
|
+
store.setState((prev) => {
|
|
135
|
+
const { [key]: _, ...rest } = prev.actors;
|
|
136
|
+
return { ...prev, actors: rest };
|
|
137
|
+
});
|
|
138
|
+
cache.delete(key);
|
|
139
|
+
}, 0);
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
};
|
|
143
|
+
cache.set(key, {
|
|
144
|
+
state: derived,
|
|
145
|
+
key,
|
|
146
|
+
mount,
|
|
147
|
+
create: create.bind(void 0, client, store, key),
|
|
148
|
+
refCount: 0,
|
|
149
|
+
cleanupTimeout: null
|
|
150
|
+
});
|
|
151
|
+
return {
|
|
152
|
+
mount,
|
|
153
|
+
state: derived,
|
|
154
|
+
key
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
function create(client, store, key) {
|
|
158
|
+
const actor = store.state.actors[key];
|
|
159
|
+
if (!actor) {
|
|
160
|
+
throw new Error(
|
|
161
|
+
`Actor with key "${key}" not found in store. This indicates a bug in cleanup logic.`
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
updateActor(store, key, {
|
|
165
|
+
connStatus: "connecting",
|
|
166
|
+
error: null
|
|
167
|
+
});
|
|
168
|
+
try {
|
|
169
|
+
const handle = actor.opts.noCreate ? client.get(
|
|
170
|
+
actor.opts.name,
|
|
171
|
+
actor.opts.key,
|
|
172
|
+
{
|
|
173
|
+
params: actor.opts.params
|
|
174
|
+
}
|
|
175
|
+
) : client.getOrCreate(
|
|
176
|
+
actor.opts.name,
|
|
177
|
+
actor.opts.key,
|
|
178
|
+
{
|
|
179
|
+
params: actor.opts.params,
|
|
180
|
+
createInRegion: actor.opts.createInRegion,
|
|
181
|
+
createWithInput: actor.opts.createWithInput
|
|
250
182
|
}
|
|
183
|
+
);
|
|
184
|
+
const connection = handle.connect();
|
|
185
|
+
updateActor(store, key, {
|
|
186
|
+
handle,
|
|
187
|
+
connection
|
|
251
188
|
});
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
...c,
|
|
189
|
+
connection.onStatusChange((status) => {
|
|
190
|
+
store.setState((prev) => {
|
|
191
|
+
var _a;
|
|
192
|
+
const isActiveConnection = ((_a = prev.actors[key]) == null ? void 0 : _a.connection) === connection;
|
|
193
|
+
if (!isActiveConnection) return prev;
|
|
194
|
+
return {
|
|
195
|
+
...prev,
|
|
260
196
|
actors: {
|
|
261
|
-
...
|
|
262
|
-
[
|
|
197
|
+
...prev.actors,
|
|
198
|
+
[key]: {
|
|
199
|
+
...prev.actors[key],
|
|
200
|
+
connStatus: status,
|
|
201
|
+
// Only clear error when successfully connected
|
|
202
|
+
...status === "connected" ? { error: null } : {}
|
|
203
|
+
}
|
|
263
204
|
}
|
|
264
205
|
};
|
|
265
206
|
});
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
207
|
+
});
|
|
208
|
+
connection.onError((error) => {
|
|
209
|
+
store.setState((prev) => {
|
|
210
|
+
var _a;
|
|
211
|
+
if (((_a = prev.actors[key]) == null ? void 0 : _a.connection) !== connection) return prev;
|
|
212
|
+
return {
|
|
213
|
+
...prev,
|
|
214
|
+
actors: {
|
|
215
|
+
...prev.actors,
|
|
216
|
+
[key]: {
|
|
217
|
+
...prev.actors[key],
|
|
218
|
+
error
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
} catch (error) {
|
|
225
|
+
console.error("Failed to create actor connection", error);
|
|
226
|
+
updateActor(store, key, {
|
|
227
|
+
connStatus: "disconnected",
|
|
228
|
+
error
|
|
229
|
+
});
|
|
287
230
|
}
|
|
288
|
-
return {
|
|
289
|
-
getOrCreateActor: o,
|
|
290
|
-
store: t
|
|
291
|
-
};
|
|
292
231
|
}
|
|
293
|
-
function
|
|
294
|
-
return JSON.stringify({ name
|
|
232
|
+
function defaultHashFunction({ name, key, params, noCreate }) {
|
|
233
|
+
return JSON.stringify({ name, key, params, noCreate });
|
|
234
|
+
}
|
|
235
|
+
function optsEqual(a, b) {
|
|
236
|
+
return _fastdeepequal2.default.call(void 0, a, b);
|
|
295
237
|
}
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
exports.createRivetKit = createRivetKit;
|
|
241
|
+
//# 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;AAoKA,SAAS,cAAA,CAGd,MAAA,EAA0B,WAAA,EAA8C,CAAC,CAAA,EAAG;AAC7E,EAAA,MAAM,MAAA,EAAQ,IAAI,iBAAA,CAA+C;AAAA,IAChE,MAAA,EAAQ,CAAC;AAAA,EACV,CAAC,CAAA;AAED,EAAA,MAAM,MAAA,kBAAsC,IAAI,GAAA,CAAI,CAAA;AAEpD,EAAA,OAAO;AAAA,IACN,gBAAA,EAAkB,CACjB,SAAA,EAAA,GACI,gBAAA,CAAiB,MAAA,EAAQ,UAAA,EAAY,KAAA,EAAO,KAAA,EAAO,SAAS,CAAA;AAAA,IACjE;AAAA,EACD,CAAA;AACD;AAOA,SAAS,WAAA,CAIR,KAAA,EACA,GAAA,EACA,OAAA,EACC;AACD,EAAA,KAAA,CAAM,QAAA,CAAS,CAAC,IAAA,EAAA,GAAA,CAAU;AAAA,IACzB,GAAG,IAAA;AAAA,IACH,MAAA,EAAQ;AAAA,MACP,GAAG,IAAA,CAAK,MAAA;AAAA,MACR,CAAC,GAAG,CAAA,EAAG,EAAE,GAAG,IAAA,CAAK,MAAA,CAAO,GAAG,CAAA,EAAG,GAAG,QAAQ;AAAA,IAC1C;AAAA,EACD,CAAA,CAAE,CAAA;AACH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AC7B,MAAA,KAAA,CAAM,QAAA,CAAS,CAAC,IAAA,EAAA,GAAS;AA1e5B,QAAA,IAAA,EAAA;AA4eI,QAAA,GAAA,CAAA,CAAA,CAAI,GAAA,EAAA,IAAA,CAAK,MAAA,CAAO,GAAG,CAAA,EAAA,GAAf,KAAA,EAAA,KAAA,EAAA,EAAA,EAAA,CAAkB,UAAA,EAAA,IAAe,UAAA,EAAY,OAAO,IAAA;AACxD,QAAA,OAAO;AAAA,UACN,GAAG,IAAA;AAAA,UACH,MAAA,EAAQ;AAAA,YACP,GAAG,IAAA,CAAK,MAAA;AAAA,YACR,CAAC,GAAG,CAAA,EAAG;AAAA,cACN,GAAG,IAAA,CAAK,MAAA,CAAO,GAAG,CAAA;AAAA,cAClB;AAAA,YACD;AAAA,UACD;AAAA,QACD,CAAA;AAAA,MACD,CAAC,CAAA;AAAA,IACF,CAAC,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;AD7RA;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<AD extends AnyActorDefinition> {\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<AD> | 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<AD> | 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: keyof AD;\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?: Record<string, string>;\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\tRegistry extends AnyActorRegistry,\n\tActors extends ExtractActorsFromRegistry<Registry>,\n> {\n\tactors: Record<string, ActorStateReference<Actors>>;\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>,\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?: Registry[ExtractActorsFromRegistry<Registry>][\"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>,\n> = Derived<\n\tOmit<\n\t\tInternalRivetKitStore<\n\t\t\tRegistry,\n\t\t\tExtractActorsFromRegistry<Registry>\n\t\t>[\"actors\"][string],\n\t\t\"handle\" | \"connection\"\n\t> & {\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<\n\tRegistry extends AnyActorRegistry,\n\tActors extends ExtractActorsFromRegistry<Registry>,\n> = InternalRivetKitStore<Registry, Actors>[\"actors\"][string] & {\n\t/** @deprecated Use `connStatus === \"connected\"` instead */\n\tisConnected: boolean;\n};\n\ntype ActorCache<\n\tRegistry extends AnyActorRegistry,\n\tActors extends ExtractActorsFromRegistry<Registry>,\n> = Map<\n\tstring,\n\t{\n\t\tstate: Derived<ComputedActorState<Registry, Actors>>;\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<\n\tRegistry extends AnyActorRegistry,\n\tActors extends ExtractActorsFromRegistry<Registry>,\n>(client: Client<Registry>, createOpts: CreateRivetKitOptions<Registry> = {}) {\n\tconst store = new Store<InternalRivetKitStore<Registry, Actors>>({\n\t\tactors: {},\n\t});\n\n\tconst cache: ActorCache<Registry, Actors> = new Map();\n\n\treturn {\n\t\tgetOrCreateActor: <ActorName extends keyof Actors>(\n\t\t\tactorOpts: ActorOptions<Registry, ActorName>,\n\t\t) => getOrCreateActor(client, createOpts, store, cache, actorOpts),\n\t\tstore,\n\t};\n}\n\ntype ActorUpdates<\n\tRegistry extends AnyActorRegistry,\n\tActors extends ExtractActorsFromRegistry<Registry>,\n> = Partial<InternalRivetKitStore<Registry, Actors>[\"actors\"][string]>;\n\nfunction updateActor<\n\tRegistry extends AnyActorRegistry,\n\tActors extends ExtractActorsFromRegistry<Registry>,\n>(\n\tstore: Store<InternalRivetKitStore<Registry, Actors>>,\n\tkey: string,\n\tupdates: ActorUpdates<Registry, Actors>,\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\tActors extends ExtractActorsFromRegistry<Registry>,\n\tActorName extends keyof Actors,\n>(\n\tclient: Client<Registry>,\n\tcreateOpts: CreateRivetKitOptions<Registry>,\n\tstore: Store<InternalRivetKitStore<Registry, Actors>>,\n\tcache: ActorCache<Registry, Actors>,\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 (\n\t\t\t\tactor.connStatus === \"idle\" &&\n\t\t\t\tactor.opts.enabled\n\t\t\t) {\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\tcreate<Registry, Actors, ActorName>(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 (\n\t\t\t\tactor &&\n\t\t\t\tactor.opts.enabled &&\n\t\t\t\tactor.connStatus === \"idle\"\n\t\t\t) {\n\t\t\t\tcreate<Registry, Actors, ActorName>(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\tcache.set(key, {\n\t\tstate: derived,\n\t\tkey,\n\t\tmount,\n\t\tcreate: create.bind(undefined, client, store, key),\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\tActors extends ExtractActorsFromRegistry<Registry>,\n\tActorName extends keyof Actors,\n>(\n\tclient: Client<Registry>,\n\tstore: Store<InternalRivetKitStore<Registry, Actors>>,\n\tkey: string,\n) {\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(\n\t\t\t\t\tactor.opts.name as string,\n\t\t\t\t\tactor.opts.key,\n\t\t\t\t\t{\n\t\t\t\t\t\tparams: actor.opts.params,\n\t\t\t\t\t},\n\t\t\t\t)\n\t\t\t: client.getOrCreate(\n\t\t\t\t\tactor.opts.name as string,\n\t\t\t\t\tactor.opts.key,\n\t\t\t\t\t{\n\t\t\t\t\t\tparams: actor.opts.params,\n\t\t\t\t\t\tcreateInRegion: actor.opts.createInRegion,\n\t\t\t\t\t\tcreateWithInput: actor.opts.createWithInput,\n\t\t\t\t\t},\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<Actors[ActorName]>,\n\t\t\tconnection: connection as ActorConn<Actors[ActorName]>,\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 = prev.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\"\n\t\t\t\t\t\t\t\t? { error: null }\n\t\t\t\t\t\t\t\t: {}),\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,241 @@
|
|
|
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(client, createOpts, store, cache, actorOpts),
|
|
12
|
+
store
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
function updateActor(store, key, updates) {
|
|
16
|
+
store.setState((prev) => ({
|
|
17
|
+
...prev,
|
|
18
|
+
actors: {
|
|
19
|
+
...prev.actors,
|
|
20
|
+
[key]: { ...prev.actors[key], ...updates }
|
|
21
|
+
}
|
|
22
|
+
}));
|
|
23
|
+
}
|
|
24
|
+
function getOrCreateActor(client, createOpts, store, cache, actorOpts) {
|
|
25
|
+
const hash = createOpts.hashFunction || defaultHashFunction;
|
|
26
|
+
const normalizedOpts = {
|
|
27
|
+
...actorOpts,
|
|
28
|
+
enabled: actorOpts.enabled ?? true
|
|
29
|
+
};
|
|
30
|
+
const key = hash(normalizedOpts);
|
|
31
|
+
const existing = store.state.actors[key];
|
|
32
|
+
if (!existing) {
|
|
33
|
+
store.setState((prev) => ({
|
|
34
|
+
...prev,
|
|
35
|
+
actors: {
|
|
36
|
+
...prev.actors,
|
|
37
|
+
[key]: {
|
|
38
|
+
hash: key,
|
|
39
|
+
connStatus: "idle",
|
|
40
|
+
connection: null,
|
|
41
|
+
handle: null,
|
|
42
|
+
error: null,
|
|
43
|
+
opts: normalizedOpts
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}));
|
|
47
|
+
} else if (!optsEqual(existing.opts, normalizedOpts)) {
|
|
48
|
+
queueMicrotask(() => {
|
|
49
|
+
updateActor(store, key, { opts: normalizedOpts });
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
const cached = cache.get(key);
|
|
53
|
+
if (cached) {
|
|
54
|
+
return {
|
|
55
|
+
...cached,
|
|
56
|
+
state: cached.state
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
const derived = new Derived({
|
|
60
|
+
fn: ({ currDepVals: [store2] }) => {
|
|
61
|
+
const actor = store2.actors[key];
|
|
62
|
+
return {
|
|
63
|
+
...actor,
|
|
64
|
+
/** @deprecated Use `connStatus === "connected"` instead */
|
|
65
|
+
isConnected: actor.connStatus === "connected"
|
|
66
|
+
};
|
|
67
|
+
},
|
|
68
|
+
deps: [store]
|
|
69
|
+
});
|
|
70
|
+
const effect = new Effect({
|
|
71
|
+
fn: () => {
|
|
72
|
+
const actor = store.state.actors[key];
|
|
73
|
+
if (!actor) {
|
|
74
|
+
throw new Error(
|
|
75
|
+
`Actor with key "${key}" not found in store. This indicates a bug in cleanup logic.`
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
if (!actor.opts.enabled && actor.connection) {
|
|
79
|
+
actor.connection.dispose();
|
|
80
|
+
updateActor(store, key, {
|
|
81
|
+
connection: null,
|
|
82
|
+
handle: null,
|
|
83
|
+
connStatus: "idle"
|
|
84
|
+
});
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
if (actor.connStatus === "idle" && actor.opts.enabled) {
|
|
88
|
+
queueMicrotask(() => {
|
|
89
|
+
const currentActor = store.state.actors[key];
|
|
90
|
+
if (currentActor && currentActor.connStatus === "idle" && currentActor.opts.enabled) {
|
|
91
|
+
create(client, store, key);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
deps: [derived]
|
|
97
|
+
});
|
|
98
|
+
let unsubscribeDerived = null;
|
|
99
|
+
let unsubscribeEffect = null;
|
|
100
|
+
const mount = () => {
|
|
101
|
+
const cached2 = cache.get(key);
|
|
102
|
+
if (!cached2) {
|
|
103
|
+
throw new Error(
|
|
104
|
+
`Actor with key "${key}" not found in cache. This indicates a bug in cleanup logic.`
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
if (cached2.cleanupTimeout !== null) {
|
|
108
|
+
clearTimeout(cached2.cleanupTimeout);
|
|
109
|
+
cached2.cleanupTimeout = null;
|
|
110
|
+
}
|
|
111
|
+
cached2.refCount++;
|
|
112
|
+
if (cached2.refCount === 1) {
|
|
113
|
+
unsubscribeDerived = derived.mount();
|
|
114
|
+
unsubscribeEffect = effect.mount();
|
|
115
|
+
const actor = store.state.actors[key];
|
|
116
|
+
if (actor && actor.opts.enabled && actor.connStatus === "idle") {
|
|
117
|
+
create(client, store, key);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return () => {
|
|
121
|
+
cached2.refCount--;
|
|
122
|
+
if (cached2.refCount === 0) {
|
|
123
|
+
cached2.cleanupTimeout = setTimeout(() => {
|
|
124
|
+
cached2.cleanupTimeout = null;
|
|
125
|
+
if (cached2.refCount > 0) return;
|
|
126
|
+
unsubscribeDerived == null ? void 0 : unsubscribeDerived();
|
|
127
|
+
unsubscribeEffect == null ? void 0 : unsubscribeEffect();
|
|
128
|
+
unsubscribeDerived = null;
|
|
129
|
+
unsubscribeEffect = null;
|
|
130
|
+
const actor = store.state.actors[key];
|
|
131
|
+
if (actor == null ? void 0 : actor.connection) {
|
|
132
|
+
actor.connection.dispose();
|
|
133
|
+
}
|
|
134
|
+
store.setState((prev) => {
|
|
135
|
+
const { [key]: _, ...rest } = prev.actors;
|
|
136
|
+
return { ...prev, actors: rest };
|
|
137
|
+
});
|
|
138
|
+
cache.delete(key);
|
|
139
|
+
}, 0);
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
};
|
|
143
|
+
cache.set(key, {
|
|
144
|
+
state: derived,
|
|
145
|
+
key,
|
|
146
|
+
mount,
|
|
147
|
+
create: create.bind(void 0, client, store, key),
|
|
148
|
+
refCount: 0,
|
|
149
|
+
cleanupTimeout: null
|
|
150
|
+
});
|
|
151
|
+
return {
|
|
152
|
+
mount,
|
|
153
|
+
state: derived,
|
|
154
|
+
key
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
function create(client, store, key) {
|
|
158
|
+
const actor = store.state.actors[key];
|
|
159
|
+
if (!actor) {
|
|
160
|
+
throw new Error(
|
|
161
|
+
`Actor with key "${key}" not found in store. This indicates a bug in cleanup logic.`
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
updateActor(store, key, {
|
|
165
|
+
connStatus: "connecting",
|
|
166
|
+
error: null
|
|
167
|
+
});
|
|
168
|
+
try {
|
|
169
|
+
const handle = actor.opts.noCreate ? client.get(
|
|
170
|
+
actor.opts.name,
|
|
171
|
+
actor.opts.key,
|
|
172
|
+
{
|
|
173
|
+
params: actor.opts.params
|
|
174
|
+
}
|
|
175
|
+
) : client.getOrCreate(
|
|
176
|
+
actor.opts.name,
|
|
177
|
+
actor.opts.key,
|
|
178
|
+
{
|
|
179
|
+
params: actor.opts.params,
|
|
180
|
+
createInRegion: actor.opts.createInRegion,
|
|
181
|
+
createWithInput: actor.opts.createWithInput
|
|
182
|
+
}
|
|
183
|
+
);
|
|
184
|
+
const connection = handle.connect();
|
|
185
|
+
updateActor(store, key, {
|
|
186
|
+
handle,
|
|
187
|
+
connection
|
|
188
|
+
});
|
|
189
|
+
connection.onStatusChange((status) => {
|
|
190
|
+
store.setState((prev) => {
|
|
191
|
+
var _a;
|
|
192
|
+
const isActiveConnection = ((_a = prev.actors[key]) == null ? void 0 : _a.connection) === connection;
|
|
193
|
+
if (!isActiveConnection) return prev;
|
|
194
|
+
return {
|
|
195
|
+
...prev,
|
|
196
|
+
actors: {
|
|
197
|
+
...prev.actors,
|
|
198
|
+
[key]: {
|
|
199
|
+
...prev.actors[key],
|
|
200
|
+
connStatus: status,
|
|
201
|
+
// Only clear error when successfully connected
|
|
202
|
+
...status === "connected" ? { error: null } : {}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
connection.onError((error) => {
|
|
209
|
+
store.setState((prev) => {
|
|
210
|
+
var _a;
|
|
211
|
+
if (((_a = prev.actors[key]) == null ? void 0 : _a.connection) !== connection) return prev;
|
|
212
|
+
return {
|
|
213
|
+
...prev,
|
|
214
|
+
actors: {
|
|
215
|
+
...prev.actors,
|
|
216
|
+
[key]: {
|
|
217
|
+
...prev.actors[key],
|
|
218
|
+
error
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
} catch (error) {
|
|
225
|
+
console.error("Failed to create actor connection", error);
|
|
226
|
+
updateActor(store, key, {
|
|
227
|
+
connStatus: "disconnected",
|
|
228
|
+
error
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
function defaultHashFunction({ name, key, params, noCreate }) {
|
|
233
|
+
return JSON.stringify({ name, key, params, noCreate });
|
|
234
|
+
}
|
|
235
|
+
function optsEqual(a, b) {
|
|
236
|
+
return equal(a, b);
|
|
237
|
+
}
|
|
238
|
+
export {
|
|
239
|
+
createRivetKit
|
|
240
|
+
};
|
|
241
|
+
//# 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<AD extends AnyActorDefinition> {\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<AD> | 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<AD> | 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: keyof AD;\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?: Record<string, string>;\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\tRegistry extends AnyActorRegistry,\n\tActors extends ExtractActorsFromRegistry<Registry>,\n> {\n\tactors: Record<string, ActorStateReference<Actors>>;\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>,\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?: Registry[ExtractActorsFromRegistry<Registry>][\"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>,\n> = Derived<\n\tOmit<\n\t\tInternalRivetKitStore<\n\t\t\tRegistry,\n\t\t\tExtractActorsFromRegistry<Registry>\n\t\t>[\"actors\"][string],\n\t\t\"handle\" | \"connection\"\n\t> & {\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<\n\tRegistry extends AnyActorRegistry,\n\tActors extends ExtractActorsFromRegistry<Registry>,\n> = InternalRivetKitStore<Registry, Actors>[\"actors\"][string] & {\n\t/** @deprecated Use `connStatus === \"connected\"` instead */\n\tisConnected: boolean;\n};\n\ntype ActorCache<\n\tRegistry extends AnyActorRegistry,\n\tActors extends ExtractActorsFromRegistry<Registry>,\n> = Map<\n\tstring,\n\t{\n\t\tstate: Derived<ComputedActorState<Registry, Actors>>;\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<\n\tRegistry extends AnyActorRegistry,\n\tActors extends ExtractActorsFromRegistry<Registry>,\n>(client: Client<Registry>, createOpts: CreateRivetKitOptions<Registry> = {}) {\n\tconst store = new Store<InternalRivetKitStore<Registry, Actors>>({\n\t\tactors: {},\n\t});\n\n\tconst cache: ActorCache<Registry, Actors> = new Map();\n\n\treturn {\n\t\tgetOrCreateActor: <ActorName extends keyof Actors>(\n\t\t\tactorOpts: ActorOptions<Registry, ActorName>,\n\t\t) => getOrCreateActor(client, createOpts, store, cache, actorOpts),\n\t\tstore,\n\t};\n}\n\ntype ActorUpdates<\n\tRegistry extends AnyActorRegistry,\n\tActors extends ExtractActorsFromRegistry<Registry>,\n> = Partial<InternalRivetKitStore<Registry, Actors>[\"actors\"][string]>;\n\nfunction updateActor<\n\tRegistry extends AnyActorRegistry,\n\tActors extends ExtractActorsFromRegistry<Registry>,\n>(\n\tstore: Store<InternalRivetKitStore<Registry, Actors>>,\n\tkey: string,\n\tupdates: ActorUpdates<Registry, Actors>,\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\tActors extends ExtractActorsFromRegistry<Registry>,\n\tActorName extends keyof Actors,\n>(\n\tclient: Client<Registry>,\n\tcreateOpts: CreateRivetKitOptions<Registry>,\n\tstore: Store<InternalRivetKitStore<Registry, Actors>>,\n\tcache: ActorCache<Registry, Actors>,\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 (\n\t\t\t\tactor.connStatus === \"idle\" &&\n\t\t\t\tactor.opts.enabled\n\t\t\t) {\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\tcreate<Registry, Actors, ActorName>(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 (\n\t\t\t\tactor &&\n\t\t\t\tactor.opts.enabled &&\n\t\t\t\tactor.connStatus === \"idle\"\n\t\t\t) {\n\t\t\t\tcreate<Registry, Actors, ActorName>(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\tcache.set(key, {\n\t\tstate: derived,\n\t\tkey,\n\t\tmount,\n\t\tcreate: create.bind(undefined, client, store, key),\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\tActors extends ExtractActorsFromRegistry<Registry>,\n\tActorName extends keyof Actors,\n>(\n\tclient: Client<Registry>,\n\tstore: Store<InternalRivetKitStore<Registry, Actors>>,\n\tkey: string,\n) {\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(\n\t\t\t\t\tactor.opts.name as string,\n\t\t\t\t\tactor.opts.key,\n\t\t\t\t\t{\n\t\t\t\t\t\tparams: actor.opts.params,\n\t\t\t\t\t},\n\t\t\t\t)\n\t\t\t: client.getOrCreate(\n\t\t\t\t\tactor.opts.name as string,\n\t\t\t\t\tactor.opts.key,\n\t\t\t\t\t{\n\t\t\t\t\t\tparams: actor.opts.params,\n\t\t\t\t\t\tcreateInRegion: actor.opts.createInRegion,\n\t\t\t\t\t\tcreateWithInput: actor.opts.createWithInput,\n\t\t\t\t\t},\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<Actors[ActorName]>,\n\t\t\tconnection: connection as ActorConn<Actors[ActorName]>,\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 = prev.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\"\n\t\t\t\t\t\t\t\t? { error: null }\n\t\t\t\t\t\t\t\t: {}),\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;AAoKA,SAAS,eAGd,QAA0B,aAA8C,CAAC,GAAG;AAC7E,QAAM,QAAQ,IAAI,MAA+C;AAAA,IAChE,QAAQ,CAAC;AAAA,EACV,CAAC;AAED,QAAM,QAAsC,oBAAI,IAAI;AAEpD,SAAO;AAAA,IACN,kBAAkB,CACjB,cACI,iBAAiB,QAAQ,YAAY,OAAO,OAAO,SAAS;AAAA,IACjE;AAAA,EACD;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A,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,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rivetkit/framework-base",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.4-rc.1",
|
|
4
|
+
"description": "Base framework utilities for RivetKit client integrations",
|
|
4
5
|
"license": "Apache-2.0",
|
|
5
6
|
"keywords": [
|
|
6
7
|
"rivetkit",
|
|
@@ -9,8 +10,10 @@
|
|
|
9
10
|
"utilities",
|
|
10
11
|
"integration"
|
|
11
12
|
],
|
|
12
|
-
"sideEffects":
|
|
13
|
-
|
|
13
|
+
"sideEffects": [
|
|
14
|
+
"./dist/chunk-*.js",
|
|
15
|
+
"./dist/chunk-*.cjs"
|
|
16
|
+
],
|
|
14
17
|
"files": [
|
|
15
18
|
"dist",
|
|
16
19
|
"package.json"
|
|
@@ -18,29 +21,27 @@
|
|
|
18
21
|
"exports": {
|
|
19
22
|
".": {
|
|
20
23
|
"import": {
|
|
21
|
-
"types": "./dist/mod.d.
|
|
22
|
-
"default": "./dist/mod.
|
|
24
|
+
"types": "./dist/mod.d.mts",
|
|
25
|
+
"default": "./dist/mod.mjs"
|
|
23
26
|
},
|
|
24
27
|
"require": {
|
|
25
|
-
"types": "./dist/mod.d.
|
|
26
|
-
"default": "./dist/mod.
|
|
28
|
+
"types": "./dist/mod.d.ts",
|
|
29
|
+
"default": "./dist/mod.js"
|
|
27
30
|
}
|
|
28
31
|
}
|
|
29
32
|
},
|
|
30
33
|
"devDependencies": {
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"vite-plugin-dts": "^4.5.4",
|
|
34
|
-
"vitest": "^3.1.1"
|
|
34
|
+
"tsup": "^8.4.0",
|
|
35
|
+
"typescript": "^5.5.2"
|
|
35
36
|
},
|
|
36
37
|
"stableVersion": "0.8.0",
|
|
37
38
|
"dependencies": {
|
|
38
39
|
"@tanstack/store": "^0.7.1",
|
|
39
|
-
"
|
|
40
|
+
"fast-deep-equal": "^3.1.3",
|
|
41
|
+
"rivetkit": "2.0.4-rc.1"
|
|
40
42
|
},
|
|
41
43
|
"scripts": {
|
|
42
|
-
"
|
|
43
|
-
"build": "tsc && vite build",
|
|
44
|
+
"build": "tsup src/mod.ts",
|
|
44
45
|
"check-types": "tsc --noEmit"
|
|
45
46
|
}
|
|
46
47
|
}
|
package/dist/mod.cjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=new WeakMap,S=new WeakMap,g={current:[]};let b=!1;const v=new Set,k=new Map;function C(i){const n=Array.from(i).sort((t,e)=>t instanceof f&&t.options.deps.includes(e)?1:e instanceof f&&e.options.deps.includes(t)?-1:0);for(const t of n){if(g.current.includes(t))continue;g.current.push(t),t.recompute();const e=S.get(t);if(e)for(const s of e){const o=p.get(s);o&&C(o)}}}function O(i){i.listeners.forEach(n=>n({prevVal:i.prevState,currentVal:i.state}))}function F(i){i.listeners.forEach(n=>n({prevVal:i.prevState,currentVal:i.state}))}function R(i){if(v.add(i),!b)try{for(b=!0;v.size>0;){const n=Array.from(v);v.clear();for(const t of n){const e=k.get(t)??t.prevState;t.prevState=e,O(t)}for(const t of n){const e=p.get(t);e&&(g.current.push(t),C(e))}for(const t of n){const e=p.get(t);if(e)for(const s of e)F(s)}}}finally{b=!1,g.current=[],k.clear()}}function G(i){return typeof i=="function"}class m{constructor(n,t){this.listeners=new Set,this.subscribe=e=>{var s,o;this.listeners.add(e);const h=(o=(s=this.options)==null?void 0:s.onSubscribe)==null?void 0:o.call(s,e,this);return()=>{this.listeners.delete(e),h==null||h()}},this.prevState=n,this.state=n,this.options=t}setState(n){var t,e,s;this.prevState=this.state,(t=this.options)!=null&&t.updateFn?this.state=this.options.updateFn(this.prevState)(n):G(n)?this.state=n(this.prevState):this.state=n,(s=(e=this.options)==null?void 0:e.onUpdate)==null||s.call(e),R(this)}}class f{constructor(n){this.listeners=new Set,this._subscriptions=[],this.lastSeenDepValues=[],this.getDepVals=()=>{const t=[],e=[];for(const s of this.options.deps)t.push(s.prevState),e.push(s.state);return this.lastSeenDepValues=e,{prevDepVals:t,currDepVals:e,prevVal:this.prevState??void 0}},this.recompute=()=>{var t,e;this.prevState=this.state;const{prevDepVals:s,currDepVals:o,prevVal:h}=this.getDepVals();this.state=this.options.fn({prevDepVals:s,currDepVals:o,prevVal:h}),(e=(t=this.options).onUpdate)==null||e.call(t)},this.checkIfRecalculationNeededDeeply=()=>{for(const o of this.options.deps)o instanceof f&&o.checkIfRecalculationNeededDeeply();let t=!1;const e=this.lastSeenDepValues,{currDepVals:s}=this.getDepVals();for(let o=0;o<s.length;o++)if(s[o]!==e[o]){t=!0;break}t&&this.recompute()},this.mount=()=>(this.registerOnGraph(),this.checkIfRecalculationNeededDeeply(),()=>{this.unregisterFromGraph();for(const t of this._subscriptions)t()}),this.subscribe=t=>{var e,s;this.listeners.add(t);const o=(s=(e=this.options).onSubscribe)==null?void 0:s.call(e,t,this);return()=>{this.listeners.delete(t),o==null||o()}},this.options=n,this.state=n.fn({prevDepVals:void 0,prevVal:void 0,currDepVals:this.getDepVals().currDepVals})}registerOnGraph(n=this.options.deps){for(const t of n)if(t instanceof f)t.registerOnGraph(),this.registerOnGraph(t.options.deps);else if(t instanceof m){let e=p.get(t);e||(e=new Set,p.set(t,e)),e.add(this);let s=S.get(this);s||(s=new Set,S.set(this,s)),s.add(t)}}unregisterFromGraph(n=this.options.deps){for(const t of n)if(t instanceof f)this.unregisterFromGraph(t.options.deps);else if(t instanceof m){const e=p.get(t);e&&e.delete(this);const s=S.get(this);s&&s.delete(t)}}}class I{constructor(n){const{eager:t,fn:e,...s}=n;this._derived=new f({...s,fn:()=>{},onUpdate(){e()}}),t&&e()}mount(){return this._derived.mount()}}function L(i,n={}){const t=new m({actors:{}}),e=n.hashFunction||N,s=new Map;function o(h){const r=e(h),_=s.get(r);if(_)return{..._,state:_.state};const d=new f({fn:({currDepVals:[a]})=>a.actors[r],deps:[t]});function V(){async function a(){const c=t.state.actors[r];try{const u=i.getOrCreate(c.opts.name,c.opts.key,{params:c.opts.params,createInRegion:c.opts.createInRegion,createWithInput:c.opts.createWithInput}),l=u.connect();await u.resolve(),t.setState(D=>({...D,actors:{...D.actors,[r]:{...D.actors[r],isConnected:!0,isConnecting:!1,handle:u,connection:l,isError:!1,error:null}}}))}catch(u){t.setState(l=>({...l,actors:{...l.actors,[r]:{...l.actors[r],isError:!0,isConnecting:!1,error:u}}}))}}t.setState(c=>(c.actors[r].isConnecting=!0,c.actors[r].isError=!1,c.actors[r].error=null,a(),c))}const E=new I({fn:()=>{const a=t.state.actors[r];JSON.stringify(t.prevState.actors[r].opts)===JSON.stringify(t.state.actors[r].opts)&&!a.isConnected&&!a.isConnecting&&!a.isError&&a.opts.enabled&&V()},deps:[d]});t.setState(a=>a.actors[r]?a:{...a,actors:{...a.actors,[r]:{hash:r,isConnected:!1,isConnecting:!1,connection:null,handle:null,isError:!1,error:null,opts:h}}});function y(a){t.setState(c=>{const u=c.actors[r];if(!u)throw new Error(`Actor with key "${r}" does not exist.`);let l;return typeof a=="function"?l=a(u):l=a,{...c,actors:{...c.actors,[r]:l}}})}const w=()=>{const a=d.mount(),c=E.mount();return()=>{a(),c()}};return s.set(r,{state:d,key:r,mount:w,setState:y,create:V,addEventListener}),{mount:w,setState:y,state:d,create:V,key:r}}return{getOrCreateActor:o,store:t}}function N({name:i,key:n,params:t}){return JSON.stringify({name:i,key:n,params:t})}exports.createRivetKit=L;
|