1
0

Fix sync with silverbullet.md

This commit is contained in:
Zef Hemel 2023-01-20 16:51:06 +01:00
parent 2577a2db32
commit a3fb2ffe92
3 changed files with 16 additions and 32 deletions

View File

@ -93,10 +93,11 @@ export class SpaceSync {
logger: Logger, logger: Logger,
) => Promise<number>, ) => Promise<number>,
): Promise<number> { ): Promise<number> {
console.log("Syncing", name, primaryHash, secondaryHash);
let operations = 0; let operations = 0;
if ( if (
primaryHash && !secondaryHash && primaryHash !== undefined && secondaryHash === undefined &&
!this.snapshot.has(name) !this.snapshot.has(name)
) { ) {
// New file, created on primary, copy from primary to secondary // New file, created on primary, copy from primary to secondary
@ -117,7 +118,7 @@ export class SpaceSync {
]); ]);
operations++; operations++;
} else if ( } else if (
secondaryHash && !primaryHash && secondaryHash !== undefined && primaryHash === undefined &&
!this.snapshot.has(name) !this.snapshot.has(name)
) { ) {
// New file, created on secondary, copy from secondary to primary // New file, created on secondary, copy from secondary to primary
@ -138,8 +139,8 @@ export class SpaceSync {
]); ]);
operations++; operations++;
} else if ( } else if (
primaryHash && this.snapshot.has(name) && primaryHash !== undefined && this.snapshot.has(name) &&
!secondaryHash secondaryHash === undefined
) { ) {
// File deleted on B // File deleted on B
this.logger.log( this.logger.log(
@ -151,8 +152,8 @@ export class SpaceSync {
this.snapshot.delete(name); this.snapshot.delete(name);
operations++; operations++;
} else if ( } else if (
secondaryHash && this.snapshot.has(name) && secondaryHash !== undefined && this.snapshot.has(name) &&
!primaryHash primaryHash === undefined
) { ) {
// File deleted on A // File deleted on A
this.logger.log( this.logger.log(
@ -164,8 +165,8 @@ export class SpaceSync {
this.snapshot.delete(name); this.snapshot.delete(name);
operations++; operations++;
} else if ( } else if (
this.snapshot.has(name) && !primaryHash && this.snapshot.has(name) && primaryHash === undefined &&
!secondaryHash secondaryHash === undefined
) { ) {
// File deleted on both sides, :shrug: // File deleted on both sides, :shrug:
this.logger.log( this.logger.log(
@ -176,7 +177,7 @@ export class SpaceSync {
this.snapshot.delete(name); this.snapshot.delete(name);
operations++; operations++;
} else if ( } else if (
primaryHash && secondaryHash && primaryHash !== undefined && secondaryHash !== undefined &&
this.snapshot.get(name) && this.snapshot.get(name) &&
primaryHash !== this.snapshot.get(name)![0] && primaryHash !== this.snapshot.get(name)![0] &&
secondaryHash === this.snapshot.get(name)![1] secondaryHash === this.snapshot.get(name)![1]
@ -199,7 +200,7 @@ export class SpaceSync {
]); ]);
operations++; operations++;
} else if ( } else if (
primaryHash && secondaryHash && primaryHash !== undefined && secondaryHash !== undefined &&
this.snapshot.get(name) && this.snapshot.get(name) &&
secondaryHash !== this.snapshot.get(name)![1] && secondaryHash !== this.snapshot.get(name)![1] &&
primaryHash === this.snapshot.get(name)![0] primaryHash === this.snapshot.get(name)![0]
@ -218,7 +219,7 @@ export class SpaceSync {
operations++; operations++;
} else if ( } else if (
( // File changed on both ends, but we don't have any info in the snapshot (resync scenario?): have to run through conflict handling ( // File changed on both ends, but we don't have any info in the snapshot (resync scenario?): have to run through conflict handling
primaryHash && secondaryHash && primaryHash !== undefined && secondaryHash !== undefined &&
!this.snapshot.has(name) !this.snapshot.has(name)
) || ) ||
( // File changed on both ends, CONFLICT! ( // File changed on both ends, CONFLICT!

View File

@ -59,9 +59,7 @@ export function syncSyscalls(
const localHash = (await localSpace.getFileMeta(name)).lastModified; const localHash = (await localSpace.getFileMeta(name)).lastModified;
let remoteHash: number | undefined = undefined; let remoteHash: number | undefined = undefined;
try { try {
remoteHash = remoteHash = (await remoteSpace.getFileMeta(name)).lastModified;
(await race([remoteSpace.getFileMeta(name), timeout(1000)]))
.lastModified;
} catch (e: any) { } catch (e: any) {
if (e.message.includes("File not found")) { if (e.message.includes("File not found")) {
// File doesn't exist remotely, that's ok // File doesn't exist remotely, that's ok
@ -95,12 +93,9 @@ export function syncSyscalls(
endpoint.user, endpoint.user,
endpoint.password, endpoint.password,
); );
// Let's just fetch metadata for the SETTINGS.md file (which should always exist) // Let's just fetch the file list and see if it works
try { try {
await race([ await syncSpace.fetchFileList();
syncSpace.getFileMeta("SETTINGS.md"),
timeout(2000),
]);
} catch (e: any) { } catch (e: any) {
console.error("Sync check failure", e.message); console.error("Sync check failure", e.message);
throw e; throw e;

View File

@ -1,11 +1,5 @@
import { store } from "$sb/plugos-syscall/mod.ts"; import { store } from "$sb/plugos-syscall/mod.ts";
import { import { editor, space, sync, system } from "$sb/silverbullet-syscall/mod.ts";
editor,
index,
space,
sync,
system,
} from "$sb/silverbullet-syscall/mod.ts";
import type { SyncEndpoint } from "$sb/silverbullet-syscall/sync.ts"; import type { SyncEndpoint } from "$sb/silverbullet-syscall/sync.ts";
export async function configureCommand() { export async function configureCommand() {
@ -136,12 +130,6 @@ export async function performSync() {
return; return;
} }
try {
await sync.check(config);
} catch (e: any) {
console.error("Sync check failed", e.message);
return;
}
// Check if sync not already in progress // Check if sync not already in progress
const ongoingSync: number | undefined = await store.get("sync.startTime"); const ongoingSync: number | undefined = await store.get("sync.startTime");
if (ongoingSync) { if (ongoingSync) {