Skip to content
This repository was archived by the owner on Oct 22, 2025. It is now read-only.

Commit 06c5bb2

Browse files
committed
refactor(react): 0.9
1 parent fd29304 commit 06c5bb2

33 files changed

+2086
-302
lines changed

examples/chat-room/actors/app.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { actor, setup } from "rivetkit";
1+
import { worker, setup } from "rivetkit";
2+
import { serve } from "@rivetkit/nodejs";
23

34
// state managed by the actor
45
export interface State {
56
messages: { username: string; message: string }[];
67
}
78

8-
export const chatRoom = actor({
9+
export const chatRoom = worker({
910
// initialize state
1011
state: { messages: [] } as State,
1112

@@ -27,9 +28,18 @@ export const chatRoom = actor({
2728
});
2829

2930
// Create and export the app
30-
export const app = setup({
31-
actors: { chatRoom },
31+
export const registry = setup({
32+
workers: { chatRoom },
33+
cors: {
34+
origin: "*", // Allow all origins
35+
allowMethods: ["GET", "POST", "OPTIONS"], // Allow specific methods
36+
allowHeaders: ["Content-Type", "Authorization", "User-Agent"], // Allow specific headers
37+
},
38+
});
39+
40+
serve(registry, {
41+
hostname: "localhost",
3242
});
3343

3444
// Export type for client type checking
35-
export type App = typeof app;
45+
export type Registry = typeof registry;

examples/chat-room/scripts/cli.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ async function main() {
1010

1111
// connect to chat room - now accessed via property
1212
// can still pass parameters like room
13-
const chatRoom = client.chatRoom.getOrCreate(room, {
14-
params: { room },
15-
}).connect();
13+
const chatRoom = client.chatRoom
14+
.getOrCreate(room, {
15+
params: { room },
16+
})
17+
.connect();
1618

1719
// fetch history
1820
const history = await chatRoom.getHistory();

examples/chat-room/tests/chat-room.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { test, expect } from "vitest";
22
import { setupTest } from "rivetkit/test";
3-
import { app } from "../actors/app";
3+
import { registry } from "../actors/app";
44

55
test("chat room should handle messages", async (test) => {
6-
const { client } = await setupTest(test, app);
6+
const { client } = await setupTest(test, registry);
77

88
// Connect to chat room
99
const chatRoom = client.chatRoom.getOrCreate().connect();

packages/core/src/client/client.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ export interface ClientOptions {
9696
export interface QueryOptions {
9797
/** Parameters to pass to the connection. */
9898
params?: unknown;
99+
/** Signal to abort the request. */
100+
signal?: AbortSignal;
99101
}
100102

101103
/**
@@ -171,17 +173,20 @@ export interface ClientDriver {
171173
req: HonoRequest | undefined,
172174
workerQuery: WorkerQuery,
173175
encodingKind: Encoding,
176+
opts: { signal?: AbortSignal } | undefined,
174177
): Promise<string>;
175178
connectWebSocket(
176179
req: HonoRequest | undefined,
177180
workerQuery: WorkerQuery,
178181
encodingKind: Encoding,
182+
opts: { signal?: AbortSignal } | undefined,
179183
): Promise<WebSocket>;
180184
connectSse(
181185
req: HonoRequest | undefined,
182186
workerQuery: WorkerQuery,
183187
encodingKind: Encoding,
184188
params: unknown,
189+
opts: { signal?: AbortSignal } | undefined,
185190
): Promise<EventSource>;
186191
sendHttpMessage(
187192
req: HonoRequest | undefined,
@@ -190,6 +195,7 @@ export interface ClientDriver {
190195
connectionId: string,
191196
connectionToken: string,
192197
message: wsToServer.ToServer,
198+
opts: { signal?: AbortSignal } | undefined,
193199
): Promise<Response>;
194200
}
195201

@@ -362,6 +368,7 @@ export class ClientRaw {
362368
undefined,
363369
createQuery,
364370
this.#encodingKind,
371+
opts?.signal ? { signal: opts.signal } : undefined,
365372
);
366373
logger().debug("created worker with ID", {
367374
name,

packages/core/src/client/http-client-driver.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,22 @@ export function createHttpClientDriver(managerEndpoint: string): ClientDriver {
5353
name: string,
5454
...args: Args
5555
): Promise<Response> => {
56+
let maybeActionOpts = args.at(-1);
57+
if (maybeActionOpts && typeof maybeActionOpts === "object") {
58+
// If the last argument is an options object, we remove it from the args
59+
// and pass it as the params to the request.
60+
if (
61+
"signal" in maybeActionOpts &&
62+
maybeActionOpts.signal instanceof AbortSignal
63+
) {
64+
// If the options object has a signal, we remove it from the args
65+
// and pass it as the signal to the request.
66+
args = args.slice(0, -1);
67+
} else {
68+
maybeActionOpts = undefined;
69+
}
70+
}
71+
5672
logger().debug("worker handle action", {
5773
name,
5874
args,
@@ -72,6 +88,7 @@ export function createHttpClientDriver(managerEndpoint: string): ClientDriver {
7288
},
7389
body: { a: args } satisfies ActionRequest,
7490
encoding: encoding,
91+
signal: maybeActionOpts?.signal,
7592
},
7693
);
7794

packages/core/src/client/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export interface HttpRequestOpts<Body> {
3131
body?: Body;
3232
encoding: Encoding;
3333
skipParseResponse?: boolean;
34+
signal?: AbortSignal;
3435
customFetch?: (req: Request) => Promise<Response>;
3536
}
3637

@@ -74,7 +75,8 @@ export async function sendHttpRequest<
7475
: {}),
7576
"User-Agent": httpUserAgent(),
7677
},
77-
body: bodyData
78+
body: bodyData,
79+
signal: opts.signal,
7880
}),
7981
);
8082
} catch (error) {

packages/core/src/client/worker-conn.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ import {
1919
} from "./client";
2020
import * as errors from "./errors";
2121
import { logger } from "./log";
22-
import { type WebSocketMessage as ConnMessage, messageLength, serializeWithEncoding } from "./utils";
22+
import {
23+
type WebSocketMessage as ConnMessage,
24+
messageLength,
25+
serializeWithEncoding,
26+
} from "./utils";
2327
import {
2428
HEADER_WORKER_ID,
2529
HEADER_WORKER_QUERY,
@@ -58,6 +62,7 @@ export type WorkerErrorCallback = (error: errors.WorkerError) => void;
5862

5963
export interface SendHttpMessageOpts {
6064
ephemeral: boolean;
65+
signal?: AbortSignal;
6166
}
6267

6368
export type ConnTransport = { websocket: WebSocket } | { sse: EventSource };
@@ -249,11 +254,12 @@ enc
249254
}
250255
}
251256

252-
async #connectWebSocket() {
257+
async #connectWebSocket({ signal }: { signal?: AbortSignal } = {}) {
253258
const ws = await this.#driver.connectWebSocket(
254259
undefined,
255260
this.#workerQuery,
256261
this.#encodingKind,
262+
signal ? { signal } : undefined,
257263
);
258264
this.#transport = { websocket: ws };
259265
ws.onopen = () => {
@@ -280,12 +286,13 @@ enc
280286
};
281287
}
282288

283-
async #connectSse() {
289+
async #connectSse({ signal }: { signal?: AbortSignal } = {}) {
284290
const eventSource = await this.#driver.connectSse(
285291
undefined,
286292
this.#workerQuery,
287293
this.#encodingKind,
288294
this.#params,
295+
signal ? { signal } : undefined,
289296
);
290297
this.#transport = { sse: eventSource };
291298
eventSource.onopen = () => {
@@ -657,6 +664,7 @@ enc
657664
this.#connectionId,
658665
this.#connectionToken,
659666
message,
667+
opts?.signal ? { signal: opts.signal } : undefined,
660668
);
661669

662670
if (!res.ok) {

packages/core/src/client/worker-handle.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export class WorkerHandleRaw {
9999
*
100100
* @returns {Promise<string>} - A promise that resolves to the worker's ID
101101
*/
102-
async resolve(): Promise<string> {
102+
async resolve({ signal }: { signal?: AbortSignal } = {}): Promise<string> {
103103
if (
104104
"getForKey" in this.#workerQuery ||
105105
"getOrCreateForKey" in this.#workerQuery
@@ -109,6 +109,11 @@ export class WorkerHandleRaw {
109109
undefined,
110110
this.#workerQuery,
111111
this.#encodingKind,
112+
signal
113+
? {
114+
signal,
115+
}
116+
: undefined,
112117
);
113118
this.#workerQuery = { getForId: { workerId } };
114119
return workerId;

packages/core/src/common/utils.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,12 @@ export function deconstructError(
166166

167167
export function stringifyError(error: unknown): string {
168168
if (error instanceof Error) {
169-
if (process.env._WORKER_CORE_ERROR_STACK === "1") {
170-
return `${error.name}: ${error.message}${error.stack ? `\n${error.stack}` : ""}`;
171-
} else {
172-
return `${error.name}: ${error.message}`;
173-
}
169+
// if (process.env._WORKER_CORE_ERROR_STACK === "1") {
170+
// return `${error.name}: ${error.message}${error.stack ? `\n${error.stack}` : ""}`;
171+
// } else {
172+
// return `${error.name}: ${error.message}`;
173+
// }
174+
return `${error.name}: ${error.message}`;
174175
} else if (typeof error === "string") {
175176
return error;
176177
} else if (typeof error === "object" && error !== null) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>◢ RivetKit ⋅ Framework Base </title>
8+
</head>
9+
<body>
10+
<div id="root"></div>
11+
<script type="module" src="/src/main.ts"></script>
12+
</body>
13+
</html>

0 commit comments

Comments
 (0)