mirror of
https://github.com/vrtmrz/obsidian-livesync.git
synced 2026-10-22 01:02:30 +00:00
Improve timing issue
This commit is contained in:
+11
-11
@@ -2,14 +2,14 @@ import { expect } from "vitest";
|
|||||||
import { waitForIdle, type LiveSyncHarness } from "../harness/harness";
|
import { waitForIdle, type LiveSyncHarness } from "../harness/harness";
|
||||||
import { LOG_LEVEL_INFO, RemoteTypes, type ObsidianLiveSyncSettings } from "@/lib/src/common/types";
|
import { LOG_LEVEL_INFO, RemoteTypes, type ObsidianLiveSyncSettings } from "@/lib/src/common/types";
|
||||||
|
|
||||||
import { delay } from "@/lib/src/common/utils";
|
import { delay, fireAndForget } from "@/lib/src/common/utils";
|
||||||
import { commands } from "vitest/browser";
|
import { commands } from "vitest/browser";
|
||||||
import { LiveSyncTrysteroReplicator } from "@/lib/src/replication/trystero/LiveSyncTrysteroReplicator";
|
import { LiveSyncTrysteroReplicator } from "@/lib/src/replication/trystero/LiveSyncTrysteroReplicator";
|
||||||
import { waitTaskWithFollowups } from "../lib/util";
|
import { waitTaskWithFollowups } from "../lib/util";
|
||||||
async function waitForP2PPeers(harness: LiveSyncHarness) {
|
async function waitForP2PPeers(harness: LiveSyncHarness) {
|
||||||
if (harness.plugin.settings.remoteType === RemoteTypes.REMOTE_P2P) {
|
if (harness.plugin.settings.remoteType === RemoteTypes.REMOTE_P2P) {
|
||||||
// Wait for peers to connect
|
// Wait for peers to connect
|
||||||
const maxRetries = 10;
|
const maxRetries = 20;
|
||||||
let retries = maxRetries;
|
let retries = maxRetries;
|
||||||
const replicator = await harness.plugin.services.replicator.getActiveReplicator();
|
const replicator = await harness.plugin.services.replicator.getActiveReplicator();
|
||||||
if (!(replicator instanceof LiveSyncTrysteroReplicator)) {
|
if (!(replicator instanceof LiveSyncTrysteroReplicator)) {
|
||||||
@@ -20,17 +20,18 @@ async function waitForP2PPeers(harness: LiveSyncHarness) {
|
|||||||
throw new Error("P2P Replicator is not initialized");
|
throw new Error("P2P Replicator is not initialized");
|
||||||
}
|
}
|
||||||
while (retries-- > 0) {
|
while (retries-- > 0) {
|
||||||
|
fireAndForget(() => commands.acceptWebPeer());
|
||||||
|
await delay(1000);
|
||||||
const peers = p2pReplicator.knownAdvertisements;
|
const peers = p2pReplicator.knownAdvertisements;
|
||||||
|
|
||||||
if (peers && peers.length > 0) {
|
if (peers && peers.length > 0) {
|
||||||
console.log("P2P peers connected:", peers);
|
console.log("P2P peers connected:", peers);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await commands.acceptWebPeer();
|
fireAndForget(() => commands.acceptWebPeer());
|
||||||
console.log(`Waiting for any P2P peers to be connected... ${maxRetries - retries}/${maxRetries}`);
|
console.log(`Waiting for any P2P peers to be connected... ${maxRetries - retries}/${maxRetries}`);
|
||||||
console.dir(peers);
|
console.dir(peers);
|
||||||
await delay(3000);
|
await delay(1000);
|
||||||
await commands.acceptWebPeer();
|
|
||||||
}
|
}
|
||||||
console.log("Failed to connect P2P peers after retries");
|
console.log("Failed to connect P2P peers after retries");
|
||||||
throw new Error("P2P peers did not connect in time.");
|
throw new Error("P2P peers did not connect in time.");
|
||||||
@@ -45,7 +46,6 @@ export async function closeP2PReplicatorConnections(harness: LiveSyncHarness) {
|
|||||||
replicator.closeReplication();
|
replicator.closeReplication();
|
||||||
await delay(30);
|
await delay(30);
|
||||||
replicator.closeReplication();
|
replicator.closeReplication();
|
||||||
replicator.closeReplication();
|
|
||||||
await delay(1000);
|
await delay(1000);
|
||||||
console.log("P2P replicator connections closed");
|
console.log("P2P replicator connections closed");
|
||||||
// if (replicator instanceof LiveSyncTrysteroReplicator) {
|
// if (replicator instanceof LiveSyncTrysteroReplicator) {
|
||||||
@@ -65,7 +65,7 @@ export async function performReplication(harness: LiveSyncHarness) {
|
|||||||
p,
|
p,
|
||||||
() => {
|
() => {
|
||||||
// Accept any peer dialogs during replication (fire and forget)
|
// Accept any peer dialogs during replication (fire and forget)
|
||||||
void commands.acceptWebPeer();
|
fireAndForget(() => commands.acceptWebPeer());
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
},
|
},
|
||||||
30000,
|
30000,
|
||||||
@@ -73,10 +73,10 @@ export async function performReplication(harness: LiveSyncHarness) {
|
|||||||
)
|
)
|
||||||
: p;
|
: p;
|
||||||
const result = await task;
|
const result = await task;
|
||||||
await waitForIdle(harness);
|
// await waitForIdle(harness);
|
||||||
if (harness.plugin.settings.remoteType === RemoteTypes.REMOTE_P2P) {
|
// if (harness.plugin.settings.remoteType === RemoteTypes.REMOTE_P2P) {
|
||||||
await closeP2PReplicatorConnections(harness);
|
// await closeP2PReplicatorConnections(harness);
|
||||||
}
|
// }
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user