Route interactive P2P UI through focused views

This commit is contained in:
vorotamoroz
2026-09-01 11:33:54 +00:00
parent f737701695
commit cab6679c2b
5 changed files with 41 additions and 111 deletions
+10 -28
View File
@@ -1,8 +1,6 @@
import { eventHub, EVENT_REQUEST_OPEN_P2P } from "@/common/events";
import { reactiveSource } from "octagonal-wheels/dataobject/reactive_v2";
import type { NecessaryServices } from "@vrtmrz/livesync-commonlib/compat/interfaces/ServiceModule";
import { type UseP2PReplicatorResult } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/UseP2PReplicatorResult";
import { P2PLogCollector } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/P2PLogCollector";
import {
P2PServerStatusPaneView,
VIEW_TYPE_P2P_SERVER_STATUS,
@@ -21,6 +19,9 @@ class LegacyP2PStatusPaneView extends P2PServerStatusPaneView {
}
}
/** Host-owned peer-selection entry used by the two adjunct P2P commands. */
export type OpenInteractiveP2PReplication = (showResult: boolean) => Promise<boolean | void>;
export function hasP2PConfiguration(settings: Partial<ObsidianLiveSyncSettings>): boolean {
if (
settings.remoteType === REMOTE_P2P ||
@@ -61,7 +62,8 @@ export function useP2PReplicatorUI(
never
>,
core: LiveSyncCore,
replicator: UseP2PReplicatorResult
replicator: UseP2PReplicatorResult,
openInteractiveReplication: OpenInteractiveP2PReplication
) {
const api = host.services.API as {
showWindow: (type: string) => Promise<void>;
@@ -80,21 +82,6 @@ export function useP2PReplicatorUI(
) => { addClass?: (name: string) => unknown; remove?: () => void } | undefined;
};
// const env: LiveSyncTrysteroReplicatorEnv = { services: host.services as any };
const getReplicator = () => replicator.replicator;
const p2pLogCollector = new P2PLogCollector(host.services.context.events);
const storeP2PStatusLine = reactiveSource("");
p2pLogCollector.p2pReplicationLine.onChanged((line) => {
storeP2PStatusLine.value = line.value;
});
const p2pParams = {
get replicator() {
return getReplicator();
},
p2pLogCollector,
storeP2PStatusLine,
};
const statusFactory = (leaf: WorkspaceLeaf) => {
return new P2PServerStatusPaneView(leaf, core, replicator);
};
@@ -108,15 +95,11 @@ 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();
// The deprecated compatibility result only opens the ordinary UI; the
// actual transfer runs through the stable targeted-transfer view.
void host.services.replicator.runFiniteReplicationActivity(
() => activeReplicator.openReplication(settings, false, true, false),
{ label: "replication" }
);
// Peer selection is a host UI concern. The injected operation opens
// the dialogue, while its transfer callbacks use focused P2P views.
void host.services.replicator.runFiniteReplicationActivity(() => openInteractiveReplication(true), {
label: "replication",
});
};
// Keep the retired view type registered only long enough to restore an
// existing workspace leaf with the current status UI. Layout-ready
@@ -244,5 +227,4 @@ export function useP2PReplicatorUI(
);
return true;
});
return p2pParams;
}
@@ -13,6 +13,8 @@ vi.mock("@/features/P2PSync/P2PReplicator/P2PServerStatusPaneView", () => ({
import { useP2PReplicatorUI } from "./useP2PReplicatorUI";
const noopOpenInteractiveReplication = () => Promise.resolve(false);
describe("useP2PReplicatorUI commands", () => {
it("waits for settings to load before deciding whether to show the P2P ribbon", async () => {
let initialise: (() => Promise<unknown>) | undefined;
@@ -49,7 +51,7 @@ describe("useP2PReplicatorUI commands", () => {
},
} as any;
useP2PReplicatorUI(host, {} as any, { replicator: undefined } as any);
useP2PReplicatorUI(host, {} as any, {} as any, noopOpenInteractiveReplication);
await expect(initialise?.()).resolves.toBe(true);
expect(currentSettings).not.toHaveBeenCalled();
@@ -64,7 +66,7 @@ describe("useP2PReplicatorUI commands", () => {
it("exposes a direct modal P2P replication command as finite replication activity", async () => {
const commands: Array<{ id: string; checkCallback?: (isChecking: boolean) => unknown }> = [];
let initialise: (() => Promise<unknown>) | undefined;
const openReplication = vi.fn(async () => true);
const openInteractiveReplication = vi.fn(async () => true);
const runFiniteReplicationActivity = vi.fn(async (task: () => unknown) => await task());
const host = {
services: {
@@ -96,58 +98,18 @@ describe("useP2PReplicatorUI commands", () => {
} as any;
const p2p = {
transportLifecycle: { isConnected: true },
replicator: {
server: { isServing: true },
openReplication,
replicateFromCommand: vi.fn(),
},
} as any;
useP2PReplicatorUI(host, {} as any, p2p);
useP2PReplicatorUI(host, {} as any, p2p, openInteractiveReplication);
await initialise?.();
commands.find((command) => command.id === "replicate-now-by-p2p")?.checkCallback?.(false);
await vi.waitFor(() => expect(openReplication).toHaveBeenCalledOnce());
await vi.waitFor(() => expect(openInteractiveReplication).toHaveBeenCalledWith(true));
expect(runFiniteReplicationActivity).toHaveBeenCalledWith(expect.any(Function), {
label: "replication",
});
});
it("keeps the current replicator in the pane parameters after replacement", () => {
const first = { id: "first" };
const second = { id: "second" };
let current = first;
const p2p = {
get replicator() {
return current;
},
} as any;
const host = {
services: {
context: createServiceContext(),
API: {
showWindow: vi.fn(async () => undefined),
registerWindow: vi.fn(),
addCommand: vi.fn(),
addRibbonIcon: vi.fn(),
getPlatform: vi.fn(() => "obsidian"),
},
appLifecycle: {
onInitialise: { addHandler: vi.fn() },
onSettingLoaded: { addHandler: vi.fn() },
onLayoutReady: { addHandler: vi.fn() },
},
setting: { currentSettings: vi.fn(() => ({ remoteType: "COUCHDB" })) },
replicator: { runFiniteReplicationActivity: vi.fn() },
},
} as any;
const paneParams = useP2PReplicatorUI(host, {} as any, p2p);
current = second;
expect(paneParams.replicator).toBe(second);
});
it("retains only the current P2P status command and routes existing open requests to it", async () => {
const commands: Array<{
id: string;
@@ -186,9 +148,9 @@ describe("useP2PReplicatorUI commands", () => {
replicator: { runFiniteReplicationActivity: vi.fn() },
},
} as any;
const p2p = { replicator: undefined } as any;
const p2p = {} as any;
useP2PReplicatorUI(host, {} as any, p2p);
useP2PReplicatorUI(host, {} as any, p2p, noopOpenInteractiveReplication);
await initialise?.();
expect(commands.map((command) => command.id)).not.toContain("open-p2p-replicator");
@@ -237,19 +199,13 @@ describe("useP2PReplicatorUI commands", () => {
replicator: { runFiniteReplicationActivity },
},
} as any;
const replicateFromCommand = vi.fn();
const synchroniseConfiguredTargets = vi.fn(async () => ({ status: "completed" }));
const p2p = {
transportLifecycle: { isConnected: true },
targetedTransfer: { synchroniseConfiguredTargets },
replicator: {
server: { isServing: true },
openReplication: vi.fn(),
replicateFromCommand,
},
} as any;
useP2PReplicatorUI(host, {} as any, p2p);
useP2PReplicatorUI(host, {} as any, p2p, noopOpenInteractiveReplication);
await initialise?.();
for (const commandId of [
@@ -283,7 +239,6 @@ describe("useP2PReplicatorUI commands", () => {
commands.find(({ id }) => id === "p2p-sync-targets")?.checkCallback?.(false);
await vi.waitFor(() => expect(synchroniseConfiguredTargets).toHaveBeenCalledOnce());
expect(replicateFromCommand).not.toHaveBeenCalled();
expect(runFiniteReplicationActivity).toHaveBeenCalledWith(expect.any(Function), {
label: "replication",
});
@@ -323,7 +278,7 @@ describe("useP2PReplicatorUI commands", () => {
},
} as any;
useP2PReplicatorUI(host, {} as any, { replicator: undefined } as any);
useP2PReplicatorUI(host, {} as any, {} as any, noopOpenInteractiveReplication);
await layoutReady?.();
expect(showWindow).not.toHaveBeenCalled();
@@ -379,7 +334,7 @@ describe("useP2PReplicatorUI commands", () => {
},
} as any;
useP2PReplicatorUI(host, {} as any, { replicator: undefined } as any);
useP2PReplicatorUI(host, {} as any, {} as any, noopOpenInteractiveReplication);
await initialise?.();
await settingLoaded?.();
expect(addRibbonIcon).not.toHaveBeenCalled();
@@ -453,7 +408,7 @@ describe("useP2PReplicatorUI commands", () => {
},
} as any;
useP2PReplicatorUI(host, {} as any, { replicator: undefined } as any);
useP2PReplicatorUI(host, {} as any, {} as any, noopOpenInteractiveReplication);
await layoutReady?.();
expect(legacyLeaf.setViewState).toHaveBeenCalledWith({