refactor: simplify note mutation flushing

This commit is contained in:
lucaronin
2026-04-28 06:25:39 +02:00
parent b85d54bb5d
commit 2a224f78f8
4 changed files with 18 additions and 38 deletions

View File

@@ -15,8 +15,7 @@ export interface NoteActionsConfig {
removeEntry: (path: string) => void
entries: VaultEntry[]
flushBeforeNoteSwitch?: (path: string) => Promise<void>
flushBeforeFrontmatterChange?: (path: string) => Promise<void>
flushBeforePathRename?: (path: string) => Promise<void>
flushBeforeNoteMutation?: (path: string) => Promise<void>
reloadVault?: () => Promise<unknown>
setToastMessage: (msg: string | null) => void
updateEntry: (path: string, patch: Partial<VaultEntry>) => void
@@ -127,30 +126,14 @@ interface MaybeRenameAfterFrontmatterUpdateParams {
deps: TitleRenameDeps
}
async function flushBeforeTitleRename(
async function flushBeforeNoteMutation(
path: string,
key: string,
value: FrontmatterValue,
flushBeforePathRename?: (path: string) => Promise<void>,
flushBeforeMutation?: (path: string) => Promise<void>,
): Promise<boolean> {
if (!shouldRenameOnTitleUpdate(key, value) || !flushBeforePathRename) return true
if (!flushBeforeMutation) return true
try {
await flushBeforePathRename(path)
return true
} catch {
return false
}
}
async function flushBeforeFrontmatterMutation(
path: string,
flushBeforeFrontmatterChange?: (path: string) => Promise<void>,
): Promise<boolean> {
if (!flushBeforeFrontmatterChange) return true
try {
await flushBeforeFrontmatterChange(path)
await flushBeforeMutation(path)
return true
} catch {
return false
@@ -196,12 +179,9 @@ async function updateFrontmatterAndMaybeRename({
runFrontmatterOp,
value,
}: UpdateFrontmatterAndMaybeRenameParams): Promise<void> {
const canFlush = await flushBeforeFrontmatterMutation(path, config.flushBeforeFrontmatterChange)
const canFlush = await flushBeforeNoteMutation(path, config.flushBeforeNoteMutation)
if (!canFlush) return
const canRename = await flushBeforeTitleRename(path, key, value, config.flushBeforePathRename)
if (!canRename) return
const newContent = await runFrontmatterOp('update', path, key, value, options)
if (!applyFrontmatterCallbacks({ config, path, newContent })) return
@@ -289,7 +269,7 @@ function useFrontmatterActionHandlers({
}, [activeTabPathRef, config, handleSwitchTab, renameTabsRef, runFrontmatterOp, setTabs, setToastMessage, updateTabContent])
const handleDeleteProperty = useCallback(async (path: string, key: string, options?: FrontmatterOpOptions) => {
const canFlush = await flushBeforeFrontmatterMutation(path, config.flushBeforeFrontmatterChange)
const canFlush = await flushBeforeNoteMutation(path, config.flushBeforeNoteMutation)
if (!canFlush) return
const newContent = await runFrontmatterOp('delete', path, key, undefined, options)
@@ -298,7 +278,7 @@ function useFrontmatterActionHandlers({
}, [config, runFrontmatterOp])
const handleAddProperty = useCallback(async (path: string, key: string, value: FrontmatterValue) => {
const canFlush = await flushBeforeFrontmatterMutation(path, config.flushBeforeFrontmatterChange)
const canFlush = await flushBeforeNoteMutation(path, config.flushBeforeNoteMutation)
if (!canFlush) return
const newContent = await runFrontmatterOp('update', path, key, value)