Class: Listen::Adapter::BSD
- Inherits:
-
Base
- Object
- Base
- Listen::Adapter::BSD
show all
- Defined in:
- lib/listen/adapter/bsd.rb
Overview
Listener implementation for BSD's kqueue.
Constant Summary
collapse
- EVENTS =
[:delete, :write, :extend, :attrib, :rename]
- BUNDLER_DECLARE_GEM =
The message to show when wdm gem isn't available
" Please add the following to your Gemfile to avoid polling for changes:\n require 'rbconfig'\n gem 'rb-kqueue', '>= 0.2' if RbConfig::CONFIG['target_os'] =~ /freebsd/i\n".gsub(/^ {6}/, '')
Constants inherited
from Base
Listen::Adapter::Base::DEFAULT_LATENCY
Instance Attribute Summary
Attributes inherited from Base
#listener
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Base
#_directories_path, #_latency, #_notify_change, #initialize
Class Method Details
.usable? ⇒ Boolean
22
23
24
25
26
27
28
29
30
|
# File 'lib/listen/adapter/bsd.rb', line 22
def self.usable?
if RbConfig::CONFIG['target_os'] =~ /freebsd/i
require 'rb-kqueue'
require 'find'
true
end
rescue Gem::LoadError
Kernel.warn BUNDLER_DECLARE_GEM
end
|
Instance Method Details
#_change(event_flags) ⇒ Object
64
65
66
67
68
69
70
71
|
# File 'lib/listen/adapter/bsd.rb', line 64
def _change(event_flags)
{ modified: [:attrib, :extend],
added: [:write],
removed: [:rename, :delete] }.each do |change, flags|
return change unless (flags & event_flags).empty?
end
nil
end
|
#_event_path(event) ⇒ Object
73
74
75
|
# File 'lib/listen/adapter/bsd.rb', line 73
def _event_path(event)
Pathname.new(event.watcher.path)
end
|
#_init_worker ⇒ INotify::Notifier
Initializes a kqueue Queue and adds a watcher for each files in
the directories passed to the adapter.
44
45
46
47
48
49
50
|
# File 'lib/listen/adapter/bsd.rb', line 44
def _init_worker
KQueue::Queue.new.tap do |queue|
_directories_path.each do |path|
Find.find(path) { |file_path| _watch_file(file_path, queue) }
end
end
end
|
#_new_file_added?(event) ⇒ Boolean
77
78
79
|
# File 'lib/listen/adapter/bsd.rb', line 77
def _new_file_added?(event)
File.directory?(event.watcher.path) && event.flags.include?(:write)
end
|
#_watch_file(path, queue) ⇒ Object
88
89
90
|
# File 'lib/listen/adapter/bsd.rb', line 88
def _watch_file(path, queue)
queue.watch_file(path, *EVENTS, &_worker_callback)
end
|
#_watch_for_new_file(event) ⇒ Object
81
82
83
84
85
86
|
# File 'lib/listen/adapter/bsd.rb', line 81
def _watch_for_new_file(event)
queue = event.watcher.queue
Find.find(path) do |file_path|
_watch_file(file_path, queue) unless queue.watchers.detect { |k,v| v.path == file.to_s }
end
end
|
#_worker_callback ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/listen/adapter/bsd.rb', line 52
def _worker_callback
lambda do |event|
_notify_change(_event_path(event), type: 'file', change: _change(event.flags))
_watch_for_new_file(event) if _new_file_added?(event)
end
end
|
#start ⇒ Object
32
33
34
35
|
# File 'lib/listen/adapter/bsd.rb', line 32
def start
worker = _init_worker
worker.poll
end
|