Propagate mirror replay success status during bootstrap
Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
1 parent
07f4dbf480
commit
9a6575655c
4 files changed
+41
-7
No files matched your search
@@ -66,17 +66,17 @@ export class LiveStoreStateMirror implements StateEventMirror {
|
||||
this.storeId = options.storeId ?? "t3-shadow-sync";
|
||||
}
|
||||
|
||||
async mirrorStateEvent(event: StateEvent): Promise<void> {
|
||||
async mirrorStateEvent(event: StateEvent): Promise<boolean> {
|
||||
if (!this.enabled || this.disposed) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
if (event.seq <= this.lastMirroredSeq) {
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
|
||||
const store = await this.getStore();
|
||||
if (!store) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -91,12 +91,14 @@ export class LiveStoreStateMirror implements StateEventMirror {
|
||||
);
|
||||
this.applyProjection(event);
|
||||
this.lastMirroredSeq = event.seq;
|
||||
return true;
|
||||
} catch (error) {
|
||||
this.logger.warn("failed to commit mirrored state event", {
|
||||
error,
|
||||
seq: event.seq,
|
||||
eventType: event.eventType,
|
||||
});
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -112,4 +112,33 @@ describe("bootstrapMirrorFromCatchUp", () => {
|
||||
}),
|
||||
).rejects.toThrow(/failed to bootstrap livestore mirror/i);
|
||||
});
|
||||
|
||||
it("treats mirrorStateEvent false return as a bootstrap failure", async () => {
|
||||
const source = {
|
||||
catchUp: vi.fn().mockReturnValue({
|
||||
events: [
|
||||
{
|
||||
seq: 1,
|
||||
entityId: "project-1",
|
||||
eventType: "project.upsert",
|
||||
payload: { id: "project-1", name: "Demo", path: "/demo" },
|
||||
createdAt: new Date().toISOString(),
|
||||
},
|
||||
],
|
||||
lastStateSeq: 1,
|
||||
}),
|
||||
};
|
||||
|
||||
const result = await bootstrapMirrorFromCatchUp({
|
||||
source,
|
||||
mirror: { mirrorStateEvent: vi.fn().mockResolvedValue(false) },
|
||||
logger: createLogger("mirror-bootstrap-test"),
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
mirroredCount: 0,
|
||||
lastStateSeq: 1,
|
||||
complete: false,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -5,7 +5,7 @@ interface CatchUpSource {
|
||||
}
|
||||
|
||||
interface MirrorSink {
|
||||
mirrorStateEvent(event: StateEvent): void | Promise<void>;
|
||||
mirrorStateEvent(event: StateEvent): void | boolean | Promise<void | boolean>;
|
||||
}
|
||||
|
||||
interface LoggerLike {
|
||||
@@ -29,7 +29,10 @@ export async function bootstrapMirrorFromCatchUp(options: {
|
||||
let mirroredCount = 0;
|
||||
for (const event of catchUpResult.events) {
|
||||
try {
|
||||
await mirror.mirrorStateEvent(event);
|
||||
const mirrorResult = await mirror.mirrorStateEvent(event);
|
||||
if (mirrorResult === false) {
|
||||
throw new Error("mirrorStateEvent returned false");
|
||||
}
|
||||
mirroredCount += 1;
|
||||
} catch (error) {
|
||||
if (failOnError) {
|
||||
|
||||
@@ -32,7 +32,7 @@ interface StateSyncEngineShadowEvents {
|
||||
}
|
||||
|
||||
export interface StateEventMirror {
|
||||
mirrorStateEvent(event: StateEvent): Promise<void>;
|
||||
mirrorStateEvent(event: StateEvent): Promise<void | boolean>;
|
||||
debugReadSnapshot?(): StateBootstrapResult;
|
||||
debugCatchUp?(afterSeq: number): StateCatchUpResult;
|
||||
debugListMessages?(raw: StateListMessagesInput): StateListMessagesResult;
|
||||
|
||||
Reference in new issue
Block a user