The best way to control Claude Code, Codex and more from any computer or phone

main
ts 39 lines 1.2 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import * as Effect from "effect/Effect";
3
4 /**
5 * One instance per short link: keeps the click count in transactional
6 * storage and pushes every change to the dashboards watching it over
7 * hibernatable WebSockets.
8 */
9 export default class LinkRoom extends Cloudflare.DurableObject<LinkRoom>()(
10 "LinkRoom",
11 Effect.gen(function* () {
12 const state = yield* Cloudflare.DurableObjectState;
13
14 return Effect.gen(function* () {
15 let clicks = (yield* state.storage.get<number>("clicks")) ?? 0;
16
17 const broadcast = Effect.fn(function* () {
18 for (const socket of yield* state.getWebSockets()) {
19 yield* socket.send(JSON.stringify({ clicks }));
20 }
21 });
22
23 return {
24 record: Effect.fn(function* (n: number) {
25 clicks += n;
26 yield* state.storage.put("clicks", clicks);
27 yield* broadcast();
28 return clicks;
29 }),
30 clicks: () => Effect.succeed(clicks),
31 fetch: Effect.gen(function* () {
32 const [response, socket] = yield* Cloudflare.upgrade();
33 yield* socket.send(JSON.stringify({ clicks }));
34 return response;
35 }),
36 };
37 });
38 }),
39 ) {}