Protect bounded remote activity across app lifecycle

This commit is contained in:
vorotamoroz
2026-07-15 07:53:16 +00:00
parent 4cbccf85b4
commit 31e9186930
373 changed files with 1275 additions and 466 deletions
+11 -2
View File
@@ -83,6 +83,15 @@ export function useP2PReplicatorUI(
}
return api.showWindow(VIEW_TYPE_P2P_SERVER_STATUS);
};
const runOpenReplication = () => {
const activeReplicator = replicator.replicator;
if (!activeReplicator) return;
const settings = host.services.setting.currentSettings();
void host.services.replicator.runBoundedRemoteActivity(
() => activeReplicator.openReplication(settings, false, true, false),
{ label: "replication" }
);
};
api.registerWindow(viewType, factory);
api.registerWindow(VIEW_TYPE_P2P_SERVER_STATUS, statusFactory);
@@ -115,7 +124,7 @@ export function useP2PReplicatorUI(
if (settings.remoteType == REMOTE_P2P) return false;
return replicator.replicator?.server?.isServing ?? false;
}
void replicator.replicator?.openReplication(settings, false, true, false);
runOpenReplication();
},
});
host.services.API.addCommand({
@@ -127,7 +136,7 @@ export function useP2PReplicatorUI(
if (settings.remoteType == REMOTE_P2P) return false;
return replicator.replicator?.server?.isServing ?? false;
}
void replicator.replicator?.openReplication(settings, false, true, false);
runOpenReplication();
},
});
@@ -0,0 +1,58 @@
import { describe, expect, it, vi } from "vitest";
vi.mock("@/features/P2PSync/P2PReplicator/P2PReplicatorPaneView", () => ({
P2PReplicatorPaneView: class {},
VIEW_TYPE_P2P: "p2p",
}));
vi.mock("@/features/P2PSync/P2PReplicator/P2PServerStatusPaneView", () => ({
P2PServerStatusPaneView: class {},
VIEW_TYPE_P2P_SERVER_STATUS: "p2p-status",
}));
import { useP2PReplicatorUI } from "./useP2PReplicatorUI";
describe("useP2PReplicatorUI commands", () => {
it("runs a direct modal P2P replication command through the shared activity boundary", async () => {
const commands: Array<{ id: string; checkCallback?: (isChecking: boolean) => unknown }> = [];
let initialise: (() => Promise<unknown>) | undefined;
const openReplication = vi.fn(async () => true);
const runBoundedRemoteActivity = vi.fn(async (task: () => unknown) => await task());
const host = {
services: {
API: {
showWindow: vi.fn(async () => undefined),
registerWindow: vi.fn(),
addCommand: vi.fn((command) => commands.push(command)),
addRibbonIcon: vi.fn(),
getPlatform: vi.fn(() => "obsidian"),
},
appLifecycle: {
onInitialise: {
addHandler: vi.fn((handler) => {
initialise = handler;
}),
},
onLayoutReady: { addHandler: vi.fn() },
},
setting: { currentSettings: vi.fn(() => ({ remoteType: "COUCHDB" })) },
replicator: { runBoundedRemoteActivity },
},
} as any;
const p2p = {
replicator: {
server: { isServing: true },
openReplication,
replicateFromCommand: vi.fn(),
},
} as any;
useP2PReplicatorUI(host, {} as any, p2p);
await initialise?.();
commands.find((command) => command.id === "replicate-now-by-p2p")?.checkCallback?.(false);
await vi.waitFor(() => expect(openReplication).toHaveBeenCalledOnce());
expect(runBoundedRemoteActivity).toHaveBeenCalledWith(expect.any(Function), {
label: "replication",
});
});
});