diff --git a/apps/web/src/rpc/environmentAtomRpc.ts b/apps/web/src/rpc/environmentAtomRpc.ts index 97b2f87e4d..53507c363d 100644 --- a/apps/web/src/rpc/environmentAtomRpc.ts +++ b/apps/web/src/rpc/environmentAtomRpc.ts @@ -1,33 +1,13 @@ -import { WsRpcGroup, type EnvironmentId } from "@t3tools/contracts"; -import { Layer } from "effect"; -import { Atom, AtomRegistry, AtomRpc } from "effect/unstable/reactivity"; -import type * as Rpc from "effect/unstable/rpc/Rpc"; -import type * as RpcGroup from "effect/unstable/rpc/RpcGroup"; +import { createT3RpcAtomRuntime, type T3RpcAtomRuntime } from "@t3tools/client-runtime"; +import type { EnvironmentId } from "@t3tools/contracts"; import { createWsRpcProtocolLayer, type WsProtocolLifecycleHandlers, type WsRpcProtocolSocketUrlProvider, } from "./protocol"; -import { createServerAtomRpc, type ServerAtomRpc } from "./serverAtomRpc"; -type WsRpcGroupRpcs = - typeof WsRpcGroup extends RpcGroup.RpcGroup ? Rpcs : never; - -export interface WsAtomRpcService extends AtomRpc.AtomRpcClient< - WsAtomRpcService, - string, - WsRpcGroupRpcs -> {} - -export interface EnvironmentAtomRpcClient { - readonly environmentId: EnvironmentId; - readonly registry: AtomRegistry.AtomRegistry; - readonly service: WsAtomRpcService; - readonly server: ServerAtomRpc; - readonly reset: () => void; - readonly dispose: () => void; -} +export type EnvironmentAtomRpcClient = T3RpcAtomRuntime; export interface EnvironmentAtomRpcClientInput { readonly environmentId: EnvironmentId; @@ -38,28 +18,9 @@ export interface EnvironmentAtomRpcClientInput { export function createEnvironmentAtomRpcClient( input: EnvironmentAtomRpcClientInput, ): EnvironmentAtomRpcClient { - const registry = AtomRegistry.make(); - const runtime = Atom.context({ memoMap: Layer.makeMemoMapUnsafe() }); - const service = AtomRpc.Service()( - `t3tools/web/environment-rpc/${input.environmentId}`, - { - group: WsRpcGroup, - protocol: createWsRpcProtocolLayer(input.url, input.lifecycleHandlers), - runtime, - spanPrefix: `EnvironmentRpc:${input.environmentId}`, - }, - ); - - return { + return createT3RpcAtomRuntime({ environmentId: input.environmentId, - registry, - service, - server: createServerAtomRpc({ registry, service }), - reset: () => { - registry.reset(); - }, - dispose: () => { - registry.dispose(); - }, - }; + protocol: createWsRpcProtocolLayer(input.url, input.lifecycleHandlers), + spanPrefix: `EnvironmentRpc:${input.environmentId}`, + }); } diff --git a/apps/web/src/rpc/serverAtomRpc.ts b/apps/web/src/rpc/serverAtomRpc.ts deleted file mode 100644 index 39da0b9cf5..0000000000 --- a/apps/web/src/rpc/serverAtomRpc.ts +++ /dev/null @@ -1,112 +0,0 @@ -import { - WS_METHODS, - type ServerConfig, - type ServerSettings, - type ServerSettingsPatch, -} from "@t3tools/contracts"; -import { Effect } from "effect"; -import { AtomRegistry } from "effect/unstable/reactivity"; - -import type { WsAtomRpcService } from "./environmentAtomRpc"; - -const SERVER_CONFIG_REACTIVITY_KEY = ["server", "config"] as const; -const SERVER_SETTINGS_REACTIVITY_KEY = ["server", "settings"] as const; - -export interface ServerAtomRpcInput { - readonly registry: AtomRegistry.AtomRegistry; - readonly service: WsAtomRpcService; -} - -export interface ServerAtomRpc { - readonly atoms: { - readonly config: ReturnType; - readonly settings: ReturnType; - }; - readonly mutations: { - readonly updateSettings: ReturnType; - }; - readonly getConfig: Effect.Effect; - readonly getSettings: Effect.Effect; - readonly updateSettings: ( - patch: ServerSettingsPatch, - ) => Effect.Effect; -} - -export type ServerAtomRpcError = - | AtomFailure - | AtomFailure - | MutationFailure; - -type QueryAtom<_Tag extends Parameters[0]> = ReturnType< - WsAtomRpcService["query"] ->; - -type MutationAtom<_Tag extends Parameters[0]> = ReturnType< - WsAtomRpcService["mutation"] ->; - -type AtomFailure[0]> = - QueryAtom extends import("effect/unstable/reactivity").Atom.Atom< - import("effect/unstable/reactivity").AsyncResult.AsyncResult - > - ? E - : never; - -type MutationFailure[0]> = - MutationAtom extends import("effect/unstable/reactivity").Atom.AtomResultFn< - unknown, - unknown, - infer E - > - ? E - : never; - -export function createServerAtomRpc(input: ServerAtomRpcInput): ServerAtomRpc { - const config = input.service.query( - WS_METHODS.serverGetConfig, - {}, - { - reactivityKeys: SERVER_CONFIG_REACTIVITY_KEY, - serializationKey: "server.getConfig", - timeToLive: Infinity, - }, - ); - const settings = input.service.query( - WS_METHODS.serverGetSettings, - {}, - { - reactivityKeys: SERVER_SETTINGS_REACTIVITY_KEY, - serializationKey: "server.getSettings", - timeToLive: Infinity, - }, - ); - const updateSettings = input.service.mutation(WS_METHODS.serverUpdateSettings); - - return { - atoms: { - config, - settings, - }, - mutations: { - updateSettings, - }, - getConfig: AtomRegistry.getResult(input.registry, config, { suspendOnWaiting: true }), - getSettings: AtomRegistry.getResult(input.registry, settings, { suspendOnWaiting: true }), - updateSettings: (patch) => - Effect.gen(function* () { - yield* Effect.sync(() => { - input.registry.set(updateSettings, { - payload: { patch }, - reactivityKeys: { - config: SERVER_CONFIG_REACTIVITY_KEY, - settings: SERVER_SETTINGS_REACTIVITY_KEY, - }, - }); - }); - - return yield* AtomRegistry.getResult(input.registry, updateSettings, { - suspendOnWaiting: true, - }); - }), - }; -} diff --git a/packages/client-runtime/src/index.ts b/packages/client-runtime/src/index.ts index cb7472dff5..c3c51ca2dc 100644 --- a/packages/client-runtime/src/index.ts +++ b/packages/client-runtime/src/index.ts @@ -1,4 +1,6 @@ export * from "./advertisedEndpoint.ts"; export * from "./knownEnvironment.ts"; +export * from "./rpcAtomRuntime.ts"; export * from "./scoped.ts"; +export * from "./serverRpcAtoms.ts"; export * from "./sourceControlDiscoveryState.ts"; diff --git a/packages/client-runtime/src/rpcAtomRuntime.ts b/packages/client-runtime/src/rpcAtomRuntime.ts new file mode 100644 index 0000000000..1417cad30e --- /dev/null +++ b/packages/client-runtime/src/rpcAtomRuntime.ts @@ -0,0 +1,83 @@ +import { WsRpcGroup, type EnvironmentId } from "@t3tools/contracts"; +import { Effect, Layer } from "effect"; +import { Atom, AtomRegistry, AtomRpc } from "effect/unstable/reactivity"; +import type * as Rpc from "effect/unstable/rpc/Rpc"; +import type * as RpcClient from "effect/unstable/rpc/RpcClient"; +import type * as RpcGroup from "effect/unstable/rpc/RpcGroup"; + +type WsRpcGroupRpcs = + typeof WsRpcGroup extends RpcGroup.RpcGroup ? Rpcs : never; + +export interface T3RpcAtomService extends AtomRpc.AtomRpcClient< + T3RpcAtomService, + string, + WsRpcGroupRpcs +> {} + +export type T3RpcAtomProtocol = Layer.Layer; + +export interface T3RpcAtomRuntime { + readonly environmentId: EnvironmentId; + readonly registry: AtomRegistry.AtomRegistry; + readonly rpc: T3RpcAtomService; + readonly query: T3RpcAtomService["query"]; + readonly mutation: T3RpcAtomService["mutation"]; + readonly getResult: ( + atom: Atom.Atom>, + options?: { + readonly suspendOnWaiting?: boolean; + }, + ) => Effect.Effect; + readonly runMutation: ( + atom: Atom.AtomResultFn, + arg: Arg, + options?: { + readonly suspendOnWaiting?: boolean; + }, + ) => Effect.Effect; + readonly reset: () => void; + readonly dispose: () => void; +} + +export interface T3RpcAtomRuntimeInput { + readonly environmentId: EnvironmentId; + readonly protocol: T3RpcAtomProtocol; + readonly spanPrefix?: string; +} + +export function createT3RpcAtomRuntime(input: T3RpcAtomRuntimeInput): T3RpcAtomRuntime { + const registry = AtomRegistry.make(); + const runtime = Atom.context({ memoMap: Layer.makeMemoMapUnsafe() }); + const rpc = AtomRpc.Service()( + `t3tools/runtime/environment-rpc/${input.environmentId}`, + { + group: WsRpcGroup, + protocol: input.protocol, + runtime, + spanPrefix: input.spanPrefix ?? `EnvironmentRpc:${input.environmentId}`, + }, + ); + + return { + environmentId: input.environmentId, + registry, + rpc, + query: rpc.query, + mutation: rpc.mutation, + getResult: (atom, options) => AtomRegistry.getResult(registry, atom, options), + runMutation: (atom, arg, options) => + Effect.gen(function* () { + yield* Effect.sync(() => { + registry.set(atom, arg); + }); + + return yield* AtomRegistry.getResult(registry, atom, options); + }), + reset: () => { + registry.reset(); + }, + dispose: () => { + registry.dispose(); + }, + }; +} diff --git a/packages/client-runtime/src/serverRpcAtoms.ts b/packages/client-runtime/src/serverRpcAtoms.ts new file mode 100644 index 0000000000..ef6125b2b7 --- /dev/null +++ b/packages/client-runtime/src/serverRpcAtoms.ts @@ -0,0 +1,44 @@ +import { WS_METHODS, type ServerSettingsPatch } from "@t3tools/contracts"; + +import type { T3RpcAtomRuntime } from "./rpcAtomRuntime.ts"; + +export const SERVER_CONFIG_REACTIVITY_KEY = ["server", "config"] as const; +export const SERVER_SETTINGS_REACTIVITY_KEY = ["server", "settings"] as const; + +export function serverConfigAtom(runtime: Pick) { + return runtime.query( + WS_METHODS.serverGetConfig, + {}, + { + reactivityKeys: SERVER_CONFIG_REACTIVITY_KEY, + serializationKey: "server.getConfig", + timeToLive: Infinity, + }, + ); +} + +export function serverSettingsAtom(runtime: Pick) { + return runtime.query( + WS_METHODS.serverGetSettings, + {}, + { + reactivityKeys: SERVER_SETTINGS_REACTIVITY_KEY, + serializationKey: "server.getSettings", + timeToLive: Infinity, + }, + ); +} + +export function updateServerSettingsMutation(runtime: Pick) { + return runtime.mutation(WS_METHODS.serverUpdateSettings); +} + +export function updateServerSettingsArg(patch: ServerSettingsPatch) { + return { + payload: { patch }, + reactivityKeys: { + config: SERVER_CONFIG_REACTIVITY_KEY, + settings: SERVER_SETTINGS_REACTIVITY_KEY, + }, + } as const; +}