Skip to content

Commit

Permalink
Improve resources watching performance (#5882)
Browse files Browse the repository at this point in the history
- Try at reducing Windows devices resources use
- Remove any `.git` folder from being watched (for project using versioning with git)
  • Loading branch information
AlexandreSi authored Nov 8, 2023
1 parent 2d5b2a4 commit 2965f37
Show file tree
Hide file tree
Showing 7 changed files with 551 additions and 42 deletions.
41 changes: 35 additions & 6 deletions newIDE/app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion newIDE/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
"algoliasearch": "3.33.0",
"axios": "^0.18.1",
"blueimp-md5": "^2.10.0",
"chokidar": "3.5.3",
"classnames": "2.2.5",
"date-fns": "2.16.1",
"element-closest": "2.0.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import debounce from 'lodash/debounce';
import wrap from 'lodash/wrap';
import memoize from 'lodash/memoize';

const fileWatcher = optionalRequire('chokidar');
const path = optionalRequire('path');
const electron = optionalRequire('electron');
const ipcRenderer = electron ? electron.ipcRenderer : null;

export const setupResourcesWatcher =
fileWatcher && path
ipcRenderer && path
? (
fileMetadata: FileMetadata,
callback: ({| identifier: string |}) => void
Expand All @@ -36,28 +37,34 @@ export const setupResourcesWatcher =
const folderPath = path.dirname(fileMetadata.fileIdentifier);
const gameFile = path.basename(fileMetadata.fileIdentifier);
const autosaveFile = gameFile + '.autosave';
const watcher = fileWatcher
.watch(folderPath, {
ignored: [
`**/.DS_Store`,
ipcRenderer.on('project-file-changed', (event, path) => {
// TODO: Is it safe to let it like that since the OS could for some reason
// do never-ending operations on the folder or its children, making the debounce
// never ending.
debouncedCallback(path);
});
const subscriptionIdPromise = ipcRenderer.invoke(
'local-filesystem-watcher-setup',

folderPath,
JSON.stringify({
ignore: [
'**/.DS_Store', // macOS folder attributes file
'**/.git/**', // For projects using git as a versioning tool.
path.join(folderPath, gameFile),
path.join(folderPath, autosaveFile),
],
ignoreInitial: true,
awaitWriteFinish: {
stabilityThreshold: 250,
pollInterval: 100,
},
})
.on(
'change',
// TODO: Is it safe to let it like that since the OS could for some reason
// do never-ending operations on the folder or its children, making the debounce
// never ending.
debouncedCallback
)
.on('unlink', debouncedCallback)
.on('add', debouncedCallback);
return () => watcher.unwatch(folderPath);
);

return () => {
ipcRenderer.removeAllListeners('project-file-changed');
subscriptionIdPromise.then(subscriptionId => {
ipcRenderer.invoke(
'local-filesystem-watcher-disable',
subscriptionId
);
});
};
}
: undefined;
45 changes: 45 additions & 0 deletions newIDE/electron-app/app/LocalFilesystemWatcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// @flow
const watcher = require('@parcel/watcher');

let subscriptions = {};
let newSubscriptionId = 1

const getNewSubscriptionId = () => {
const id = newSubscriptionId++;
return id.toString();
};

const setupWatcher = (folderPath, fileWiseCallback, serializedOptions) => {
const options = JSON.parse(serializedOptions);
const newSubscriptionId = getNewSubscriptionId();
watcher
.subscribe(
folderPath,
(err, fileChangeEvents) => {
fileChangeEvents.forEach(fileChangeEvent =>
fileWiseCallback(fileChangeEvent.path)
);
},
options
)
.then(subscription => {
subscriptions[newSubscriptionId] = subscription;
});
return newSubscriptionId;
};

const disableWatcher = id => {
const subscription = subscriptions[id];
if (!subscription) {
console.log('No watcher subscription to disable.');
return;
}
subscription.unsubscribe().then(() => {
delete subscriptions[id];
});
};

module.exports = {
setupWatcher,
disableWatcher,
};
Loading

0 comments on commit 2965f37

Please sign in to comment.