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

Commit 731c3db

Browse files
committed
chore(cloudflare-workers,redis): export DriverContext (#1160)
1 parent e5207db commit 731c3db

File tree

8 files changed

+43
-132
lines changed

8 files changed

+43
-132
lines changed

examples/cloudflare-workers/scripts/client.ts

Lines changed: 22 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -10,60 +10,28 @@ async function main() {
1010
console.log("🚀 Cloudflare Workers Client Demo");
1111

1212
try {
13-
// // Create counter instance
14-
// const counter = client.counter.getOrCreate("demo");
15-
// const conn = counter.connect();
16-
// conn.on("foo", (x) => console.log("output", x));
17-
//
18-
// // Increment counter
19-
// console.log("Incrementing counter 'demo'...");
20-
// const result1 = await counter.increment(1);
21-
// console.log("New count:", result1);
22-
//
23-
// // Increment again with larger value
24-
// console.log("Incrementing counter 'demo' by 5...");
25-
// const result2 = await counter.increment(5);
26-
// console.log("New count:", result2);
27-
//
28-
// // Create another counter
29-
// const counter2 = client.counter.getOrCreate("another");
30-
// console.log("Incrementing counter 'another' by 10...");
31-
// const result3 = await counter2.increment(10);
32-
// console.log("New count:", result3);
33-
//
34-
// console.log("✅ Demo completed!");
35-
36-
const ws = await client.counter.getOrCreate("demo").websocket();
37-
38-
console.log("point 1");
39-
await new Promise<void>((resolve) => {
40-
ws.addEventListener("open", () => resolve(), { once: true });
41-
});
42-
43-
console.log("point 2");
44-
// Skip welcome message
45-
await new Promise<void>((resolve) => {
46-
ws.addEventListener("message", () => resolve(), { once: true });
47-
});
48-
console.log("point 3");
49-
50-
// Send and receive echo
51-
const testMessage = { test: "data", timestamp: Date.now() };
52-
ws.send(JSON.stringify(testMessage));
53-
console.log("point 4");
54-
55-
const echoMessage = await new Promise<any>((resolve) => {
56-
ws.addEventListener(
57-
"message",
58-
(event: any) => {
59-
resolve(JSON.parse(event.data as string));
60-
},
61-
{ once: true },
62-
);
63-
});
64-
console.log("point 3");
65-
66-
ws.close();
13+
// Create counter instance
14+
const counter = client.counter.getOrCreate("demo");
15+
const conn = counter.connect();
16+
conn.on("foo", (x) => console.log("output", x));
17+
18+
// Increment counter
19+
console.log("Incrementing counter 'demo'...");
20+
const result1 = await counter.increment(1);
21+
console.log("New count:", result1);
22+
23+
// Increment again with larger value
24+
console.log("Incrementing counter 'demo' by 5...");
25+
const result2 = await counter.increment(5);
26+
console.log("New count:", result2);
27+
28+
// Create another counter
29+
const counter2 = client.counter.getOrCreate("another");
30+
console.log("Incrementing counter 'another' by 10...");
31+
const result3 = await counter2.increment(10);
32+
console.log("New count:", result3);
33+
34+
console.log("✅ Demo completed!");
6735
} catch (error) {
6836
console.error("❌ Error:", error);
6937
process.exit(1);

examples/cloudflare-workers/src/registry.ts

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { actor, setup } from "@rivetkit/actor";
1+
import { ActorContext, actor, setup } from "@rivetkit/actor";
22

33
export const counter = actor({
44
onAuth: () => {
@@ -12,69 +12,6 @@ export const counter = actor({
1212
return c.state.count;
1313
},
1414
},
15-
onWebSocket: (ctx, websocket) => {
16-
// ctx.state.connectionCount = ctx.state.connectionCount + 1;
17-
18-
// Send welcome message
19-
websocket.send(
20-
JSON.stringify({
21-
type: "welcome",
22-
connectionCount: ctx.state.connectionCount,
23-
}),
24-
);
25-
26-
// Echo messages back
27-
websocket.addEventListener("message", (event: any) => {
28-
//ctx.state.messageCount++;
29-
30-
const data = event.data;
31-
if (typeof data === "string") {
32-
try {
33-
const parsed = JSON.parse(data);
34-
if (parsed.type === "ping") {
35-
websocket.send(
36-
JSON.stringify({
37-
type: "pong",
38-
timestamp: Date.now(),
39-
}),
40-
);
41-
} else if (parsed.type === "getStats") {
42-
websocket.send(
43-
JSON.stringify({
44-
type: "stats",
45-
connectionCount: ctx.state.connectionCount,
46-
messageCount: ctx.state.messageCount,
47-
}),
48-
);
49-
} else if (parsed.type === "getAuthData") {
50-
// Auth data is not directly available in raw WebSocket handler
51-
// Send a message indicating this limitation
52-
websocket.send(
53-
JSON.stringify({
54-
type: "authData",
55-
authData: null,
56-
message: "Auth data not available in raw WebSocket handler",
57-
}),
58-
);
59-
} else {
60-
// Echo back
61-
websocket.send(data);
62-
}
63-
} catch {
64-
// If not JSON, just echo it back
65-
websocket.send(data);
66-
}
67-
} else {
68-
// Echo binary data
69-
websocket.send(data);
70-
}
71-
});
72-
73-
// Handle close
74-
websocket.addEventListener("close", () => {
75-
// ctx.state.connectionCount = ctx.state.connectionCount - 1;
76-
});
77-
},
7815
});
7916

8017
export const registry = setup({

packages/core/src/actor/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ type CreateVars<S, CP, CS, V, I, AD, DB> =
161161
/**
162162
* @experimental
163163
*/
164-
createVars: (
164+
createVars: <DC = unknown>(
165165
c: ActorContext<
166166
undefined,
167167
undefined,
@@ -171,7 +171,7 @@ type CreateVars<S, CP, CS, V, I, AD, DB> =
171171
undefined,
172172
undefined
173173
>,
174-
driverCtx: unknown,
174+
driverCtx: DC,
175175
) => V | Promise<V>;
176176
}
177177
| Record<never, never>;

packages/drivers/cloudflare-workers/src/actor-driver.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ export class CloudflareDurableObjectGlobalState {
4242
}
4343
}
4444

45-
export interface ActorDriverContext {
46-
ctx: DurableObjectState;
47-
env: unknown;
45+
export interface DriverContext {
46+
state: DurableObjectState;
4847
}
4948

5049
// Actor handler to track running instances
@@ -143,9 +142,9 @@ export class CloudflareActorsActorDriver implements ActorDriver {
143142
return handler.genericConnGlobalState;
144143
}
145144

146-
getContext(actorId: string): ActorDriverContext {
145+
getContext(actorId: string): DriverContext {
147146
const state = this.#globalState.getDOState(actorId);
148-
return { ctx: state.ctx, env: state.env };
147+
return { state: state.ctx };
149148
}
150149

151150
async readPersistedData(actorId: string): Promise<Uint8Array | undefined> {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
export type { DriverContext } from "./actor-driver";
12
export type { InputConfig as Config } from "./config";
23
export { type Bindings, createServer, createServerHandler } from "./handler";

packages/drivers/redis/src/actor.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ import { logger } from "./log";
2020
// Define AnyClient locally since it's not exported
2121
type AnyClient = any;
2222

23-
export interface ActorDriverContext {
23+
export interface DriverContext {
2424
redis: Redis;
25+
keyPrefix: string;
2526
}
2627

2728
/**
@@ -54,8 +55,8 @@ export class RedisActorDriver implements ActorDriver {
5455
return peer.genericConnGlobalState;
5556
}
5657

57-
getContext(_actorId: string): ActorDriverContext {
58-
return { redis: this.#redis };
58+
getContext(_actorId: string): DriverContext {
59+
return { redis: this.#redis, keyPrefix: this.#driverConfig.keyPrefix };
5960
}
6061

6162
async readPersistedData(actorId: string): Promise<Uint8Array | undefined> {

packages/drivers/redis/src/mod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { Node } from "./coordinate/node/mod";
1818
import type { GlobalState } from "./coordinate/types";
1919
import { RedisManagerDriver } from "./manager";
2020

21-
export { RedisActorDriver } from "./actor";
21+
export { type DriverContext, RedisActorDriver } from "./actor";
2222
export { RedisManagerDriver } from "./manager";
2323

2424
export function createRedisDriver(

packages/frameworks/react/src/mod.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import {
55
type CreateRivetKitOptions,
66
createRivetKit as createVanillaRivetKit,
77
} from "@rivetkit/framework-base";
8-
import { useEffect, useRef } from "react";
98
import { useStore } from "@tanstack/react-store";
9+
import { useEffect, useRef } from "react";
1010

1111
export { createClient } from "@rivetkit/core/client";
1212

@@ -77,8 +77,13 @@ export function createRivetKit<Registry extends AnyActorRegistry>(
7777
ref.current(...args);
7878
}
7979
return actorState.connection.on(eventName, eventHandler);
80-
}, [actorState.connection, actorState.isConnected, actorState.hash, eventName]);
81-
};
80+
}, [
81+
actorState.connection,
82+
actorState.isConnected,
83+
actorState.hash,
84+
eventName,
85+
]);
86+
}
8287

8388
return {
8489
...actorState,

0 commit comments

Comments
 (0)