| 78 | * @category Layers |
| 79 | */ |
| 80 | export const layerRpcHandlers = < |
| 81 | const Type extends string, |
| 82 | Rpcs extends Rpc.Any |
| 83 | >(entity: Entity.Entity<Type, Rpcs>): Layer.Layer<RpcHandlers<Rpcs, Type>, never, Sharding | Rpc.Context<Rpcs>> => |
| 84 | Layer.effectContext(Effect.gen(function*() { |
| 85 | const context = yield* Effect.context<never>() |
| 86 | const client = yield* entity.client |
| 87 | const handlers = new Map<string, Rpc.Handler<string>>() |
| 88 | for (const parentRpc_ of entity.protocol.requests.values()) { |
| 89 | const parentRpc = parentRpc_ as any as Rpc.AnyWithProps |
| 90 | const tag = `${entity.type}.${parentRpc._tag}` as const |
| 91 | const key = `@effect/rpc/Rpc/${tag}` |
| 92 | handlers.set(key, { |
| 93 | context, |
| 94 | tag, |
| 95 | handler: ({ entityId, payload }: any) => (client(entityId) as any)[parentRpc._tag](payload) as any |
| 96 | } as any) |
| 97 | handlers.set(`${key}Discard`, { |
| 98 | context, |
| 99 | tag, |
| 100 | handler: ({ entityId, payload }: any) => |
| 101 | (client(entityId) as any)[parentRpc._tag](payload, { discard: true }) as any |
| 102 | } as any) |
| 103 | } |
| 104 | return Context.unsafeMake(handlers) |
| 105 | })) |
| 106 | |
| 107 | /** |
| 108 | * @since 1.0.0 |