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

main
ts 40 lines 1.16 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import * as Effect from "effect/Effect";
3
4 // #region show
5 export default class LinkRoom extends Cloudflare.DurableObject<LinkRoom>()(
6 "LinkRoom",
7 Effect.gen(function* () {
8 const state = yield* Cloudflare.DurableObjectState;
9
10 return Effect.gen(function* () {
11 // #region body
12 // #region count
13 let clicks = (yield* state.storage.get<number>("clicks")) ?? 0;
14 // #endregion count
15
16 return {
17 // #region record
18 record: Effect.fn(function* () {
19 clicks += 1;
20 yield* state.storage.put("clicks", clicks);
21 // #region push
22 for (const socket of yield* state.getWebSockets()) {
23 yield* socket.send(JSON.stringify({ clicks }));
24 }
25 // #endregion push
26 }),
27 // #endregion record
28 // #region socket
29 fetch: Effect.gen(function* () {
30 const [response, socket] = yield* Cloudflare.upgrade();
31 yield* socket.send(JSON.stringify({ clicks }));
32 return response;
33 }),
34 // #endregion socket
35 };
36 // #endregion body
37 });
38 }),
39 ) {}
40 // #endregion show