|
1 | 1 | import { batch, createContext, onCleanup, useContext, type ParentProps } from "solid-js" |
2 | 2 | import { createStore, produce, reconcile, type Store } from "solid-js/store" |
3 | 3 | import path from "path" |
4 | | -import { mkdirSync, readFileSync, watch } from "fs" |
| 4 | +import { mkdirSync, readFileSync } from "fs" |
5 | 5 | import { Flock } from "@opencode/util/flock" |
6 | 6 | import { writeJsonAtomic } from "../util/persistence" |
| 7 | +import { safeWatch } from "../util/watch" |
7 | 8 | import { useTuiApp, useTuiPaths } from "./runtime" |
8 | 9 |
|
9 | 10 | type Options<Value extends object> = { |
@@ -111,26 +112,16 @@ export function createStorage(root: string, channel: string) { |
111 | 112 | } |
112 | 113 |
|
113 | 114 | let reloadTimer: ReturnType<typeof setTimeout> | undefined |
114 | | - let watcher: ReturnType<typeof watch> | undefined |
115 | | - try { |
116 | | - watcher = watch(directory, () => { |
117 | | - clearTimeout(reloadTimer) |
118 | | - // Atomic writes notify for the temporary file before its final rename, and some |
119 | | - // platforms coalesce the rename event. Reload after the event burst has settled. |
120 | | - reloadTimer = setTimeout(() => entries.forEach((entry) => entry.reload()), 50) |
121 | | - }) |
122 | | - watcher.on("error", (error) => { |
123 | | - clearTimeout(reloadTimer) |
124 | | - watcher?.close() |
125 | | - watcher = undefined |
126 | | - console.error("Storage directory watcher failed, live-reload disabled", { directory, error }) |
127 | | - }) |
128 | | - } catch (error) { |
129 | | - // fs.watch throws synchronously (e.g. ENOSPC when the inotify watch limit is |
130 | | - // exhausted). Losing cross-process live-reload is recoverable; crashing the |
131 | | - // whole TUI over it is not, so degrade instead of propagating. |
132 | | - console.error("Failed to watch storage directory, live-reload disabled", { directory, error }) |
133 | | - } |
| 115 | + let watcher = safeWatch(directory, () => { |
| 116 | + clearTimeout(reloadTimer) |
| 117 | + // Atomic writes notify for the temporary file before its final rename, and some |
| 118 | + // platforms coalesce the rename event. Reload after the event burst has settled. |
| 119 | + reloadTimer = setTimeout(() => entries.forEach((entry) => entry.reload()), 50) |
| 120 | + }) |
| 121 | + watcher?.on("error", () => { |
| 122 | + clearTimeout(reloadTimer) |
| 123 | + watcher = undefined |
| 124 | + }) |
134 | 125 | return { |
135 | 126 | storage, |
136 | 127 | close: () => { |
|
0 commit comments