|
| 1 | +import { actor, type UniversalWebSocket } from "@rivetkit/core"; |
| 2 | + |
| 3 | +export const SLEEP_TIMEOUT = 500; |
| 4 | + |
| 5 | +export const sleep = actor({ |
| 6 | + onAuth: () => {}, |
| 7 | + state: { startCount: 0, sleepCount: 0 }, |
| 8 | + onStart: (c) => { |
| 9 | + c.state.startCount += 1; |
| 10 | + }, |
| 11 | + onStop: (c) => { |
| 12 | + c.state.sleepCount += 1; |
| 13 | + }, |
| 14 | + actions: { |
| 15 | + triggerSleep: (c) => { |
| 16 | + c.sleep(); |
| 17 | + }, |
| 18 | + getCounts: (c) => { |
| 19 | + return { startCount: c.state.startCount, sleepCount: c.state.sleepCount }; |
| 20 | + }, |
| 21 | + setAlarm: async (c, duration: number) => { |
| 22 | + await c.schedule.after(duration, "onAlarm"); |
| 23 | + }, |
| 24 | + onAlarm: (c) => { |
| 25 | + c.log.info("alarm called"); |
| 26 | + }, |
| 27 | + }, |
| 28 | + options: { |
| 29 | + sleepTimeout: SLEEP_TIMEOUT, |
| 30 | + }, |
| 31 | +}); |
| 32 | + |
| 33 | +export const sleepWithLongRpc = actor({ |
| 34 | + onAuth: () => {}, |
| 35 | + state: { startCount: 0, sleepCount: 0 }, |
| 36 | + createVars: () => ({}) as { longRunningResolve: PromiseWithResolvers<void> }, |
| 37 | + onStart: (c) => { |
| 38 | + c.state.startCount += 1; |
| 39 | + }, |
| 40 | + onStop: (c) => { |
| 41 | + c.state.sleepCount += 1; |
| 42 | + }, |
| 43 | + actions: { |
| 44 | + getCounts: (c) => { |
| 45 | + return { startCount: c.state.startCount, sleepCount: c.state.sleepCount }; |
| 46 | + }, |
| 47 | + longRunningRpc: async (c) => { |
| 48 | + c.log.info("starting long running rpc"); |
| 49 | + c.vars.longRunningResolve = Promise.withResolvers(); |
| 50 | + c.broadcast("waiting"); |
| 51 | + await c.vars.longRunningResolve.promise; |
| 52 | + c.log.info("finished long running rpc"); |
| 53 | + }, |
| 54 | + finishLongRunningRpc: (c) => c.vars.longRunningResolve?.resolve(), |
| 55 | + }, |
| 56 | + options: { |
| 57 | + sleepTimeout: SLEEP_TIMEOUT, |
| 58 | + }, |
| 59 | +}); |
| 60 | + |
| 61 | +export const sleepWithRawHttp = actor({ |
| 62 | + onAuth: () => {}, |
| 63 | + state: { startCount: 0, sleepCount: 0, requestCount: 0 }, |
| 64 | + onStart: (c) => { |
| 65 | + c.state.startCount += 1; |
| 66 | + }, |
| 67 | + onStop: (c) => { |
| 68 | + c.state.sleepCount += 1; |
| 69 | + }, |
| 70 | + onFetch: async (c, request) => { |
| 71 | + c.state.requestCount += 1; |
| 72 | + const url = new URL(request.url); |
| 73 | + |
| 74 | + if (url.pathname === "/long-request") { |
| 75 | + const duration = parseInt(url.searchParams.get("duration") || "1000"); |
| 76 | + c.log.info("starting long fetch request", { duration }); |
| 77 | + await new Promise((resolve) => setTimeout(resolve, duration)); |
| 78 | + c.log.info("finished long fetch request"); |
| 79 | + return new Response(JSON.stringify({ completed: true }), { |
| 80 | + headers: { "Content-Type": "application/json" }, |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + return new Response("Not Found", { status: 404 }); |
| 85 | + }, |
| 86 | + actions: { |
| 87 | + getCounts: (c) => { |
| 88 | + return { |
| 89 | + startCount: c.state.startCount, |
| 90 | + sleepCount: c.state.sleepCount, |
| 91 | + requestCount: c.state.requestCount, |
| 92 | + }; |
| 93 | + }, |
| 94 | + }, |
| 95 | + options: { |
| 96 | + sleepTimeout: SLEEP_TIMEOUT, |
| 97 | + }, |
| 98 | +}); |
| 99 | + |
| 100 | +export const sleepWithRawWebSocket = actor({ |
| 101 | + onAuth: () => {}, |
| 102 | + state: { startCount: 0, sleepCount: 0, connectionCount: 0 }, |
| 103 | + onStart: (c) => { |
| 104 | + c.state.startCount += 1; |
| 105 | + }, |
| 106 | + onStop: (c) => { |
| 107 | + c.state.sleepCount += 1; |
| 108 | + }, |
| 109 | + onWebSocket: (c, websocket: UniversalWebSocket, opts) => { |
| 110 | + c.state.connectionCount += 1; |
| 111 | + c.log.info("websocket connected", { |
| 112 | + connectionCount: c.state.connectionCount, |
| 113 | + }); |
| 114 | + |
| 115 | + websocket.send( |
| 116 | + JSON.stringify({ |
| 117 | + type: "connected", |
| 118 | + connectionCount: c.state.connectionCount, |
| 119 | + }), |
| 120 | + ); |
| 121 | + |
| 122 | + websocket.addEventListener("message", (event: any) => { |
| 123 | + const data = event.data; |
| 124 | + if (typeof data === "string") { |
| 125 | + try { |
| 126 | + const parsed = JSON.parse(data); |
| 127 | + if (parsed.type === "getCounts") { |
| 128 | + websocket.send( |
| 129 | + JSON.stringify({ |
| 130 | + type: "counts", |
| 131 | + startCount: c.state.startCount, |
| 132 | + sleepCount: c.state.sleepCount, |
| 133 | + connectionCount: c.state.connectionCount, |
| 134 | + }), |
| 135 | + ); |
| 136 | + } else if (parsed.type === "keepAlive") { |
| 137 | + // Just acknowledge to keep connection alive |
| 138 | + websocket.send(JSON.stringify({ type: "ack" })); |
| 139 | + } |
| 140 | + } catch { |
| 141 | + // Echo non-JSON messages |
| 142 | + websocket.send(data); |
| 143 | + } |
| 144 | + } |
| 145 | + }); |
| 146 | + |
| 147 | + websocket.addEventListener("close", () => { |
| 148 | + c.state.connectionCount -= 1; |
| 149 | + c.log.info("websocket disconnected", { |
| 150 | + connectionCount: c.state.connectionCount, |
| 151 | + }); |
| 152 | + }); |
| 153 | + }, |
| 154 | + actions: { |
| 155 | + getCounts: (c) => { |
| 156 | + return { |
| 157 | + startCount: c.state.startCount, |
| 158 | + sleepCount: c.state.sleepCount, |
| 159 | + connectionCount: c.state.connectionCount, |
| 160 | + }; |
| 161 | + }, |
| 162 | + }, |
| 163 | + options: { |
| 164 | + sleepTimeout: SLEEP_TIMEOUT, |
| 165 | + }, |
| 166 | +}); |
| 167 | + |
| 168 | +export const sleepWithNoSleepOption = actor({ |
| 169 | + onAuth: () => {}, |
| 170 | + state: { startCount: 0, sleepCount: 0 }, |
| 171 | + onStart: (c) => { |
| 172 | + c.state.startCount += 1; |
| 173 | + }, |
| 174 | + onStop: (c) => { |
| 175 | + c.state.sleepCount += 1; |
| 176 | + }, |
| 177 | + actions: { |
| 178 | + getCounts: (c) => { |
| 179 | + return { startCount: c.state.startCount, sleepCount: c.state.sleepCount }; |
| 180 | + }, |
| 181 | + }, |
| 182 | + options: { |
| 183 | + sleepTimeout: SLEEP_TIMEOUT, |
| 184 | + noSleep: true, |
| 185 | + }, |
| 186 | +}); |
0 commit comments