fix(syncguard): notify local preference changes without reapplying sync settings

This commit is contained in:
Artem Ovchinnikov committed 2026-10-29 13:09:26 +03:00
1 parent 3eabc2b305
commit ed6408cc5a
5 files changed
+29 -6

No files matched your search

@@ -8,6 +8,7 @@ import {
type SettingServiceDependencies,
} from "@vrtmrz/livesync-commonlib/compat/services/base/SettingService";
import type { ObsidianServiceContext } from "@/modules/services/ObsidianServiceContext";
import { EVENT_SYNC_GUARD_PREFERENCES_CHANGED } from "@/common/events.ts";
export type SyncGuardSettings = {
syncGuardEnabled: boolean;
@@ -92,7 +93,9 @@ export class ObsidianSettingService<T extends ObsidianServiceContext> extends Se
if (this._syncGuardSettings[key] === value) return;
await this.config.setObject(key, value);
this._syncGuardSettings = { ...this._syncGuardSettings, [key]: value };
this.context.events.emitEvent(EVENT_SETTING_SAVED, this.currentSettings());
// These preferences change only the workspace overlay. Re-applying all
// replication settings would unnecessarily interrupt an active sync.
this.context.events.emitEvent(EVENT_SYNC_GUARD_PREFERENCES_CHANGED);
}
protected override async saveData(data: ObsidianLiveSyncSettings): Promise<void> {
@@ -2,6 +2,7 @@ import { describe, expect, it, vi } from "vitest";
import { createServiceContext } from "@vrtmrz/livesync-commonlib/context";
import { DEFAULT_SETTINGS, type ObsidianLiveSyncSettings } from "@vrtmrz/livesync-commonlib/compat/common/types";
import { EVENT_SETTING_SAVED } from "@vrtmrz/livesync-commonlib/compat/events/coreEvents";
import { EVENT_SYNC_GUARD_PREFERENCES_CHANGED } from "@/common/events.ts";
import type { IAPIService } from "@vrtmrz/livesync-commonlib/compat/services/base/IService";
import { MemoryValueStore } from "@vrtmrz/value-store";
import type { ObsidianServiceContext } from "@/modules/services/ObsidianServiceContext";
@@ -142,7 +143,9 @@ describe("SyncGuard device-local settings", () => {
await second.loadSettings();
const initialSaveCount = saveData.mock.calls.length;
const changed = vi.fn();
first.context.events.onEvent(EVENT_SETTING_SAVED, changed);
const unrelatedReplicationSettingsSaved = vi.fn();
first.context.events.onEvent(EVENT_SYNC_GUARD_PREFERENCES_CHANGED, changed);
first.context.events.onEvent(EVENT_SETTING_SAVED, unrelatedReplicationSettingsSaved);
await setSyncGuardSetting(first, "syncGuardEnabled", true);
await setSyncGuardSetting(first, "syncGuardMinimumFiles", 8);
@@ -153,6 +156,7 @@ describe("SyncGuard device-local settings", () => {
await expect(localValues.getObject("first-vault--syncGuardEnabled")).resolves.toBe(true);
await expect(localValues.getObject("first-vault--syncGuardMinimumFiles")).resolves.toBe(8);
expect(changed).toHaveBeenCalledTimes(2);
expect(unrelatedReplicationSettingsSaved).not.toHaveBeenCalled();
expect(saveData).toHaveBeenCalledTimes(initialSaveCount);
});
});