Add web state-source mode seam for read-pilot
Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
1 parent
6513750a9c
commit
1bffbcfb22
4 files changed
+57
-4
No files matched your search
@@ -120,6 +120,12 @@ Optional diagnostics:
|
||||
- `T3CODE_LIVESTORE_CATCHUP_PARITY_CHECK=1`
|
||||
- In `livestore-read-pilot` mode, compares LiveStore vs legacy `state.catchUp` responses and logs drift diagnostics.
|
||||
|
||||
Web client state-source selection:
|
||||
|
||||
- `VITE_T3CODE_STATE_SOURCE_MODE=legacy-api|livestore-read-pilot`
|
||||
- Selects the web state-source adapter mode.
|
||||
- Current behavior is protocol-compatible in both modes (delegates to `api.state.*`) while preserving a client-side seam for future LiveStore-native source wiring.
|
||||
|
||||
## Provider architecture
|
||||
|
||||
The web app communicates with the server via WebSocket using a simple JSON-RPC-style protocol:
|
||||
|
||||
@@ -13,7 +13,7 @@ import { AnchoredToastProvider, ToastProvider } from "../components/ui/toast";
|
||||
import { isElectron } from "../env";
|
||||
import { useNativeApi } from "../hooks/useNativeApi";
|
||||
import { invalidateGitQueries } from "../lib/gitReactQuery";
|
||||
import { createStateSource } from "../stateSource";
|
||||
import { createStateSource, resolveStateSourceMode } from "../stateSource";
|
||||
import { useStore } from "../store";
|
||||
import { onServerWelcome } from "../wsNativeApi";
|
||||
|
||||
@@ -130,9 +130,19 @@ function StateSyncRouter() {
|
||||
strict: false,
|
||||
select: (params) => params.threadId,
|
||||
});
|
||||
const stateSourceMode = useMemo(
|
||||
() =>
|
||||
resolveStateSourceMode(
|
||||
(import.meta.env.VITE_T3CODE_STATE_SOURCE_MODE as string | undefined) ?? undefined,
|
||||
),
|
||||
[],
|
||||
);
|
||||
const lastStateSeqRef = useRef(0);
|
||||
const stateQueueRef = useRef(Promise.resolve());
|
||||
const stateSource = useMemo(() => (api ? createStateSource(api) : null), [api]);
|
||||
const stateSource = useMemo(
|
||||
() => (api ? createStateSource(api, { mode: stateSourceMode }) : null),
|
||||
[api, stateSourceMode],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (!api || !stateSource) return;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import type { NativeApi, StateEvent } from "@t3tools/contracts";
|
||||
import { createStateSource } from "./stateSource";
|
||||
import { createStateSource, resolveStateSourceMode } from "./stateSource";
|
||||
|
||||
function makeApi(): NativeApi {
|
||||
const onStateEvent = vi.fn<(callback: (event: StateEvent) => void) => () => void>((callback) => {
|
||||
@@ -164,6 +164,7 @@ describe("createStateSource", () => {
|
||||
it("delegates bootstrap/catchUp/onEvent to api.state", async () => {
|
||||
const api = makeApi();
|
||||
const source = createStateSource(api);
|
||||
expect(source.mode).toBe("legacy-api");
|
||||
|
||||
await expect(source.bootstrap()).resolves.toEqual({
|
||||
projects: [],
|
||||
@@ -194,4 +195,23 @@ describe("createStateSource", () => {
|
||||
expect(api.state.catchUp).toHaveBeenCalledWith({ afterSeq: 1 });
|
||||
expect(api.state.onEvent).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("supports explicit read-pilot mode selection", async () => {
|
||||
const api = makeApi();
|
||||
const source = createStateSource(api, { mode: "livestore-read-pilot" });
|
||||
expect(source.mode).toBe("livestore-read-pilot");
|
||||
await source.bootstrap();
|
||||
expect(api.state.bootstrap).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveStateSourceMode", () => {
|
||||
it("defaults to legacy-api for missing/unknown values", () => {
|
||||
expect(resolveStateSourceMode(undefined)).toBe("legacy-api");
|
||||
expect(resolveStateSourceMode("unknown")).toBe("legacy-api");
|
||||
});
|
||||
|
||||
it("accepts livestore-read-pilot value", () => {
|
||||
expect(resolveStateSourceMode("livestore-read-pilot")).toBe("livestore-read-pilot");
|
||||
});
|
||||
});
|
||||
@@ -6,14 +6,31 @@ import type {
|
||||
StateEvent,
|
||||
} from "@t3tools/contracts";
|
||||
|
||||
export type StateSourceMode = "legacy-api" | "livestore-read-pilot";
|
||||
|
||||
export interface StateSource {
|
||||
mode: StateSourceMode;
|
||||
bootstrap(): Promise<StateBootstrapResult>;
|
||||
catchUp(input: StateCatchUpInput): Promise<StateCatchUpResult>;
|
||||
onEvent(listener: (event: StateEvent) => void): () => void;
|
||||
}
|
||||
|
||||
export function createStateSource(api: NativeApi): StateSource {
|
||||
export function resolveStateSourceMode(rawMode: string | undefined): StateSourceMode {
|
||||
if (rawMode === "livestore-read-pilot") {
|
||||
return "livestore-read-pilot";
|
||||
}
|
||||
return "legacy-api";
|
||||
}
|
||||
|
||||
export function createStateSource(
|
||||
api: NativeApi,
|
||||
options: {
|
||||
mode?: StateSourceMode;
|
||||
} = {},
|
||||
): StateSource {
|
||||
const mode = options.mode ?? "legacy-api";
|
||||
return {
|
||||
mode,
|
||||
bootstrap: () => api.state.bootstrap(),
|
||||
catchUp: (input) => api.state.catchUp(input),
|
||||
onEvent: (listener) => api.state.onEvent(listener),
|
||||
|
||||
Reference in new issue
Block a user