summaryrefslogtreecommitdiffhomepage
path: root/packages/app/src/context/file/watcher.ts
diff options
context:
space:
mode:
authorAdam <[email protected]>2026-02-06 10:02:31 -0600
committerGitHub <[email protected]>2026-02-06 10:02:31 -0600
commit2c58dd6203df7806f57ef6b29672091cb764e871 (patch)
tree10fca96d3098465b497f78e29de8d0a585c4dac3 /packages/app/src/context/file/watcher.ts
parenta4bc883595df9ea0f752079519081bc602408553 (diff)
downloadopencode-2c58dd6203df7806f57ef6b29672091cb764e871.tar.gz
opencode-2c58dd6203df7806f57ef6b29672091cb764e871.zip
chore: refactoring and tests, splitting up files (#12495)
Diffstat (limited to 'packages/app/src/context/file/watcher.ts')
-rw-r--r--packages/app/src/context/file/watcher.ts52
1 files changed, 52 insertions, 0 deletions
diff --git a/packages/app/src/context/file/watcher.ts b/packages/app/src/context/file/watcher.ts
new file mode 100644
index 000000000..a3a98eae4
--- /dev/null
+++ b/packages/app/src/context/file/watcher.ts
@@ -0,0 +1,52 @@
+import type { FileNode } from "@opencode-ai/sdk/v2"
+
+type WatcherEvent = {
+ type: string
+ properties: unknown
+}
+
+type WatcherOps = {
+ normalize: (input: string) => string
+ hasFile: (path: string) => boolean
+ loadFile: (path: string) => void
+ node: (path: string) => FileNode | undefined
+ isDirLoaded: (path: string) => boolean
+ refreshDir: (path: string) => void
+}
+
+export function invalidateFromWatcher(event: WatcherEvent, ops: WatcherOps) {
+ if (event.type !== "file.watcher.updated") return
+ const props =
+ typeof event.properties === "object" && event.properties ? (event.properties as Record<string, unknown>) : undefined
+ const rawPath = typeof props?.file === "string" ? props.file : undefined
+ const kind = typeof props?.event === "string" ? props.event : undefined
+ if (!rawPath) return
+ if (!kind) return
+
+ const path = ops.normalize(rawPath)
+ if (!path) return
+ if (path.startsWith(".git/")) return
+
+ if (ops.hasFile(path)) {
+ ops.loadFile(path)
+ }
+
+ if (kind === "change") {
+ const dir = (() => {
+ if (path === "") return ""
+ const node = ops.node(path)
+ if (node?.type !== "directory") return
+ return path
+ })()
+ if (dir === undefined) return
+ if (!ops.isDirLoaded(dir)) return
+ ops.refreshDir(dir)
+ return
+ }
+ if (kind !== "add" && kind !== "unlink") return
+
+ const parent = path.split("/").slice(0, -1).join("/")
+ if (!ops.isDirLoaded(parent)) return
+
+ ops.refreshDir(parent)
+}