Pick up simple client

This commit is contained in:
Andras Schmelczer 2025-02-20 22:26:39 +00:00
parent eb1ad9921a
commit bb9acaf656
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
4 changed files with 98 additions and 191 deletions

View file

@ -11,85 +11,43 @@ import { StatusBar } from "./views/status-bar";
import { LogsView } from "./views/logs-view"; import { LogsView } from "./views/logs-view";
import { StatusDescription } from "./views/status-description"; import { StatusDescription } from "./views/status-description";
import { import { Logger, SyncClient } from "sync-client";
applyRemoteChangesLocally,
Database,
Logger,
Syncer,
SyncHistory,
SyncService,
initialize,
Settings
} from "sync-client";
export default class VaultLinkPlugin extends Plugin { export default class VaultLinkPlugin extends Plugin {
private readonly operations = new ObsidianFileOperations(this.app.vault); private settingsTab: SyncSettingsTab | undefined;
private readonly history = new SyncHistory(); private client!: SyncClient;
private settingsTab: SyncSettingsTab;
private remoteListenerIntervalId: number | null = null;
public async onload(): Promise<void> { public async onload(): Promise<void> {
Logger.getInstance().info("Starting plugin"); Logger.getInstance().info("Starting plugin");
await initialize(); this.client = await SyncClient.create(
new ObsidianFileOperations(this.app.vault),
let state = (await this.loadData()) ?? { {
settings: undefined, load: this.loadData.bind(this),
database: undefined save: this.saveData.bind(this)
};
const database = new Database(
state.database,
async (data: unknown): Promise<void> => {
state = { ...state, database: data };
return this.saveData(state);
} }
); );
const settings = new Settings( const statusDescription = new StatusDescription(this.client);
state.settings,
async (data: unknown): Promise<void> => {
state = { ...state, settings: data };
return this.saveData(state);
}
);
const syncService = new SyncService(database);
const syncer = new Syncer(
database,
settings,
syncService,
this.operations,
this.history
);
const statusDescription = new StatusDescription(
settings,
database,
syncService,
this.history,
syncer
);
this.settingsTab = new SyncSettingsTab({ this.settingsTab = new SyncSettingsTab({
app: this.app, app: this.app,
plugin: this, plugin: this,
settings, syncClient: this.client,
syncService, statusDescription
statusDescription,
syncer
}); });
this.addSettingTab(this.settingsTab); this.addSettingTab(this.settingsTab);
new StatusBar(settings, this, this.history, syncer); new StatusBar(this, this.client);
this.registerView( this.registerView(
HistoryView.TYPE, HistoryView.TYPE,
(leaf) => new HistoryView(leaf, settings, this.history) (leaf) =>
new HistoryView(leaf, this.client.settings, this.client.history)
); );
this.registerView( this.registerView(
LogsView.TYPE, LogsView.TYPE,
(leaf) => new LogsView(this, settings, leaf) (leaf) => new LogsView(this, this.client.settings, leaf)
); );
this.addRibbonIcon( this.addRibbonIcon(
@ -103,7 +61,7 @@ export default class VaultLinkPlugin extends Plugin {
async (_: MouseEvent) => this.activateView(LogsView.TYPE) async (_: MouseEvent) => this.activateView(LogsView.TYPE)
); );
const eventHandler = new ObsidianFileEventHandler(syncer); const eventHandler = new ObsidianFileEventHandler(this.client.syncer);
this.app.workspace.onLayoutReady(async () => { this.app.workspace.onLayoutReady(async () => {
Logger.getInstance().info("Initialising sync handlers"); Logger.getInstance().info("Initialising sync handlers");
@ -131,44 +89,12 @@ export default class VaultLinkPlugin extends Plugin {
Logger.getInstance().info("Sync handlers initialised"); Logger.getInstance().info("Sync handlers initialised");
void syncer.scheduleSyncForOfflineChanges(); void this.client.syncer.scheduleSyncForOfflineChanges();
}); });
this.registerRemoteEventListener(
settings,
database,
syncService,
syncer,
settings.getSettings().fetchChangesUpdateIntervalMs
);
settings.addOnSettingsChangeHandlers((newSettings, oldSettings) => {
this.registerRemoteEventListener(
settings,
database,
syncService,
syncer,
newSettings.fetchChangesUpdateIntervalMs
);
if (!oldSettings.isSyncEnabled && newSettings.isSyncEnabled) {
syncer
.scheduleSyncForOfflineChanges()
.catch((_error: unknown) => {
Logger.getInstance().error(
"Failed to schedule sync for offline changes"
);
});
}
});
Logger.getInstance().info("Plugin loaded");
} }
public onunload(): void { public onunload(): void {
if (this.remoteListenerIntervalId !== null) { this.client.onunload();
window.clearInterval(this.remoteListenerIntervalId);
}
} }
public openSettings(): void { public openSettings(): void {
@ -200,28 +126,4 @@ export default class VaultLinkPlugin extends Plugin {
await workspace.revealLeaf(leaf); await workspace.revealLeaf(leaf);
} }
} }
private registerRemoteEventListener(
settings: Settings,
database: Database,
syncService: SyncService,
syncer: Syncer,
intervalMs: number
): void {
if (this.remoteListenerIntervalId !== null) {
window.clearInterval(this.remoteListenerIntervalId);
}
this.remoteListenerIntervalId = window.setInterval(
// eslint-disable-next-line @typescript-eslint/no-misused-promises
async () =>
applyRemoteChangesLocally({
settings,
database,
syncService,
syncer
}),
intervalMs
);
}
} }

View file

@ -5,43 +5,35 @@ import type VaultLinkPlugin from "src/vault-link-plugin";
import type { StatusDescription } from "./status-description"; import type { StatusDescription } from "./status-description";
import { LogsView } from "./logs-view"; import { LogsView } from "./logs-view";
import { HistoryView } from "./history-view"; import { HistoryView } from "./history-view";
import type { SyncService, Syncer, Settings } from "sync-client"; import type { SyncClient } from "sync-client";
import { Logger, LogLevel } from "sync-client"; import { LogLevel } from "sync-client";
export class SyncSettingsTab extends PluginSettingTab { export class SyncSettingsTab extends PluginSettingTab {
private editedVaultName: string; private editedVaultName: string;
private readonly plugin: VaultLinkPlugin; private readonly plugin: VaultLinkPlugin;
private readonly settings: Settings; private readonly syncClient: SyncClient;
private readonly syncService: SyncService;
private readonly statusDescription: StatusDescription; private readonly statusDescription: StatusDescription;
private readonly syncer: Syncer;
private statusDescriptionSubscription: (() => void) | undefined; private statusDescriptionSubscription: (() => void) | undefined;
public constructor({ public constructor({
app, app,
plugin, plugin,
settings, syncClient,
syncService, statusDescription
statusDescription,
syncer
}: { }: {
app: App; app: App;
plugin: VaultLinkPlugin; plugin: VaultLinkPlugin;
settings: Settings; syncClient: SyncClient;
syncService: SyncService;
statusDescription: StatusDescription; statusDescription: StatusDescription;
syncer: Syncer;
}) { }) {
super(app, plugin); super(app, plugin);
this.plugin = plugin; this.plugin = plugin;
this.settings = settings; this.syncClient = syncClient;
this.syncService = syncService;
this.statusDescription = statusDescription; this.statusDescription = statusDescription;
this.syncer = syncer;
this.editedVaultName = this.settings.getSettings().vaultName; this.editedVaultName = this.syncClient.settings.getSettings().vaultName;
this.settings.addOnSettingsChangeHandlers( this.syncClient.settings.addOnSettingsChangeHandlers(
(newSettings, oldSettings) => { (newSettings, oldSettings) => {
if (newSettings.vaultName !== oldSettings.vaultName) { if (newSettings.vaultName !== oldSettings.vaultName) {
this.editedVaultName = newSettings.vaultName; this.editedVaultName = newSettings.vaultName;
@ -130,15 +122,15 @@ export class SyncSettingsTab extends PluginSettingTab {
.addText((text) => .addText((text) =>
text text
.setPlaceholder("https://example.com:3030") .setPlaceholder("https://example.com:3030")
.setValue(this.settings.getSettings().remoteUri) .setValue(this.syncClient.settings.getSettings().remoteUri)
.onChange(async (value) => .onChange(async (value) =>
this.settings.setSetting("remoteUri", value) this.syncClient.settings.setSetting("remoteUri", value)
) )
) )
.addButton((button) => .addButton((button) =>
button.setButtonText("Test connection").onClick(async () => { button.setButtonText("Test connection").onClick(async () => {
new Notice( new Notice(
(await this.syncService.checkConnection()).message (await this.syncClient.checkConnection()).message
); );
await this.statusDescription.updateConnectionState(); await this.statusDescription.updateConnectionState();
}) })
@ -154,9 +146,9 @@ export class SyncSettingsTab extends PluginSettingTab {
.addTextArea((text) => .addTextArea((text) =>
text text
.setPlaceholder("ey...") .setPlaceholder("ey...")
.setValue(this.settings.getSettings().token) .setValue(this.syncClient.settings.getSettings().token)
.onChange(async (value) => .onChange(async (value) =>
this.settings.setSetting("token", value) this.syncClient.settings.setSetting("token", value)
) )
); );
@ -169,23 +161,22 @@ export class SyncSettingsTab extends PluginSettingTab {
.addText((text) => .addText((text) =>
text text
.setPlaceholder("My Obsidian Vault") .setPlaceholder("My Obsidian Vault")
.setValue(this.settings.getSettings().vaultName) .setValue(this.syncClient.settings.getSettings().vaultName)
.onChange((value) => (this.editedVaultName = value)) .onChange((value) => (this.editedVaultName = value))
) )
.addButton((button) => .addButton((button) =>
button.setButtonText("Apply").onClick(async () => { button.setButtonText("Apply").onClick(async () => {
if ( if (
this.editedVaultName === this.editedVaultName ===
this.settings.getSettings().vaultName this.syncClient.settings.getSettings().vaultName
) { ) {
return; return;
} }
await this.settings.setSetting( await this.syncClient.settings.setSetting(
"vaultName", "vaultName",
this.editedVaultName this.editedVaultName
); );
await this.syncer.reset(); await this.syncClient.reset();
Logger.getInstance().reset();
new Notice( new Notice(
"Sync state has been reset, you will need to resync" "Sync state has been reset, you will need to resync"
); );
@ -203,8 +194,7 @@ export class SyncSettingsTab extends PluginSettingTab {
) )
.addButton((button) => .addButton((button) =>
button.setButtonText("Reset sync state").onClick(async () => { button.setButtonText("Reset sync state").onClick(async () => {
await this.syncer.reset(); await this.syncClient.reset();
Logger.getInstance().reset();
new Notice( new Notice(
"Sync state has been reset, you will need to resync" "Sync state has been reset, you will need to resync"
); );
@ -223,11 +213,11 @@ export class SyncSettingsTab extends PluginSettingTab {
.setDynamicTooltip() .setDynamicTooltip()
.setInstant(false) .setInstant(false)
.setValue( .setValue(
this.settings.getSettings() this.syncClient.settings.getSettings()
.fetchChangesUpdateIntervalMs / 1000 .fetchChangesUpdateIntervalMs / 1000
) )
.onChange(async (value) => .onChange(async (value) =>
this.settings.setSetting( this.syncClient.settings.setSetting(
"fetchChangesUpdateIntervalMs", "fetchChangesUpdateIntervalMs",
value * 1000 value * 1000
) )
@ -244,9 +234,14 @@ export class SyncSettingsTab extends PluginSettingTab {
.setLimits(1, 16, 1) .setLimits(1, 16, 1)
.setDynamicTooltip() .setDynamicTooltip()
.setInstant(false) .setInstant(false)
.setValue(this.settings.getSettings().syncConcurrency) .setValue(
this.syncClient.settings.getSettings().syncConcurrency
)
.onChange(async (value) => .onChange(async (value) =>
this.settings.setSetting("syncConcurrency", value) this.syncClient.settings.setSetting(
"syncConcurrency",
value
)
) )
); );
@ -260,9 +255,14 @@ export class SyncSettingsTab extends PluginSettingTab {
.setLimits(0, 32, 1) .setLimits(0, 32, 1)
.setDynamicTooltip() .setDynamicTooltip()
.setInstant(false) .setInstant(false)
.setValue(this.settings.getSettings().maxFileSizeMB) .setValue(
this.syncClient.settings.getSettings().maxFileSizeMB
)
.onChange(async (value) => .onChange(async (value) =>
this.settings.setSetting("maxFileSizeMB", value) this.syncClient.settings.setSetting(
"maxFileSizeMB",
value
)
) )
); );
@ -276,9 +276,14 @@ export class SyncSettingsTab extends PluginSettingTab {
) )
.addToggle((toggle) => .addToggle((toggle) =>
toggle toggle
.setValue(this.settings.getSettings().isSyncEnabled) .setValue(
this.syncClient.settings.getSettings().isSyncEnabled
)
.onChange(async (value) => .onChange(async (value) =>
this.settings.setSetting("isSyncEnabled", value) this.syncClient.settings.setSetting(
"isSyncEnabled",
value
)
) )
); );
} }
@ -293,9 +298,15 @@ export class SyncSettingsTab extends PluginSettingTab {
) )
.addToggle((toggle) => .addToggle((toggle) =>
toggle toggle
.setValue(this.settings.getSettings().displayNoopSyncEvents) .setValue(
this.syncClient.settings.getSettings()
.displayNoopSyncEvents
)
.onChange(async (value) => .onChange(async (value) =>
this.settings.setSetting("displayNoopSyncEvents", value) this.syncClient.settings.setSetting(
"displayNoopSyncEvents",
value
)
) )
); );
@ -312,9 +323,11 @@ export class SyncSettingsTab extends PluginSettingTab {
[LogLevel.WARNING]: LogLevel.WARNING, [LogLevel.WARNING]: LogLevel.WARNING,
[LogLevel.ERROR]: LogLevel.ERROR [LogLevel.ERROR]: LogLevel.ERROR
}) })
.setValue(this.settings.getSettings().minimumLogLevel) .setValue(
this.syncClient.settings.getSettings().minimumLogLevel
)
.onChange(async (value) => .onChange(async (value) =>
this.settings.setSetting( this.syncClient.settings.setSetting(
"minimumLogLevel", "minimumLogLevel",
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
value as LogLevel value as LogLevel

View file

@ -1,4 +1,4 @@
import type { HistoryStats, Settings, SyncHistory, Syncer } from "sync-client"; import type { HistoryStats, SyncClient } from "sync-client";
import type VaultLinkPlugin from "src/vault-link-plugin"; import type VaultLinkPlugin from "src/vault-link-plugin";
export class StatusBar { export class StatusBar {
@ -8,23 +8,23 @@ export class StatusBar {
private lastRemaining: number | undefined; private lastRemaining: number | undefined;
public constructor( public constructor(
private readonly settings: Settings,
private readonly plugin: VaultLinkPlugin, private readonly plugin: VaultLinkPlugin,
history: SyncHistory, private readonly syncClient: SyncClient
syncer: Syncer
) { ) {
this.statusBarItem = plugin.addStatusBarItem(); this.statusBarItem = plugin.addStatusBarItem();
history.addSyncHistoryUpdateListener((status) => { this.syncClient.history.addSyncHistoryUpdateListener((status) => {
this.lastHistoryStats = status; this.lastHistoryStats = status;
this.updateStatus(); this.updateStatus();
}); });
syncer.addRemainingOperationsListener((remainingOperations) => { this.syncClient.syncer.addRemainingOperationsListener(
this.lastRemaining = remainingOperations; (remainingOperations) => {
this.updateStatus(); this.lastRemaining = remainingOperations;
}); this.updateStatus();
}
);
settings.addOnSettingsChangeHandlers(() => { this.syncClient.settings.addOnSettingsChangeHandlers(() => {
this.updateStatus(); this.updateStatus();
}); });
} }
@ -57,7 +57,7 @@ export class StatusBar {
} }
if (!hasShownMessage) { if (!hasShownMessage) {
if (this.settings.getSettings().isSyncEnabled) { if (this.syncClient.settings.getSettings().isSyncEnabled) {
container.createSpan({ text: "VaultLink is idle" }); container.createSpan({ text: "VaultLink is idle" });
} else { } else {
const button = container.createEl("button", { const button = container.createEl("button", {

View file

@ -1,11 +1,7 @@
import type { import type {
HistoryStats, HistoryStats,
CheckConnectionResult, CheckConnectionResult,
SyncService, SyncClient
SyncHistory,
Syncer,
Database,
Settings
} from "sync-client"; } from "sync-client";
export class StatusDescription { export class StatusDescription {
@ -15,32 +11,28 @@ export class StatusDescription {
private statusChangeListeners: (() => void)[] = []; private statusChangeListeners: (() => void)[] = [];
public constructor( public constructor(private readonly syncClient: SyncClient) {
private readonly settings: Settings,
private readonly database: Database,
private readonly syncService: SyncService,
history: SyncHistory,
syncer: Syncer
) {
void this.updateConnectionState(); void this.updateConnectionState();
history.addSyncHistoryUpdateListener((status) => { syncClient.history.addSyncHistoryUpdateListener((status) => {
this.lastHistoryStats = status; this.lastHistoryStats = status;
this.updateDescription(); this.updateDescription();
}); });
syncer.addRemainingOperationsListener((remainingOperations) => { this.syncClient.syncer.addRemainingOperationsListener(
this.lastRemaining = remainingOperations; (remainingOperations) => {
this.updateDescription(); this.lastRemaining = remainingOperations;
}); this.updateDescription();
}
);
settings.addOnSettingsChangeHandlers(() => { this.syncClient.settings.addOnSettingsChangeHandlers(() => {
void this.updateConnectionState(); void this.updateConnectionState();
}); });
} }
public async updateConnectionState(): Promise<void> { public async updateConnectionState(): Promise<void> {
this.lastConnectionState = await this.syncService.checkConnection(); this.lastConnectionState = await this.syncClient.checkConnection();
this.updateDescription(); this.updateDescription();
} }
@ -75,15 +67,15 @@ export class StatusDescription {
container.createSpan({ text: "VaultLink is connected to the server " }); container.createSpan({ text: "VaultLink is connected to the server " });
container.createEl("a", { container.createEl("a", {
text: this.settings.getSettings().remoteUri, text: this.syncClient.settings.getSettings().remoteUri,
href: this.settings.getSettings().remoteUri href: this.syncClient.settings.getSettings().remoteUri
}); });
container.createSpan({ container.createSpan({
text: ` and has indexed approximately ` text: ` and has indexed approximately `
}); });
container.createSpan({ container.createSpan({
text: `${this.database.getDocuments().size}`, text: `${this.syncClient.documentCount}`,
cls: "number" cls: "number"
}); });
container.createSpan({ container.createSpan({
@ -95,7 +87,7 @@ export class StatusDescription {
(this.lastHistoryStats?.success ?? 0) === 0 && (this.lastHistoryStats?.success ?? 0) === 0 &&
(this.lastHistoryStats?.error ?? 0) === 0 (this.lastHistoryStats?.error ?? 0) === 0
) { ) {
if (this.settings.getSettings().isSyncEnabled) { if (this.syncClient.settings.getSettings().isSyncEnabled) {
container.createSpan({ container.createSpan({
text: "Syncing is enabled but VaultLink hasn't found anything to sync yet." text: "Syncing is enabled but VaultLink hasn't found anything to sync yet."
}); });