@base44-preview/sdk 0.8.46-pr.272.f64ff62 → 0.8.46-pr.273.f546cdd
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/modules/actors.types.d.ts +148 -20
- package/package.json +1 -1
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
*
|
|
8
8
|
* @example
|
|
9
9
|
* ```typescript
|
|
10
|
+
* // Declare message types for a deployed actor
|
|
10
11
|
* declare module "@base44/sdk" {
|
|
11
12
|
* interface ActorRegistry {
|
|
12
13
|
* ChatRoom: {
|
|
@@ -35,15 +36,26 @@ type ToServerFor<N extends string> = N extends keyof ActorRegistry ? ActorRegist
|
|
|
35
36
|
/** Options for {@link ActorRef.connect}. */
|
|
36
37
|
export interface ActorConnectOptions {
|
|
37
38
|
/**
|
|
38
|
-
* The connection id, used as the actor's `conn.id`. Supply a stable value
|
|
39
|
-
*
|
|
40
|
-
* identity
|
|
39
|
+
* The connection id, used as the actor's `conn.id`. Supply a stable value,
|
|
40
|
+
* such as one persisted per tab, so a reconnect reuses the same server-side
|
|
41
|
+
* identity. Omit it for an auto-generated per-connection id.
|
|
41
42
|
*/
|
|
42
43
|
id?: string;
|
|
43
44
|
}
|
|
44
45
|
/** Handle for one listener registered via {@link Connection.subscribe}. */
|
|
45
46
|
export interface ActorSubscription {
|
|
46
|
-
/**
|
|
47
|
+
/**
|
|
48
|
+
* Removes this listener. Other listeners on the same connection, and the
|
|
49
|
+
* connection itself, stay live. To close the connection as well, call
|
|
50
|
+
* {@link Connection.close}.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* // Stop listening without closing the connection
|
|
55
|
+
* const sub = conn.subscribe((msg) => console.log(msg));
|
|
56
|
+
* sub.unsubscribe();
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
47
59
|
unsubscribe(): void;
|
|
48
60
|
}
|
|
49
61
|
/**
|
|
@@ -52,17 +64,62 @@ export interface ActorSubscription {
|
|
|
52
64
|
* socket has been opened, so there's no pre-connect state to guard against.
|
|
53
65
|
*/
|
|
54
66
|
export interface Connection<N extends string = string> {
|
|
55
|
-
/** The connection id
|
|
67
|
+
/** The connection id, which is the value the actor sees as `conn.id`. */
|
|
56
68
|
readonly id: string;
|
|
57
|
-
/**
|
|
69
|
+
/**
|
|
70
|
+
* Registers a listener for messages sent by the actor. Any number of
|
|
71
|
+
* listeners can be registered on one connection, and each receives every
|
|
72
|
+
* message.
|
|
73
|
+
*
|
|
74
|
+
* The callback payload is typed when the actor is declared in
|
|
75
|
+
* {@link ActorRegistry}, and is `unknown` otherwise.
|
|
76
|
+
*
|
|
77
|
+
* @param callback - Called with each message the actor sends to this client.
|
|
78
|
+
* @returns An {@link ActorSubscription} that removes this one listener.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```typescript
|
|
82
|
+
* // Listen for messages from the actor
|
|
83
|
+
* const sub = conn.subscribe((msg) => {
|
|
84
|
+
* if (msg.type === "message") console.log(msg.text);
|
|
85
|
+
* });
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
58
88
|
subscribe(callback: (data: ToClientFor<N>) => void): ActorSubscription;
|
|
59
|
-
/**
|
|
60
|
-
*
|
|
89
|
+
/**
|
|
90
|
+
* Sends a message to the actor.
|
|
91
|
+
*
|
|
92
|
+
* Messages sent before the socket finishes opening are buffered and flushed
|
|
93
|
+
* on open. Messages sent after {@link close} are dropped silently.
|
|
94
|
+
*
|
|
95
|
+
* The payload is typed when the actor is declared in {@link ActorRegistry},
|
|
96
|
+
* and is `unknown` otherwise.
|
|
97
|
+
*
|
|
98
|
+
* @param data - The message to send to the actor.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```typescript
|
|
102
|
+
* // Send a message to the actor
|
|
103
|
+
* conn.send({ type: "message", text: "hi" });
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
61
106
|
send(data: ToServerFor<N>): void;
|
|
62
107
|
/**
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
108
|
+
* Closes the connection, tearing down the socket, the heartbeat, and every
|
|
109
|
+
* listener registered on it. Safe to call more than once.
|
|
110
|
+
*
|
|
111
|
+
* A connection also closes itself when it fails permanently. See
|
|
112
|
+
* {@link ActorRef.connect} for how to recover from that.
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* ```typescript
|
|
116
|
+
* // Close when the view that opened the connection goes away.
|
|
117
|
+
* useEffect(() => {
|
|
118
|
+
* const conn = base44.actors.ChatRoom(roomId).connect();
|
|
119
|
+
* conn.subscribe(setMessage);
|
|
120
|
+
* return () => conn.close();
|
|
121
|
+
* }, [roomId]);
|
|
122
|
+
* ```
|
|
66
123
|
*/
|
|
67
124
|
close(): void;
|
|
68
125
|
}
|
|
@@ -72,13 +129,38 @@ export interface Connection<N extends string = string> {
|
|
|
72
129
|
*/
|
|
73
130
|
export interface ActorRef<N extends string = string> {
|
|
74
131
|
/**
|
|
75
|
-
*
|
|
76
|
-
* connection is open
|
|
132
|
+
* Opens the WebSocket to this actor instance and returns the
|
|
133
|
+
* {@link Connection}. Idempotent while the connection is open, so calling it
|
|
134
|
+
* again returns the same connection rather than opening a second socket.
|
|
135
|
+
*
|
|
136
|
+
* The returned connection is usable straight away. Messages passed to
|
|
137
|
+
* {@link Connection.send} before the socket finishes opening are buffered and
|
|
138
|
+
* flushed on open.
|
|
139
|
+
*
|
|
140
|
+
* A connection that fails permanently, for example because the actor doesn't
|
|
141
|
+
* exist or the caller isn't allowed to connect, closes itself and reports the
|
|
142
|
+
* error to the client's `onError` handler. Call `connect()` again once the
|
|
143
|
+
* cause is fixed to get a fresh {@link Connection}, then re-subscribe, as
|
|
144
|
+
* listeners do not carry over.
|
|
145
|
+
*
|
|
146
|
+
* @param options - Connection options. See {@link ActorConnectOptions}.
|
|
147
|
+
* @returns A live {@link Connection} to this actor instance.
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* ```typescript
|
|
151
|
+
* // Connect to an actor instance
|
|
152
|
+
* const conn = base44.actors.ChatRoom("room-1").connect();
|
|
153
|
+
* ```
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```typescript
|
|
157
|
+
* // Reuse a stable connection id so a reconnect keeps the same
|
|
158
|
+
* // server-side identity.
|
|
159
|
+
* let id = sessionStorage.getItem("chat-conn-id") ?? crypto.randomUUID();
|
|
160
|
+
* sessionStorage.setItem("chat-conn-id", id);
|
|
77
161
|
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
* error to the client's `onError` handler. Call `connect()` again after
|
|
81
|
-
* fixing the cause to get a fresh {@link Connection}, and re-subscribe.
|
|
162
|
+
* const conn = base44.actors.ChatRoom("room-1").connect({ id });
|
|
163
|
+
* ```
|
|
82
164
|
*/
|
|
83
165
|
connect(options?: ActorConnectOptions): Connection<N>;
|
|
84
166
|
}
|
|
@@ -91,16 +173,62 @@ export interface ActorClient<N extends string = string> {
|
|
|
91
173
|
(instanceId: string): ActorRef<N>;
|
|
92
174
|
}
|
|
93
175
|
/**
|
|
94
|
-
*
|
|
176
|
+
* Actors module for real-time messaging with Cloudflare Durable Object-backed
|
|
95
177
|
* Actors deployed by the Base44 platform.
|
|
96
178
|
*
|
|
179
|
+
* An Actor is a named server-side object with persistent state. Each instance
|
|
180
|
+
* is addressed by an id, so `base44.actors.ChatRoom("room-1")` and
|
|
181
|
+
* `base44.actors.ChatRoom("room-2")` are separate instances with separate
|
|
182
|
+
* state. Clients open a WebSocket to an instance and exchange messages with it.
|
|
183
|
+
*
|
|
184
|
+
* This module provides:
|
|
185
|
+
* - Per-instance WebSocket connections, opened with {@link ActorRef.connect}
|
|
186
|
+
* - Message listeners, registered with {@link Connection.subscribe}
|
|
187
|
+
* - Message sending, with {@link Connection.send}
|
|
188
|
+
* - Automatic reconnection with backoff, including recovery from half-open
|
|
189
|
+
* sockets that stop delivering messages without emitting a close event
|
|
190
|
+
* - End-to-end typing of message payloads through {@link ActorRegistry}
|
|
191
|
+
*
|
|
192
|
+
* This module is available to use with a client in anonymous and user
|
|
193
|
+
* authentication modes. Connecting anonymously requires a browser environment.
|
|
194
|
+
*
|
|
195
|
+
* Connections stay open until you call {@link Connection.close}, so close them
|
|
196
|
+
* when the view that opened them goes away.
|
|
197
|
+
*
|
|
198
|
+
* @example
|
|
97
199
|
* ```typescript
|
|
98
|
-
*
|
|
99
|
-
* const
|
|
200
|
+
* // Open a connection to one instance of the ChatRoom actor.
|
|
201
|
+
* const conn = base44.actors.ChatRoom("room-1").connect();
|
|
202
|
+
*
|
|
203
|
+
* const sub = conn.subscribe((msg) => {
|
|
204
|
+
* console.log(msg);
|
|
205
|
+
* });
|
|
206
|
+
*
|
|
100
207
|
* conn.send({ type: "message", text: "hi" });
|
|
208
|
+
*
|
|
209
|
+
* // Later, when the view goes away.
|
|
101
210
|
* sub.unsubscribe();
|
|
102
211
|
* conn.close();
|
|
103
212
|
* ```
|
|
213
|
+
*
|
|
214
|
+
* @example
|
|
215
|
+
* ```typescript
|
|
216
|
+
* // Register the actor to type both directions of the conversation.
|
|
217
|
+
* declare module "@base44/sdk" {
|
|
218
|
+
* interface ActorRegistry {
|
|
219
|
+
* ChatRoom: {
|
|
220
|
+
* toClient: { type: "joined" | "message"; from?: string; text?: string };
|
|
221
|
+
* toServer: { type: "message"; text: string };
|
|
222
|
+
* };
|
|
223
|
+
* }
|
|
224
|
+
* }
|
|
225
|
+
*
|
|
226
|
+
* const conn = base44.actors.ChatRoom("room-1").connect();
|
|
227
|
+
* conn.subscribe((msg) => {
|
|
228
|
+
* // msg is typed as the toClient union.
|
|
229
|
+
* if (msg.type === "message") console.log(msg.text);
|
|
230
|
+
* });
|
|
231
|
+
* ```
|
|
104
232
|
*/
|
|
105
233
|
export type ActorsModule = {
|
|
106
234
|
[K in AllActorNames]: K extends keyof ActorRegistry ? ActorClient<string & K> : ActorClient;
|