Extract settings from database

This commit is contained in:
Andras Schmelczer 2025-02-19 21:32:40 +00:00
parent aef5952c4d
commit 614e4a780a
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
20 changed files with 344 additions and 319 deletions

View file

@ -18,7 +18,8 @@ import {
Syncer,
SyncHistory,
SyncService,
initialize
initialize,
Settings
} from "sync-client";
export default class VaultLinkPlugin extends Plugin {
@ -32,21 +33,38 @@ export default class VaultLinkPlugin extends Plugin {
await initialize();
let state = (await this.loadData()) ?? {
settings: undefined,
database: undefined
};
const database = new Database(
await this.loadData(),
this.saveData.bind(this)
state.database,
async (data: unknown): Promise<void> => {
state = { ...state, database: data };
return this.saveData(state);
}
);
const settings = new Settings(
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,
@ -56,22 +74,22 @@ export default class VaultLinkPlugin extends Plugin {
this.settingsTab = new SyncSettingsTab({
app: this.app,
plugin: this,
database,
settings,
syncService,
statusDescription,
syncer
});
this.addSettingTab(this.settingsTab);
new StatusBar(database, this, this.history, syncer);
new StatusBar(settings, this, this.history, syncer);
this.registerView(
HistoryView.TYPE,
(leaf) => new HistoryView(leaf, database, this.history)
(leaf) => new HistoryView(leaf, settings, this.history)
);
this.registerView(
LogsView.TYPE,
(leaf) => new LogsView(this, database, leaf)
(leaf) => new LogsView(this, settings, leaf)
);
this.addRibbonIcon(
@ -117,21 +135,23 @@ export default class VaultLinkPlugin extends Plugin {
});
this.registerRemoteEventListener(
settings,
database,
syncService,
syncer,
database.getSettings().fetchChangesUpdateIntervalMs
settings.getSettings().fetchChangesUpdateIntervalMs
);
database.addOnSettingsChangeHandlers((settings, oldSettings) => {
settings.addOnSettingsChangeHandlers((newSettings, oldSettings) => {
this.registerRemoteEventListener(
settings,
database,
syncService,
syncer,
settings.fetchChangesUpdateIntervalMs
newSettings.fetchChangesUpdateIntervalMs
);
if (!oldSettings.isSyncEnabled && settings.isSyncEnabled) {
if (!oldSettings.isSyncEnabled && newSettings.isSyncEnabled) {
syncer
.scheduleSyncForOfflineChanges()
.catch((_error: unknown) => {
@ -182,6 +202,7 @@ export default class VaultLinkPlugin extends Plugin {
}
private registerRemoteEventListener(
settings: Settings,
database: Database,
syncService: SyncService,
syncer: Syncer,
@ -195,6 +216,7 @@ export default class VaultLinkPlugin extends Plugin {
// eslint-disable-next-line @typescript-eslint/no-misused-promises
async () =>
applyRemoteChangesLocally({
settings,
database,
syncService,
syncer

View file

@ -2,7 +2,7 @@ import type { IconName, WorkspaceLeaf } from "obsidian";
import { ItemView, setIcon } from "obsidian";
import { intlFormatDistance } from "date-fns";
import type { SyncHistory, HistoryEntry, Database } from "sync-client";
import type { SyncHistory, HistoryEntry, Settings } from "sync-client";
import { SyncType, SyncSource, SyncStatus, Logger } from "sync-client";
export class HistoryView extends ItemView {
@ -12,7 +12,7 @@ export class HistoryView extends ItemView {
public constructor(
leaf: WorkspaceLeaf,
private readonly database: Database,
private readonly settings: Settings,
private readonly history: SyncHistory
) {
super(leaf);
@ -101,7 +101,7 @@ export class HistoryView extends ItemView {
.filter(
(entry) =>
entry.status !== SyncStatus.NO_OP ||
this.database.getSettings().displayNoopSyncEvents
this.settings.getSettings().displayNoopSyncEvents
);
entries.forEach((entry) => {

View file

@ -1,7 +1,7 @@
import type { WorkspaceLeaf } from "obsidian";
import { ItemView } from "obsidian";
import type VaultLinkPlugin from "src/vault-link-plugin";
import type { Database } from "sync-client";
import type { Settings } from "sync-client";
import { Logger } from "sync-client";
export class LogsView extends ItemView {
@ -10,7 +10,7 @@ export class LogsView extends ItemView {
public constructor(
private readonly plugin: VaultLinkPlugin,
private readonly database: Database,
private readonly settings: Settings,
leaf: WorkspaceLeaf
) {
super(leaf);
@ -19,7 +19,7 @@ export class LogsView extends ItemView {
this.updateView();
});
database.addOnSettingsChangeHandlers((newSettings, oldSettings) => {
settings.addOnSettingsChangeHandlers((newSettings, oldSettings) => {
if (newSettings.minimumLogLevel !== oldSettings.minimumLogLevel) {
this.updateView();
}
@ -79,7 +79,7 @@ export class LogsView extends ItemView {
);
const logs = Logger.getInstance().getMessages(
this.database.getSettings().minimumLogLevel
this.settings.getSettings().minimumLogLevel
);
if (logs.length === 0) {

View file

@ -5,14 +5,14 @@ import type VaultLinkPlugin from "src/vault-link-plugin";
import type { StatusDescription } from "./status-description";
import { LogsView } from "./logs-view";
import { HistoryView } from "./history-view";
import type { SyncService, Syncer, Database } from "sync-client";
import type { SyncService, Syncer, Settings } from "sync-client";
import { Logger, LogLevel } from "sync-client";
export class SyncSettingsTab extends PluginSettingTab {
private editedVaultName: string;
private readonly plugin: VaultLinkPlugin;
private readonly database: Database;
private readonly settings: Settings;
private readonly syncService: SyncService;
private readonly statusDescription: StatusDescription;
private readonly syncer: Syncer;
@ -21,27 +21,27 @@ export class SyncSettingsTab extends PluginSettingTab {
public constructor({
app,
plugin,
database,
settings,
syncService,
statusDescription,
syncer
}: {
app: App;
plugin: VaultLinkPlugin;
database: Database;
settings: Settings;
syncService: SyncService;
statusDescription: StatusDescription;
syncer: Syncer;
}) {
super(app, plugin);
this.plugin = plugin;
this.database = database;
this.settings = settings;
this.syncService = syncService;
this.statusDescription = statusDescription;
this.syncer = syncer;
this.editedVaultName = this.database.getSettings().vaultName;
this.database.addOnSettingsChangeHandlers(
this.editedVaultName = this.settings.getSettings().vaultName;
this.settings.addOnSettingsChangeHandlers(
(newSettings, oldSettings) => {
if (newSettings.vaultName !== oldSettings.vaultName) {
this.editedVaultName = newSettings.vaultName;
@ -130,9 +130,9 @@ export class SyncSettingsTab extends PluginSettingTab {
.addText((text) =>
text
.setPlaceholder("https://example.com:3030")
.setValue(this.database.getSettings().remoteUri)
.setValue(this.settings.getSettings().remoteUri)
.onChange(async (value) =>
this.database.setSetting("remoteUri", value)
this.settings.setSetting("remoteUri", value)
)
)
.addButton((button) =>
@ -154,9 +154,9 @@ export class SyncSettingsTab extends PluginSettingTab {
.addTextArea((text) =>
text
.setPlaceholder("ey...")
.setValue(this.database.getSettings().token)
.setValue(this.settings.getSettings().token)
.onChange(async (value) =>
this.database.setSetting("token", value)
this.settings.setSetting("token", value)
)
);
@ -169,18 +169,18 @@ export class SyncSettingsTab extends PluginSettingTab {
.addText((text) =>
text
.setPlaceholder("My Obsidian Vault")
.setValue(this.database.getSettings().vaultName)
.setValue(this.settings.getSettings().vaultName)
.onChange((value) => (this.editedVaultName = value))
)
.addButton((button) =>
button.setButtonText("Apply").onClick(async () => {
if (
this.editedVaultName ===
this.database.getSettings().vaultName
this.settings.getSettings().vaultName
) {
return;
}
await this.database.setSetting(
await this.settings.setSetting(
"vaultName",
this.editedVaultName
);
@ -223,11 +223,11 @@ export class SyncSettingsTab extends PluginSettingTab {
.setDynamicTooltip()
.setInstant(false)
.setValue(
this.database.getSettings()
this.settings.getSettings()
.fetchChangesUpdateIntervalMs / 1000
)
.onChange(async (value) =>
this.database.setSetting(
this.settings.setSetting(
"fetchChangesUpdateIntervalMs",
value * 1000
)
@ -244,9 +244,9 @@ export class SyncSettingsTab extends PluginSettingTab {
.setLimits(1, 16, 1)
.setDynamicTooltip()
.setInstant(false)
.setValue(this.database.getSettings().syncConcurrency)
.setValue(this.settings.getSettings().syncConcurrency)
.onChange(async (value) =>
this.database.setSetting("syncConcurrency", value)
this.settings.setSetting("syncConcurrency", value)
)
);
@ -260,9 +260,9 @@ export class SyncSettingsTab extends PluginSettingTab {
.setLimits(0, 32, 1)
.setDynamicTooltip()
.setInstant(false)
.setValue(this.database.getSettings().maxFileSizeMB)
.setValue(this.settings.getSettings().maxFileSizeMB)
.onChange(async (value) =>
this.database.setSetting("maxFileSizeMB", value)
this.settings.setSetting("maxFileSizeMB", value)
)
);
@ -276,9 +276,9 @@ export class SyncSettingsTab extends PluginSettingTab {
)
.addToggle((toggle) =>
toggle
.setValue(this.database.getSettings().isSyncEnabled)
.setValue(this.settings.getSettings().isSyncEnabled)
.onChange(async (value) =>
this.database.setSetting("isSyncEnabled", value)
this.settings.setSetting("isSyncEnabled", value)
)
);
}
@ -293,9 +293,9 @@ export class SyncSettingsTab extends PluginSettingTab {
)
.addToggle((toggle) =>
toggle
.setValue(this.database.getSettings().displayNoopSyncEvents)
.setValue(this.settings.getSettings().displayNoopSyncEvents)
.onChange(async (value) =>
this.database.setSetting("displayNoopSyncEvents", value)
this.settings.setSetting("displayNoopSyncEvents", value)
)
);
@ -312,9 +312,9 @@ export class SyncSettingsTab extends PluginSettingTab {
[LogLevel.WARNING]: LogLevel.WARNING,
[LogLevel.ERROR]: LogLevel.ERROR
})
.setValue(this.database.getSettings().minimumLogLevel)
.setValue(this.settings.getSettings().minimumLogLevel)
.onChange(async (value) =>
this.database.setSetting(
this.settings.setSetting(
"minimumLogLevel",
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
value as LogLevel

View file

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

View file

@ -4,7 +4,8 @@ import type {
SyncService,
SyncHistory,
Syncer,
Database
Database,
Settings
} from "sync-client";
export class StatusDescription {
@ -15,6 +16,7 @@ export class StatusDescription {
private statusChangeListeners: (() => void)[] = [];
public constructor(
private readonly settings: Settings,
private readonly database: Database,
private readonly syncService: SyncService,
history: SyncHistory,
@ -32,7 +34,7 @@ export class StatusDescription {
this.updateDescription();
});
database.addOnSettingsChangeHandlers(() => {
settings.addOnSettingsChangeHandlers(() => {
void this.updateConnectionState();
});
}
@ -73,8 +75,8 @@ export class StatusDescription {
container.createSpan({ text: "VaultLink is connected to the server " });
container.createEl("a", {
text: this.database.getSettings().remoteUri,
href: this.database.getSettings().remoteUri
text: this.settings.getSettings().remoteUri,
href: this.settings.getSettings().remoteUri
});
container.createSpan({
@ -93,7 +95,7 @@ export class StatusDescription {
(this.lastHistoryStats?.success ?? 0) === 0 &&
(this.lastHistoryStats?.error ?? 0) === 0
) {
if (this.database.getSettings().isSyncEnabled) {
if (this.settings.getSettings().isSyncEnabled) {
container.createSpan({
text: "Syncing is enabled but VaultLink hasn't found anything to sync yet."
});