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

Function onEffectRequest

packages/effect/src/rpc/RpcClient.ts:362–438  ·  view source on GitHub ↗
(
    rpc: Rpc.AnyWithProps,
    middleware: (
      send: (request: Request<Rpcs>) => Effect.Effect<any, E>,
      request: Request<Rpcs>
    ) => Effect.Effect<any, E>,
    span: Tracer.Span | undefined,
    payload: any,
    headers: Headers.Headers,
    context: Context.Context<never>,
    discard: boolean
  )

Source from the content-addressed store, hash-verified

360 }
361
362 const onEffectRequest = (
363 rpc: Rpc.AnyWithProps,
364 middleware: (
365 send: (request: Request<Rpcs>) => Effect.Effect<any, E>,
366 request: Request<Rpcs>
367 ) => Effect.Effect<any, E>,
368 span: Tracer.Span | undefined,
369 payload: any,
370 headers: Headers.Headers,
371 context: Context.Context<never>,
372 discard: boolean
373 ) =>
374 Effect.withFiber<any, any, any>((parentFiber) => {
375 if (isShutdown) {
376 return Effect.interrupt
377 }
378 const id = generateRequestId()
379 const send = middleware(
380 (message) => options.onFromClient({ message, context }),
381 {
382 _tag: "Request",
383 id,
384 tag: rpc._tag as Rpc.Tag<Rpcs>,
385 payload,
386 ...traceFields(parentFiber, span),
387 headers: Headers.merge(parentFiber.getRef(CurrentHeaders), headers),
388 ...(discard ? { discard: true as const } : {})
389 }
390 )
391 let fiber: Fiber.Fiber<any, any>
392 return Effect.onInterrupt(
393 Effect.callback<any, any>((resume) => {
394 let acked = false
395 const entry: ClientEntry = {
396 _tag: "Effect",
397 rpc,
398 context,
399 resume(exit) {
400 if (discard && exit._tag === "Success") {
401 // A successful acknowledgement waits for the send to finish, as
402 // interrupting the send could cancel the call: an HTTP handler
403 // is scoped to its request. Failures complete the call at once.
404 acked = true
405 if (fiber?.pollUnsafe()) resume(Exit.void)
406 return
407 }
408 resume(exit)
409 if (fiber && !fiber.pollUnsafe()) {
410 parentFiber.currentDispatcher.scheduleTask(() => {
411 fiber.interruptUnsafe(parentFiber.id)
412 }, 0)
413 }
414 }
415 }
416 entries.set(id, entry)
417 fiber = send.pipe(
418 span ? Effect.withParentSpan(span, { captureStackTrace: false }) : identity,
419 Effect.runForkWith(parentFiber.context)

Callers 1

onRequestFunction · 0.85

Calls 12

traceFieldsFunction · 0.85
sendInterruptFunction · 0.85
getRefMethod · 0.80
addObserverMethod · 0.80
fromMethod · 0.80
middlewareFunction · 0.70
resumeFunction · 0.70
mergeMethod · 0.65
setMethod · 0.65
pipeMethod · 0.65
deleteMethod · 0.65
interruptMethod · 0.45

Tested by

no test coverage detected