Use updated APIs
This commit is contained in:
parent
213a9e18fb
commit
3cdd2a4387
1 changed files with 116 additions and 75 deletions
|
|
@ -1,16 +1,17 @@
|
||||||
import type { PersistenceProvider } from "./persistence/persistence";
|
import type { PersistenceProvider } from "./persistence/persistence";
|
||||||
import type { HistoryEntry, HistoryStats } from "./tracing/sync-history";
|
import type { HistoryEntry, HistoryStats } from "./tracing/sync-history";
|
||||||
import { SyncHistory } from "./tracing/sync-history";
|
import { SyncHistory } from "./tracing/sync-history";
|
||||||
import { Logger } from "./tracing/logger";
|
import { Logger, LogLevel, LogLine } from "./tracing/logger";
|
||||||
import type { RelativePath, StoredDatabase } from "./persistence/database";
|
import type { RelativePath, StoredDatabase } from "./persistence/database";
|
||||||
import { Database } from "./persistence/database";
|
import { Database } from "./persistence/database";
|
||||||
|
import * as Sentry from "@sentry/browser";
|
||||||
import type { SyncSettings } from "./persistence/settings";
|
import type { SyncSettings } from "./persistence/settings";
|
||||||
import { Settings } from "./persistence/settings";
|
import { DEFAULT_SETTINGS, Settings } from "./persistence/settings";
|
||||||
import { SyncService } from "./services/sync-service";
|
import { SyncService } from "./services/sync-service";
|
||||||
import { Syncer } from "./sync-operations/syncer";
|
import { Syncer } from "./sync-operations/syncer";
|
||||||
import type { FileSystemOperations } from "./file-operations/filesystem-operations";
|
import type { FileSystemOperations } from "./file-operations/filesystem-operations";
|
||||||
import { FileOperations } from "./file-operations/file-operations";
|
import { FileOperations } from "./file-operations/file-operations";
|
||||||
import { ConnectionStatus } from "./services/connection-status";
|
import { FetchController } from "./services/fetch-controller";
|
||||||
import { UnrestrictedSyncer } from "./sync-operations/unrestricted-syncer";
|
import { UnrestrictedSyncer } from "./sync-operations/unrestricted-syncer";
|
||||||
import { rateLimit } from "./utils/rate-limit";
|
import { rateLimit } from "./utils/rate-limit";
|
||||||
import type { NetworkConnectionStatus } from "./types/network-connection-status";
|
import type { NetworkConnectionStatus } from "./types/network-connection-status";
|
||||||
|
|
@ -23,9 +24,9 @@ import type { MaybeOutdatedClientCursors } from "./types/maybe-outdated-client-c
|
||||||
import { FileChangeNotifier } from "./sync-operations/file-change-notifier";
|
import { FileChangeNotifier } from "./sync-operations/file-change-notifier";
|
||||||
import { FixedSizeDocumentCache } from "./utils/data-structures/fix-sized-cache";
|
import { FixedSizeDocumentCache } from "./utils/data-structures/fix-sized-cache";
|
||||||
import { setUpTelemetry } from "./utils/set-up-telemetry";
|
import { setUpTelemetry } from "./utils/set-up-telemetry";
|
||||||
|
import { DIFF_CACHE_SIZE_MB, MINIMUM_SAVE_INTERVAL_MS } from "./consts";
|
||||||
|
|
||||||
export class SyncClient {
|
export class SyncClient {
|
||||||
private static readonly MINIMUM_SAVE_INTERVAL_MS = 1000;
|
|
||||||
private hasStartedOfflineSync = false;
|
private hasStartedOfflineSync = false;
|
||||||
private hasFinishedOfflineSync = false;
|
private hasFinishedOfflineSync = false;
|
||||||
private unloadTelemetry?: () => void;
|
private unloadTelemetry?: () => void;
|
||||||
|
|
@ -37,53 +38,84 @@ export class SyncClient {
|
||||||
private readonly syncer: Syncer,
|
private readonly syncer: Syncer,
|
||||||
private readonly syncService: SyncService,
|
private readonly syncService: SyncService,
|
||||||
private readonly webSocketManager: WebSocketManager,
|
private readonly webSocketManager: WebSocketManager,
|
||||||
private readonly _logger: Logger,
|
public readonly logger: Logger,
|
||||||
private readonly connectionStatus: ConnectionStatus,
|
private readonly fetchController: FetchController,
|
||||||
private readonly cursorTracker: CursorTracker,
|
private readonly cursorTracker: CursorTracker,
|
||||||
private readonly fileChangeNotifier: FileChangeNotifier,
|
private readonly fileChangeNotifier: FileChangeNotifier,
|
||||||
private readonly contentCache: FixedSizeDocumentCache
|
private readonly contentCache: FixedSizeDocumentCache,
|
||||||
) {
|
private readonly persistence: PersistenceProvider<
|
||||||
if (settings.getSettings().enableTelemetry) {
|
Partial<{
|
||||||
|
settings: Partial<SyncSettings>;
|
||||||
|
database: Partial<StoredDatabase>;
|
||||||
|
}>
|
||||||
|
>
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public async start(): Promise<void> {
|
||||||
|
if (this.settings.getSettings().enableTelemetry) {
|
||||||
this.unloadTelemetry = setUpTelemetry();
|
this.unloadTelemetry = setUpTelemetry();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.settings.addOnSettingsChangeListener(
|
this.logger.addOnMessageListener((log): void => {
|
||||||
async (newSettings, oldSettings) => {
|
if (log.level === LogLevel.ERROR && Sentry.isInitialized()) {
|
||||||
if (newSettings.vaultName !== oldSettings.vaultName) {
|
Sentry.captureMessage(log.message);
|
||||||
await this.reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (newSettings.isSyncEnabled !== oldSettings.isSyncEnabled) {
|
|
||||||
if (newSettings.isSyncEnabled) {
|
|
||||||
await this.start();
|
|
||||||
} else {
|
|
||||||
this.stop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
newSettings.diffCacheSizeMB !== oldSettings.diffCacheSizeMB
|
|
||||||
) {
|
|
||||||
this.contentCache.resize(
|
|
||||||
newSettings.diffCacheSizeMB * 1024 * 1024
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
newSettings.enableTelemetry !== oldSettings.enableTelemetry
|
|
||||||
) {
|
|
||||||
if (newSettings.enableTelemetry) {
|
|
||||||
this.unloadTelemetry = setUpTelemetry();
|
|
||||||
} else {
|
|
||||||
this.unloadTelemetry?.();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
this.settings.addOnSettingsChangeListener(
|
||||||
|
this.onSettingsChange.bind(this)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (this.settings.getSettings().isSyncEnabled) {
|
||||||
|
this.logger.info("Starting SyncClient");
|
||||||
|
await this.startSyncing();
|
||||||
|
this.logger.info("SyncClient has successfully started");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public get logger(): Logger {
|
// Reload settings from disk overriding current in-memory settings.
|
||||||
return this._logger;
|
// Missing values will be filled in from DEFAULT_SETTINGS rather than
|
||||||
|
// retaining current in-memory settings.
|
||||||
|
public async reloadSettings(): Promise<void> {
|
||||||
|
let state = (await this.persistence.load()) ?? {
|
||||||
|
settings: undefined
|
||||||
|
};
|
||||||
|
|
||||||
|
const settings = {
|
||||||
|
...DEFAULT_SETTINGS,
|
||||||
|
...(state.settings ?? {})
|
||||||
|
};
|
||||||
|
|
||||||
|
this.setSettings(settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async onSettingsChange(
|
||||||
|
newSettings: SyncSettings,
|
||||||
|
oldSettings: SyncSettings
|
||||||
|
): Promise<void> {
|
||||||
|
if (newSettings.vaultName !== oldSettings.vaultName) {
|
||||||
|
await this.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (newSettings.isSyncEnabled !== oldSettings.isSyncEnabled) {
|
||||||
|
if (newSettings.isSyncEnabled) {
|
||||||
|
await this.startSyncing();
|
||||||
|
} else {
|
||||||
|
this.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (newSettings.diffCacheSizeMB !== oldSettings.diffCacheSizeMB) {
|
||||||
|
this.contentCache.resize(newSettings.diffCacheSizeMB * 1024 * 1024);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (newSettings.enableTelemetry !== oldSettings.enableTelemetry) {
|
||||||
|
if (newSettings.enableTelemetry) {
|
||||||
|
this.unloadTelemetry = setUpTelemetry();
|
||||||
|
} else {
|
||||||
|
this.unloadTelemetry?.();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public get documentCount(): number {
|
public get documentCount(): number {
|
||||||
|
|
@ -116,7 +148,7 @@ export class SyncClient {
|
||||||
|
|
||||||
const deviceId = createClientId();
|
const deviceId = createClientId();
|
||||||
|
|
||||||
logger.info(`Initialising SyncClient with client id ${deviceId}`);
|
logger.info(`Creating SyncClient with client id ${deviceId}`);
|
||||||
|
|
||||||
const history = new SyncHistory(logger);
|
const history = new SyncHistory(logger);
|
||||||
|
|
||||||
|
|
@ -127,7 +159,7 @@ export class SyncClient {
|
||||||
|
|
||||||
const rateLimitedSave = rateLimit(
|
const rateLimitedSave = rateLimit(
|
||||||
persistence.save,
|
persistence.save,
|
||||||
SyncClient.MINIMUM_SAVE_INTERVAL_MS
|
MINIMUM_SAVE_INTERVAL_MS
|
||||||
);
|
);
|
||||||
|
|
||||||
const database = new Database(
|
const database = new Database(
|
||||||
|
|
@ -148,19 +180,19 @@ export class SyncClient {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
const connectionStatus = new FetchController(
|
const fetchController = new FetchController(
|
||||||
settings.getSettings().isSyncEnabled,
|
settings.getSettings().isSyncEnabled,
|
||||||
logger
|
logger
|
||||||
);
|
);
|
||||||
settings.addOnSettingsChangeListener((newSettings, oldSettings) => {
|
settings.addOnSettingsChangeListener((newSettings, oldSettings) => {
|
||||||
if (oldSettings.isSyncEnabled != newSettings.isSyncEnabled) {
|
if (oldSettings.isSyncEnabled != newSettings.isSyncEnabled) {
|
||||||
connectionStatus.canFetch = newSettings.isSyncEnabled;
|
fetchController.canFetch = newSettings.isSyncEnabled;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const syncService = new SyncService(
|
const syncService = new SyncService(
|
||||||
deviceId,
|
deviceId,
|
||||||
connectionStatus,
|
fetchController,
|
||||||
settings,
|
settings,
|
||||||
logger,
|
logger,
|
||||||
fetch
|
fetch
|
||||||
|
|
@ -173,7 +205,9 @@ export class SyncClient {
|
||||||
nativeLineEndings
|
nativeLineEndings
|
||||||
);
|
);
|
||||||
|
|
||||||
const contentCache = new FixedSizeDocumentCache(1024 * 1024 * 2); // 2 MB cache
|
const contentCache = new FixedSizeDocumentCache(
|
||||||
|
1024 * 1024 * DIFF_CACHE_SIZE_MB
|
||||||
|
);
|
||||||
const unrestrictedSyncer = new UnrestrictedSyncer(
|
const unrestrictedSyncer = new UnrestrictedSyncer(
|
||||||
logger,
|
logger,
|
||||||
database,
|
database,
|
||||||
|
|
@ -184,22 +218,22 @@ export class SyncClient {
|
||||||
contentCache
|
contentCache
|
||||||
);
|
);
|
||||||
|
|
||||||
const syncer = new Syncer(
|
const webSocketManager = new WebSocketManager(
|
||||||
|
deviceId,
|
||||||
logger,
|
logger,
|
||||||
database,
|
|
||||||
settings,
|
settings,
|
||||||
syncService,
|
webSocket
|
||||||
fileOperations,
|
|
||||||
unrestrictedSyncer
|
|
||||||
);
|
);
|
||||||
|
|
||||||
const webSocketManager = new WebSocketManager(
|
const syncer = new Syncer(
|
||||||
deviceId,
|
deviceId,
|
||||||
logger,
|
logger,
|
||||||
database,
|
database,
|
||||||
settings,
|
settings,
|
||||||
syncer,
|
syncService,
|
||||||
webSocket
|
webSocketManager,
|
||||||
|
fileOperations,
|
||||||
|
unrestrictedSyncer
|
||||||
);
|
);
|
||||||
|
|
||||||
const fileChangeNotifier = new FileChangeNotifier();
|
const fileChangeNotifier = new FileChangeNotifier();
|
||||||
|
|
@ -217,13 +251,14 @@ export class SyncClient {
|
||||||
syncService,
|
syncService,
|
||||||
webSocketManager,
|
webSocketManager,
|
||||||
logger,
|
logger,
|
||||||
connectionStatus,
|
fetchController,
|
||||||
cursorTracker,
|
cursorTracker,
|
||||||
fileChangeNotifier,
|
fileChangeNotifier,
|
||||||
contentCache
|
contentCache,
|
||||||
|
persistence
|
||||||
);
|
);
|
||||||
|
|
||||||
logger.info("SyncClient initialised");
|
logger.info("SyncClient created successfully");
|
||||||
|
|
||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
|
|
@ -247,39 +282,48 @@ export class SyncClient {
|
||||||
this.history.addSyncHistoryUpdateListener(listener);
|
this.history.addSyncHistoryUpdateListener(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async start(): Promise<void> {
|
private async startSyncing(): Promise<void> {
|
||||||
if (!this.hasStartedOfflineSync) {
|
if (!this.hasStartedOfflineSync) {
|
||||||
await this.syncer.scheduleSyncForOfflineChanges();
|
|
||||||
this.hasStartedOfflineSync = true;
|
this.hasStartedOfflineSync = true;
|
||||||
|
await this.syncer.scheduleSyncForOfflineChanges();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.hasFinishedOfflineSync = true;
|
this.hasFinishedOfflineSync = true;
|
||||||
this.webSocketManager.start();
|
this.webSocketManager.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public stop(): void {
|
private stop(): void {
|
||||||
this.hasFinishedOfflineSync = false;
|
this.hasFinishedOfflineSync = false;
|
||||||
this.webSocketManager.stop();
|
this.webSocketManager.stop();
|
||||||
|
|
||||||
|
this.unloadTelemetry?.();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async waitAndStop(): Promise<void> {
|
public async waitUntilStopped(): Promise<void> {
|
||||||
this.stop();
|
|
||||||
await this.syncer.waitUntilFinished();
|
await this.syncer.waitUntilFinished();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async applyChangedConnectionSettings(): Promise<void> {
|
||||||
|
this.fetchController.startReset();
|
||||||
|
this.webSocketManager.stop();
|
||||||
|
|
||||||
|
this.webSocketManager.start();
|
||||||
|
this.fetchController.finishReset();
|
||||||
|
}
|
||||||
|
|
||||||
/// Wait for the in-flight operations to finish, reset all tracking,
|
/// Wait for the in-flight operations to finish, reset all tracking,
|
||||||
/// and the local database but retain the settings.
|
/// and the local database but retain the settings.
|
||||||
/// The SyncClient can be used again after calling this method.
|
/// The SyncClient can be used again after calling this method.
|
||||||
public async reset(): Promise<void> {
|
private async reset(): Promise<void> {
|
||||||
this.stop();
|
this.stop();
|
||||||
this.connectionStatus.startReset();
|
this.fetchController.startReset();
|
||||||
this.contentCache.clear();
|
this.contentCache.clear();
|
||||||
await this.syncer.reset();
|
await this.syncer.reset();
|
||||||
this.history.reset();
|
this.history.reset();
|
||||||
this.database.reset();
|
this.database.reset();
|
||||||
this._logger.reset();
|
this.logger.reset();
|
||||||
this.connectionStatus.finishReset();
|
this.fetchController.finishReset();
|
||||||
await this.start();
|
await this.startSyncing();
|
||||||
}
|
}
|
||||||
|
|
||||||
public getSettings(): SyncSettings {
|
public getSettings(): SyncSettings {
|
||||||
|
|
@ -298,9 +342,9 @@ export class SyncClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
public addOnSettingsChangeListener(
|
public addOnSettingsChangeListener(
|
||||||
handler: (settings: SyncSettings, oldSettings: SyncSettings) => unknown
|
listener: (settings: SyncSettings, oldSettings: SyncSettings) => unknown
|
||||||
): void {
|
): void {
|
||||||
this.settings.addOnSettingsChangeListener(handler);
|
this.settings.addOnSettingsChangeListener(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
public addRemainingSyncOperationsListener(
|
public addRemainingSyncOperationsListener(
|
||||||
|
|
@ -348,10 +392,7 @@ export class SyncClient {
|
||||||
return DocumentSyncStatus.SYNCING_IS_DISABLED;
|
return DocumentSyncStatus.SYNCING_IS_DISABLED;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (!this.syncer.isFirstSyncComplete || !this.hasFinishedOfflineSync) {
|
||||||
!this.webSocketManager.isFirstSyncCompleted ||
|
|
||||||
!this.hasFinishedOfflineSync
|
|
||||||
) {
|
|
||||||
return DocumentSyncStatus.SYNCING;
|
return DocumentSyncStatus.SYNCING;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue