-
Notifications
You must be signed in to change notification settings - Fork 916
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve resources watching performance (#5882)
- Try at reducing Windows devices resources use - Remove any `.git` folder from being watched (for project using versioning with git)
- Loading branch information
1 parent
2d5b2a4
commit 2965f37
Showing
7 changed files
with
551 additions
and
42 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
Oops, something went wrong.