Skip to content

Commit

Permalink
Revert "Reload db when it was actually modified (#850)"
Browse files Browse the repository at this point in the history
This reverts commit 2ad9051.
  • Loading branch information
runk authored Oct 1, 2024
1 parent 2ad9051 commit 69d55f0
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,21 @@ export const open = async <T extends Response>(
persistent: opts.watchForUpdatesNonPersistent !== true,
};

fs.watchFile(filepath, watcherOptions, async (curr, prev) => {
// Make sure file was modified, not just accessed
if (!curr || prev && prev.mtimeMs === curr.mtimeMs) {
fs.watchFile(filepath, watcherOptions, async () => {
// When database file is being replaced,
// it could be removed for a fraction of a second.
const waitExists = async () => {
for (let i = 0; i < 3; i++) {
if (fs.existsSync(filepath)) {
return true;
}

await new Promise((a) => setTimeout(a, 500));
}

return false;
};
if (!(await waitExists())) {
return;
}
const updatedDatabase = await fs.readFile(filepath);
Expand Down

0 comments on commit 69d55f0

Please sign in to comment.