This commit is contained in:
Andras Schmelczer 2025-03-22 20:50:43 +00:00
parent 62427183fd
commit a8cadd1e53
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
4 changed files with 27 additions and 24 deletions

View file

@ -7,7 +7,7 @@ export {
export { Logger, LogLevel, LogLine } from "./tracing/logger";
export type { CheckConnectionResult } from "./services/sync-service";
export { type SyncSettings } from "./persistence/settings";
export type { RelativePath } from "./persistence/database";
export type { RelativePath, StoredDatabase } from "./persistence/database";
export type { FileSystemOperations } from "./file-operations/filesystem-operations";
export type { PersistenceProvider } from "./persistence/persistence";

View file

@ -173,8 +173,7 @@ export class SyncClient {
}
public async waitAndStop(): Promise<void> {
await this.syncer.applyRemoteChangesLocally();
await this.syncer.waitForSyncQueue();
await this.syncer.waitUntilFinished();
this.stop();
}

View file

@ -1,7 +1,6 @@
import type { Database, RelativePath } from "../persistence/database";
import type { SyncService } from "../services/sync-service";
import type { Logger } from "../tracing/logger";
import type { SyncHistory } from "../tracing/sync-history";
import PQueue from "p-queue";
import { hash } from "../utils/hash";
import { v4 as uuidv4 } from "uuid";
@ -9,7 +8,7 @@ import type { components } from "../services/types";
import type { Settings } from "../persistence/settings";
import type { FileOperations } from "../file-operations/file-operations";
import { findMatchingFile } from "../utils/find-matching-file";
import { UnrestrictedSyncer } from "./unrestricted-syncer";
import type { UnrestrictedSyncer } from "./unrestricted-syncer";
import { createPromise } from "../utils/create-promise";
import { SyncResetError } from "../services/sync-reset-error";
@ -17,21 +16,18 @@ export class Syncer {
private readonly remainingOperationsListeners: ((
remainingOperations: number
) => void)[] = [];
private readonly syncQueue: PQueue;
private runningScheduleSyncForOfflineChanges: Promise<void> | undefined;
private runningApplyRemoteChangesLocally: Promise<void> | undefined;
private readonly internalSyncer: UnrestrictedSyncer;
public constructor(
private readonly logger: Logger,
private readonly database: Database,
settings: Settings,
private readonly syncService: SyncService,
private readonly operations: FileOperations,
history: SyncHistory
private readonly internalSyncer: UnrestrictedSyncer
) {
this.syncQueue = new PQueue({
concurrency: settings.getSettings().syncConcurrency
@ -49,15 +45,6 @@ export class Syncer {
listener(this.syncQueue.size);
});
});
this.internalSyncer = new UnrestrictedSyncer(
logger,
database,
settings,
syncService,
operations,
history
);
}
public addRemainingOperationsListener(
@ -246,13 +233,17 @@ export class Syncer {
}
}
public async waitForSyncQueue(): Promise<void> {
return this.syncQueue.onEmpty();
public async reset(): Promise<void> {
await this.waitUntilFinished();
this.internalSyncer.reset();
}
public async reset(): Promise<void> {
await this.syncQueue.onEmpty();
this.internalSyncer.reset();
public async waitUntilFinished(): Promise<void> {
await Promise.allSettled([
this.runningScheduleSyncForOfflineChanges,
this.runningApplyRemoteChangesLocally
]);
return this.syncQueue.onEmpty();
}
private async internalApplyRemoteChangesLocally(): Promise<void> {

View file

@ -1,3 +1,4 @@
import type { StoredDatabase } from "sync-client/dist/types/persistence/database";
import { assert } from "../utils/assert";
import {
type RelativePath,
@ -9,7 +10,17 @@ import {
export class MockClient implements FileSystemOperations {
protected readonly localFiles = new Map<string, Uint8Array>();
protected client!: SyncClient;
protected data: object | undefined = undefined;
protected data: Partial<{
settings: Partial<SyncSettings>;
database: Partial<StoredDatabase>;
}> = {
database: {
// Assume all clients start at the same time so there's no need to fetch
// any shared state.
hasInitialSyncCompleted: true
}
};
public constructor(
private readonly initialSettings: Partial<SyncSettings>,
@ -37,6 +48,8 @@ export class MockClient implements FileSystemOperations {
);
})
);
await this.client.start();
}
public async listAllFiles(): Promise<RelativePath[]> {