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

Function streamEffect

packages/effect/src/unstable/rpc/RpcServer.ts:396–444  ·  view source on GitHub ↗
(
    client: Client,
    request: Request<Rpcs>,
    stream:
      | Stream.Stream<any, any>
      | Effect.Effect<Queue.Dequeue<any, any>, any, Scope.Scope>
  )

Source from the content-addressed store, hash-verified

394 }
395
396 const streamEffect = (
397 client: Client,
398 request: Request<Rpcs>,
399 stream:
400 | Stream.Stream<any, any>
401 | Effect.Effect<Queue.Dequeue<any, any>, any, Scope.Scope>
402 ) => {
403 let latch = client.latches.get(request.id)
404 if (supportsAck && !latch) {
405 latch = Latch.makeUnsafe(false)
406 client.latches.set(request.id, latch)
407 }
408 if (Effect.isEffect(stream)) {
409 return stream.pipe(
410 Effect.flatMap((queue) =>
411 Effect.whileLoop({
412 while: constTrue,
413 body: constant(
414 Effect.flatMap(Queue.takeAll(queue), (values) => {
415 const write = options.onFromServer({
416 _tag: "Chunk",
417 clientId: client.id,
418 requestId: request.id,
419 values
420 })
421 if (!latch) return write
422 latch.closeUnsafe()
423 return Effect.flatMap(write, () => latch.await)
424 })
425 ),
426 step: constVoid
427 })
428 ),
429 Pull.catchDone(() => Effect.void),
430 Effect.scoped
431 )
432 }
433 return Stream.runForEachArray(stream, (values) => {
434 const write = options.onFromServer({
435 _tag: "Chunk",
436 clientId: client.id,
437 requestId: request.id,
438 values
439 })
440 if (!latch) return write
441 latch.closeUnsafe()
442 return Effect.andThen(write, latch.await)
443 })
444 }
445
446 const sendDefect = (client: Client, defect: unknown) =>
447 Effect.suspend(() => {

Callers 1

handleRequestFunction · 0.85

Calls 6

constantFunction · 0.90
getMethod · 0.65
setMethod · 0.65
pipeMethod · 0.65
takeAllMethod · 0.65
closeUnsafeMethod · 0.65

Tested by

no test coverage detected