MCPcopy
hub / github.com/marktext/marktext / watch

Method watch

packages/desktop/src/main/filesystem/watcher.ts:191–339  ·  view source on GitHub ↗
(win: BrowserWindow, watchPath: string, type: WatchType = 'dir')

Source from the content-addressed store, hash-verified

189 }
190
191 watch(win: BrowserWindow, watchPath: string, type: WatchType = 'dir'): () => void {
192 const usePolling = isOsx ? true : this._preferences.getItem('watcherUsePolling')
193
194 const id = getUniqueId()
195
196 const watcher = chokidar.watch(watchPath, {
197 ignored: (pathname: string, fileInfo?: { isDirectory: () => boolean }) => {
198 if (!fileInfo) {
199 return /(?:^|[/\\])(?:node_modules|(?:.+\.asar))/.test(pathname)
200 }
201
202 if (/(?:^|[/\\])(?:node_modules|(?:.+\.asar))/.test(pathname)) {
203 return true
204 }
205
206 if (
207 checkPathExcludePattern(pathname, this._preferences.getItem('treePathExcludePatterns'))
208 ) {
209 return true
210 }
211 if (fileInfo.isDirectory()) {
212 return false
213 }
214 return !hasMarkdownExtension(pathname)
215 },
216 ignoreInitial: type === 'file',
217 persistent: true,
218 ignorePermissionErrors: true,
219
220 depth: type === 'file' ? (isOsx ? 1 : 0) : undefined,
221
222 // Please see GH#1043
223 awaitWriteFinish: {
224 stabilityThreshold: WATCHER_STABILITY_THRESHOLD,
225 pollInterval: WATCHER_STABILITY_POLL_INTERVAL
226 },
227
228 usePolling
229 // eslint-disable-next-line @typescript-eslint/no-explicit-any -- chokidar's `ignored` callback signature varies between versions; this options bag works at runtime but defies the bundled type
230 } as any)
231
232 let disposed = false
233 let enospcReached = false
234 let renameTimer: NodeJS.Timeout | null = null
235
236 watcher
237 .on('add', async(pathname: string) => {
238 if (!(await this._shouldIgnoreEvent(win.id, pathname, type, usePolling))) {
239 const { _preferences } = this
240 const eol = _preferences.getPreferredEol() as LineEnding
241 const { autoGuessEncoding, trimTrailingNewline, autoNormalizeLineEndings } =
242 _preferences.getAll()
243 add(
244 win,
245 pathname,
246 type,
247 eol,
248 autoGuessEncoding,

Callers 2

watchDirectoryFunction · 0.80
_listenForIpcMainMethod · 0.80

Calls 15

_shouldIgnoreEventMethod · 0.95
getUniqueIdFunction · 0.90
checkPathExcludePatternFunction · 0.90
hasMarkdownExtensionFunction · 0.90
existsFunction · 0.90
addFunction · 0.85
changeFunction · 0.85
unlinkFunction · 0.85
addDirFunction · 0.85
unlinkDirFunction · 0.85
isDirectoryMethod · 0.80
getPreferredEolMethod · 0.80

Tested by

no test coverage detected