Use new settings API exposed directly through SyncClient

This commit is contained in:
Andras Schmelczer 2025-03-20 20:44:03 +00:00
parent a39e0886c7
commit d772cda164
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
7 changed files with 50 additions and 72 deletions

View file

@ -18,15 +18,11 @@ export class LogsView extends ItemView {
this.updateView(); this.updateView();
}); });
this.client.settings.addOnSettingsChangeHandlers( this.client.addOnSettingsChangeHandlers((newSettings, oldSettings) => {
(newSettings, oldSettings) => { if (newSettings.minimumLogLevel !== oldSettings.minimumLogLevel) {
if ( this.updateView();
newSettings.minimumLogLevel !== oldSettings.minimumLogLevel
) {
this.updateView();
}
} }
); });
} }
private static formatTimestamp(timestamp: Date): string { private static formatTimestamp(timestamp: Date): string {
@ -82,7 +78,7 @@ export class LogsView extends ItemView {
); );
const logs = this.client.logger.getMessages( const logs = this.client.logger.getMessages(
this.client.settings.getSettings().minimumLogLevel this.client.getSettings().minimumLogLevel
); );
if (logs.length === 0) { if (logs.length === 0) {

View file

@ -32,8 +32,8 @@ export class SyncSettingsTab extends PluginSettingTab {
this.syncClient = syncClient; this.syncClient = syncClient;
this.statusDescription = statusDescription; this.statusDescription = statusDescription;
this.editedVaultName = this.syncClient.settings.getSettings().vaultName; this.editedVaultName = this.syncClient.getSettings().vaultName;
this.syncClient.settings.addOnSettingsChangeHandlers( this.syncClient.addOnSettingsChangeHandlers(
(newSettings, oldSettings) => { (newSettings, oldSettings) => {
if (newSettings.vaultName !== oldSettings.vaultName) { if (newSettings.vaultName !== oldSettings.vaultName) {
this.editedVaultName = newSettings.vaultName; this.editedVaultName = newSettings.vaultName;
@ -122,9 +122,9 @@ export class SyncSettingsTab extends PluginSettingTab {
.addText((text) => .addText((text) =>
text text
.setPlaceholder("https://example.com:3000") .setPlaceholder("https://example.com:3000")
.setValue(this.syncClient.settings.getSettings().remoteUri) .setValue(this.syncClient.getSettings().remoteUri)
.onChange(async (value) => .onChange(async (value) =>
this.syncClient.settings.setSetting("remoteUri", value) this.syncClient.setSetting("remoteUri", value)
) )
) )
.addButton((button) => .addButton((button) =>
@ -146,9 +146,9 @@ export class SyncSettingsTab extends PluginSettingTab {
.addTextArea((text) => .addTextArea((text) =>
text text
.setPlaceholder("ey...") .setPlaceholder("ey...")
.setValue(this.syncClient.settings.getSettings().token) .setValue(this.syncClient.getSettings().token)
.onChange(async (value) => .onChange(async (value) =>
this.syncClient.settings.setSetting("token", value) this.syncClient.setSetting("token", value)
) )
); );
@ -161,22 +161,21 @@ export class SyncSettingsTab extends PluginSettingTab {
.addText((text) => .addText((text) =>
text text
.setPlaceholder("My Obsidian Vault") .setPlaceholder("My Obsidian Vault")
.setValue(this.syncClient.settings.getSettings().vaultName) .setValue(this.syncClient.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.syncClient.settings.getSettings().vaultName this.syncClient.getSettings().vaultName
) { ) {
return; return;
} }
await this.syncClient.settings.setSetting( await this.syncClient.setSetting(
"vaultName", "vaultName",
this.editedVaultName this.editedVaultName
); );
await this.syncClient.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"
); );
@ -213,11 +212,11 @@ export class SyncSettingsTab extends PluginSettingTab {
.setDynamicTooltip() .setDynamicTooltip()
.setInstant(false) .setInstant(false)
.setValue( .setValue(
this.syncClient.settings.getSettings() this.syncClient.getSettings()
.fetchChangesUpdateIntervalMs / 1000 .fetchChangesUpdateIntervalMs / 1000
) )
.onChange(async (value) => .onChange(async (value) =>
this.syncClient.settings.setSetting( this.syncClient.setSetting(
"fetchChangesUpdateIntervalMs", "fetchChangesUpdateIntervalMs",
value * 1000 value * 1000
) )
@ -234,14 +233,9 @@ export class SyncSettingsTab extends PluginSettingTab {
.setLimits(1, 16, 1) .setLimits(1, 16, 1)
.setDynamicTooltip() .setDynamicTooltip()
.setInstant(false) .setInstant(false)
.setValue( .setValue(this.syncClient.getSettings().syncConcurrency)
this.syncClient.settings.getSettings().syncConcurrency
)
.onChange(async (value) => .onChange(async (value) =>
this.syncClient.settings.setSetting( this.syncClient.setSetting("syncConcurrency", value)
"syncConcurrency",
value
)
) )
); );
@ -255,14 +249,9 @@ export class SyncSettingsTab extends PluginSettingTab {
.setLimits(0, 32, 1) .setLimits(0, 32, 1)
.setDynamicTooltip() .setDynamicTooltip()
.setInstant(false) .setInstant(false)
.setValue( .setValue(this.syncClient.getSettings().maxFileSizeMB)
this.syncClient.settings.getSettings().maxFileSizeMB
)
.onChange(async (value) => .onChange(async (value) =>
this.syncClient.settings.setSetting( this.syncClient.setSetting("maxFileSizeMB", value)
"maxFileSizeMB",
value
)
) )
); );
@ -276,14 +265,9 @@ export class SyncSettingsTab extends PluginSettingTab {
) )
.addToggle((toggle) => .addToggle((toggle) =>
toggle toggle
.setValue( .setValue(this.syncClient.getSettings().isSyncEnabled)
this.syncClient.settings.getSettings().isSyncEnabled
)
.onChange(async (value) => .onChange(async (value) =>
this.syncClient.settings.setSetting( this.syncClient.setSetting("isSyncEnabled", value)
"isSyncEnabled",
value
)
) )
); );
} }
@ -304,11 +288,9 @@ export class SyncSettingsTab extends PluginSettingTab {
[LogLevel.WARNING]: LogLevel.WARNING, [LogLevel.WARNING]: LogLevel.WARNING,
[LogLevel.ERROR]: LogLevel.ERROR [LogLevel.ERROR]: LogLevel.ERROR
}) })
.setValue( .setValue(this.syncClient.getSettings().minimumLogLevel)
this.syncClient.settings.getSettings().minimumLogLevel
)
.onChange(async (value) => .onChange(async (value) =>
this.syncClient.settings.setSetting( this.syncClient.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

@ -24,7 +24,7 @@ export class StatusBar {
} }
); );
this.syncClient.settings.addOnSettingsChangeHandlers(() => { this.syncClient.addOnSettingsChangeHandlers(() => {
this.updateStatus(); this.updateStatus();
}); });
} }
@ -57,7 +57,7 @@ export class StatusBar {
} }
if (!hasShownMessage) { if (!hasShownMessage) {
if (this.syncClient.settings.getSettings().isSyncEnabled) { if (this.syncClient.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

@ -26,7 +26,7 @@ export class StatusDescription {
} }
); );
this.syncClient.settings.addOnSettingsChangeHandlers(() => { this.syncClient.addOnSettingsChangeHandlers(() => {
void this.updateConnectionState(); void this.updateConnectionState();
}); });
} }
@ -67,8 +67,8 @@ 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.syncClient.settings.getSettings().remoteUri, text: this.syncClient.getSettings().remoteUri,
href: this.syncClient.settings.getSettings().remoteUri href: this.syncClient.getSettings().remoteUri
}); });
container.createSpan({ container.createSpan({
@ -87,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.syncClient.settings.getSettings().isSyncEnabled) { if (this.syncClient.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."
}); });

View file

@ -50,15 +50,6 @@ export class Settings {
return this.settings; return this.settings;
} }
public async setSettings(value: SyncSettings): Promise<void> {
const oldSettings = this.settings;
this.settings = value;
this.onSettingsChangeHandlers.forEach((handler) => {
handler(value, oldSettings);
});
await this.save();
}
public addOnSettingsChangeHandlers( public addOnSettingsChangeHandlers(
handler: (settings: SyncSettings, oldSettings: SyncSettings) => void handler: (settings: SyncSettings, oldSettings: SyncSettings) => void
): void { ): void {
@ -74,6 +65,15 @@ export class Settings {
await this.setSettings(newSettings); await this.setSettings(newSettings);
} }
private async setSettings(value: SyncSettings): Promise<void> {
const oldSettings = this.settings;
this.settings = value;
this.onSettingsChangeHandlers.forEach((handler) => {
handler(value, oldSettings);
});
await this.save();
}
private async save(): Promise<void> { private async save(): Promise<void> {
await this.saveData(this.settings); await this.saveData(this.settings);
} }

View file

@ -43,7 +43,7 @@ export class MockAgent extends MockClient {
}; };
this.client.logger.addOnMessageListener((logLine: LogLine) => { this.client.logger.addOnMessageListener((logLine: LogLine) => {
const state = this.client.settings.getSettings().isSyncEnabled const state = this.client.getSettings().isSyncEnabled
? "(online) " ? "(online) "
: "(offline)"; : "(offline)";
const formatted = `[${this.name} ${state}] ${logLine.timestamp.toISOString()} ${logLine.level} ${logLine.message}`; const formatted = `[${this.name} ${state}] ${logLine.timestamp.toISOString()} ${logLine.level} ${logLine.message}`;
@ -91,7 +91,7 @@ export class MockAgent extends MockClient {
this.changeFetchChangesUpdateIntervalMsAction.bind(this) this.changeFetchChangesUpdateIntervalMsAction.bind(this)
]; ];
if (this.client.settings.getSettings().isSyncEnabled) { if (this.client.getSettings().isSyncEnabled) {
if (this.doNotTouchWhileOffline.length === 0) { if (this.doNotTouchWhileOffline.length === 0) {
options.push(this.disableSyncAction.bind(this)); options.push(this.disableSyncAction.bind(this));
} }
@ -131,7 +131,7 @@ export class MockAgent extends MockClient {
} }
public async finish(): Promise<void> { public async finish(): Promise<void> {
await this.client.settings.setSetting("isSyncEnabled", true); await this.client.setSetting("isSyncEnabled", true);
await Promise.all(this.pendingActions); await Promise.all(this.pendingActions);
this.client.stop(); this.client.stop();
await this.client.syncer.waitForSyncQueue(); await this.client.syncer.waitForSyncQueue();
@ -239,7 +239,7 @@ export class MockAgent extends MockClient {
const file = this.getFileName(); const file = this.getFileName();
if ( if (
(!this.client.settings.getSettings().isSyncEnabled && (!this.client.getSettings().isSyncEnabled &&
this.doNotTouchWhileOffline.includes(file)) || this.doNotTouchWhileOffline.includes(file)) ||
(await this.exists(file)) (await this.exists(file))
) { ) {
@ -258,7 +258,7 @@ export class MockAgent extends MockClient {
this.client.logger.info( this.client.logger.info(
`Decided to change fetchChangesUpdateIntervalMs` `Decided to change fetchChangesUpdateIntervalMs`
); );
return this.client.settings.setSetting( return this.client.setSetting(
"fetchChangesUpdateIntervalMs", "fetchChangesUpdateIntervalMs",
Math.random() * 2000 + 100 Math.random() * 2000 + 100
); );
@ -266,12 +266,12 @@ export class MockAgent extends MockClient {
private async disableSyncAction(): Promise<void> { private async disableSyncAction(): Promise<void> {
this.client.logger.info(`Decided to disable sync`); this.client.logger.info(`Decided to disable sync`);
await this.client.settings.setSetting("isSyncEnabled", false); await this.client.setSetting("isSyncEnabled", false);
} }
private async enableSyncAction(): Promise<void> { private async enableSyncAction(): Promise<void> {
this.client.logger.info(`Decided to enable sync`); this.client.logger.info(`Decided to enable sync`);
await this.client.settings.setSetting("isSyncEnabled", true); await this.client.setSetting("isSyncEnabled", true);
} }
private async renameFileAction(files: RelativePath[]): Promise<void> { private async renameFileAction(files: RelativePath[]): Promise<void> {
@ -280,7 +280,7 @@ export class MockAgent extends MockClient {
// We can't edit files offline that have been updated while offline. // We can't edit files offline that have been updated while offline.
// Otherwise, the resolution logic couldn't handle it. // Otherwise, the resolution logic couldn't handle it.
if ( if (
!this.client.settings.getSettings().isSyncEnabled && !this.client.getSettings().isSyncEnabled &&
this.doNotTouchWhileOffline.includes(file) this.doNotTouchWhileOffline.includes(file)
) { ) {
this.client.logger.info( this.client.logger.info(
@ -292,7 +292,7 @@ export class MockAgent extends MockClient {
const newName = this.getFileName(); const newName = this.getFileName();
if ( if (
(!this.client.settings.getSettings().isSyncEnabled && (!this.client.getSettings().isSyncEnabled &&
this.doNotTouchWhileOffline.includes(newName)) || this.doNotTouchWhileOffline.includes(newName)) ||
(await this.exists(newName)) (await this.exists(newName))
) { ) {
@ -311,7 +311,7 @@ export class MockAgent extends MockClient {
// We can't edit files offline that have been updated while offline. // We can't edit files offline that have been updated while offline.
// Otherwise, the resolution logic couldn't handle it. // Otherwise, the resolution logic couldn't handle it.
if ( if (
!this.client.settings.getSettings().isSyncEnabled && !this.client.getSettings().isSyncEnabled &&
this.doNotTouchWhileOffline.includes(file) this.doNotTouchWhileOffline.includes(file)
) { ) {
this.client.logger.info( this.client.logger.info(

View file

@ -25,7 +25,7 @@ export class MockClient implements FileSystemOperations {
await Promise.all( await Promise.all(
Object.keys(this.initialSettings).map(async (key) => { Object.keys(this.initialSettings).map(async (key) => {
const settingKey = key as keyof SyncSettings; // eslint-disable-line @typescript-eslint/no-unsafe-type-assertion const settingKey = key as keyof SyncSettings; // eslint-disable-line @typescript-eslint/no-unsafe-type-assertion
return this.client.settings.setSetting( return this.client.setSetting(
settingKey, settingKey,
this.initialSettings[settingKey]! // eslint-disable-line @typescript-eslint/no-non-null-assertion this.initialSettings[settingKey]! // eslint-disable-line @typescript-eslint/no-non-null-assertion
); );