Skip to content

Commit

Permalink
fix: improve file watching and hook triggers for document operations …
Browse files Browse the repository at this point in the history
…on Windows
  • Loading branch information
purocean committed Feb 5, 2025
1 parent f417fdf commit 7b6bd99
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/renderer/plugins/watch-file-refresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ export default {
const watchHandler = await ctx.api.watchFs(
repo,
path,
{ awaitWriteFinish: { stabilityThreshold: 500, pollInterval: 50 }, alwaysStat: true },
{
awaitWriteFinish: { stabilityThreshold: 500, pollInterval: 50 },
alwaysStat: true,
usePolling: ctx.env.isWindows, // fix parent folder rename / delete on Windows https://github.com/paulmillr/chokidar/issues/664
},
payload => {
logger.debug('startWatch onResult', payload)
if (payload.eventName === 'ready') {
Expand Down
2 changes: 2 additions & 0 deletions src/renderer/services/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ export async function deleteDoc (doc: PathItem, skipConfirm = false) {
}

try {
triggerHook('DOC_BEFORE_DELETE', { doc })
await api.deleteFile(doc, true)
} catch (error: any) {
const force = await useModal().confirm({
Expand Down Expand Up @@ -571,6 +572,7 @@ export async function moveDoc (doc: Doc, newPath?: string) {
}

try {
triggerHook('DOC_BEFORE_MOVE', { doc, newDoc })
await api.moveFile(doc, newPath)
triggerHook('DOC_MOVED', { oldDoc: doc, newDoc })
} catch (error: any) {
Expand Down
13 changes: 11 additions & 2 deletions src/renderer/startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { init } from '@fe/core/plugin'
import { getActionHandler } from '@fe/core/action'
import { registerHook, triggerHook } from '@fe/core/hook'
import store from '@fe/support/store'
import { isElectron } from '@fe/support/env'
import { isElectron, isWindows } from '@fe/support/env'
import { useToast } from './support/ui/toast'
import { useModal } from '@fe/support/ui/modal'
import type { BuildInSettings, Doc, FrontMatterAttrs } from '@fe/types'
import type { BuildInSettings, Doc, FrontMatterAttrs, PathItem } from '@fe/types'
import { reloadMainWindow } from '@fe/services/base'
import { createDoc, isMarkdownFile, isMarked, markDoc, switchDoc, toUri, unmarkDoc } from '@fe/services/document'
import { DEFAULT_MARKDOWN_EDITOR_NAME, whenEditorReady } from '@fe/services/editor'
Expand Down Expand Up @@ -55,6 +55,13 @@ function switchDefaultPreviewer () {
}
}

function reWatchFsOnWindows ({ doc }: { doc: PathItem & { type?: Doc['type'] }}) {
// fix parent folder rename / delete on Windows https://github.com/paulmillr/chokidar/issues/664
if (isWindows && doc.type === 'dir') {
setTimeout(indexer.triggerWatchCurrentRepo, 100)
}
}

let autoRefreshedAt = 0
const refreshTree = async () => {
await tree.refreshTree()
Expand All @@ -70,6 +77,8 @@ registerHook('DOC_CREATED', refreshTree)
registerHook('DOC_DELETED', refreshTree)
registerHook('DOC_MOVED', refreshTree)
registerHook('DOC_SWITCH_FAILED', refreshTree)
registerHook('DOC_BEFORE_DELETE', reWatchFsOnWindows)
registerHook('DOC_BEFORE_MOVE', reWatchFsOnWindows)

registerHook('INDEXER_FS_CHANGE', async () => {
if (Date.now() - autoRefreshedAt > 3000) {
Expand Down
2 changes: 2 additions & 0 deletions src/renderer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,9 @@ export type BuildInHookTypes = {
EDITOR_CURRENT_EDITOR_CHANGE: { current?: CustomEditor | null },
EDITOR_CONTENT_CHANGE: { uri: string, value: string },
DOC_CREATED: { doc: Doc },
DOC_BEFORE_DELETE: { doc: PathItem },
DOC_DELETED: { doc: PathItem },
DOC_BEFORE_MOVE: { doc: Doc, newDoc: Doc },
DOC_MOVED: { oldDoc: Doc, newDoc: Doc },
DOC_PRE_SWITCH: { doc?: Doc | null, opts?: SwitchDocOpts },
DOC_BEFORE_SAVE: { doc: Doc, content: string },
Expand Down

0 comments on commit 7b6bd99

Please sign in to comment.