mirror of
https://github.com/vrtmrz/obsidian-livesync.git
synced 2026-07-25 05:53:00 +00:00
Keep P2P consumers on the current transport
This commit is contained in:
@@ -13,12 +13,9 @@ import type { WorkspaceLeaf } from "@/deps";
|
||||
import { REMOTE_P2P } from "@vrtmrz/livesync-commonlib/compat/common/models/setting.const";
|
||||
|
||||
/**
|
||||
* ServiceFeature: P2P Replicator lifecycle management.
|
||||
* Binds a LiveSyncTrysteroReplicator to the host's lifecycle events,
|
||||
* following the same middleware style as useOfflineScanner.
|
||||
*
|
||||
* @param viewTypeAndFactory Optional [viewType, factory] pair for registering the P2P pane view.
|
||||
* When provided, also registers commands and ribbon icon via services.API.
|
||||
* Obsidian-specific P2P views, commands, status collection, and ribbon wiring.
|
||||
* Replicator ownership and lifecycle remain in Commonlib's
|
||||
* `useP2PReplicatorFeature`; this feature only consumes its current result.
|
||||
*/
|
||||
|
||||
export function useP2PReplicatorUI(
|
||||
@@ -59,22 +56,21 @@ export function useP2PReplicatorUI(
|
||||
p2pLogCollector.p2pReplicationLine.onChanged((line) => {
|
||||
storeP2PStatusLine.value = line.value;
|
||||
});
|
||||
const p2pParams = {
|
||||
get replicator() {
|
||||
return getReplicator();
|
||||
},
|
||||
p2pLogCollector,
|
||||
storeP2PStatusLine,
|
||||
};
|
||||
|
||||
// Register view, commands and ribbon if a view factory is provided
|
||||
const viewType = VIEW_TYPE_P2P;
|
||||
const factory = (leaf: WorkspaceLeaf) => {
|
||||
return new P2PReplicatorPaneView(leaf, core, {
|
||||
replicator: getReplicator(),
|
||||
p2pLogCollector,
|
||||
storeP2PStatusLine,
|
||||
});
|
||||
return new P2PReplicatorPaneView(leaf, core, p2pParams);
|
||||
};
|
||||
const statusFactory = (leaf: WorkspaceLeaf) => {
|
||||
return new P2PServerStatusPaneView(leaf, core, {
|
||||
replicator: getReplicator(),
|
||||
p2pLogCollector,
|
||||
storeP2PStatusLine,
|
||||
});
|
||||
return new P2PServerStatusPaneView(leaf, core, p2pParams);
|
||||
};
|
||||
const openPane = () => api.showWindow(viewType);
|
||||
const openStatusPane = () => {
|
||||
@@ -173,5 +169,5 @@ export function useP2PReplicatorUI(
|
||||
}
|
||||
return Promise.resolve(true);
|
||||
});
|
||||
return { replicator: getReplicator(), p2pLogCollector, storeP2PStatusLine };
|
||||
return p2pParams;
|
||||
}
|
||||
|
||||
@@ -57,4 +57,38 @@ describe("useP2PReplicatorUI commands", () => {
|
||||
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() },
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user