Pick up API changes

This commit is contained in:
Andras Schmelczer 2025-01-02 10:58:47 +00:00
commit fb729b7d89
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
3 changed files with 39 additions and 81 deletions

View file

@ -3,35 +3,24 @@ import { TFile } from "obsidian";
import type { FileEventHandler } from "./file-event-handler"; import type { FileEventHandler } from "./file-event-handler";
import type { SyncService } from "src/services/sync-service"; import type { SyncService } from "src/services/sync-service";
import type { Database } from "src/database/database"; import type { Database } from "src/database/database";
import { syncLocallyDeletedFile } from "src/sync-operations/sync-locally-deleted-file";
import { syncLocallyUpdatedFile } from "src/sync-operations/sync-locally-updated-file";
import type { FileOperations } from "src/file-operations/file-operations"; import type { FileOperations } from "src/file-operations/file-operations";
import { syncLocallyCreatedFile } from "src/sync-operations/sync-locally-created-file";
import { Logger } from "src/tracing/logger"; import { Logger } from "src/tracing/logger";
import type { SyncHistory } from "src/tracing/sync-history"; import type { SyncHistory } from "src/tracing/sync-history";
import { Syncer } from "src/sync-operations/syncer";
export class ObsidianFileEventHandler implements FileEventHandler { export class ObsidianFileEventHandler implements FileEventHandler {
public constructor( public constructor(private readonly syncer: Syncer) {}
private readonly database: Database,
private readonly syncServer: SyncService,
private readonly operations: FileOperations,
private readonly history: SyncHistory
) {}
public async onCreate(file: TAbstractFile): Promise<void> { public async onCreate(file: TAbstractFile): Promise<void> {
if (file instanceof TFile) { if (file instanceof TFile) {
Logger.getInstance().info(`File created: ${file.path}`); Logger.getInstance().info(`File created: ${file.path}`);
await syncLocallyCreatedFile({ await this.syncer.syncLocallyCreatedFile(
database: this.database, file.path,
syncServer: this.syncServer, new Date(file.stat.ctime)
operations: this.operations, );
updateTime: new Date(file.stat.ctime),
relativePath: file.path,
history: this.history,
});
} else { } else {
Logger.getInstance().info(`Folder created: ${file.path}, ignored`); Logger.getInstance().debug(`Folder created: ${file.path}, ignored`);
} }
} }
@ -39,14 +28,9 @@ export class ObsidianFileEventHandler implements FileEventHandler {
if (file instanceof TFile) { if (file instanceof TFile) {
Logger.getInstance().info(`File deleted: ${file.path}`); Logger.getInstance().info(`File deleted: ${file.path}`);
await syncLocallyDeletedFile({ await this.syncer.syncLocallyDeletedFile(file.path);
database: this.database,
syncServer: this.syncServer,
history: this.history,
relativePath: file.path,
});
} else { } else {
Logger.getInstance().info(`Folder deleted: ${file.path}, ignored`); Logger.getInstance().debug(`Folder deleted: ${file.path}, ignored`);
} }
} }
@ -56,17 +40,13 @@ export class ObsidianFileEventHandler implements FileEventHandler {
`File renamed: ${oldPath} -> ${file.path}` `File renamed: ${oldPath} -> ${file.path}`
); );
await syncLocallyUpdatedFile({ await this.syncer.syncLocallyUpdatedFile({
database: this.database,
syncServer: this.syncServer,
operations: this.operations,
history: this.history,
updateTime: new Date(file.stat.ctime),
relativePath: file.path,
oldPath, oldPath,
relativePath: file.path,
updateTime: new Date(file.stat.ctime),
}); });
} else { } else {
Logger.getInstance().info( Logger.getInstance().debug(
`Folder renamed: ${oldPath} -> ${file.path}, ignored` `Folder renamed: ${oldPath} -> ${file.path}, ignored`
); );
} }
@ -76,16 +56,14 @@ export class ObsidianFileEventHandler implements FileEventHandler {
if (file instanceof TFile) { if (file instanceof TFile) {
Logger.getInstance().info(`File modified: ${file.path}`); Logger.getInstance().info(`File modified: ${file.path}`);
await syncLocallyUpdatedFile({ await this.syncer.syncLocallyUpdatedFile({
database: this.database,
syncServer: this.syncServer,
operations: this.operations,
history: this.history,
updateTime: new Date(file.stat.ctime),
relativePath: file.path, relativePath: file.path,
updateTime: new Date(file.stat.ctime),
}); });
} else { } else {
Logger.getInstance().info(`Folder modified: ${file.path}, ignored`); Logger.getInstance().debug(
`Folder modified: ${file.path}, ignored`
);
} }
} }
} }

View file

