| 598 | } |
| 599 | |
| 600 | const decodeReplies = ( |
| 601 | messages: Map<string, Message.OutgoingRequest<any>>, |
| 602 | encodedReplies: Array<Reply.ReplyEncoded<any>> |
| 603 | ) => { |
| 604 | const replies: Array<Reply.Reply<any>> = [] |
| 605 | const ignoredRequests = new Set<string>() |
| 606 | let index = 0 |
| 607 | |
| 608 | const decodeReply: Effect.Effect<void | Reply.Reply<any>> = Effect.catchAll( |
| 609 | Effect.suspend(() => { |
| 610 | const reply = encodedReplies[index] |
| 611 | if (ignoredRequests.has(reply.requestId)) return Effect.void |
| 612 | const message = messages.get(reply.requestId) |
| 613 | if (!message) return Effect.void |
| 614 | const schema = Reply.Reply(message.rpc) |
| 615 | return Schema.decode(schema)(reply).pipe( |
| 616 | Effect.locally(FiberRef.currentContext, message.context) |
| 617 | ) as Effect.Effect<Reply.Reply<any>, ParseError> |
| 618 | }), |
| 619 | (error) => { |
| 620 | const reply = encodedReplies[index] |
| 621 | ignoredRequests.add(reply.requestId) |
| 622 | return Effect.succeed( |
| 623 | new Reply.WithExit({ |
| 624 | id: snowflakeGen.unsafeNext(), |
| 625 | requestId: Snowflake.Snowflake(reply.requestId), |
| 626 | exit: Exit.die(error) |
| 627 | }) |
| 628 | ) |
| 629 | } |
| 630 | ) |
| 631 | |
| 632 | return Effect.as( |
| 633 | Effect.whileLoop({ |
| 634 | while: () => index < encodedReplies.length, |
| 635 | body: () => decodeReply, |
| 636 | step: (reply) => { |
| 637 | index++ |
| 638 | if (reply) replies.push(reply) |
| 639 | } |
| 640 | }), |
| 641 | replies |
| 642 | ) |
| 643 | } |
| 644 | |
| 645 | return storage |
| 646 | }) |