@@ -36,8 +36,6 @@ const fs = exports;
3636const { Buffer } = require ( 'buffer' ) ;
3737const errors = require ( 'internal/errors' ) ;
3838const {
39- ERR_FS_WATCHER_ALREADY_STARTED ,
40- ERR_FS_WATCHER_NOT_STARTED ,
4139 ERR_INVALID_ARG_TYPE ,
4240 ERR_INVALID_CALLBACK ,
4341 ERR_OUT_OF_RANGE
@@ -1342,25 +1340,26 @@ util.inherits(FSWatcher, EventEmitter);
13421340
13431341// FIXME(joyeecheung): this method is not documented.
13441342// At the moment if filename is undefined, we
1345- // 1. Throw an Error from C++ land if it's the first time .start() is called
1346- // 2. Return silently from C++ land if .start() has already been called
1343+ // 1. Throw an Error if it's the first time .start() is called
1344+ // 2. Return silently if .start() has already been called
13471345// on a valid filename and the wrap has been initialized
1346+ // This method is a noop if the watcher has already been started.
13481347FSWatcher . prototype . start = function ( filename ,
13491348 persistent ,
13501349 recursive ,
13511350 encoding ) {
13521351 lazyAssert ( ) ( this . _handle instanceof FSEvent , 'handle must be a FSEvent' ) ;
13531352 if ( this . _handle . initialized ) {
1354- throw new ERR_FS_WATCHER_ALREADY_STARTED ( ) ;
1353+ return ;
13551354 }
13561355
13571356 filename = getPathFromURL ( filename ) ;
13581357 validatePath ( filename , 'filename' ) ;
13591358
1360- var err = this . _handle . start ( pathModule . toNamespacedPath ( filename ) ,
1361- persistent ,
1362- recursive ,
1363- encoding ) ;
1359+ const err = this . _handle . start ( pathModule . toNamespacedPath ( filename ) ,
1360+ persistent ,
1361+ recursive ,
1362+ encoding ) ;
13641363 if ( err ) {
13651364 const error = errors . uvException ( {
13661365 errno : err ,
@@ -1372,10 +1371,11 @@ FSWatcher.prototype.start = function(filename,
13721371 }
13731372} ;
13741373
1374+ // This method is a noop if the watcher has not been started.
13751375FSWatcher . prototype . close = function ( ) {
13761376 lazyAssert ( ) ( this . _handle instanceof FSEvent , 'handle must be a FSEvent' ) ;
13771377 if ( ! this . _handle . initialized ) {
1378- throw new ERR_FS_WATCHER_NOT_STARTED ( ) ;
1378+ return ;
13791379 }
13801380 this . _handle . close ( ) ;
13811381} ;
@@ -1449,18 +1449,43 @@ util.inherits(StatWatcher, EventEmitter);
14491449
14501450// FIXME(joyeecheung): this method is not documented.
14511451// At the moment if filename is undefined, we
1452- // 1. Throw an Error from C++ land if it's the first time .start() is called
1453- // 2. Return silently from C++ land if .start() has already been called
1452+ // 1. Throw an Error if it's the first time .start() is called
1453+ // 2. Return silently if .start() has already been called
14541454// on a valid filename and the wrap has been initialized
1455+ // This method is a noop if the watcher has already been started.
14551456StatWatcher . prototype . start = function ( filename , persistent , interval ) {
1457+ lazyAssert ( ) ( this . _handle instanceof binding . StatWatcher ,
1458+ 'handle must be a StatWatcher' ) ;
1459+ if ( this . _handle . isActive ) {
1460+ return ;
1461+ }
1462+
14561463 filename = getPathFromURL ( filename ) ;
1457- nullCheck ( filename , 'filename' ) ;
1458- this . _handle . start ( pathModule . toNamespacedPath ( filename ) ,
1459- persistent , interval ) ;
1464+ validatePath ( filename , 'filename' ) ;
1465+ validateUint32 ( interval , 'interval' ) ;
1466+ const err = this . _handle . start ( pathModule . toNamespacedPath ( filename ) ,
1467+ persistent , interval ) ;
1468+ if ( err ) {
1469+ const error = errors . uvException ( {
1470+ errno : err ,
1471+ syscall : 'watch' ,
1472+ path : filename
1473+ } ) ;
1474+ error . filename = filename ;
1475+ throw error ;
1476+ }
14601477} ;
14611478
1462-
1479+ // FIXME(joyeecheung): this method is not documented while there is
1480+ // another documented fs.unwatchFile(). The counterpart in
1481+ // FSWatcher is .close()
1482+ // This method is a noop if the watcher has not been started.
14631483StatWatcher . prototype . stop = function ( ) {
1484+ lazyAssert ( ) ( this . _handle instanceof binding . StatWatcher ,
1485+ 'handle must be a StatWatcher' ) ;
1486+ if ( ! this . _handle . isActive ) {
1487+ return ;
1488+ }
14641489 this . _handle . stop ( ) ;
14651490} ;
14661491
0 commit comments