You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-06-30 23:44:55 +02:00
Improve resource file watching
This commit is contained in:
@ -14,6 +14,7 @@ interface WatchedItem {
|
|||||||
lastResourceUpdatedTime: number,
|
lastResourceUpdatedTime: number,
|
||||||
path:string,
|
path:string,
|
||||||
asyncSaveQueue: AsyncActionQueue,
|
asyncSaveQueue: AsyncActionQueue,
|
||||||
|
size: number,
|
||||||
}
|
}
|
||||||
|
|
||||||
interface WatchedItems {
|
interface WatchedItems {
|
||||||
@ -111,7 +112,12 @@ export default class ResourceEditWatcher {
|
|||||||
const stat = await shim.fsDriver().stat(path);
|
const stat = await shim.fsDriver().stat(path);
|
||||||
const editedFileUpdatedTime = stat.mtime.getTime();
|
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
|
// 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.
|
// 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
|
// 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}`);
|
this.logger().debug(`ResourceEditWatcher: Queuing save action: ${resourceId}`);
|
||||||
watchedItem.asyncSaveQueue.push(makeSaveAction(resourceId, path));
|
watchedItem.asyncSaveQueue.push(makeSaveAction(resourceId, path));
|
||||||
watchedItem.lastFileUpdatedTime = editedFileUpdatedTime;
|
watchedItem.lastFileUpdatedTime = editedFileUpdatedTime;
|
||||||
|
watchedItem.size = stat.size;
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!this.watcher_) {
|
if (!this.watcher_) {
|
||||||
@ -185,6 +192,7 @@ export default class ResourceEditWatcher {
|
|||||||
lastResourceUpdatedTime: 0,
|
lastResourceUpdatedTime: 0,
|
||||||
asyncSaveQueue: new AsyncActionQueue(1000),
|
asyncSaveQueue: new AsyncActionQueue(1000),
|
||||||
path: '',
|
path: '',
|
||||||
|
size: -1,
|
||||||
};
|
};
|
||||||
|
|
||||||
this.watchedItems_[resourceId] = watchedItem;
|
this.watchedItems_[resourceId] = watchedItem;
|
||||||
@ -200,6 +208,7 @@ export default class ResourceEditWatcher {
|
|||||||
watchedItem.path = editFilePath;
|
watchedItem.path = editFilePath;
|
||||||
watchedItem.lastFileUpdatedTime = stat.mtime.getTime();
|
watchedItem.lastFileUpdatedTime = stat.mtime.getTime();
|
||||||
watchedItem.lastResourceUpdatedTime = resource.updated_time;
|
watchedItem.lastResourceUpdatedTime = resource.updated_time;
|
||||||
|
watchedItem.size = stat.size;
|
||||||
|
|
||||||
this.watch(editFilePath);
|
this.watch(editFilePath);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user