Pick up API changes

This commit is contained in:
Andras Schmelczer 2025-01-02 10:58:47 +00:00
parent 6d32e51c3e
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 { SyncService } from "src/services/sync-service";
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 { syncLocallyCreatedFile } from "src/sync-operations/sync-locally-created-file";
import { Logger } from "src/tracing/logger";
import type { SyncHistory } from "src/tracing/sync-history";
import { Syncer } from "src/sync-operations/syncer";
export class ObsidianFileEventHandler implements FileEventHandler {
public constructor(
private readonly database: Database,
private readonly syncServer: SyncService,
private readonly operations: FileOperations,
private readonly history: SyncHistory
) {}
public constructor(private readonly syncer: Syncer) {}
public async onCreate(file: TAbstractFile): Promise<void> {
if (file instanceof TFile) {
Logger.getInstance().info(`File created: ${file.path}`);
await syncLocallyCreatedFile({
database: this.database,
syncServer: this.syncServer,
operations: this.operations,
updateTime: new Date(file.stat.ctime),
relativePath: file.path,
history: this.history,
});
await this.syncer.syncLocallyCreatedFile(
file.path,
new Date(file.stat.ctime)
);
} 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) {
Logger.getInstance().info(`File deleted: ${file.path}`);
await syncLocallyDeletedFile({
database: this.database,
syncServer: this.syncServer,
history: this.history,
relativePath: file.path,
});
await this.syncer.syncLocallyDeletedFile(file.path);
} 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}`
);
await syncLocallyUpdatedFile({
database: this.database,
syncServer: this.syncServer,
operations: this.operations,
history: this.history,
updateTime: new Date(file.stat.ctime),
relativePath: file.path,
await this.syncer.syncLocallyUpdatedFile({
oldPath,
relativePath: file.path,
updateTime: new Date(file.stat.ctime),
});
} else {
Logger.getInstance().info(
Logger.getInstance().debug(
`Folder renamed: ${oldPath} -> ${file.path}, ignored`
);
}
@ -76,16 +56,14 @@ export class ObsidianFileEventHandler implements FileEventHandler {
if (file instanceof TFile) {
Logger.getInstance().info(`File modified: ${file.path}`);
await syncLocallyUpdatedFile({
database: this.database,
syncServer: this.syncServer,
operations: this.operations,
history: this.history,
updateTime: new Date(file.stat.ctime),
await this.syncer.syncLocallyUpdatedFile({
relativePath: file.path,
updateTime: new Date(file.stat.ctime),
});
} 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 { applyRemoteChangesLocally } from "./sync-operations/apply-remote-changes-locally";
import { ObsidianFileOperations } from "./file-operations/obsidian-file-operations";
import { applyLocalChangesRemotely } from "./sync-operations/apply-local-changes-remotely";
import { StatusBar } from "./views/status-bar";
import { Logger } from "./tracing/logger.js";
import { SyncHistory } from "./tracing/sync-history.js";
import { LogsView } from "./views/logs-view.js";
import { Syncer } from "./sync-operations/syncer.js";
export default class SyncPlugin extends Plugin {
private remoteListenerIntervalId: number | null = null;
@ -39,24 +39,20 @@ export default class SyncPlugin extends Plugin {
const syncServer = new SyncService(database);
new StatusBar(this, this.history);
const syncer = new Syncer({
database,
operations: this.operations,
syncServer,
history: this.history,
});
this.addSettingTab(
new SyncSettingsTab(
this.app,
this,
database,
syncServer,
this.history
)
new SyncSettingsTab(this.app, this, database, syncServer, syncer)
);
const eventHandler = new ObsidianFileEventHandler(
database,
syncServer,
this.operations,
this.history
);
new StatusBar(this, this.history, syncer);
const eventHandler = new ObsidianFileEventHandler(syncer);
this.app.workspace.onLayoutReady(async () => {
Logger.getInstance().info("Initialising sync handlers");
@ -82,12 +78,7 @@ export default class SyncPlugin extends Plugin {
this.registerEvent(event);
});
await applyLocalChangesRemotely({
database,
syncServer,
operations: this.operations,
history: this.history,
});
await syncer.scheduleSyncForOfflineChanges();
Logger.getInstance().info("Sync handlers initialised");
});
@ -95,6 +86,7 @@ export default class SyncPlugin extends Plugin {
this.registerRemoteEventListener(
database,
syncServer,
syncer,
database.getSettings().fetchChangesUpdateIntervalMs
);
@ -103,16 +95,12 @@ export default class SyncPlugin extends Plugin {
this.registerRemoteEventListener(
database,
syncServer,
syncer,
settings.fetchChangesUpdateIntervalMs
);
if (!oldSettings.isSyncEnabled && settings.isSyncEnabled) {
await applyLocalChangesRemotely({
database: database,
syncServer,
operations: this.operations,
history: this.history,
});
await syncer.scheduleSyncForOfflineChanges();
}
});
@ -163,6 +151,7 @@ export default class SyncPlugin extends Plugin {
private registerRemoteEventListener(
database: Database,
syncServer: SyncService,
syncer: Syncer,
intervalMs: number
): void {
if (this.remoteListenerIntervalId !== null) {
@ -175,8 +164,7 @@ export default class SyncPlugin extends Plugin {
applyRemoteChangesLocally({
database,
syncServer,
operations: this.operations,
history: this.history,
syncer,
}),
intervalMs
);

View file

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