| 1 | import * as Layer from "effect/Layer"; |
| 2 | import * as ManagedRuntime from "effect/ManagedRuntime"; |
| 3 | import * as Socket from "effect/socket/Socket"; |
| 4 | |
| 5 | import { layerRemoteHttpClient } from "@t3tools/client-runtime/rpc"; |
| 6 | |
| 7 | import * as Dpop from "../features/cloud/dpop"; |
| 8 | import * as ManagedRelayLayer from "../features/cloud/managedRelayLayer"; |
| 9 | import { resolveCloudPublicConfig } from "../features/cloud/publicConfig"; |
| 10 | import * as Persistence from "../persistence/layer"; |
| 11 | import { disposeOnFoundationReplace, type FoundationHotModule } from "./foundation-fast-refresh"; |
| 12 | |
| 13 | declare const module: { readonly hot?: FoundationHotModule } | undefined; |
| 14 | |
| 15 | function configuredRelayUrl(): string { |
| 16 | return resolveCloudPublicConfig().relay.url ?? "http://relay.invalid"; |
| 17 | } |
| 18 | |
| 19 | const layerHttpClient = layerRemoteHttpClient(fetch); |
| 20 | |
| 21 | type RuntimeLayerSource = |
| 22 | | ReturnType<typeof ManagedRelayLayer.layer> |
| 23 | | typeof Socket.layerWebSocketConstructorGlobal |
| 24 | | typeof Dpop.layer |
| 25 | | typeof layerHttpClient |
| 26 | | typeof Persistence.layer; |
| 27 | |
| 28 | const layerRuntime = Layer.merge( |
| 29 | ManagedRelayLayer.layer(configuredRelayUrl()), |
| 30 | Socket.layerWebSocketConstructorGlobal, |
| 31 | ).pipe( |
| 32 | Layer.provideMerge(Dpop.layer), |
| 33 | Layer.provideMerge(layerHttpClient), |
| 34 | Layer.provideMerge(Persistence.layer), |
| 35 | ); |
| 36 | |
| 37 | export const runtime: ManagedRuntime.ManagedRuntime< |
| 38 | Layer.Success<RuntimeLayerSource>, |
| 39 | Layer.Error<RuntimeLayerSource> |
| 40 | > = ManagedRuntime.make(layerRuntime); |
| 41 | |
| 42 | export const layer: Layer.Layer< |
| 43 | Layer.Success<RuntimeLayerSource>, |
| 44 | Layer.Error<RuntimeLayerSource> |
| 45 | > = Layer.effectContext(runtime.contextEffect); |
| 46 | |
| 47 | disposeOnFoundationReplace(typeof module === "undefined" ? undefined : module.hot, () => |
| 48 | runtime.dispose(), |
| 49 | ); |