mirror of
https://github.com/vrtmrz/obsidian-livesync.git
synced 2026-08-28 22:37:08 +00:00
Limit commands to applicable contexts
This commit is contained in:
@@ -54,10 +54,7 @@ import { EVENT_SETTING_SAVED, eventHub } from "@/common/events.ts";
|
||||
import { Semaphore } from "octagonal-wheels/concurrency/semaphore";
|
||||
import type { LiveSyncCore } from "@/main.ts";
|
||||
import { tryGetFilePath } from "@vrtmrz/livesync-commonlib/compat/common/utils.doc";
|
||||
import {
|
||||
configureHiddenFileSyncMode,
|
||||
type ConfigureHiddenFileSyncResult,
|
||||
} from "./configureHiddenFileSyncMode.ts";
|
||||
import { configureHiddenFileSyncMode, type ConfigureHiddenFileSyncResult } from "./configureHiddenFileSyncMode.ts";
|
||||
import type { OptionalSyncFeatureMode } from "@/features/optionalSyncFeatures.ts";
|
||||
import { getObsidianCommunityPluginManager } from "@/common/obsidianCommunityPlugins.ts";
|
||||
type SyncDirection = "push" | "pull" | "safe" | "pullForce" | "pushForce";
|
||||
@@ -109,37 +106,45 @@ export class HiddenFileSync extends LiveSyncCommands {
|
||||
this.services.API.addCommand({
|
||||
id: "livesync-sync-internal",
|
||||
name: "(re)initialise hidden files between storage and database",
|
||||
callback: () => {
|
||||
if (this.isReady()) {
|
||||
checkCallback: (checking) => {
|
||||
if (!this.isManualCommandAvailable()) return false;
|
||||
if (!checking) {
|
||||
void this.initialiseInternalFileSync("safe", true);
|
||||
}
|
||||
return true;
|
||||
},
|
||||
});
|
||||
this.services.API.addCommand({
|
||||
id: "livesync-scaninternal-storage",
|
||||
name: "Scan hidden file changes on the storage",
|
||||
callback: () => {
|
||||
if (this.isReady()) {
|
||||
checkCallback: (checking) => {
|
||||
if (!this.isManualCommandAvailable()) return false;
|
||||
if (!checking) {
|
||||
void this.scanAllStorageChanges(true);
|
||||
}
|
||||
return true;
|
||||
},
|
||||
});
|
||||
this.services.API.addCommand({
|
||||
id: "livesync-scaninternal-database",
|
||||
name: "Scan hidden file changes on the local database",
|
||||
callback: () => {
|
||||
if (this.isReady()) {
|
||||
checkCallback: (checking) => {
|
||||
if (!this.isManualCommandAvailable()) return false;
|
||||
if (!checking) {
|
||||
void this.scanAllDatabaseChanges(true);
|
||||
}
|
||||
return true;
|
||||
},
|
||||
});
|
||||
this.services.API.addCommand({
|
||||
id: "livesync-internal-scan-offline-changes",
|
||||
name: "Scan and apply all offline hidden-file changes",
|
||||
callback: () => {
|
||||
if (this.isReady()) {
|
||||
checkCallback: (checking) => {
|
||||
if (!this.isManualCommandAvailable()) return false;
|
||||
if (!checking) {
|
||||
void this.applyOfflineChanges(true);
|
||||
}
|
||||
return true;
|
||||
},
|
||||
});
|
||||
eventHub.onEvent(EVENT_SETTING_SAVED, () => {
|
||||
@@ -191,12 +196,16 @@ export class HiddenFileSync extends LiveSyncCommands {
|
||||
}
|
||||
|
||||
isReady() {
|
||||
if (!this._isMainReady) return false;
|
||||
if (!this._isMainReady()) return false;
|
||||
if (this._isMainSuspended()) return false;
|
||||
if (!this.isThisModuleEnabled()) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
private isManualCommandAvailable() {
|
||||
return this.settings.useAdvancedMode && this.isReady() && this._isDatabaseReady();
|
||||
}
|
||||
|
||||
async performStartupScan(showNotice: boolean) {
|
||||
await this.applyOfflineChanges(showNotice);
|
||||
}
|
||||
|
||||
@@ -8,13 +8,16 @@ vi.mock("@/features/HiddenFileCommon/JsonResolveModal.ts", () => ({
|
||||
vi.mock("@/features/LiveSyncCommands.ts", () => ({
|
||||
LiveSyncCommands: class LiveSyncCommands {
|
||||
plugin!: { app: unknown };
|
||||
core!: { services: unknown };
|
||||
core!: { services: unknown; settings: unknown };
|
||||
get app() {
|
||||
return this.plugin.app;
|
||||
}
|
||||
get services() {
|
||||
return this.core.services;
|
||||
}
|
||||
get settings() {
|
||||
return this.core.settings;
|
||||
}
|
||||
},
|
||||
}));
|
||||
vi.mock("./configureHiddenFileSyncMode.ts", () => ({
|
||||
@@ -25,6 +28,66 @@ import { HiddenFileSync } from "./CmdHiddenFileSync.ts";
|
||||
import { configureHiddenFileSyncMode } from "./configureHiddenFileSyncMode.ts";
|
||||
|
||||
describe("HiddenFileSync configuration-change notices", () => {
|
||||
it("shows manual Hidden File Sync commands only when the feature, Advanced mode, and runtime are ready", () => {
|
||||
const commands: Array<{
|
||||
id: string;
|
||||
checkCallback?: (checking: boolean) => boolean | void;
|
||||
}> = [];
|
||||
const settings = {
|
||||
syncInternalFiles: false,
|
||||
useAdvancedMode: false,
|
||||
};
|
||||
const hiddenFileSync = Object.create(HiddenFileSync.prototype) as HiddenFileSync;
|
||||
Object.assign(hiddenFileSync, {
|
||||
core: {
|
||||
settings,
|
||||
services: {
|
||||
API: {
|
||||
addCommand: vi.fn((command) => commands.push(command)),
|
||||
},
|
||||
},
|
||||
},
|
||||
_isMainReady: vi.fn(() => true),
|
||||
_isMainSuspended: vi.fn(() => false),
|
||||
_isDatabaseReady: vi.fn(() => true),
|
||||
});
|
||||
|
||||
hiddenFileSync.onload();
|
||||
|
||||
const commandIds = [
|
||||
"livesync-sync-internal",
|
||||
"livesync-scaninternal-storage",
|
||||
"livesync-scaninternal-database",
|
||||
"livesync-internal-scan-offline-changes",
|
||||
];
|
||||
for (const commandId of commandIds) {
|
||||
const command = commands.find(({ id }) => id === commandId);
|
||||
expect(command?.checkCallback?.(true)).toBe(false);
|
||||
}
|
||||
|
||||
settings.syncInternalFiles = true;
|
||||
settings.useAdvancedMode = true;
|
||||
for (const commandId of commandIds) {
|
||||
const command = commands.find(({ id }) => id === commandId);
|
||||
expect(command?.checkCallback?.(true)).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it("does not report Hidden File Sync as ready before the main runtime is ready", () => {
|
||||
const hiddenFileSync = Object.create(HiddenFileSync.prototype) as HiddenFileSync;
|
||||
Object.assign(hiddenFileSync, {
|
||||
core: {
|
||||
settings: {
|
||||
syncInternalFiles: true,
|
||||
},
|
||||
},
|
||||
_isMainReady: vi.fn(() => false),
|
||||
_isMainSuspended: vi.fn(() => false),
|
||||
});
|
||||
|
||||
expect(hiddenFileSync.isReady()).toBe(false);
|
||||
});
|
||||
|
||||
it("groups plug-in reloads and an Obsidian restart into one finished Notice", async () => {
|
||||
const noticeGroups = {
|
||||
setItem: vi.fn(),
|
||||
|
||||
Reference in New Issue
Block a user