Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fd4c354212 | ||
|
|
8c48db4409 | ||
|
|
6f585399bd |
No files matched your search
@@ -9,12 +9,14 @@ import type {
|
||||
import type { KnownEnvironment } from "@t3tools/client-runtime";
|
||||
|
||||
import type { WsRpcClient } from "~/rpc/wsRpcClient";
|
||||
import type { EnvironmentAtomRpcClient } from "~/rpc/environmentAtomRpc";
|
||||
|
||||
export interface EnvironmentConnection {
|
||||
readonly kind: "primary" | "saved";
|
||||
readonly environmentId: EnvironmentId;
|
||||
readonly knownEnvironment: KnownEnvironment;
|
||||
readonly client: WsRpcClient;
|
||||
readonly atomRpc?: EnvironmentAtomRpcClient;
|
||||
readonly ensureBootstrapped: () => Promise<void>;
|
||||
readonly reconnect: () => Promise<void>;
|
||||
readonly dispose: () => Promise<void>;
|
||||
@@ -36,6 +38,7 @@ interface EnvironmentConnectionInput extends OrchestrationHandlers {
|
||||
readonly kind: "primary" | "saved";
|
||||
readonly knownEnvironment: KnownEnvironment;
|
||||
readonly client: WsRpcClient;
|
||||
readonly atomRpc?: EnvironmentAtomRpcClient;
|
||||
readonly refreshMetadata?: () => Promise<void>;
|
||||
readonly onConfigSnapshot?: (config: ServerConfig) => void;
|
||||
readonly onWelcome?: (payload: ServerLifecycleWelcomePayload) => void;
|
||||
@@ -162,9 +165,11 @@ export function createEnvironmentConnection(
|
||||
environmentId,
|
||||
knownEnvironment: input.knownEnvironment,
|
||||
client: input.client,
|
||||
...(input.atomRpc ? { atomRpc: input.atomRpc } : {}),
|
||||
ensureBootstrapped: () => bootstrapGate.wait(),
|
||||
reconnect: async () => {
|
||||
bootstrapGate.reset();
|
||||
input.atomRpc?.reset();
|
||||
try {
|
||||
await input.client.reconnect();
|
||||
await input.refreshMetadata?.();
|
||||
@@ -176,7 +181,11 @@ export function createEnvironmentConnection(
|
||||
},
|
||||
dispose: async () => {
|
||||
cleanup();
|
||||
await input.client.dispose();
|
||||
try {
|
||||
await input.client.dispose();
|
||||
} finally {
|
||||
input.atomRpc?.dispose();
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -28,3 +28,4 @@ export {
|
||||
startEnvironmentConnectionService,
|
||||
subscribeEnvironmentConnections,
|
||||
} from "./service";
|
||||
export type { EnvironmentConnection } from "./connection";
|
||||
@@ -63,10 +63,15 @@ import {
|
||||
} from "~/store";
|
||||
import { useTerminalStateStore } from "~/terminalStateStore";
|
||||
import { useUiStateStore } from "~/uiStateStore";
|
||||
import type { WsProtocolCloseContext } from "../../rpc/protocol";
|
||||
import type {
|
||||
WsProtocolCloseContext,
|
||||
WsProtocolLifecycleHandlers,
|
||||
WsRpcProtocolSocketUrlProvider,
|
||||
} from "../../rpc/protocol";
|
||||
import { getServerConfig } from "../../rpc/serverState";
|
||||
import { WsTransport } from "../../rpc/wsTransport";
|
||||
import { createWsRpcClient, type WsRpcClient } from "../../rpc/wsRpcClient";
|
||||
import { createEnvironmentAtomRpcClient } from "../../rpc/environmentAtomRpc";
|
||||
import { appendVersionMismatchHint, resolveServerConfigVersionMismatch } from "../../versionSkew";
|
||||
import {
|
||||
deriveLogicalProjectKeyFromSettings,
|
||||
@@ -1130,17 +1135,40 @@ function createPrimaryEnvironmentClient(
|
||||
`Unable to resolve websocket URL for ${knownEnvironment?.label ?? "primary environment"}.`,
|
||||
);
|
||||
}
|
||||
const connectionLabel = knownEnvironment?.label ?? null;
|
||||
|
||||
return createWsRpcClient(
|
||||
new WsTransport(wsBaseUrl, {
|
||||
getConnectionLabel: () => connectionLabel,
|
||||
getVersionMismatchHint: () =>
|
||||
resolveServerConfigVersionMismatch(getServerConfig())?.hint ?? null,
|
||||
}),
|
||||
new WsTransport(wsBaseUrl, createPrimaryEnvironmentLifecycleHandlers(knownEnvironment)),
|
||||
);
|
||||
}
|
||||
|
||||
function createPrimaryEnvironmentAtomRpcClient(
|
||||
knownEnvironment: ReturnType<typeof getPrimaryKnownEnvironment>,
|
||||
) {
|
||||
const wsBaseUrl = getKnownEnvironmentWsBaseUrl(knownEnvironment);
|
||||
if (!wsBaseUrl || !knownEnvironment?.environmentId) {
|
||||
throw new Error(
|
||||
`Unable to resolve websocket URL for ${knownEnvironment?.label ?? "primary environment"}.`,
|
||||
);
|
||||
}
|
||||
|
||||
return createEnvironmentAtomRpcClient({
|
||||
environmentId: knownEnvironment.environmentId,
|
||||
url: wsBaseUrl,
|
||||
lifecycleHandlers: createPrimaryEnvironmentLifecycleHandlers(knownEnvironment),
|
||||
});
|
||||
}
|
||||
|
||||
function createPrimaryEnvironmentLifecycleHandlers(
|
||||
knownEnvironment: ReturnType<typeof getPrimaryKnownEnvironment>,
|
||||
): WsProtocolLifecycleHandlers {
|
||||
const connectionLabel = knownEnvironment?.label ?? null;
|
||||
return {
|
||||
getConnectionLabel: () => connectionLabel,
|
||||
getVersionMismatchHint: () =>
|
||||
resolveServerConfigVersionMismatch(getServerConfig())?.hint ?? null,
|
||||
};
|
||||
}
|
||||
|
||||
function createSavedEnvironmentClient(
|
||||
environmentId: EnvironmentId,
|
||||
bearerToken: string,
|
||||
@@ -1149,67 +1177,90 @@ function createSavedEnvironmentClient(
|
||||
|
||||
return createWsRpcClient(
|
||||
new WsTransport(
|
||||
async () => {
|
||||
const record = getSavedEnvironmentRecord(environmentId);
|
||||
if (!record) {
|
||||
throw new Error(`Saved environment ${environmentId} not found.`);
|
||||
}
|
||||
return record.desktopSsh
|
||||
? await resolveDesktopSshWebSocketConnectionUrl(
|
||||
record.wsBaseUrl,
|
||||
record.httpBaseUrl,
|
||||
bearerToken,
|
||||
)
|
||||
: await resolveRemoteWebSocketConnectionUrl({
|
||||
wsBaseUrl: record.wsBaseUrl,
|
||||
httpBaseUrl: record.httpBaseUrl,
|
||||
bearerToken,
|
||||
});
|
||||
},
|
||||
{
|
||||
getConnectionLabel: () => getSavedEnvironmentRecord(environmentId)?.label ?? null,
|
||||
getVersionMismatchHint: () =>
|
||||
resolveServerConfigVersionMismatch(
|
||||
useSavedEnvironmentRuntimeStore.getState().byId[environmentId]?.serverConfig,
|
||||
)?.hint ?? null,
|
||||
onAttempt: () => {
|
||||
setRuntimeConnecting(environmentId);
|
||||
},
|
||||
onOpen: () => {
|
||||
setRuntimeConnected(environmentId);
|
||||
},
|
||||
onError: (message: string) => {
|
||||
const mismatch = resolveServerConfigVersionMismatch(
|
||||
useSavedEnvironmentRuntimeStore.getState().byId[environmentId]?.serverConfig,
|
||||
);
|
||||
useSavedEnvironmentRuntimeStore.getState().patch(environmentId, {
|
||||
connectionState: "error",
|
||||
lastError: appendVersionMismatchHint(message, mismatch),
|
||||
lastErrorAt: isoNow(),
|
||||
});
|
||||
},
|
||||
onClose: (
|
||||
details: { readonly code: number; readonly reason: string },
|
||||
context: WsProtocolCloseContext,
|
||||
) => {
|
||||
if (context.intentional) {
|
||||
return;
|
||||
}
|
||||
setRuntimeDisconnected(
|
||||
environmentId,
|
||||
appendVersionMismatchHint(
|
||||
details.reason,
|
||||
resolveServerConfigVersionMismatch(
|
||||
useSavedEnvironmentRuntimeStore.getState().byId[environmentId]?.serverConfig,
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
},
|
||||
createSavedEnvironmentSocketUrlProvider(environmentId, bearerToken),
|
||||
createSavedEnvironmentLifecycleHandlers(environmentId),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
function createSavedEnvironmentAtomRpcClient(environmentId: EnvironmentId, bearerToken: string) {
|
||||
useSavedEnvironmentRuntimeStore.getState().ensure(environmentId);
|
||||
|
||||
return createEnvironmentAtomRpcClient({
|
||||
environmentId,
|
||||
url: createSavedEnvironmentSocketUrlProvider(environmentId, bearerToken),
|
||||
lifecycleHandlers: createSavedEnvironmentLifecycleHandlers(environmentId),
|
||||
});
|
||||
}
|
||||
|
||||
function createSavedEnvironmentSocketUrlProvider(
|
||||
environmentId: EnvironmentId,
|
||||
bearerToken: string,
|
||||
): WsRpcProtocolSocketUrlProvider {
|
||||
return async () => {
|
||||
const record = getSavedEnvironmentRecord(environmentId);
|
||||
if (!record) {
|
||||
throw new Error(`Saved environment ${environmentId} not found.`);
|
||||
}
|
||||
return record.desktopSsh
|
||||
? await resolveDesktopSshWebSocketConnectionUrl(
|
||||
record.wsBaseUrl,
|
||||
record.httpBaseUrl,
|
||||
bearerToken,
|
||||
)
|
||||
: await resolveRemoteWebSocketConnectionUrl({
|
||||
wsBaseUrl: record.wsBaseUrl,
|
||||
httpBaseUrl: record.httpBaseUrl,
|
||||
bearerToken,
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
function createSavedEnvironmentLifecycleHandlers(
|
||||
environmentId: EnvironmentId,
|
||||
): WsProtocolLifecycleHandlers {
|
||||
return {
|
||||
getConnectionLabel: () => getSavedEnvironmentRecord(environmentId)?.label ?? null,
|
||||
getVersionMismatchHint: () =>
|
||||
resolveServerConfigVersionMismatch(
|
||||
useSavedEnvironmentRuntimeStore.getState().byId[environmentId]?.serverConfig,
|
||||
)?.hint ?? null,
|
||||
onAttempt: () => {
|
||||
setRuntimeConnecting(environmentId);
|
||||
},
|
||||
onOpen: () => {
|
||||
setRuntimeConnected(environmentId);
|
||||
},
|
||||
onError: (message: string) => {
|
||||
const mismatch = resolveServerConfigVersionMismatch(
|
||||
useSavedEnvironmentRuntimeStore.getState().byId[environmentId]?.serverConfig,
|
||||
);
|
||||
useSavedEnvironmentRuntimeStore.getState().patch(environmentId, {
|
||||
connectionState: "error",
|
||||
lastError: appendVersionMismatchHint(message, mismatch),
|
||||
lastErrorAt: isoNow(),
|
||||
});
|
||||
},
|
||||
onClose: (
|
||||
details: { readonly code: number; readonly reason: string },
|
||||
context: WsProtocolCloseContext,
|
||||
) => {
|
||||
if (context.intentional) {
|
||||
return;
|
||||
}
|
||||
setRuntimeDisconnected(
|
||||
environmentId,
|
||||
appendVersionMismatchHint(
|
||||
details.reason,
|
||||
resolveServerConfigVersionMismatch(
|
||||
useSavedEnvironmentRuntimeStore.getState().byId[environmentId]?.serverConfig,
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function refreshSavedEnvironmentMetadata(
|
||||
environmentId: EnvironmentId,
|
||||
bearerToken: string,
|
||||
@@ -1284,6 +1335,7 @@ function createPrimaryEnvironmentConnection(): EnvironmentConnection {
|
||||
kind: "primary",
|
||||
knownEnvironment,
|
||||
client: createPrimaryEnvironmentClient(knownEnvironment),
|
||||
atomRpc: createPrimaryEnvironmentAtomRpcClient(knownEnvironment),
|
||||
...createEnvironmentConnectionHandlers(),
|
||||
}),
|
||||
);
|
||||
@@ -1344,6 +1396,10 @@ async function ensureSavedEnvironmentConnection(
|
||||
const client =
|
||||
options?.client ??
|
||||
createSavedEnvironmentClient(activeRecord.environmentId, activeBearerToken);
|
||||
const atomRpc =
|
||||
options?.client === undefined
|
||||
? createSavedEnvironmentAtomRpcClient(activeRecord.environmentId, activeBearerToken)
|
||||
: undefined;
|
||||
const initialConfigSnapshot = createDeferredPromise<ServerConfig>();
|
||||
const knownEnvironment = createKnownEnvironment({
|
||||
id: activeRecord.environmentId,
|
||||
@@ -1361,6 +1417,7 @@ async function ensureSavedEnvironmentConnection(
|
||||
environmentId: activeRecord.environmentId,
|
||||
},
|
||||
client,
|
||||
...(atomRpc ? { atomRpc } : {}),
|
||||
refreshMetadata: async () => {
|
||||
await refreshSavedEnvironmentMetadata(
|
||||
activeRecord.environmentId,
|
||||
|
||||
@@ -19,9 +19,14 @@ import {
|
||||
UnifiedSettings,
|
||||
} from "@t3tools/contracts/settings";
|
||||
import { ensureLocalApi } from "~/localApi";
|
||||
import { Struct } from "effect";
|
||||
import { Cause, Effect, Exit, Option, Struct } from "effect";
|
||||
import { applyServerSettingsPatch } from "@t3tools/shared/serverSettings";
|
||||
import { applySettingsUpdated, getServerConfig, useServerSettings } from "~/rpc/serverState";
|
||||
import {
|
||||
RpcAtomRuntimeUnavailableError,
|
||||
usePrimaryServerSettingsRpcResult,
|
||||
useUpdatePrimaryServerSettingsRpc,
|
||||
} from "~/rpc/useEnvironmentRpcAtoms";
|
||||
|
||||
const CLIENT_SETTINGS_PERSISTENCE_ERROR_SCOPE = "[CLIENT_SETTINGS]";
|
||||
|
||||
@@ -168,12 +173,17 @@ export function useClientSettingsHydrated(): boolean {
|
||||
}
|
||||
|
||||
export function useSettings<T = UnifiedSettings>(selector?: (s: UnifiedSettings) => T): T {
|
||||
const serverSettings = useServerSettings();
|
||||
const legacyServerSettings = useServerSettings();
|
||||
const serverSettingsRpcResult = usePrimaryServerSettingsRpcResult();
|
||||
const clientSettings = useSyncExternalStore(
|
||||
subscribeClientSettings,
|
||||
getClientSettingsSnapshot,
|
||||
() => DEFAULT_CLIENT_SETTINGS,
|
||||
);
|
||||
const serverSettings =
|
||||
serverSettingsRpcResult._tag === "Success"
|
||||
? serverSettingsRpcResult.value
|
||||
: legacyServerSettings;
|
||||
|
||||
const merged = useMemo<UnifiedSettings>(
|
||||
() => ({
|
||||
@@ -193,25 +203,44 @@ export function useSettings<T = UnifiedSettings>(selector?: (s: UnifiedSettings)
|
||||
* persisted via RPC. Client keys go through client persistence.
|
||||
*/
|
||||
export function useUpdateSettings() {
|
||||
const updateSettings = useCallback((patch: Partial<UnifiedSettings>) => {
|
||||
const { serverPatch, clientPatch } = splitPatch(patch);
|
||||
const updatePrimaryServerSettingsRpc = useUpdatePrimaryServerSettingsRpc();
|
||||
const updateSettings = useCallback(
|
||||
(patch: Partial<UnifiedSettings>) => {
|
||||
const { serverPatch, clientPatch } = splitPatch(patch);
|
||||
|
||||
if (Object.keys(serverPatch).length > 0) {
|
||||
const currentServerConfig = getServerConfig();
|
||||
if (currentServerConfig) {
|
||||
applySettingsUpdated(applyServerSettingsPatch(currentServerConfig.settings, serverPatch));
|
||||
if (Object.keys(serverPatch).length > 0) {
|
||||
const currentServerConfig = getServerConfig();
|
||||
if (currentServerConfig) {
|
||||
applySettingsUpdated(applyServerSettingsPatch(currentServerConfig.settings, serverPatch));
|
||||
}
|
||||
void Effect.runPromiseExit(updatePrimaryServerSettingsRpc(serverPatch)).then((exit) => {
|
||||
if (Exit.isSuccess(exit)) {
|
||||
applySettingsUpdated(exit.value);
|
||||
return;
|
||||
}
|
||||
|
||||
const typedError = Cause.findErrorOption(exit.cause);
|
||||
if (
|
||||
Option.isSome(typedError) &&
|
||||
typedError.value instanceof RpcAtomRuntimeUnavailableError
|
||||
) {
|
||||
void ensureLocalApi().server.updateSettings(serverPatch);
|
||||
return;
|
||||
}
|
||||
|
||||
console.error("[SERVER_SETTINGS] AtomRpc update failed", Cause.pretty(exit.cause));
|
||||
});
|
||||
}
|
||||
// Fire-and-forget RPC — push will reconcile on success
|
||||
void ensureLocalApi().server.updateSettings(serverPatch);
|
||||
}
|
||||
|
||||
if (Object.keys(clientPatch).length > 0) {
|
||||
persistClientSettings({
|
||||
...getClientSettingsSnapshot(),
|
||||
...clientPatch,
|
||||
});
|
||||
}
|
||||
}, []);
|
||||
if (Object.keys(clientPatch).length > 0) {
|
||||
persistClientSettings({
|
||||
...getClientSettingsSnapshot(),
|
||||
...clientPatch,
|
||||
});
|
||||
}
|
||||
},
|
||||
[updatePrimaryServerSettingsRpc],
|
||||
);
|
||||
|
||||
const resetSettings = useCallback(() => {
|
||||
updateSettings(DEFAULT_UNIFIED_SETTINGS);
|
||||
|
||||
@@ -4,6 +4,10 @@ import type { ReactNode } from "react";
|
||||
|
||||
export let appAtomRegistry = AtomRegistry.make();
|
||||
|
||||
export function getAppAtomRegistry(): AtomRegistry.AtomRegistry {
|
||||
return appAtomRegistry;
|
||||
}
|
||||
|
||||
export function AppAtomRegistryProvider({ children }: { readonly children: ReactNode }) {
|
||||
return <RegistryContext.Provider value={appAtomRegistry}>{children}</RegistryContext.Provider>;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
import { createT3RpcAtomRuntime, type T3RpcAtomRuntime } from "@t3tools/client-runtime";
|
||||
import type { EnvironmentId } from "@t3tools/contracts";
|
||||
|
||||
import {
|
||||
createWsRpcProtocolLayer,
|
||||
type WsProtocolLifecycleHandlers,
|
||||
type WsRpcProtocolSocketUrlProvider,
|
||||
} from "./protocol";
|
||||
import { getAppAtomRegistry } from "./atomRegistry";
|
||||
|
||||
export type EnvironmentAtomRpcClient = T3RpcAtomRuntime;
|
||||
|
||||
export interface EnvironmentAtomRpcClientInput {
|
||||
readonly environmentId: EnvironmentId;
|
||||
readonly url: WsRpcProtocolSocketUrlProvider;
|
||||
readonly lifecycleHandlers?: WsProtocolLifecycleHandlers;
|
||||
}
|
||||
|
||||
export function createEnvironmentAtomRpcClient(
|
||||
input: EnvironmentAtomRpcClientInput,
|
||||
): EnvironmentAtomRpcClient {
|
||||
return createT3RpcAtomRuntime({
|
||||
environmentId: input.environmentId,
|
||||
getRegistry: getAppAtomRegistry,
|
||||
protocol: createWsRpcProtocolLayer(input.url, input.lifecycleHandlers),
|
||||
spanPrefix: `EnvironmentRpc:${input.environmentId}`,
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
import {
|
||||
serverSettingsAtom,
|
||||
updateServerSettingsArg,
|
||||
updateServerSettingsMutation,
|
||||
type T3RpcAtomRuntime,
|
||||
} from "@t3tools/client-runtime";
|
||||
import {
|
||||
ServerSettingsError,
|
||||
type ServerSettings,
|
||||
type ServerSettingsPatch,
|
||||
} from "@t3tools/contracts";
|
||||
import { useAtomValue } from "@effect/atom-react";
|
||||
import { Data, Effect } from "effect";
|
||||
import { AsyncResult, Atom } from "effect/unstable/reactivity";
|
||||
import type { RpcClientError } from "effect/unstable/rpc/RpcClientError";
|
||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
||||
|
||||
import { getPrimaryKnownEnvironment } from "../environments/primary";
|
||||
import {
|
||||
readEnvironmentConnection,
|
||||
subscribeEnvironmentConnections,
|
||||
} from "../environments/runtime";
|
||||
|
||||
export class RpcAtomRuntimeUnavailableError extends Data.TaggedError(
|
||||
"RpcAtomRuntimeUnavailableError",
|
||||
)<{
|
||||
readonly environmentId: string | null;
|
||||
}> {}
|
||||
|
||||
export type ServerSettingsRpcAtomError =
|
||||
| ServerSettingsError
|
||||
| RpcClientError
|
||||
| RpcAtomRuntimeUnavailableError;
|
||||
|
||||
export type UpdatePrimaryServerSettingsRpc = (
|
||||
patch: ServerSettingsPatch,
|
||||
) => Effect.Effect<ServerSettings, ServerSettingsRpcAtomError>;
|
||||
|
||||
const missingServerSettingsAtom = Atom.make(
|
||||
AsyncResult.initial<ServerSettings, ServerSettingsRpcAtomError>(true),
|
||||
).pipe(Atom.keepAlive, Atom.withLabel("rpc:server-settings:missing-runtime"));
|
||||
|
||||
export function usePrimaryEnvironmentRpcAtomRuntime(): T3RpcAtomRuntime | null {
|
||||
const [, forceSync] = useState(0);
|
||||
|
||||
useEffect(() => subscribeEnvironmentConnections(() => forceSync((version) => version + 1)), []);
|
||||
|
||||
const environmentId = getPrimaryKnownEnvironment()?.environmentId ?? null;
|
||||
return environmentId ? (readEnvironmentConnection(environmentId)?.atomRpc ?? null) : null;
|
||||
}
|
||||
|
||||
export function usePrimaryServerSettingsRpcResult() {
|
||||
const runtime = usePrimaryEnvironmentRpcAtomRuntime();
|
||||
const atom = useMemo(
|
||||
() =>
|
||||
(runtime ? serverSettingsAtom(runtime) : missingServerSettingsAtom) as Atom.Atom<
|
||||
AsyncResult.AsyncResult<ServerSettings, ServerSettingsRpcAtomError>
|
||||
>,
|
||||
[runtime],
|
||||
);
|
||||
|
||||
return useAtomValue(atom);
|
||||
}
|
||||
|
||||
export function useUpdatePrimaryServerSettingsRpc(): UpdatePrimaryServerSettingsRpc {
|
||||
const runtime = usePrimaryEnvironmentRpcAtomRuntime();
|
||||
const mutation = useMemo(
|
||||
() => (runtime ? updateServerSettingsMutation(runtime) : null),
|
||||
[runtime],
|
||||
);
|
||||
|
||||
return useCallback<UpdatePrimaryServerSettingsRpc>(
|
||||
(patch: ServerSettingsPatch) => {
|
||||
if (!runtime || !mutation) {
|
||||
return Effect.fail(
|
||||
new RpcAtomRuntimeUnavailableError({
|
||||
environmentId: getPrimaryKnownEnvironment()?.environmentId ?? null,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
return runtime.runMutation(mutation, updateServerSettingsArg(patch), {
|
||||
suspendOnWaiting: true,
|
||||
});
|
||||
},
|
||||
[mutation, runtime],
|
||||
);
|
||||
}
|
||||
@@ -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,82 @@
|
||||
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 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 getRegistry: () => AtomRegistry.AtomRegistry;
|
||||
readonly spanPrefix?: string;
|
||||
}
|
||||
|
||||
export function createT3RpcAtomRuntime(input: T3RpcAtomRuntimeInput): T3RpcAtomRuntime {
|
||||
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,
|
||||
rpc,
|
||||
query: rpc.query,
|
||||
mutation: rpc.mutation,
|
||||
getResult: (atom, options) => AtomRegistry.getResult(input.getRegistry(), atom, options),
|
||||
runMutation: (atom, arg, options) =>
|
||||
Effect.gen(function* () {
|
||||
yield* Effect.sync(() => {
|
||||
input.getRegistry().set(atom, arg);
|
||||
});
|
||||
|
||||
return yield* AtomRegistry.getResult(input.getRegistry(), atom, options);
|
||||
}),
|
||||
reset: () => {
|
||||
// Registry ownership stays with the host app. Environment teardown can
|
||||
// invalidate specific atoms, but it must not reset the app registry.
|
||||
},
|
||||
dispose: () => {
|
||||
// Registry ownership stays with the host app.
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -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