Clean up API
This commit is contained in:
parent
e7ec41eafe
commit
8a9f87cc05
2 changed files with 81 additions and 42 deletions
|
|
@ -62,6 +62,7 @@ export default class VaultLinkPlugin extends Plugin {
|
||||||
|
|
||||||
this.app.workspace.onLayoutReady(async () => {
|
this.app.workspace.onLayoutReady(async () => {
|
||||||
this.client.logger.info("Initialising sync handlers");
|
this.client.logger.info("Initialising sync handlers");
|
||||||
|
|
||||||
[
|
[
|
||||||
this.app.vault.on(
|
this.app.vault.on(
|
||||||
"create",
|
"create",
|
||||||
|
|
@ -83,9 +84,9 @@ export default class VaultLinkPlugin extends Plugin {
|
||||||
this.registerEvent(event);
|
this.registerEvent(event);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.client.logger.info("Sync handlers initialised");
|
void this.client.start();
|
||||||
|
|
||||||
void this.client.syncer.scheduleSyncForOfflineChanges();
|
this.client.logger.info("Sync handlers initialised");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,11 @@
|
||||||
import init from "sync_lib";
|
import initWasm from "sync_lib";
|
||||||
import wasmBin from "../../../backend/sync_lib/pkg/sync_lib_bg.wasm";
|
import wasmBin from "../../../backend/sync_lib/pkg/sync_lib_bg.wasm";
|
||||||
import type { PersistenceProvider } from "./persistence/persistence";
|
import type { PersistenceProvider } from "./persistence/persistence";
|
||||||
import { SyncHistory } from "./tracing/sync-history";
|
import {
|
||||||
|
HistoryEntry,
|
||||||
|
HistoryStats,
|
||||||
|
SyncHistory
|
||||||
|
} from "./tracing/sync-history";
|
||||||
import { Logger } from "./tracing/logger";
|
import { Logger } from "./tracing/logger";
|
||||||
import type { StoredDatabase } from "./persistence/database";
|
import type { StoredDatabase } from "./persistence/database";
|
||||||
import { Database } from "./persistence/database";
|
import { Database } from "./persistence/database";
|
||||||
|
|
@ -12,7 +16,7 @@ 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 { ConnectedState } from "./services/connected-state";
|
import { ConnectionStatus } from "./services/connection-status";
|
||||||
|
|
||||||
export class SyncClient {
|
export class SyncClient {
|
||||||
private remoteListenerIntervalId: NodeJS.Timeout | null = null;
|
private remoteListenerIntervalId: NodeJS.Timeout | null = null;
|
||||||
|
|
@ -23,17 +27,10 @@ export class SyncClient {
|
||||||
private readonly _database: Database,
|
private readonly _database: Database,
|
||||||
private readonly _syncer: Syncer,
|
private readonly _syncer: Syncer,
|
||||||
private readonly _syncService: SyncService,
|
private readonly _syncService: SyncService,
|
||||||
private readonly _logger: Logger
|
private readonly _logger: Logger,
|
||||||
|
private readonly _connectionStatus: ConnectionStatus
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public get history(): SyncHistory {
|
|
||||||
return this._history;
|
|
||||||
}
|
|
||||||
|
|
||||||
public get settings(): Settings {
|
|
||||||
return this._settings;
|
|
||||||
}
|
|
||||||
|
|
||||||
public get syncer(): Syncer {
|
public get syncer(): Syncer {
|
||||||
return this._syncer;
|
return this._syncer;
|
||||||
}
|
}
|
||||||
|
|
@ -46,10 +43,6 @@ export class SyncClient {
|
||||||
return this._database.length;
|
return this._database.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
public set fetchImplementation(fetch: typeof globalThis.fetch) {
|
|
||||||
this._syncService.fetchImplementation = fetch;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static async create(
|
public static async create(
|
||||||
fs: FileSystemOperations,
|
fs: FileSystemOperations,
|
||||||
persistence: PersistenceProvider<
|
persistence: PersistenceProvider<
|
||||||
|
|
@ -57,13 +50,15 @@ export class SyncClient {
|
||||||
settings: Partial<SyncSettings>;
|
settings: Partial<SyncSettings>;
|
||||||
database: Partial<StoredDatabase>;
|
database: Partial<StoredDatabase>;
|
||||||
}>
|
}>
|
||||||
>
|
>,
|
||||||
|
fetch: typeof globalThis.fetch = globalThis.fetch
|
||||||
): Promise<SyncClient> {
|
): Promise<SyncClient> {
|
||||||
const logger = new Logger();
|
const logger = new Logger();
|
||||||
const history = new SyncHistory(logger);
|
|
||||||
logger.info("Starting SyncClient");
|
logger.info("Starting SyncClient");
|
||||||
|
|
||||||
await init(
|
const history = new SyncHistory(logger);
|
||||||
|
|
||||||
|
await initWasm(
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
(wasmBin as any).default // it is loaded as a base64 string by webpack
|
(wasmBin as any).default // it is loaded as a base64 string by webpack
|
||||||
);
|
);
|
||||||
|
|
@ -91,10 +86,9 @@ export class SyncClient {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
const connectedState = new ConnectedState(settings, logger);
|
const connectionStatus = new ConnectionStatus(settings, logger);
|
||||||
|
const syncService = new SyncService(connectionStatus, settings, logger);
|
||||||
const syncService = new SyncService(connectedState, settings, logger);
|
syncService.fetchImplementation = fetch;
|
||||||
|
|
||||||
const syncer = new Syncer(
|
const syncer = new Syncer(
|
||||||
logger,
|
logger,
|
||||||
database,
|
database,
|
||||||
|
|
@ -110,13 +104,8 @@ export class SyncClient {
|
||||||
database,
|
database,
|
||||||
syncer,
|
syncer,
|
||||||
syncService,
|
syncService,
|
||||||
logger
|
logger,
|
||||||
);
|
connectionStatus
|
||||||
|
|
||||||
void syncer.scheduleSyncForOfflineChanges();
|
|
||||||
|
|
||||||
client.registerRemoteEventListener(
|
|
||||||
settings.getSettings().fetchChangesUpdateIntervalMs
|
|
||||||
);
|
);
|
||||||
|
|
||||||
settings.addOnSettingsChangeHandlers((newSettings, oldSettings) => {
|
settings.addOnSettingsChangeHandlers((newSettings, oldSettings) => {
|
||||||
|
|
@ -124,13 +113,21 @@ export class SyncClient {
|
||||||
newSettings.fetchChangesUpdateIntervalMs !==
|
newSettings.fetchChangesUpdateIntervalMs !==
|
||||||
oldSettings.fetchChangesUpdateIntervalMs
|
oldSettings.fetchChangesUpdateIntervalMs
|
||||||
) {
|
) {
|
||||||
client.registerRemoteEventListener(
|
client.setRemoteEventListener(
|
||||||
newSettings.fetchChangesUpdateIntervalMs
|
newSettings.fetchChangesUpdateIntervalMs
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
newSettings.vaultName !== oldSettings.vaultName ||
|
||||||
|
newSettings.token !== oldSettings.token ||
|
||||||
|
newSettings.remoteUri !== oldSettings.remoteUri
|
||||||
|
) {
|
||||||
|
client.reset();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
logger.info("SyncClient loaded");
|
logger.info("SyncClient initialised");
|
||||||
|
|
||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
|
|
@ -139,25 +136,60 @@ export class SyncClient {
|
||||||
return this._syncService.checkConnection();
|
return this._syncService.checkConnection();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public getHistoryEntries(): HistoryEntry[] {
|
||||||
|
return this._history.getEntries();
|
||||||
|
}
|
||||||
|
|
||||||
|
public addSyncHistoryUpdateListener(
|
||||||
|
listener: (stats: HistoryStats) => void
|
||||||
|
): void {
|
||||||
|
this._history.addSyncHistoryUpdateListener(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async start(): Promise<void> {
|
||||||
|
await this._syncer.scheduleSyncForOfflineChanges();
|
||||||
|
|
||||||
|
this.setRemoteEventListener(
|
||||||
|
this._settings.getSettings().fetchChangesUpdateIntervalMs
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Clear all global state that has been touched by SyncClient.
|
||||||
|
public stop(): void {
|
||||||
|
this.unsetRemoteEventListener();
|
||||||
|
}
|
||||||
|
|
||||||
/// 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> {
|
public async reset(): Promise<void> {
|
||||||
this.stop();
|
this.stop();
|
||||||
|
this._connectionStatus.reset();
|
||||||
await this._syncer.reset();
|
await this._syncer.reset();
|
||||||
this._history.reset();
|
this._history.reset();
|
||||||
this._database.resetSyncState();
|
this._database.reset();
|
||||||
this.logger.reset();
|
this._logger.reset();
|
||||||
|
void this.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Clear all global state that has been touched by SyncClient.
|
public getSettings(): SyncSettings {
|
||||||
public stop(): void {
|
return this._settings.getSettings();
|
||||||
if (this.remoteListenerIntervalId !== null) {
|
|
||||||
clearInterval(this.remoteListenerIntervalId);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private registerRemoteEventListener(intervalMs: number): void {
|
public async setSetting<T extends keyof SyncSettings>(
|
||||||
|
key: T,
|
||||||
|
value: SyncSettings[T]
|
||||||
|
): Promise<void> {
|
||||||
|
await this._settings.setSetting(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public addOnSettingsChangeHandlers(
|
||||||
|
handler: (settings: SyncSettings, oldSettings: SyncSettings) => void
|
||||||
|
): void {
|
||||||
|
this._settings.addOnSettingsChangeHandlers(handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
private setRemoteEventListener(intervalMs: number): void {
|
||||||
if (this.remoteListenerIntervalId !== null) {
|
if (this.remoteListenerIntervalId !== null) {
|
||||||
clearInterval(this.remoteListenerIntervalId);
|
clearInterval(this.remoteListenerIntervalId);
|
||||||
}
|
}
|
||||||
|
|
@ -167,4 +199,10 @@ export class SyncClient {
|
||||||
intervalMs
|
intervalMs
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private unsetRemoteEventListener(): void {
|
||||||
|
if (this.remoteListenerIntervalId !== null) {
|
||||||
|
clearInterval(this.remoteListenerIntervalId);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue