Extract shared RPC atom runtime helpers
- Move environment RPC setup into `packages/client-runtime` - Replace web-side server atom indirection with shared helpers - Remove obsolete `serverAtomRpc` wrapper
This commit is contained in:
1 parent
6f585399bd
commit
8c48db4409
5 files changed
+136
-158
No files matched your search
@@ -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<infer Rpcs extends Rpc.Any> ? 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<WsAtomRpcService>()(
|
||||
`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}`,
|
||||
});
|
||||
}
|
||||
@@ -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<WsAtomRpcService["query"]>;
|
||||
readonly settings: ReturnType<WsAtomRpcService["query"]>;
|
||||
};
|
||||
readonly mutations: {
|
||||
readonly updateSettings: ReturnType<WsAtomRpcService["mutation"]>;
|
||||
};
|
||||
readonly getConfig: Effect.Effect<ServerConfig, ServerAtomRpcError>;
|
||||
readonly getSettings: Effect.Effect<ServerSettings, ServerAtomRpcError>;
|
||||
readonly updateSettings: (
|
||||
patch: ServerSettingsPatch,
|
||||
) => Effect.Effect<ServerSettings, ServerAtomRpcError>;
|
||||
}
|
||||
|
||||
export type ServerAtomRpcError =
|
||||
| AtomFailure<typeof WS_METHODS.serverGetConfig>
|
||||
| AtomFailure<typeof WS_METHODS.serverGetSettings>
|
||||
| MutationFailure<typeof WS_METHODS.serverUpdateSettings>;
|
||||
|
||||
type QueryAtom<_Tag extends Parameters<WsAtomRpcService["query"]>[0]> = ReturnType<
|
||||
WsAtomRpcService["query"]
|
||||
>;
|
||||
|
||||
type MutationAtom<_Tag extends Parameters<WsAtomRpcService["mutation"]>[0]> = ReturnType<
|
||||
WsAtomRpcService["mutation"]
|
||||
>;
|
||||
|
||||
type AtomFailure<Tag extends Parameters<WsAtomRpcService["query"]>[0]> =
|
||||
QueryAtom<Tag> extends import("effect/unstable/reactivity").Atom.Atom<
|
||||
import("effect/unstable/reactivity").AsyncResult.AsyncResult<unknown, infer E>
|
||||
>
|
||||
? E
|
||||
: never;
|
||||
|
||||
type MutationFailure<Tag extends Parameters<WsAtomRpcService["mutation"]>[0]> =
|
||||
MutationAtom<Tag> 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,
|
||||
});
|
||||
}),
|
||||
};
|
||||
}
|
||||
@@ -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";
|
||||
@@ -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<infer Rpcs extends Rpc.Any> ? Rpcs : never;
|
||||
|
||||
export interface T3RpcAtomService extends AtomRpc.AtomRpcClient<
|
||||
T3RpcAtomService,
|
||||
string,
|
||||
WsRpcGroupRpcs
|
||||
> {}
|
||||
|
||||
export type T3RpcAtomProtocol = Layer.Layer<RpcClient.Protocol, never, never>;
|
||||
|
||||
export interface T3RpcAtomRuntime {
|
||||
readonly environmentId: EnvironmentId;
|
||||
readonly registry: AtomRegistry.AtomRegistry;
|
||||
readonly rpc: T3RpcAtomService;
|
||||
readonly query: T3RpcAtomService["query"];
|
||||
readonly mutation: T3RpcAtomService["mutation"];
|
||||
readonly getResult: <A, E>(
|
||||
atom: Atom.Atom<import("effect/unstable/reactivity").AsyncResult.AsyncResult<A, E>>,
|
||||
options?: {
|
||||
readonly suspendOnWaiting?: boolean;
|
||||
},
|
||||
) => Effect.Effect<A, E>;
|
||||
readonly runMutation: <Arg, A, E>(
|
||||
atom: Atom.AtomResultFn<Arg, A, E>,
|
||||
arg: Arg,
|
||||
options?: {
|
||||
readonly suspendOnWaiting?: boolean;
|
||||
},
|
||||
) => Effect.Effect<A, E>;
|
||||
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<T3RpcAtomService>()(
|
||||
`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();
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -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<T3RpcAtomRuntime, "query">) {
|
||||
return runtime.query(
|
||||
WS_METHODS.serverGetConfig,
|
||||
{},
|
||||
{
|
||||
reactivityKeys: SERVER_CONFIG_REACTIVITY_KEY,
|
||||
serializationKey: "server.getConfig",
|
||||
timeToLive: Infinity,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
export function serverSettingsAtom(runtime: Pick<T3RpcAtomRuntime, "query">) {
|
||||
return runtime.query(
|
||||
WS_METHODS.serverGetSettings,
|
||||
{},
|
||||
{
|
||||
reactivityKeys: SERVER_SETTINGS_REACTIVITY_KEY,
|
||||
serializationKey: "server.getSettings",
|
||||
timeToLive: Infinity,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
export function updateServerSettingsMutation(runtime: Pick<T3RpcAtomRuntime, "mutation">) {
|
||||
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;
|
||||
}
|
||||
Reference in new issue
Block a user