mirror of
https://github.com/vrtmrz/obsidian-livesync.git
synced 2026-03-30 13:45:19 +00:00
Now, Self-hosted LiveSync has finally begun to be split into the Self-hosted LiveSync plugin for Obsidian, and a properly abstracted version of it. This may not offer much benefit to Obsidian plugin users, or might even cause a slight inconvenience, but I believe it will certainly help improve testability and make the ecosystem better. However, I do not see the point in putting something with little benefit into beta, so I am handling this on the alpha branch. I would actually preferred to create an R&D branch, but I was not keen on the ampersand, and I feel it will eventually become a proper beta anyway. ### Refactored - Separated `ObsidianLiveSyncPlugin` into `ObsidianLiveSyncPlugin` and `LiveSyncBaseCore`. - Now `LiveSyncCore` indicates the type specified version of `LiveSyncBaseCore`. - Referencing `plugin.xxx` has been rewritten to referencing the corresponding service or `core.xxx`. ### Internal API changes - Storage Access APIs are now yielding Promises. This is to allow more limited storage platforms to be supported. ### R&D - Browser-version of Self-hosted LiveSync is now in development. This is not intended for public use now, but I will eventually make it available for testing. - We can see the code in `src/apps/webapp` for the browser version.
120 lines
5.0 KiB
TypeScript
120 lines
5.0 KiB
TypeScript
import { expect } from "vitest";
|
|
import { waitForIdle, type LiveSyncHarness } from "../harness/harness";
|
|
import { LOG_LEVEL_INFO, RemoteTypes, type ObsidianLiveSyncSettings } from "@/lib/src/common/types";
|
|
|
|
import { delay, fireAndForget } from "@/lib/src/common/utils";
|
|
import { commands } from "vitest/browser";
|
|
import { LiveSyncTrysteroReplicator } from "@/lib/src/replication/trystero/LiveSyncTrysteroReplicator";
|
|
import { waitTaskWithFollowups } from "../lib/util";
|
|
async function waitForP2PPeers(harness: LiveSyncHarness) {
|
|
if (harness.plugin.core.settings.remoteType === RemoteTypes.REMOTE_P2P) {
|
|
// Wait for peers to connect
|
|
const maxRetries = 20;
|
|
let retries = maxRetries;
|
|
const replicator = await harness.plugin.core.services.replicator.getActiveReplicator();
|
|
if (!(replicator instanceof LiveSyncTrysteroReplicator)) {
|
|
throw new Error("Replicator is not an instance of LiveSyncTrysteroReplicator");
|
|
}
|
|
const p2pReplicator = await replicator.getP2PConnection(LOG_LEVEL_INFO);
|
|
if (!p2pReplicator) {
|
|
throw new Error("P2P Replicator is not initialized");
|
|
}
|
|
while (retries-- > 0) {
|
|
fireAndForget(() => commands.acceptWebPeer());
|
|
await delay(1000);
|
|
const peers = p2pReplicator.knownAdvertisements;
|
|
|
|
if (peers && peers.length > 0) {
|
|
console.log("P2P peers connected:", peers);
|
|
return;
|
|
}
|
|
fireAndForget(() => commands.acceptWebPeer());
|
|
console.log(`Waiting for any P2P peers to be connected... ${maxRetries - retries}/${maxRetries}`);
|
|
console.dir(peers);
|
|
await delay(1000);
|
|
}
|
|
console.log("Failed to connect P2P peers after retries");
|
|
throw new Error("P2P peers did not connect in time.");
|
|
}
|
|
}
|
|
export async function closeP2PReplicatorConnections(harness: LiveSyncHarness) {
|
|
if (harness.plugin.core.settings.remoteType === RemoteTypes.REMOTE_P2P) {
|
|
const replicator = await harness.plugin.core.services.replicator.getActiveReplicator();
|
|
if (!(replicator instanceof LiveSyncTrysteroReplicator)) {
|
|
throw new Error("Replicator is not an instance of LiveSyncTrysteroReplicator");
|
|
}
|
|
replicator.closeReplication();
|
|
await delay(30);
|
|
replicator.closeReplication();
|
|
await delay(1000);
|
|
console.log("P2P replicator connections closed");
|
|
// if (replicator instanceof LiveSyncTrysteroReplicator) {
|
|
// replicator.closeReplication();
|
|
// await delay(1000);
|
|
// }
|
|
}
|
|
}
|
|
|
|
export async function performReplication(harness: LiveSyncHarness) {
|
|
await waitForP2PPeers(harness);
|
|
await delay(500);
|
|
const p = harness.plugin.core.services.replication.replicate(true);
|
|
const task =
|
|
harness.plugin.core.settings.remoteType === RemoteTypes.REMOTE_P2P
|
|
? waitTaskWithFollowups(
|
|
p,
|
|
() => {
|
|
// Accept any peer dialogs during replication (fire and forget)
|
|
fireAndForget(() => commands.acceptWebPeer());
|
|
return Promise.resolve();
|
|
},
|
|
30000,
|
|
500
|
|
)
|
|
: p;
|
|
const result = await task;
|
|
// await waitForIdle(harness);
|
|
// if (harness.plugin.core.settings.remoteType === RemoteTypes.REMOTE_P2P) {
|
|
// await closeP2PReplicatorConnections(harness);
|
|
// }
|
|
return result;
|
|
}
|
|
|
|
export async function closeReplication(harness: LiveSyncHarness) {
|
|
if (harness.plugin.core.settings.remoteType === RemoteTypes.REMOTE_P2P) {
|
|
return await closeP2PReplicatorConnections(harness);
|
|
}
|
|
const replicator = await harness.plugin.core.services.replicator.getActiveReplicator();
|
|
if (!replicator) {
|
|
console.log("No active replicator to close");
|
|
return;
|
|
}
|
|
await replicator.closeReplication();
|
|
await waitForIdle(harness);
|
|
console.log("Replication closed");
|
|
}
|
|
|
|
export async function prepareRemote(harness: LiveSyncHarness, setting: ObsidianLiveSyncSettings, shouldReset = false) {
|
|
if (setting.remoteType !== RemoteTypes.REMOTE_P2P) {
|
|
if (shouldReset) {
|
|
await delay(1000);
|
|
await harness.plugin.core.services.replicator
|
|
.getActiveReplicator()
|
|
?.tryResetRemoteDatabase(harness.plugin.core.settings);
|
|
} else {
|
|
await harness.plugin.core.services.replicator
|
|
.getActiveReplicator()
|
|
?.tryCreateRemoteDatabase(harness.plugin.core.settings);
|
|
}
|
|
await harness.plugin.core.services.replicator
|
|
.getActiveReplicator()
|
|
?.markRemoteResolved(harness.plugin.core.settings);
|
|
// No exceptions should be thrown
|
|
const status = await harness.plugin.core.services.replicator
|
|
.getActiveReplicator()
|
|
?.getRemoteStatus(harness.plugin.core.settings);
|
|
console.log("Remote status:", status);
|
|
expect(status).not.toBeFalsy();
|
|
}
|
|
}
|