Skip to content

Commit

Permalink
feat(watchForUpdates): add watchForUpdatesNonPersistent option
Browse files Browse the repository at this point in the history
  * add option that makes fs watch non persistent (avoid stuck process)
  * fix UT
  * fix a deprecation message from sinon sandbox
  * update doc
  * update package-lock file for travis build in node 10

(nonPersistentWatcher)
  • Loading branch information
furanzujin committed Jun 6, 2018
1 parent d99c81f commit 6c1b9c6
Show file tree
Hide file tree
Showing 4 changed files with 2,055 additions and 977 deletions.
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -74,6 +74,16 @@ var lookup = maxmind.openSync('/path/to/GeoLite2.mmdb', { watchForUpdates: true
lookup.get('1.1.1.1');
```

You also can specify wether the watcher should be persistent or not. If it is persistent, a node process will be blocked in watching state if the watcher is the only thing still running in the program. You can use `watchForUpdatesNonPersistent` option (default `false`) to prevent this behavior.
```javascript
var lookup = maxmind.openSync('/path/to/GeoLite2.mmdb', {
watchForUpdates: true,
watchForUpdateNonPersistent: true,
});
lookup.get('1.1.1.1');
```


Also, you can specify custom hook function on database update.

```javascript
Expand Down
6 changes: 4 additions & 2 deletions index.js
Expand Up @@ -31,7 +31,8 @@ exports.open = function(filepath, opts, cb) {
if (opts.watchForUpdatesHook && typeof opts.watchForUpdatesHook != 'function') {
throw new Error('opts.watchForUpdatesHook should be a function');
}
fs.watch(filepath, function() {
var watcherOptions = {persistent: opts.watchForUpdatesNonPersistent !== true};
fs.watch(filepath, watcherOptions, function() {
fs.readFile(filepath, function(err, database) {
if (err) return cb(err);
reader.load(database, opts);
Expand All @@ -51,7 +52,8 @@ exports.openSync = function(filepath, opts) {
if (opts.watchForUpdatesHook && typeof opts.watchForUpdatesHook != 'function') {
throw new Error('opts.watchForUpdatesHook should be a function');
}
fs.watch(filepath, function() {
var watcherOptions = {persistent: opts.watchForUpdatesNonPersistent !== true};
fs.watch(filepath, watcherOptions, function() {
reader.load(fs.readFileSync(filepath), opts);
if (opts.watchForUpdatesHook) {
opts.watchForUpdatesHook();
Expand Down

0 comments on commit 6c1b9c6

Please sign in to comment.