(
storage:
& Omit<
MessageStorage["Service"],
"saveReply" | "registerReplyHandler" | "unregisterReplyHandler" | "unregisterShardReplyHandlers"
>
& {
/**
* Save the provided `Reply`, returning the reply as it was persisted.
*
* Implementations that persist a different representation, such as a
* defect reply when the original cannot be encoded, return that fallback
* so reply handlers deliver exactly what storage recorded.
*/
readonly saveReply: <R extends Rpc.Any>(
reply: Reply.ReplyWithContext<R>
) => Effect.Effect<Reply.ReplyWithContext<R>, PersistenceError | MalformedMessage>
}
)
| 619 | * @since 4.0.0 |
| 620 | */ |
| 621 | export const make = ( |
| 622 | storage: |
| 623 | & Omit< |
| 624 | MessageStorage["Service"], |
| 625 | "saveReply" | "registerReplyHandler" | "unregisterReplyHandler" | "unregisterShardReplyHandlers" |
| 626 | > |
| 627 | & { |
| 628 | /** |
| 629 | * Save the provided `Reply`, returning the reply as it was persisted. |
| 630 | * |
| 631 | * Implementations that persist a different representation, such as a |
| 632 | * defect reply when the original cannot be encoded, return that fallback |
| 633 | * so reply handlers deliver exactly what storage recorded. |
| 634 | */ |
| 635 | readonly saveReply: <R extends Rpc.Any>( |
| 636 | reply: Reply.ReplyWithContext<R> |
| 637 | ) => Effect.Effect<Reply.ReplyWithContext<R>, PersistenceError | MalformedMessage> |
| 638 | } |
| 639 | ): Effect.Effect<MessageStorage["Service"]> => |
| 640 | Effect.sync(() => { |
| 641 | type ReplyHandler = { |
| 642 | readonly message: Message.OutgoingRequest<any> | Message.IncomingRequest<any> |
| 643 | readonly shardSet: Set<ReplyHandler> |
| 644 | readonly respond: (reply: Reply.ReplyWithContext<any>) => Effect.Effect<void, PersistenceError | MalformedMessage> |
| 645 | readonly resume: (effect: Effect.Effect<void, EntityNotAssignedToRunner>) => void |
| 646 | } |
| 647 | const replyHandlers = new Map<Snowflake.Snowflake, Array<ReplyHandler>>() |
| 648 | const replyHandlersShard = new Map<string, Set<ReplyHandler>>() |
| 649 | const resumeUnregistered = (handler: ReplyHandler, options?: { readonly interrupt?: boolean | undefined }) => |
| 650 | handler.resume( |
| 651 | options?.interrupt ? ClusterAbandon.interrupt : Effect.fail( |
| 652 | new EntityNotAssignedToRunner({ |
| 653 | address: handler.message.envelope.address |
| 654 | }) |
| 655 | ) |
| 656 | ) |
| 657 | return MessageStorage.of({ |
| 658 | ...storage, |
| 659 | // Nested transactions defer notifications to the outermost commit. |
| 660 | withTransaction: (effect) => |
| 661 | TransactionNotifications.use((outer) => { |
| 662 | const notifications: Array<Effect.Effect<void, PersistenceError | MalformedMessage>> = [] |
| 663 | return storage.withTransaction(Effect.provideService(effect, TransactionNotifications, notifications)).pipe( |
| 664 | Effect.tap(() => |
| 665 | outer |
| 666 | ? Effect.sync(() => { |
| 667 | outer.push(...notifications) |
| 668 | }) |
| 669 | : Effect.orDie(Effect.forEach(notifications, identity, { discard: true })) |
| 670 | ) |
| 671 | ) |
| 672 | }), |
| 673 | registerReplyHandler: (message) => { |
| 674 | const requestId = message.envelope.requestId |
| 675 | return Effect.callback<void, EntityNotAssignedToRunner>((resume) => { |
| 676 | const shardId = message.envelope.address.shardId.toString() |
| 677 | let handlers = replyHandlers.get(requestId) |
| 678 | if (handlers === undefined) { |
no test coverage detected