1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-11-24 08:12:24 +02:00

Improve resource file watching

This commit is contained in:
Laurent Cozic 2020-10-16 11:08:29 +01:00
parent eec32cf70a
commit f537d22d7f

View File

@ -14,6 +14,7 @@ interface WatchedItem {
lastResourceUpdatedTime: number,
path:string,
asyncSaveQueue: AsyncActionQueue,
size: number,
}
interface WatchedItems {
@ -111,7 +112,12 @@ export default class ResourceEditWatcher {
const stat = await shim.fsDriver().stat(path);
const editedFileUpdatedTime = stat.mtime.getTime();
if (watchedItem.lastFileUpdatedTime === editedFileUpdatedTime) {
// To check if the item has really changed we look at the updated time and size, which
// in most cases is sufficient. It could be a problem if the editing tool is making a change
// that neither changes the timestamp nor the file size. The alternative would be to compare
// the files byte for byte but that could be slow and the file might have changed again by
// the time we finished comparing.
if (watchedItem.lastFileUpdatedTime === editedFileUpdatedTime && watchedItem.size === stat.size) {
// chokidar is buggy and emits "change" events even when nothing has changed
// so double-check the modified time and skip processing if there's no change.
// In particular it emits two such events just after the file has been copied
@ -128,6 +134,7 @@ export default class ResourceEditWatcher {
this.logger().debug(`ResourceEditWatcher: Queuing save action: ${resourceId}`);
watchedItem.asyncSaveQueue.push(makeSaveAction(resourceId, path));
watchedItem.lastFileUpdatedTime = editedFileUpdatedTime;
watchedItem.size = stat.size;
};
if (!this.watcher_) {
@ -185,6 +192,7 @@ export default class ResourceEditWatcher {
lastResourceUpdatedTime: 0,
asyncSaveQueue: new AsyncActionQueue(1000),
path: '',
size: -1,
};
this.watchedItems_[resourceId] = watchedItem;
@ -200,6 +208,7 @@ export default class ResourceEditWatcher {
watchedItem.path = editFilePath;
watchedItem.lastFileUpdatedTime = stat.mtime.getTime();
watchedItem.lastResourceUpdatedTime = resource.updated_time;
watchedItem.size = stat.size;
this.watch(editFilePath);