Skip to content

Commit

Permalink
Merge pull request #278 from runk/fix-database-update
Browse files Browse the repository at this point in the history
Fix database update
  • Loading branch information
Dmitry Shirokov authored Mar 10, 2020
2 parents 3b29edd + 5e7f7f9 commit a1bd9f9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'fs';
import util from 'util';

export default {
existsSync: fs.existsSync,
readFile: util.promisify(fs.readFile),
readFileSync: fs.readFileSync,
watch: fs.watch,
Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export const open = async <T>(
};

fs.watch(filepath, watcherOptions, async () => {
// When database file is being replaced,
// it could be removed for a fraction of a second.
if (!fs.existsSync(filepath)) {
return;
}
const updateDatabase = await fs.readFile(filepath);
reader.load(updateDatabase, opts);
if (opts.watchForUpdatesHook) {
Expand Down

0 comments on commit a1bd9f9

Please sign in to comment.