listen 1.3.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -1,304 +1,173 @@
1
- # Listen [![Gem Version](https://badge.fury.io/rb/listen.png)](http://badge.fury.io/rb/listen) [![Build Status](https://secure.travis-ci.org/guard/listen.png?branch=master)](http://travis-ci.org/guard/listen) [![Dependency Status](https://gemnasium.com/guard/listen.png)](https://gemnasium.com/guard/listen) [![Code Climate](https://codeclimate.com/github/guard/listen.png)](https://codeclimate.com/github/guard/listen) [![Coverage Status](https://coveralls.io/repos/guard/listen/badge.png?branch=master)](https://coveralls.io/r/guard/listen)
1
+ # Listen [![Gem Version](https://badge.fury.io/rb/listen.png)](http://badge.fury.io/rb/listen) [![Build Status](https://travis-ci.org/guard/listen.png)](https://travis-ci.org/guard/listen) [![Dependency Status](https://gemnasium.com/guard/listen.png)](https://gemnasium.com/guard/listen) [![Code Climate](https://codeclimate.com/github/guard/listen.png)](https://codeclimate.com/github/guard/listen) [![Coverage Status](https://coveralls.io/repos/guard/listen/badge.png?branch=master)](https://coveralls.io/r/guard/listen)
2
2
 
3
3
  The Listen gem listens to file modifications and notifies you about the changes.
4
4
 
5
5
  ## Features
6
6
 
7
- * Works everywhere!
8
7
  * Supports watching multiple directories from a single listener.
