Await mirror replay during startup hydration
Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
1 parent
2dd87351ac
commit
07f4dbf480
3 files changed
+12
-12
No files matched your search
@@ -146,7 +146,7 @@ async function main() {
|
||||
});
|
||||
const isReadPilotMode = syncEngineMode === "livestore-read-pilot" || syncEngineMode === "livestore";
|
||||
if (syncEngineMode === "shadow" || isReadPilotMode) {
|
||||
const bootstrapResult = bootstrapMirrorFromCatchUp({
|
||||
const bootstrapResult = await bootstrapMirrorFromCatchUp({
|
||||
source: legacyStateSyncEngine,
|
||||
mirror: liveStoreMirror,
|
||||
logger,
|
||||
|
||||
@@ -3,7 +3,7 @@ import { createLogger } from "../logger";
|
||||
import { bootstrapMirrorFromCatchUp } from "./mirrorBootstrap";
|
||||
|
||||
describe("bootstrapMirrorFromCatchUp", () => {
|
||||
it("mirrors catch-up history into the mirror sink", () => {
|
||||
it("mirrors catch-up history into the mirror sink", async () => {
|
||||
const mirrorStateEvent = vi.fn();
|
||||
const source = {
|
||||
catchUp: vi.fn().mockReturnValue({
|
||||
@@ -27,7 +27,7 @@ describe("bootstrapMirrorFromCatchUp", () => {
|
||||
}),
|
||||
};
|
||||
|
||||
const result = bootstrapMirrorFromCatchUp({
|
||||
const result = await bootstrapMirrorFromCatchUp({
|
||||
source,
|
||||
mirror: { mirrorStateEvent },
|
||||
logger: createLogger("mirror-bootstrap-test"),
|
||||
@@ -41,7 +41,7 @@ describe("bootstrapMirrorFromCatchUp", () => {
|
||||
expect(mirrorStateEvent).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it("returns partial progress when non-fatal mirroring errors occur", () => {
|
||||
it("returns partial progress when non-fatal mirroring errors occur", async () => {
|
||||
const mirrorStateEvent = vi
|
||||
.fn()
|
||||
.mockImplementationOnce(() => {})
|
||||
@@ -70,7 +70,7 @@ describe("bootstrapMirrorFromCatchUp", () => {
|
||||
}),
|
||||
};
|
||||
|
||||
const result = bootstrapMirrorFromCatchUp({
|
||||
const result = await bootstrapMirrorFromCatchUp({
|
||||
source,
|
||||
mirror: { mirrorStateEvent },
|
||||
logger: createLogger("mirror-bootstrap-test"),
|
||||
@@ -84,7 +84,7 @@ describe("bootstrapMirrorFromCatchUp", () => {
|
||||
expect(mirrorStateEvent).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it("throws when failOnError is enabled", () => {
|
||||
it("throws when failOnError is enabled", async () => {
|
||||
const mirrorStateEvent = vi.fn().mockImplementation(() => {
|
||||
throw new Error("mirror write failed");
|
||||
});
|
||||
@@ -103,13 +103,13 @@ describe("bootstrapMirrorFromCatchUp", () => {
|
||||
}),
|
||||
};
|
||||
|
||||
expect(() =>
|
||||
await expect(
|
||||
bootstrapMirrorFromCatchUp({
|
||||
source,
|
||||
mirror: { mirrorStateEvent },
|
||||
logger: createLogger("mirror-bootstrap-test"),
|
||||
failOnError: true,
|
||||
}),
|
||||
).toThrow(/failed to bootstrap livestore mirror/i);
|
||||
).rejects.toThrow(/failed to bootstrap livestore mirror/i);
|
||||
});
|
||||
});
|
||||
@@ -5,7 +5,7 @@ interface CatchUpSource {
|
||||
}
|
||||
|
||||
interface MirrorSink {
|
||||
mirrorStateEvent(event: StateEvent): void;
|
||||
mirrorStateEvent(event: StateEvent): void | Promise<void>;
|
||||
}
|
||||
|
||||
interface LoggerLike {
|
||||
@@ -18,18 +18,18 @@ export interface MirrorBootstrapResult {
|
||||
complete: boolean;
|
||||
}
|
||||
|
||||
export function bootstrapMirrorFromCatchUp(options: {
|
||||
export async function bootstrapMirrorFromCatchUp(options: {
|
||||
source: CatchUpSource;
|
||||
mirror: MirrorSink;
|
||||
logger: LoggerLike;
|
||||
failOnError?: boolean;
|
||||
}): MirrorBootstrapResult {
|
||||
}): Promise<MirrorBootstrapResult> {
|
||||
const { source, mirror, logger, failOnError = false } = options;
|
||||
const catchUpResult = source.catchUp({ afterSeq: 0 });
|
||||
let mirroredCount = 0;
|
||||
for (const event of catchUpResult.events) {
|
||||
try {
|
||||
mirror.mirrorStateEvent(event);
|
||||
await mirror.mirrorStateEvent(event);
|
||||
mirroredCount += 1;
|
||||
} catch (error) {
|
||||
if (failOnError) {
|
||||
|
||||
Reference in new issue
Block a user