@ -11,11 +11,11 @@ import { SyncService } from "./services/sync-service";
import { Database } from "./database/database"; import { Database } from "./database/database";
import { applyRemoteChangesLocally } from "./sync-operations/apply-remote-changes-locally"; import { applyRemoteChangesLocally } from "./sync-operations/apply-remote-changes-locally";
import { ObsidianFileOperations } from "./file-operations/obsidian-file-operations"; import { ObsidianFileOperations } from "./file-operations/obsidian-file-operations";
import { applyLocalChangesRemotely } from "./sync-operations/apply-local-changes-remotely";
import { StatusBar } from "./views/status-bar"; import { StatusBar } from "./views/status-bar";
import { Logger } from "./tracing/logger.js"; import { Logger } from "./tracing/logger.js";
import { SyncHistory } from "./tracing/sync-history.js"; import { SyncHistory } from "./tracing/sync-history.js";
import { LogsView } from "./views/logs-view.js"; import { LogsView } from "./views/logs-view.js";
import { Syncer } from "./sync-operations/syncer.js";
export default class SyncPlugin extends Plugin { export default class SyncPlugin extends Plugin {
private remoteListenerIntervalId: number | null = null; private remoteListenerIntervalId: number | null = null;
@ -39,24 +39,20 @@ export default class SyncPlugin extends Plugin {
const syncServer = new SyncService(database); const syncServer = new SyncService(database);
new StatusBar(this, this.history); const syncer = new Syncer({
database,
operations: this.operations,
syncServer,
history: this.history,
});
this.addSettingTab( this.addSettingTab(
new SyncSettingsTab( new SyncSettingsTab(this.app, this, database, syncServer, syncer)
this.app,
this,
database,
syncServer,
this.history
)
); );
const eventHandler = new ObsidianFileEventHandler( new StatusBar(this, this.history, syncer);
database,
syncServer, const eventHandler = new ObsidianFileEventHandler(syncer);
this.operations,
this.history
);
this.app.workspace.onLayoutReady(async () => { this.app.workspace.onLayoutReady(async () => {
Logger.getInstance().info("Initialising sync handlers"); Logger.getInstance().info("Initialising sync handlers");
@ -82,12 +78,7 @@ export default class SyncPlugin extends Plugin {
this.registerEvent(event); this.registerEvent(event);
}); });
await applyLocalChangesRemotely({ await syncer.scheduleSyncForOfflineChanges();
database,
syncServer,
operations: this.operations,
history: this.history,
});
Logger.getInstance().info("Sync handlers initialised"); Logger.getInstance().info("Sync handlers initialised");
}); });
@ -95,6 +86,7 @@ export default class SyncPlugin extends Plugin {
this.registerRemoteEventListener( this.registerRemoteEventListener(
database, database,
syncServer, syncServer,
syncer,
database.getSettings().fetchChangesUpdateIntervalMs database.getSettings().fetchChangesUpdateIntervalMs
); );
@ -103,16 +95,12 @@ export default class SyncPlugin extends Plugin {
this.registerRemoteEventListener( this.registerRemoteEventListener(
database, database,
syncServer, syncServer,
syncer,
settings.fetchChangesUpdateIntervalMs settings.fetchChangesUpdateIntervalMs
); );
if (!oldSettings.isSyncEnabled && settings.isSyncEnabled) { if (!oldSettings.isSyncEnabled && settings.isSyncEnabled) {
await applyLocalChangesRemotely({ await syncer.scheduleSyncForOfflineChanges();
database: database,
syncServer,
operations: this.operations,
history: this.history,
});
} }
}); });
@ -163,6 +151,7 @@ export default class SyncPlugin extends Plugin {
private registerRemoteEventListener( private registerRemoteEventListener(
database: Database, database: Database,
syncServer: SyncService, syncServer: SyncService,
syncer: Syncer,
intervalMs: number intervalMs: number
): void { ): void {
if (this.remoteListenerIntervalId !== null) { if (this.remoteListenerIntervalId !== null) {
@ -175,8 +164,7 @@ export default class SyncPlugin extends Plugin {
applyRemoteChangesLocally({ applyRemoteChangesLocally({
database, database,
syncServer, syncServer,
operations: this.operations, syncer,
history: this.history,
}), }),
intervalMs intervalMs
); );

View file

@ -1,22 +1,20 @@
import type { Database } from "src/database/database"; import type { Database } from "src/database/database";
import type { FileOperations } from "src/file-operations/file-operations"; import type { FileOperations } from "src/file-operations/file-operations";
import type { SyncService } from "src/services/sync-service"; import type { SyncService } from "src/services/sync-service";
import { syncRemotelyUpdatedFile } from "./sync-remotely-updated-file";
import { Logger } from "src/tracing/logger"; import { Logger } from "src/tracing/logger";
import type { SyncHistory } from "src/tracing/sync-history"; import type { SyncHistory } from "src/tracing/sync-history";
import { Syncer } from "./syncer";
let isRunning = false; let isRunning = false;
export async function applyRemoteChangesLocally({ export async function applyRemoteChangesLocally({
database, database,
syncServer, syncServer,
operations, syncer,
history,
}: { }: {
database: Database; database: Database;
syncServer: SyncService; syncServer: SyncService;
operations: FileOperations; syncer: Syncer;
history: SyncHistory;
}): Promise<void> { }): Promise<void> {
if (!database.getSettings().isSyncEnabled) { if (!database.getSettings().isSyncEnabled) {
Logger.getInstance().debug( Logger.getInstance().debug(
@ -44,13 +42,7 @@ export async function applyRemoteChangesLocally({
await Promise.all( await Promise.all(
remote.latestDocuments.map(async (remoteDocument) => remote.latestDocuments.map(async (remoteDocument) =>
syncRemotelyUpdatedFile({ syncer.syncRemotelyUpdatedFile(remoteDocument)
database,
syncServer,
history,
operations,
remoteVersion: remoteDocument,
})
) )
); );