| 129 | * @since 4.0.0 |
| 130 | */ |
| 131 | export const Service = <Self>() => |
| 132 | < |
| 133 | const Id extends string, |
| 134 | Rpcs extends Rpc.Any, |
| 135 | ER, |
| 136 | RM = |
| 137 | | RpcClient.Protocol |
| 138 | | Rpc.MiddlewareClient<NoInfer<Rpcs>> |
| 139 | | Rpc.ServicesClient<NoInfer<Rpcs>> |
| 140 | >( |
| 141 | id: Id, |
| 142 | options: { |
| 143 | readonly group: RpcGroup.RpcGroup<Rpcs> |
| 144 | readonly protocol: |
| 145 | | Layer.Layer<Exclude<NoInfer<RM>, Scope>, ER> |
| 146 | | ((get: Atom.AtomContext) => Layer.Layer<Exclude<NoInfer<RM>, Scope>, ER>) |
| 147 | readonly spanPrefix?: string | undefined |
| 148 | readonly spanAttributes?: Record<string, unknown> | undefined |
| 149 | readonly generateRequestId?: (() => RequestId) | undefined |
| 150 | readonly disableTracing?: boolean | undefined |
| 151 | readonly makeEffect?: |
| 152 | | Effect.Effect< |
| 153 | RpcClient.RpcClient.Flat<Rpcs, RpcClientError>, |
| 154 | never, |
| 155 | RM |
| 156 | > |
| 157 | | undefined |
| 158 | readonly runtime?: Atom.RuntimeFactory | undefined |
| 159 | } |
| 160 | ): AtomRpcClient<Self, Id, Rpcs> => { |
| 161 | const self: Mutable<AtomRpcClient<Self, Id, Rpcs>> = Context.Service< |
| 162 | Self, |
| 163 | RpcClient.RpcClient.Flat<Rpcs, RpcClientError> |
| 164 | >()(id) as any |
| 165 | |
| 166 | const layer = Layer.effect( |
| 167 | self, |
| 168 | options.makeEffect ?? |
| 169 | (RpcClient.make(options.group, { |
| 170 | ...options, |
| 171 | flatten: true |
| 172 | }) as Effect.Effect< |
| 173 | RpcClient.RpcClient.Flat<Rpcs, RpcClientError>, |
| 174 | never, |
| 175 | RM |
| 176 | >) |
| 177 | ) |
| 178 | const runtimeFactory = options.runtime ?? Atom.runtime |
| 179 | self.runtime = runtimeFactory( |
| 180 | typeof options.protocol === "function" ? |
| 181 | (get) => |
| 182 | Layer.provide( |
| 183 | layer, |
| 184 | Layer.orDie( |
| 185 | (options.protocol as ((get: Atom.AtomContext) => Layer.Layer<Exclude<NoInfer<RM>, Scope>, ER>))(get) |
| 186 | ) |
| 187 | ) : |
| 188 | Layer.provide(layer, Layer.orDie(options.protocol)) |