Skip to content

Commit 173f414

Browse files
committed
v0.0.11 - Avoid breaking the process when watched files are deleted, instead fire missing event
1 parent 974a842 commit 173f414

File tree

3 files changed

+35
-26
lines changed

3 files changed

+35
-26
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ lib-cov
1111
pids
1212
logs
1313
results
14+
test
15+
node_modules
1416

1517
npm-debug.log

lib/pointer.js

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ function Pointer(obj) {
1414
obj = utils.normalize(obj);
1515

1616
var self = this;
17+
var watcher = null;
1718
var eventer = new events.EventEmitter();
1819

1920
var stats = obj.stats;
@@ -147,35 +148,12 @@ function Pointer(obj) {
147148
value: function(){
148149
eventer.emit.apply(eventer, arguments);
149150
}
150-
},
151-
// Stop watching file changes
152-
__stopWatch: {
153-
value: function () {
154-
if (watcher) {
155-
watcher.close();
156-
watcher = null;
157-
}
158-
}
159-
},
160-
// Start watching file changes
161-
__startWatch: {
162-
value: function () {
163-
if (!watcher) {
164-
forwardEvents(fs.watch(self._path, {persistent: false}));
165-
}
166-
}
167151
}
168152
});
169153

170-
// If configuration states start watching
171-
if (obj.watch) {
172-
self.__startWatch();
173-
}
154+
// File watching
174155

175-
// Watcher is unstable, have to renew after each event
176-
var watcher = null;
177-
178-
function forwardEvents(fsWatch) {
156+
function forwardEvents(fsWatch) { // Watcher is unstable, have to renew after each event
179157
watcher = fsWatch;
180158
['change', 'error'].forEach(function (ev) {
181159
fsWatch.on(ev, function () {
@@ -187,6 +165,35 @@ function Pointer(obj) {
187165
});
188166
});
189167
}
168+
169+
Object.defineProperties(self, {
170+
// Stop watching file changes
171+
__stopWatch: {
172+
value: function () {
173+
if (watcher) {
174+
watcher.close();
175+
watcher = null;
176+
}
177+
}
178+
},
179+
// Start watching file changes
180+
__startWatch: {
181+
value: function (persistent) {
182+
if (!watcher) {
183+
try {
184+
forwardEvents(fs.watch(self._path, {persistent: persistent}));
185+
} catch(e) {
186+
self.__emit('missing', e);
187+
}
188+
}
189+
}
190+
}
191+
});
192+
193+
// If configuration states start watching
194+
if (obj.watch) {
195+
self.__startWatch();
196+
}
190197
}
191198

192199
function createObj(obj){

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"files",
88
"pointer"
99
],
10-
"version": "0.0.10",
10+
"version": "0.0.11",
1111
"repository": {
1212
"type": "git",
1313
"url": "git@github.com:DeadAlready/node-file-pointer"

0 commit comments

Comments
 (0)