Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions packages/app/src/context/file/watcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,61 @@ describe("file watcher invalidation", () => {
expect(loads).toEqual(["src/open.ts"])
})

test("normalizes watcher paths before matching open files", () => {
const loads: string[] = []

invalidateFromWatcher(
{
type: "file.watcher.updated",
properties: {
file: "src\\open.ts",
event: "change",
},
},
{
normalize: (input) => input,
hasFile: () => false,
isOpen: (path) => path === "src/open.ts",
loadFile: (path) => loads.push(path),
node: () => ({
path: "src/open.ts",
type: "file",
name: "open.ts",
absolute: "/repo/src/open.ts",
ignored: false,
}),
isDirLoaded: () => false,
refreshDir: () => {},
},
)

expect(loads).toEqual(["src/open.ts"])
})

test("refreshes nearest loaded ancestor on add", () => {
const refresh: string[] = []

invalidateFromWatcher(
{
type: "file.watcher.updated",
properties: {
file: "src/nested/deep/new.ts",
event: "add",
},
},
{
normalize: (input) => input,
hasFile: () => false,
loadFile: () => {},
node: () => undefined,
isDirLoaded: (path) => path === "src",
refreshDir: (path) => refresh.push(path),
},
)

expect(refresh).toEqual(["src"])
})

test("refreshes only changed loaded directory nodes", () => {
const refresh: string[] = []

Expand Down
20 changes: 17 additions & 3 deletions packages/app/src/context/file/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ type WatcherOps = {
refreshDir: (path: string) => void
}

function toWatcherKey(path: string) {
return path.replace(/\\/g, "/")
}

function nearestLoadedParent(path: string, ops: WatcherOps) {
const parts = path.split("/").slice(0, -1)
while (true) {
const dir = parts.join("/")
if (ops.isDirLoaded(dir)) return dir
if (parts.length === 0) return
parts.pop()
}
}

export function invalidateFromWatcher(event: WatcherEvent, ops: WatcherOps) {
if (event.type !== "file.watcher.updated") return
const props =
Expand All @@ -24,7 +38,7 @@ export function invalidateFromWatcher(event: WatcherEvent, ops: WatcherOps) {
if (!rawPath) return
if (!kind) return

const path = ops.normalize(rawPath)
const path = toWatcherKey(ops.normalize(rawPath))
if (!path) return
if (path.startsWith(".git/")) return

Expand All @@ -46,8 +60,8 @@ export function invalidateFromWatcher(event: WatcherEvent, ops: WatcherOps) {
}
if (kind !== "add" && kind !== "unlink") return

const parent = path.split("/").slice(0, -1).join("/")
if (!ops.isDirLoaded(parent)) return
const parent = nearestLoadedParent(path, ops)
if (parent === undefined) return

ops.refreshDir(parent)
}
Loading