Add web state source seam for sync provider migration
Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
1 parent
40bb6cb7a7
commit
637eae09a4
3 files changed
+228
-8
No files matched your search
@@ -4,7 +4,7 @@ import {
|
||||
type ErrorComponentProps,
|
||||
useParams,
|
||||
} from "@tanstack/react-router";
|
||||
import { useEffect, useRef } from "react";
|
||||
import { useEffect, useMemo, useRef } from "react";
|
||||
import { QueryClient, useQueryClient } from "@tanstack/react-query";
|
||||
|
||||
import { APP_DISPLAY_NAME } from "../branding";
|
||||
@@ -13,6 +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 { useStore } from "../store";
|
||||
import { onServerWelcome } from "../wsNativeApi";
|
||||
|
||||
@@ -131,16 +132,17 @@ function StateSyncRouter() {
|
||||
});
|
||||
const lastStateSeqRef = useRef(0);
|
||||
const stateQueueRef = useRef(Promise.resolve());
|
||||
const stateSource = useMemo(() => (api ? createStateSource(api) : null), [api]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!api) return;
|
||||
if (!api || !stateSource) return;
|
||||
let disposed = false;
|
||||
let retryDelayMs = 500;
|
||||
let retryTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
const bootstrap = async () => {
|
||||
try {
|
||||
const snapshot = await api.state.bootstrap();
|
||||
const snapshot = await stateSource.bootstrap();
|
||||
if (disposed) return;
|
||||
dispatch({
|
||||
type: "HYDRATE_FROM_SERVER",
|
||||
@@ -165,11 +167,11 @@ function StateSyncRouter() {
|
||||
clearTimeout(retryTimer);
|
||||
}
|
||||
};
|
||||
}, [api, dispatch]);
|
||||
}, [api, dispatch, stateSource]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!api) return;
|
||||
return api.state.onEvent((event) => {
|
||||
if (!api || !stateSource) return;
|
||||
return stateSource.onEvent((event) => {
|
||||
stateQueueRef.current = stateQueueRef.current
|
||||
.then(async () => {
|
||||
if (event.seq <= lastStateSeqRef.current) {
|
||||
@@ -177,7 +179,7 @@ function StateSyncRouter() {
|
||||
}
|
||||
|
||||
if (event.seq > lastStateSeqRef.current + 1) {
|
||||
const catchUp = await api.state.catchUp({ afterSeq: lastStateSeqRef.current });
|
||||
const catchUp = await stateSource.catchUp({ afterSeq: lastStateSeqRef.current });
|
||||
for (const missingEvent of catchUp.events) {
|
||||
if (missingEvent.seq <= lastStateSeqRef.current) continue;
|
||||
dispatch({
|
||||
@@ -198,7 +200,7 @@ function StateSyncRouter() {
|
||||
})
|
||||
.catch(() => undefined);
|
||||
});
|
||||
}, [api, dispatch]);
|
||||
}, [api, dispatch, stateSource]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!api) return;
|
||||
|
||||
@@ -0,0 +1,197 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import type { NativeApi, StateEvent } from "@t3tools/contracts";
|
||||
import { createStateSource } from "./stateSource";
|
||||
|
||||
function makeApi(): NativeApi {
|
||||
const onStateEvent = vi.fn<(callback: (event: StateEvent) => void) => () => void>((callback) => {
|
||||
callback({
|
||||
seq: 1,
|
||||
eventType: "project.upsert",
|
||||
entityId: "project-1",
|
||||
payload: {},
|
||||
createdAt: "2026-02-20T00:00:00.000Z",
|
||||
});
|
||||
return () => undefined;
|
||||
});
|
||||
|
||||
return {
|
||||
todos: {
|
||||
list: async () => [],
|
||||
add: async () => [],
|
||||
toggle: async () => [],
|
||||
remove: async () => [],
|
||||
},
|
||||
dialogs: {
|
||||
pickFolder: async () => null,
|
||||
confirm: async () => true,
|
||||
},
|
||||
terminal: {
|
||||
open: vi.fn(async () => {
|
||||
throw new Error("not implemented");
|
||||
}),
|
||||
write: vi.fn(async () => undefined),
|
||||
resize: vi.fn(async () => undefined),
|
||||
clear: vi.fn(async () => undefined),
|
||||
restart: vi.fn(async () => {
|
||||
throw new Error("not implemented");
|
||||
}),
|
||||
close: vi.fn(async () => undefined),
|
||||
onEvent: vi.fn(() => () => undefined),
|
||||
},
|
||||
agent: {
|
||||
spawn: vi.fn(async () => ""),
|
||||
kill: vi.fn(async () => undefined),
|
||||
write: vi.fn(async () => undefined),
|
||||
onOutput: vi.fn(() => () => undefined),
|
||||
onExit: vi.fn(() => () => undefined),
|
||||
},
|
||||
providers: {
|
||||
startSession: vi.fn(async () => {
|
||||
throw new Error("not implemented");
|
||||
}),
|
||||
sendTurn: vi.fn(async () => {
|
||||
throw new Error("not implemented");
|
||||
}),
|
||||
interruptTurn: vi.fn(async () => undefined),
|
||||
respondToRequest: vi.fn(async () => undefined),
|
||||
stopSession: vi.fn(async () => undefined),
|
||||
listSessions: vi.fn(async () => []),
|
||||
listCheckpoints: vi.fn(async () => {
|
||||
throw new Error("not implemented");
|
||||
}),
|
||||
getCheckpointDiff: vi.fn(async () => {
|
||||
throw new Error("not implemented");
|
||||
}),
|
||||
revertToCheckpoint: vi.fn(async () => {
|
||||
throw new Error("not implemented");
|
||||
}),
|
||||
onEvent: vi.fn(() => () => undefined),
|
||||
},
|
||||
state: {
|
||||
bootstrap: vi.fn(async () => ({
|
||||
projects: [],
|
||||
threads: [],
|
||||
lastStateSeq: 3,
|
||||
})),
|
||||
listMessages: vi.fn(async () => ({
|
||||
messages: [],
|
||||
total: 0,
|
||||
nextOffset: null,
|
||||
})),
|
||||
catchUp: vi.fn(async () => ({
|
||||
events: [],
|
||||
lastStateSeq: 3,
|
||||
})),
|
||||
onEvent: onStateEvent,
|
||||
},
|
||||
appSettings: {
|
||||
get: vi.fn(async () => ({ codexBinaryPath: "", codexHomePath: "" })),
|
||||
update: vi.fn(async () => ({ codexBinaryPath: "", codexHomePath: "" })),
|
||||
},
|
||||
threads: {
|
||||
create: vi.fn(async () => {
|
||||
throw new Error("not implemented");
|
||||
}),
|
||||
delete: vi.fn(async () => undefined),
|
||||
markVisited: vi.fn(async () => {
|
||||
throw new Error("not implemented");
|
||||
}),
|
||||
updateTerminalState: vi.fn(async () => {
|
||||
throw new Error("not implemented");
|
||||
}),
|
||||
updateModel: vi.fn(async () => {
|
||||
throw new Error("not implemented");
|
||||
}),
|
||||
updateTitle: vi.fn(async () => {
|
||||
throw new Error("not implemented");
|
||||
}),
|
||||
updateBranch: vi.fn(async () => {
|
||||
throw new Error("not implemented");
|
||||
}),
|
||||
},
|
||||
projects: {
|
||||
list: vi.fn(async () => []),
|
||||
add: vi.fn(async () => {
|
||||
throw new Error("not implemented");
|
||||
}),
|
||||
remove: vi.fn(async () => undefined),
|
||||
searchEntries: vi.fn(async () => ({ entries: [], truncated: false })),
|
||||
updateScripts: vi.fn(async () => {
|
||||
throw new Error("not implemented");
|
||||
}),
|
||||
},
|
||||
shell: {
|
||||
openInEditor: vi.fn(async () => undefined),
|
||||
openExternal: vi.fn(async () => undefined),
|
||||
},
|
||||
git: {
|
||||
pull: vi.fn(async () => {
|
||||
throw new Error("not implemented");
|
||||
}),
|
||||
status: vi.fn(async () => {
|
||||
throw new Error("not implemented");
|
||||
}),
|
||||
runStackedAction: vi.fn(async () => {
|
||||
throw new Error("not implemented");
|
||||
}),
|
||||
listBranches: vi.fn(async () => {
|
||||
throw new Error("not implemented");
|
||||
}),
|
||||
createWorktree: vi.fn(async () => {
|
||||
throw new Error("not implemented");
|
||||
}),
|
||||
removeWorktree: vi.fn(async () => undefined),
|
||||
createBranch: vi.fn(async () => undefined),
|
||||
checkout: vi.fn(async () => undefined),
|
||||
init: vi.fn(async () => undefined),
|
||||
},
|
||||
contextMenu: {
|
||||
show: vi.fn(async () => null),
|
||||
},
|
||||
server: {
|
||||
getConfig: vi.fn(async () => ({
|
||||
cwd: "/workspace",
|
||||
keybindings: [],
|
||||
})),
|
||||
upsertKeybinding: vi.fn(async () => ({
|
||||
keybindings: [],
|
||||
})),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe("createStateSource", () => {
|
||||
it("delegates bootstrap/catchUp/onEvent to api.state", async () => {
|
||||
const api = makeApi();
|
||||
const source = createStateSource(api);
|
||||
|
||||
await expect(source.bootstrap()).resolves.toEqual({
|
||||
projects: [],
|
||||
threads: [],
|
||||
lastStateSeq: 3,
|
||||
});
|
||||
await expect(source.catchUp({ afterSeq: 1 })).resolves.toEqual({
|
||||
events: [],
|
||||
lastStateSeq: 3,
|
||||
});
|
||||
|
||||
const received: StateEvent[] = [];
|
||||
const unsubscribe = source.onEvent((event) => {
|
||||
received.push(event);
|
||||
});
|
||||
expect(received).toEqual([
|
||||
{
|
||||
seq: 1,
|
||||
eventType: "project.upsert",
|
||||
entityId: "project-1",
|
||||
payload: {},
|
||||
createdAt: "2026-02-20T00:00:00.000Z",
|
||||
},
|
||||
]);
|
||||
unsubscribe();
|
||||
|
||||
expect(api.state.bootstrap).toHaveBeenCalledTimes(1);
|
||||
expect(api.state.catchUp).toHaveBeenCalledWith({ afterSeq: 1 });
|
||||
expect(api.state.onEvent).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,21 @@
|
||||
import type {
|
||||
NativeApi,
|
||||
StateBootstrapResult,
|
||||
StateCatchUpInput,
|
||||
StateCatchUpResult,
|
||||
StateEvent,
|
||||
} from "@t3tools/contracts";
|
||||
|
||||
export interface StateSource {
|
||||
bootstrap(): Promise<StateBootstrapResult>;
|
||||
catchUp(input: StateCatchUpInput): Promise<StateCatchUpResult>;
|
||||
onEvent(listener: (event: StateEvent) => void): () => void;
|
||||
}
|
||||
|
||||
export function createStateSource(api: NativeApi): StateSource {
|
||||
return {
|
||||
bootstrap: () => api.state.bootstrap(),
|
||||
catchUp: (input) => api.state.catchUp(input),
|
||||
onEvent: (listener) => api.state.onEvent(listener),
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user