Skip to content

Commit

Permalink
Reload db when it was actually modified
Browse files Browse the repository at this point in the history
[From Node.js documentation:](https://nodejs.org/docs/latest/api/fs.html#fswatchfilefilename-options-listener)

> To be notified when the file was modified, not just accessed, it is necessary to compare `curr.mtimeMs` and `prev.mtimeMs`.
  • Loading branch information
frenzzy authored Jul 2, 2024
1 parent 6b5b493 commit 4c7b137
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ export const open = async <T extends Response>(
persistent: opts.watchForUpdatesNonPersistent !== true,
};

fs.watchFile(filepath, watcherOptions, async () => {
fs.watchFile(filepath, watcherOptions, async (curr, prev) => {
// Make sure file was modified, not just accessed
if (curr.mtimeMs === prev.mtimeMs) {
return;
}
// When database file is being replaced,
// it could be removed for a fraction of a second.
const waitExists = async () => {
Expand Down

0 comments on commit 4c7b137

Please sign in to comment.