Skip to content
This repository
Browse code

Pass fs.stat() result to `add` and `change` events.

Since we get Stats every time and in many cases
users will want to have stats, it can be useful to have stats
passed as second param.
  • Loading branch information...
commit a8c2328f376802d9c0f7cf221013704bf5a2e369 1 parent 2a36a22
Paul Miller authored

Showing 2 changed files with 17 additions and 9 deletions. Show diff stats Hide diff stats

  1. +7 1 README.md
  2. +10 8 src/index.coffee
8 README.md
Source Rendered
@@ -40,6 +40,12 @@ watcher
40 40 .on('unlink', function(path) {console.log('File', path, 'has been removed');})
41 41 .on('error', function(error) {console.error('Error happened', error);})
42 42
  43 +// 'add' and 'change' events also receive stat() results as second argument.
  44 +// http://nodejs.org/api/fs.html#fs_class_fs_stats
  45 +watcher.on('change', function(path, stats) {
  46 + console.log('File', path, 'changed size to', stats.size);
  47 +});
  48 +
43 49 watcher.add('new-file');
44 50 watcher.add(['new-file-2', 'new-file-3']);
45 51
@@ -58,7 +64,7 @@ watcher.close();
58 64 * `options.ignoreInitial` (default: `false`). Indicates whether chokidar
59 65 should ignore initial `add` events or not.
60 66 * `options.interval` (default: `100`). Interval of file system polling.
61   - * `options.binaryInterval` (default: `100`). Interval of file system polling for binary files.
  67 + * `options.binaryInterval` (default: `300`). Interval of file system polling for binary files.
62 68
63 69 `chokidar.watch()` produces an instance of `FSWatcher`. Methods of `FSWatcher`:
64 70
18 src/index.coffee
@@ -30,7 +30,7 @@ exports.FSWatcher = class FSWatcher extends EventEmitter
30 30 @options.ignoreInitial ?= no
31 31 @options.ignorePermissionErrors ?= no
32 32 @options.interval ?= 100
33   - @options.binaryInterval ?= 100
  33 + @options.binaryInterval ?= 300
34 34
35 35 @enableBinaryInterval = @options.binaryInterval isnt @options.interval
36 36
@@ -117,18 +117,20 @@ exports.FSWatcher = class FSWatcher extends EventEmitter
117 117 else
118 118 @options.interval
119 119 fs.watchFile item, options, (curr, prev) =>
120   - callback item if curr.mtime.getTime() > prev.mtime.getTime()
  120 + callback item, curr if curr.mtime.getTime() > prev.mtime.getTime()
121 121
122 122 # Private: Emit `change` event once and watch file to emit it in the future
123 123 # once the file is changed.
124 124 #
125   - # file - string, fs path.
  125 + # file - string, fs path.
  126 + # stats - object, result of executing stat(1) on file.
  127 + # initialAdd - boolean, was the file added at the launch?
126 128 #
127 129 # Returns nothing.
128   - _handleFile: (file, initialAdd = no) =>
129   - @_watch file, 'file', (file) =>
130   - @emit 'change', file
131   - @emit 'add', file unless initialAdd and @options.ignoreInitial
  130 + _handleFile: (file, stats, initialAdd = no) =>
  131 + @_watch file, 'file', (file, newStats) =>
  132 + @emit 'change', file, newStats
  133 + @emit 'add', file, stats unless initialAdd and @options.ignoreInitial
132 134
133 135 # Private: Read directory to add / remove files from `@watched` list
134 136 # and re-read it on change.
@@ -184,7 +186,7 @@ exports.FSWatcher = class FSWatcher extends EventEmitter
184 186 if @options.ignorePermissionErrors and (not @_hasReadPermissions stats)
185 187 return
186 188
187   - @_handleFile item, initialAdd if stats.isFile()
  189 + @_handleFile item, stats, initialAdd if stats.isFile()
188 190 @_handleDir item if stats.isDirectory()
189 191
190 192 emit: (event, args...) ->

2 comments on commit a8c2328

Paul Miller
Owner

@vojtajina I reviewed testaculars code too and it seems it was doing [unnecessary after this change] stat() calls which affected performance.

Vojta Jina

Oh, cool. Thank you @paulmillr ! I didn't know about this change. Created issue for it karma-runner/karma#469

Please sign in to comment.
Something went wrong with that request. Please try again.