9
- * OS-specific adapters for Mac OS X 10.6+, Linux, *BSD and Windows.
10
- * Automatic fallback to polling if OS-specific adapter doesn't work.
8
+ * OS-specific adapters on MRI for Mac OS X 10.6+, Linux, *BSD and Windows, [more info](#listen-adapters) bellow.
11
9
  * Detects file modification, addition and removal.
12
- * File content checksum comparison for modifications made under the same second.
13
- * Allows supplying regexp-patterns to ignore and filter paths for better results.
14
- * Tested on all Ruby environments via [Travis CI](https://travis-ci.org/guard/listen).
10
+ * Allows supplying regexp-patterns to ignore paths for better results.
11
+ * File content checksum comparison for modifications made under the same second (OS X only).
12
+ * Tested on MRI Ruby environments (1.9+ only) via [Travis CI](https://travis-ci.org/guard/listen), JRuby and Rubinius should work with polling.
15
13
 
16
- ## Pending features
14
+ Please note that Windows and *BSD adapter aren't continuously and automaticaly tested.
17
15
 
18
- Still not implemented, pull requests are welcome.
16
+ ## Pending features
19
17
 
20
- * Symlinks support. [#25](https://github.com/guard/listen/issues/25)
21
- * Signal handling. [#105](https://github.com/guard/listen/issues/105)
22
18
  * Non-recursive directory scanning. [#111](https://github.com/guard/listen/issues/111)
19
+ * Symlinks support. [#25](https://github.com/guard/listen/issues/25)
23
20
 
24
- ## Install
25
-
26
- ### Using Bundler
27
-
28
- The simplest way to install Listen is to use Bundler.
29
-
30
- Add Listen to your Gemfile:
31
-
32
- ```ruby
33
- group :development do
34
- gem 'listen'
35
- end
36
- ```
37
-
38
- and install it by running Bundler:
39
-
40
- ```bash
41
- $ bundle
42
- ```
43
-
44
- ### Install the gem with RubyGems
45
-
46
- ```bash
47
- $ gem install listen
48
- ```
21
+ Pull request or help is very welcome for these.
49
22
 
50
- ### On Windows
23
+ ## Install
51
24
 
52
- If your are on Windows and using Ruby MRI >= 1.9.2 you can try to use the [`wdm`](https://github.com/Maher4Ever/wdm) instead of polling.
53
- Please add the following to your Gemfile:
25
+ The simplest way to install Listen is to use [Bundler](http://bundler.io).
54
26
 
55
27
  ```ruby
56
- require 'rbconfig'
57
- gem 'wdm', '>= 0.1.0' if RbConfig::CONFIG['target_os'] =~ /mswin|mingw/i
28
+ gem 'listen', '~> 2.0'
58
29
  ```
59
30
 
60
31
  ## Usage
61
32
 
62
- There are **two ways** to use Listen:
63
-
64
- 1. Block API: Call `Listen.to`/`Listen.to!` with either a single directory or multiple directories, then define the `change` callback in a block.
65
- 2. "Object" API: Create a `listener` object and use it in a chainable way.
66
-
67
- ### Block API
33
+ Call `Listen.to` with either a single directory or multiple directories, then define the "changes" callback in a block.
68
34
 
69
35
  ``` ruby
70
- # Listen to a single directory.
71
- Listen.to('dir/path/to/listen', :filter => /\.rb$/, :ignore => %r{ignored/path/}) do |modified, added, removed|
72
- # ...
36
+ listener = Listen.to('dir/to/listen', 'dir/to/listen2') do |modified, added, removed|
37
+ puts "modified absolute path: #{modified}"
38
+ puts "added absolute path: #{added}"
39
+ puts "removed absolute path: #{removed}"
73
40
  end
74
-
75
- # Listen to multiple directories.
76
- Listen.to('dir/to/awesome_app', 'dir/to/other_app', :filter => /\.rb$/, :latency => 0.1) do |modified, added, removed|
77
- # ...
78
- end
79
- ```
80
-
81
- ### "Object" API
82
-
83
- ``` ruby
84
- listener = Listen.to('dir/path/to/listen')
85
- listener = listener.ignore(%r{^ignored/path/})
86
- listener = listener.filter(/\.rb$/)
87
- listener = listener.latency(0.5)
88
- listener = listener.force_polling(true)
89
- listener = listener.polling_fallback_message(false)
90
- listener = listener.force_adapter(Listen::Adapters::Linux)
91
- listener = listener.change(&callback)
92
- listener.start
93
- ```
94
-
95
- **Note**: All the "Object" API methods except `start`/`start!` return the listener
96
- and are thus chainable:
97
-
98
- ``` ruby
99
- Listen.to('dir/path/to/listen')
100
- .ignore(%r{^ignored/path/})
101
- .filter(/\.rb$/)
102
- .latency(0.5)
103
- .force_polling(true)
104
- .polling_fallback_message('custom message')
105
- .change(&callback)
106
- .start
41
+ listener.start # not blocking
42
+ sleep
107
43
  ```
108
44
 
109
- ### Pause/Unpause
45
+ ### Pause / unpause / stop
110
46
 
111
- Listener can also easily be paused/unpaused:
47
+ Listener can also be easily paused/unpaused:
112
48
 
113
49
  ``` ruby
114
- listener = Listen.to('dir/path/to/listen')
115
- listener.start # non-blocking mode
50
+ listener = Listen.to('dir/path/to/listen') { |modified, added, removed| # ... }
51
+ listener.start
52
+ listener.listen? # => true
116
53
  listener.pause # stop listening to changes
117
54
  listener.paused? # => true
118
55
  listener.unpause # start listening to changes again
119
56
  listener.stop # stop completely the listener
120
57
  ```
121
58
 
59
+ ### Ignore / ignore!
60
+
61
+ Liste ignore some folder and extensions by default (See DEFAULT_IGNORED_DIRECTORIES and DEFAULT_IGNORED_EXTENSIONS in Listen::Silencer), you can add ignoring patterns with the `ignore` option/method or overwrite default with `ignore!` option/method.
62
+
63
+ ``` ruby
64
+ listener = Listen.to('dir/path/to/listen', ignore: /\.txt/) { |modified, added, removed| # ... }
65
+ listener.start
66
+ listener.ignore! /\.pkg/ # overwrite all patterns and only ignore pkg extension.
67
+ listener.ignore /\.rb/ # ignore rb extension in addition of pkg.
68
+ sleep
69
+ ``
70
+
122
71
  ## Changes callback
123
72
 
124
73
  Changes to the listened-to directories gets reported back to the user in a callback.
125
74
  The registered callback gets invoked, when there are changes, with **three** parameters:
126
- `modified_paths`, `added_paths` and `removed_paths` in that particular order.
75
+ `modified`, `added` and `removed` paths, in that particular order.
76
+ Paths are always returned in their absolute form.
127
77
 
128
- You can register a callback in two ways. The first way is by passing a block when calling
129
- the `Listen.to`/`Listen.to!` method or when initializing a listener object:
78
+ Example:
130
79
 
131
80
  ```ruby
132
- Listen.to('path/to/app') do |modified, added, removed|
81
+ listener = Listen.to('path/to/app') do |modified, added, removed|
133
82
  # This block will be called when there are changes.
134
83
  end
135
-
136
- # or ...
137
-
138
- listener = Listen::Listener.new('path/to/app') do |modified, added, removed|
139
- # This block will be called when there are changes.
140
- end
141
-
84
+ listener.start
85
+ sleep
142
86
  ```
143
87
 
144
- The second way to register a callback is by calling the `#change` method on a
145
- listener passing it a block:
88
+ or ...
146
89
 
147
90
  ```ruby
148
91
  # Create a callback
149
92
  callback = Proc.new do |modified, added, removed|
150
93
  # This proc will be called when there are changes.
151
94
  end
152
-
153
- listener = Listen.to('dir')
154
- listener.change(&callback) # convert the callback to a block and register it
155
-
95
+ listener = Listen.to('dir', &callback)
156
96
  listener.start
157
- ```
158
-
159
- ### Paths in callbacks
160
-
161
- Listeners invoke callbacks passing them absolute paths by default:
162
-
163
- ```ruby
164
- # Assume someone changes the 'style.css' file in '/home/user/app/css' after creating
165
- # the listener.
166
- Listen.to('/home/user/app/css') do |modified, added, removed|
167
- modified.inspect # => ['/home/user/app/css/style.css']
168
- end
169
- ```
170
-
171
- #### Relative paths in callbacks
172
-
173
- When creating a listener for a **single** path (more specifically a `Listen::Listener` instance),
174
- you can pass `:relative_paths => true` as an option to get relative paths in
175
- your callback:
176
-
177
- ```ruby
178
- # Assume someone changes the 'style.css' file in '/home/user/app/css' after creating
179
- # the listener.
180
- Listen.to('/home/user/app/css', :relative_paths => true) do |modified, added, removed|
181
- modified.inspect # => ['style.css']
182
- end
183
- ```
184
-
185
- Passing the `:relative_paths => true` option won't work when listening to multiple
186
- directories:
187
-
188
- ```ruby
189
- # Assume someone changes the 'style.css' file in '/home/user/app/css' after creating
190
- # the listener.
191
- Listen.to('/home/user/app/css', '/home/user/app/js', :relative_paths => true) do |modified, added, removed|
192
- modified.inspect # => ['/home/user/app/css/style.css']
193
- end
97
+ sleep
194
98
  ```
195
99
 
196
100
  ## Options
197
101
 
198
- All the following options can be set through the `Listen.to`/`Listen.to!` params
199
- or via ["Object" API](#object-api) methods:
102
+ All the following options can be set through the `Listen.to` after the directory path(s) params.
200
103
 
201
104
  ```ruby
202
- :ignore => %r{app/CMake/}, /\.pid$/ # Ignore a list of paths (root directory or sub-dir)
203
- # default: See DEFAULT_IGNORED_DIRECTORIES and DEFAULT_IGNORED_EXTENSIONS in Listen::DirectoryRecord
204
-
205
- :filter => /\.rb$/, /\.coffee$/ # Filter files to listen to via a regexps list.
206
- # default: none
105
+ ignore: [%r{/foo/bar}, /\.pid$/, /\.coffee$/] # Ignore a list of paths
106
+ # default: See DEFAULT_IGNORED_DIRECTORIES and DEFAULT_IGNORED_EXTENSIONS in Listen::Silencer
207
107
 
208
- :latency => 0.5 # Set the delay (**in seconds**) between checking for changes
209
- # default: 0.25 sec (1.0 sec for polling)
108
+ ignore!: %r{/foo/bar} # Same as ignore options, but overwrite default ignored paths.
210
109
 
211
- :force_adapter => Listen::Adapters::Linux # Force the use of a particular adapter class
212
- # default: none
110
+ latency: 0.5 # Set the delay (**in seconds**) between checking for changes
111
+ # default: 0.25 sec (1.0 sec for polling)
213
112
 
214
- :force_polling => true # Force the use of the polling adapter
215
- # default: none
113
+ force_polling: true # Force the use of the polling adapter
114
+ # default: none
216
115
 
217
- :polling_fallback_message => 'custom message' # Set a custom polling fallback message (or disable it with false)
218
- # default: "Listen will be polling for changes. Learn more at https://github.com/guard/listen#polling-fallback."
116
+ polling_fallback_message: 'custom message' # Set a custom polling fallback message (or disable it with false)
117
+ # default: "Listen will be polling for changes. Learn more at https://github.com/guard/listen#polling-fallback."
219
118
 
220
- :relative_paths => true # Enable the use of relative paths in the callback.
221
- # default: false
119
+ debug: true # Enable Celluloid logger
120
+ # default: false
222
121
  ```
223
122
 
224
- ### Note on the patterns for ignoring and filtering paths
225
-
226
- Just like the unix convention of beginning absolute paths with the
227
- directory-separator (forward slash `/` in unix) and with no prefix for relative paths,
228
- Listen doesn't prefix relative paths (to the watched directory) with a directory-separator.
229
-
230
- Therefore make sure _NOT_ to prefix your regexp-patterns for filtering or ignoring paths
231
- with a directory-separator, otherwise they won't work as expected.
123
+ ## Listen adapters
232
124
 
233
- As an example: to ignore the `build` directory in a C-project, use `%r{build/}`
234
- and not `%r{/build/}`.
125
+ The Listen gem has a set of adapters to notify it when there are changes.
126
+ There are 4 OS-specific adapters to support Darwin, Linux, *BSD and Windows.
127
+ These adapters are fast as they use some system-calls to implement the notifying function.
235
128
 
236
- Use `#filter!` and `#ignore!` methods to overwrites default patterns.
129
+ There is also a polling adapter which is a cross-platform adapter and it will
130
+ work on any system. This adapter is slower than the rest of the adapters.
237
131
 
238
- ## Blocking listening to changes
132
+ Darwin and Linux adapter are dependencies of the Listen gem so they work out of the box. For other adapters a specific gem need to be added to your Gemfile, please read bellow.
239
133
 
240
- Calling `Listen.to` with a block doesn't block the current thread. If you want
241
- to block the current thread instead until the listener is stopped (which needs
242
- to be done from another thread), you can use `Listen.to!`.
134
+ The Listen gem choose the good adapter (if present) automatically. If you
135
+ want to force the use of the polling adapter use the `:force_polling` option
136
+ while initializing the listener.
243
137
 
244
- Similarly, if you're using the "Object" API, you can use `#start!` instead of `#start` to block the
245
- current thread until the listener is stopped.
138
+ ### On Windows
246
139
 
247
- Here is an example of using a listener in the blocking mode:
140
+ If your are on Windows you can try to use the [`wdm`](https://github.com/Maher4Ever/wdm) instead of polling.
141
+ Please add the following to your Gemfile:
248
142
 
249
143
  ```ruby
250
- Listen.to!('dir/path/to/listen') # block execution
251
-
252
- # Code here will not run until the listener is stopped
253
-
144
+ require 'rbconfig'
145
+ gem 'wdm', '>= 0.1.0' if RbConfig::CONFIG['target_os'] =~ /mswin|mingw|cygwin/i
254
146
  ```
255
147
 
256
- Here is an example of using a listener started with the "Object" API in blocking mode:
257
-
258
- ```ruby
259
- listener = Listen.to('dir/path/to/listen')
260
- listener.start! # block execution
148
+ ### On *BSD
261
149
 
262
- # Code here will not run until the listener is stopped
150
+ If your are on *BSD you can try to use the [`rb-kqueue`](https://github.com/mat813/rb-kqueue) instead of polling.
151
+ Please add the following to your Gemfile:
263
152
 
153
+ ```ruby
154
+ require 'rbconfig'
155
+ gem 'rb-kqueue', '>= 0.2' if RbConfig::CONFIG['target_os'] =~ /freebsd/i
264
156
  ```
265
157
 
266
- **Note**: Using the `Listen.to!` helper-method with or without a callback-block
267
- will always start the listener right away and block execution of the current thread.
268
-
269
- ## Listen adapters
270
-
271
- The Listen gem has a set of adapters to notify it when there are changes.
272
- There are 4 OS-specific adapters to support Mac, Linux, *BSD and Windows.
273
- These adapters are fast as they use some system-calls to implement the notifying function.
274
-
275
- There is also a polling adapter which is a cross-platform adapter and it will
276
- work on any system. This adapter is unfortunately slower than the rest of the adapters.
277
-
278
- The Listen gem will choose the best and working adapter for your machine automatically. If you
279
- want to force the use of the polling adapter, either use the `:force_polling` option
280
- while initializing the listener or call the `#force_polling` method on your listener
281
- before starting it.
282
-
283
- It is also possible to force the use of a particular adapter, by using the `:force_adapter`
284
- option. This option skips the usual adapter choosing mechanism and uses the given
285
- adapter class instead. The adapter choosing mechanism requires write permission
286
- to your watched directories and will needlessly load code, which isn't always desirable.
287
-
288
- ## Polling fallback
158
+ ### Issues
289
159
 
290
- When a OS-specific adapter doesn't work the Listen gem automatically falls back to the polling adapter.
291
- Here are some things you could try to avoid the polling fallback:
160
+ Sometimes OS-specific adapter doesn't work, :'(
161
+ Here are some things you could try to avoid forcing polling.
292
162
 
293
163
  * [Update your Dropbox client](http://www.dropbox.com/downloading) (if used).
294
- * Increase latency. (Please [open an issue](https://github.com/guard/listen/issues/new)
295
- if you think that default is too low.)
296
164
  * Move or rename the listened folder.
297
165
  * Update/reboot your OS.
166
+ * Increase latency.
298
167
 
299
168
  If your application keeps using the polling-adapter and you can't figure out why, feel free to [open an issue](https://github.com/guard/listen/issues/new) (and be sure to [give all the details](https://github.com/guard/listen/blob/master/CONTRIBUTING.md)).
300
169
 
301
- ## Development [![Dependency Status](https://gemnasium.com/guard/listen.png?branch=master)](https://gemnasium.com/guard/listen)
170
+ ## Development
302
171
 
303
172
  * Documentation hosted at [RubyDoc](http://rubydoc.info/github/guard/listen/master/frames).
304
173
  * Source hosted at [GitHub](https://github.com/guard/listen).
@@ -309,7 +178,6 @@ Pull requests are very welcome! Please try to follow these simple rules if appli
309
178
  * Make sure your patches are well tested. All specs must pass on [Travis CI](https://travis-ci.org/guard/listen).
310
179
  * Update the [Yard](http://yardoc.org/) documentation.
311
180
  * Update the [README](https://github.com/guard/listen/blob/master/README.md).
312
- * Update the [CHANGELOG](https://github.com/guard/listen/blob/master/CHANGELOG.md) for noteworthy changes (don't forget to run `bundle exec pimpmychangelog` and watch the magic happen)!
313
181
  * Please **do not change** the version number.
314
182
 
315
183
  For questions please join us in our [Google group](http://groups.google.com/group/guard-dev) or on
@@ -321,19 +189,18 @@ For questions please join us in our [Google group](http://groups.google.com/grou
321
189
  * [Travis Tilley (ttilley)][] for this awesome work on [fssm][] & [rb-fsevent][].
322
190
  * [Nathan Weizenbaum (nex3)][] for [rb-inotify][], a thorough inotify wrapper.
323
191
  * [Mathieu Arnold (mat813)][] for [rb-kqueue][], a simple kqueue wrapper.
324
- * [stereobooster][] for [rb-fchange][], windows support wouldn't exist without him.
192
+ * [Maher Sallam][] for [wdm][], windows support wouldn't exist without him.
325
193
  * [Yehuda Katz (wycats)][] for [vigilo][], that has been a great source of inspiration.
326
194
 
327
- ## Authors
195
+ ## Author
328
196
 
329
- * [Thibaud Guillaume-Gentil][] ([@thibaudgg](http://twitter.com/thibaudgg))
330
- * [Maher Sallam][] ([@mahersalam](http://twitter.com/mahersalam))
197
+ * [Thibaud Guillaume-Gentil (thibaudgg)][] ([@thibaudgg](http://twitter.com/thibaudgg))
331
198
 
332
199
  ## Contributors
333
200
 
334
- [https://github.com/guard/listen/contributors](https://github.com/guard/listen/contributors)
201
+ [https://github.com/guard/listen/graphs/contributors](https://github.com/guard/listen/graphs/contributors)
335
202
 
336
- [Thibaud Guillaume-Gentil]: https://github.com/thibaudgg
203
+ [Thibaud Guillaume-Gentil (thibaudgg)]: https://github.com/thibaudgg
337
204
  [Maher Sallam]: https://github.com/Maher4Ever
338
205
  [Michael Kessler (netzpirat)]: https://github.com/netzpirat
339
206
  [Travis Tilley (ttilley)]: https://github.com/ttilley
@@ -347,3 +214,4 @@ For questions please join us in our [Google group](http://groups.google.com/grou
347
214
  [rb-kqueue]: https://github.com/mat813/rb-kqueue
348
215
  [Yehuda Katz (wycats)]: https://github.com/wycats
349
216
  [vigilo]: https://github.com/wycats/vigilo
217
+ [wdm]: https://github.com/Maher4Ever/wdm
@@ -0,0 +1,40 @@
1
+ module Listen
2
+ module Adapter
3
+
4
+ class Base
5
+ include Celluloid
6
+
7
+ # The default delay between checking for changes.
8
+ DEFAULT_LATENCY = 0.1
9
+
10
+ attr_accessor :listener
11
+
12
+ def initialize(listener)
13
+ @listener = listener
14
+ end
15
+
16
+ def self.usable?
17
+ raise NotImplementedError
18
+ end
19
+
20
+ def start
21
+ raise NotImplementedError
22
+ end
23
+
24
+ private
25
+
26
+ def _latency
27
+ listener.options[:latency] || DEFAULT_LATENCY
28
+ end
29
+
30
+ def _directories_path
31
+ listener.directories.map(&:to_s)
32
+ end
33
+
34
+ def _notify_change(path, options)
35
+ Actor[:listen_change_pool].async.change(path, options) if listener.listen?
36
+ end
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,94 @@
1
+ module Listen
2
+ module Adapter
3
+
4
+ # Listener implementation for BSD's `kqueue`.
5
+ #
6
+ class BSD < Base
7
+ # Watched kqueue events
8
+ #
9
+ # @see http://www.freebsd.org/cgi/man.cgi?query=kqueue
10
+ # @see https://github.com/mat813/rb-kqueue/blob/master/lib/rb-kqueue/queue.rb
11
+ #
12
+ EVENTS = [:delete, :write, :extend, :attrib, :rename] # :link, :revoke
13
+
14
+ # The message to show when wdm gem isn't available
15
+ #
16
+ BUNDLER_DECLARE_GEM = <<-EOS.gsub(/^ {6}/, '')
17
+ Please add the following to your Gemfile to avoid polling for changes:
18
+ require 'rbconfig'
19
+ gem 'rb-kqueue', '>= 0.2' if RbConfig::CONFIG['target_os'] =~ /freebsd/i
20
+ EOS
21
+
22
+ def self.usable?
23
+ if RbConfig::CONFIG['target_os'] =~ /freebsd/i
24
+ require 'rb-kqueue'
25
+ require 'find'
26
+ true
27
+ end
28
+ rescue Gem::LoadError
29
+ Kernel.warn BUNDLER_DECLARE_GEM
30
+ end
31
+
32
+ def start
33
+ worker = _init_worker
34
+ worker.poll
35
+ end
36
+
37
+ private
38
+
39
+ # Initializes a kqueue Queue and adds a watcher for each files in
40
+ # the directories passed to the adapter.
41
+ #
42
+ # @return [INotify::Notifier] initialized kqueue
43
+ #
44
+ def _init_worker
45
+ KQueue::Queue.new.tap do |queue|
46
+ _directories_path.each do |path|
47
+ Find.find(path) { |file_path| _watch_file(file_path, queue) }
48
+ end
49
+ end
50
+ end
51
+
52
+ def _worker_callback
53
+ lambda do |event|
54
+ _notify_change(_event_path(event), type: 'file', change: _change(event.flags))
55
+
56
+ # If it is a directory, and it has a write flag, it means a
57
+ # file has been added so find out which and deal with it.
58
+ # No need to check for removed files, kqueue will forget them
59
+ # when the vfs does.
60
+ _watch_for_new_file(event) if _new_file_added?(event)
61
+ end
62
+ end
63
+
64
+ def _change(event_flags)
65
+ { modified: [:attrib, :extend],
66
+ added: [:write],
67
+ removed: [:rename, :delete] }.each do |change, flags|
68
+ return change unless (flags & event_flags).empty?
69
+ end
70
+ nil
71
+ end
72
+
73
+ def _event_path(event)
74
+ Pathname.new(event.watcher.path)
75
+ end
76
+
77
+ def _new_file_added?(event)
78
+ File.directory?(event.watcher.path) && event.flags.include?(:write)
79
+ end
80
+
81
+ def _watch_for_new_file(event)
82
+ queue = event.watcher.queue
83
+ Find.find(path) do |file_path|
84
+ _watch_file(file_path, queue) unless queue.watchers.detect { |k,v| v.path == file.to_s }
85
+ end
86
+ end
87
+
88
+ def _watch_file(path, queue)
89
+ queue.watch_file(path, *EVENTS, &_worker_callback)
90
+ end
91
+ end
92
+
93
+ end
94
+ end
@@ -0,0 +1,44 @@
1
+ module Listen
2
+ module Adapter
3
+
4
+ # Adapter implementation for Mac OS X `FSEvents`.
5
+ #
6
+ class Darwin < Base
7
+
8
+ def self.usable?
9
+ RbConfig::CONFIG['target_os'] =~ /darwin(1.+)?$/i
10
+ end
11
+
12
+ def initialize(listener)
13
+ require 'rb-fsevent'
14
+ super
15
+ end
16
+
17
+ def start
18
+ worker = _init_worker
19
+ worker.run
20
+ end
21
+
22
+ private
23
+
24
+ # Initializes a FSEvent worker and adds a watcher for
25
+ # each directory listened.
26
+ #
27
+ def _init_worker
28
+ FSEvent.new.tap do |worker|
29
+ worker.watch(_directories_path, latency: _latency) do |changes|
30
+ _changes_path(changes).each { |path| _notify_change(path, type: 'Dir') }
31
+ end
32
+ end
33
+ end
34
+
35
+ def _changes_path(changes)
36
+ changes.map do |path|
37
+ path.sub!(/\/$/, '')
38
+ Pathname.new(path)
39
+ end
40
+ end
41
+ end
42
+
43
+ end
44
+ end