forked from marktext/marktext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatcher.js
More file actions
374 lines (338 loc) · 11 KB
/
watcher.js
File metadata and controls
374 lines (338 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import path from 'path'
import fsPromises from 'fs/promises'
import log from 'electron-log'
import chokidar from 'chokidar'
import { exists } from 'common/filesystem'
import { hasMarkdownExtension } from 'common/filesystem/paths'
import { getUniqueId } from '../utils'
import { loadMarkdownFile } from '../filesystem/markdown'
import { isLinux, isOsx } from '../config'
// TODO(refactor): Please see GH#1035.
export const WATCHER_STABILITY_THRESHOLD = 1000
export const WATCHER_STABILITY_POLL_INTERVAL = 150
const EVENT_NAME = {
dir: 'mt::update-object-tree',
file: 'mt::update-file'
}
const add = async (win, pathname, type, endOfLine, autoGuessEncoding, trimTrailingNewline) => {
const stats = await fsPromises.stat(pathname)
const birthTime = stats.birthtime
const isMarkdown = hasMarkdownExtension(pathname)
const file = {
pathname,
name: path.basename(pathname),
isFile: true,
isDirectory: false,
birthTime,
isMarkdown
}
if (isMarkdown) {
// HACK: But this should be removed completely in #1034/#1035.
try {
const data = await loadMarkdownFile(
pathname,
endOfLine,
autoGuessEncoding,
trimTrailingNewline
)
file.data = data
} catch (err) {
// Only notify user about opened files.
if (type === 'file') {
win.webContents.send('mt::show-notification', {
title: 'Watcher I/O error',
type: 'error',
message: err.message
})
return
}
}
win.webContents.send(EVENT_NAME[type], {
type: 'add',
change: file
})
}
}
const unlink = (win, pathname, type) => {
const file = { pathname }
win.webContents.send(EVENT_NAME[type], {
type: 'unlink',
change: file
})
}
const change = async (win, pathname, type, endOfLine, autoGuessEncoding, trimTrailingNewline) => {
// No need to update the tree view if the file content has changed.
if (type === 'dir') return
const isMarkdown = hasMarkdownExtension(pathname)
if (isMarkdown) {
// HACK: Markdown data should be removed completely in #1034/#1035 and
// should be only loaded after user interaction.
try {
const data = await loadMarkdownFile(
pathname,
endOfLine,
autoGuessEncoding,
trimTrailingNewline
)
const file = {
pathname,
data
}
win.webContents.send('mt::update-file', {
type: 'change',
change: file
})
} catch (err) {
// Only notify user about opened files.
if (type === 'file') {
win.webContents.send('mt::show-notification', {
title: 'Watcher I/O error',
type: 'error',
message: err.message
})
}
}
}
}
const addDir = (win, pathname, type) => {
if (type === 'file') return
const directory = {
pathname,
name: path.basename(pathname),
isCollapsed: true,
isDirectory: true,
isFile: false,
isMarkdown: false,
folders: [],
files: []
}
win.webContents.send('mt::update-object-tree', {
type: 'addDir',
change: directory
})
}
const unlinkDir = (win, pathname, type) => {
if (type === 'file') return
const directory = { pathname }
win.webContents.send('mt::update-object-tree', {
type: 'unlinkDir',
change: directory
})
}
class Watcher {
/**
* @param {Preference} preferences The preference instance.
*/
constructor (preferences) {
this._preferences = preferences
this._ignoreChangeEvents = []
this.watchers = {}
}
// Watch a file or directory and return a unwatch function.
watch (win, watchPath, type = 'dir'/* file or dir */) {
// TODO: Is it needed to set `watcherUsePolling` ? because macOS need to set to true.
const usePolling = isOsx ? true : this._preferences.getItem('watcherUsePolling')
const id = getUniqueId()
const watcher = chokidar.watch(watchPath, {
ignored: (pathname, fileInfo) => {
// This function is called twice, once with a single argument (the path),
// second time with two arguments (the path and the "fs.Stats" object of that path).
if (!fileInfo) {
return /(?:^|[/\\])(?:\..|node_modules|(?:.+\.asar))/.test(pathname)
}
if (/(?:^|[/\\])(?:\..|node_modules|(?:.+\.asar))/.test(pathname)) {
return true
}
if (fileInfo.isDirectory()) {
return false
}
return !hasMarkdownExtension(pathname)
},
ignoreInitial: type === 'file',
persistent: true,
ignorePermissionErrors: true,
// Just to be sure when a file is replaced with a directory don't watch recursively.
depth: type === 'file' ? (isOsx ? 1 : 0) : undefined,
// Please see GH#1043
awaitWriteFinish: {
stabilityThreshold: WATCHER_STABILITY_THRESHOLD,
pollInterval: WATCHER_STABILITY_POLL_INTERVAL
},
// Settings options
usePolling
})
let disposed = false
let enospcReached = false
let renameTimer = null
watcher
.on('add', async pathname => {
if (!await this._shouldIgnoreEvent(win.id, pathname, type, usePolling)) {
const { _preferences } = this
const eol = _preferences.getPreferredEol()
const { autoGuessEncoding, trimTrailingNewline } = _preferences.getAll()
add(win, pathname, type, eol, autoGuessEncoding, trimTrailingNewline)
}
})
.on('change', async pathname => {
if (!await this._shouldIgnoreEvent(win.id, pathname, type, usePolling)) {
const { _preferences } = this
const eol = _preferences.getPreferredEol()
const { autoGuessEncoding, trimTrailingNewline } = _preferences.getAll()
change(win, pathname, type, eol, autoGuessEncoding, trimTrailingNewline)
}
})
.on('unlink', pathname => unlink(win, pathname, type))
.on('addDir', pathname => addDir(win, pathname, type))
.on('unlinkDir', pathname => unlinkDir(win, pathname, type))
.on('raw', (event, subpath, details) => {
if (global.MARKTEXT_DEBUG_VERBOSE >= 3) {
console.log('watcher: ', event, subpath, details)
}
// Fix atomic rename on Linux (chokidar#591).
// TODO: This should also apply to macOS.
// TODO: Do we need to rewatch when the watched directory was renamed?
if (isLinux && type === 'file' && event === 'rename') {
if (renameTimer) {
clearTimeout(renameTimer)
}
renameTimer = setTimeout(async () => {
renameTimer = null
if (disposed) {
return
}
const fileExists = await exists(watchPath)
if (fileExists) {
// File still exists but we need to rewatch the file because the inode has changed.
watcher.unwatch(watchPath)
watcher.add(watchPath)
}
}, 150)
}
})
.on('error', error => {
// Check if too many file descriptors are opened and notify the user about this issue.
if (error.code === 'ENOSPC') {
if (!enospcReached) {
enospcReached = true
log.warn('inotify limit reached: Too many file descriptors are opened.')
win.webContents.send('mt::show-notification', {
title: 'inotify limit reached',
type: 'warning',
message: 'Cannot watch all files and file changes because too many file descriptors are opened.'
})
}
} else {
log.error('Error while watching files:', error)
}
})
const closeFn = () => {
disposed = true
if (this.watchers[id]) {
delete this.watchers[id]
}
if (renameTimer) {
clearTimeout(renameTimer)
renameTimer = null
}
watcher.close()
}
this.watchers[id] = {
win,
watcher,
pathname: watchPath,
type,
close: closeFn
}
// unwatcher function
return closeFn
}
// Remove a single watcher.
unwatch (win, watchPath, type = 'dir') {
for (const id of Object.keys(this.watchers)) {
const w = this.watchers[id]
if (
w.win === win &&
w.pathname === watchPath &&
w.type === type
) {
w.watcher.close()
delete this.watchers[id]
break
}
}
}
// Remove all watchers from the given window id.
unwatchByWindowId (windowId) {
const watchers = []
const watchIds = []
for (const id of Object.keys(this.watchers)) {
const w = this.watchers[id]
if (w.win.id === windowId) {
watchers.push(w.watcher)
watchIds.push(id)
}
}
if (watchers.length) {
watchIds.forEach(id => delete this.watchers[id])
watchers.forEach(watcher => watcher.close())
}
}
close () {
Object.keys(this.watchers).forEach(id => this.watchers[id].close())
this.watchers = {}
this._ignoreChangeEvents = []
}
/**
* Ignore the next changed event within a certain time for the current file and window.
*
* NOTE: Only valid for files and "add"/"change" event!
*
* @param {number} windowId The window id.
* @param {string} pathname The path to ignore.
* @param {number} [duration] The duration in ms to ignore the changed event.
*/
ignoreChangedEvent (windowId, pathname, duration = WATCHER_STABILITY_THRESHOLD + (WATCHER_STABILITY_POLL_INTERVAL * 2)) {
this._ignoreChangeEvents.push({ windowId, pathname, duration, start: new Date() })
}
/**
* Check whether we should ignore the current event because the file may be changed from MarkText itself.
*
* @param {number} winId
* @param {string} pathname
* @param {string} type
* @param {boolean} usePolling
*/
async _shouldIgnoreEvent (winId, pathname, type, usePolling) {
if (type === 'file') {
const { _ignoreChangeEvents } = this
const currentTime = new Date()
for (let i = 0; i < _ignoreChangeEvents.length; ++i) {
const { windowId, pathname: pathToIgnore, start, duration } = _ignoreChangeEvents[i]
if (windowId === winId && pathToIgnore === pathname) {
_ignoreChangeEvents.splice(i, 1)
--i
// Modification origin is the editor and we should ignore the event.
if (currentTime - start < duration) {
return true
}
// Try to catch cloud drives that emit the change event not immediately or re-sync the change (GH#3044).
if (!usePolling) {
try {
const fileInfo = await fsPromises.stat(pathname)
if (fileInfo.mtime - start < duration) {
if (global.MARKTEXT_DEBUG_VERBOSE >= 3) {
console.log(`Ignoring file event after "stat": current="${currentTime}", start="${start}", file="${fileInfo.mtime}".`)
}
return true
}
} catch (error) {
console.error('Failed to "stat" file to determine modification time:', error)
}
}
}
}
}
return false
}
}
export default Watcher