(clientId, message, opts)
| 168 | }) |
| 169 | |
| 170 | const write: RpcServer<Rpcs>["write"] = (clientId, message, opts) => |
| 171 | Effect.catchDefect( |
| 172 | Effect.withFiber((requestFiber) => { |
| 173 | if (isShutdown) return Effect.interrupt |
| 174 | let client = clients.get(clientId) |
| 175 | if (!client) { |
| 176 | client = { |
| 177 | id: clientId, |
| 178 | latches: new Map(), |
| 179 | fibers: new Map(), |
| 180 | ended: false, |
| 181 | serverClient: new Rpc.ServerClient(clientId) |
| 182 | } |
| 183 | clients.set(clientId, client) |
| 184 | } else if (client.ended) { |
| 185 | return Effect.interrupt |
| 186 | } |
| 187 | |
| 188 | switch (message._tag) { |
| 189 | case "Request": { |
| 190 | return handleRequest(requestFiber, client, message, opts) |
| 191 | } |
| 192 | case "Ack": { |
| 193 | const latch = client.latches.get(message.requestId) |
| 194 | return latch ? latch.open : Effect.void |
| 195 | } |
| 196 | case "Interrupt": { |
| 197 | const fiber = client.fibers.get(message.requestId) |
| 198 | if (fiber) { |
| 199 | fiber.interruptUnsafe(requestFiber.id, RpcSchema.ClientAbort.annotation) |
| 200 | return Effect.void |
| 201 | } |
| 202 | return options.onFromServer({ |
| 203 | _tag: "Exit", |
| 204 | clientId, |
| 205 | requestId: message.requestId, |
| 206 | exit: Exit.interrupt() |
| 207 | }) |
| 208 | } |
| 209 | case "Eof": { |
| 210 | client.ended = true |
| 211 | if (client.fibers.size > 0) return Effect.void |
| 212 | return endClient(client) |
| 213 | } |
| 214 | default: { |
| 215 | return sendDefect(client, `Unknown request tag: ${(message as any)._tag}`) |
| 216 | } |
| 217 | } |
| 218 | }), |
| 219 | (defect) => sendDefect(clients.get(clientId)!, defect) |
| 220 | ) |
| 221 | |
| 222 | const endClient = (client: Client) => { |
| 223 | clients.delete(client.id) |
nothing calls this directly
no test coverage detected