MCPcopy Create free account
hub / github.com/Effect-TS/effect / write

Function write

packages/effect/src/unstable/rpc/RpcServer.ts:170–220  ·  view source on GitHub ↗
(clientId, message, opts)

Source from the content-addressed store, hash-verified

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)

Callers

nothing calls this directly

Calls 8

ResponseDefectEncodedFunction · 0.90
endClientFunction · 0.85
sendDefectFunction · 0.85
interruptUnsafeMethod · 0.80
handleRequestFunction · 0.70
getMethod · 0.65
setMethod · 0.65
interruptMethod · 0.45

Tested by

no test coverage detected