Class: Listen::Listener
- Inherits:
-
Object
- Object
- Listen::Listener
- Defined in:
- lib/listen/listener.rb
Constant Summary collapse
- RELATIVE_PATHS_WITH_MULTIPLE_DIRECTORIES_WARNING_MESSAGE =
"The relative_paths option doesn't work when listening to multiple diretories."
Instance Attribute Summary collapse
-
#block ⇒ Object
Returns the value of attribute block.
-
#changes ⇒ Object
Returns the value of attribute changes.
-
#directories ⇒ Object
Returns the value of attribute directories.
-
#options ⇒ Object
Returns the value of attribute options.
-
#paused ⇒ Object
Returns the value of attribute paused.
-
#thread ⇒ Object
Returns the value of attribute thread.
Instance Method Summary collapse
- #_init_actors ⇒ Object private
- #_init_debug ⇒ Object private
- #_init_options(options = {}) ⇒ Object private
- #_pop_changes ⇒ Object private
- #_signals_trap ⇒ Object private
- #_terminate_actors ⇒ Object private
- #_wait_for_changes ⇒ Object private
-
#ignore(regexps) ⇒ Object
Adds ignore patterns to the existing one (See DEFAULT_IGNORED_DIRECTORIES and DEFAULT_IGNORED_EXTENSIONS in Listen::Silencer).
-
#ignore!(regexps) ⇒ Object
Overwrites ignore patterns (See DEFAULT_IGNORED_DIRECTORIES and DEFAULT_IGNORED_EXTENSIONS in Listen::Silencer).
-
#initialize(*args) {|modified, added, removed| ... } ⇒ Listener
constructor
Initializes the directories listener.
-
#listen? ⇒ Boolean
Returns true if Listener is not paused.
-
#pause ⇒ Object
Pauses listening callback (adapter still running).
-
#paused? ⇒ Boolean
Returns true if Listener is paused.
-
#start ⇒ Object
Starts the listener by initializing the adapter and building the directory record concurrently, then it starts the adapter to watch for changes.
-
#stop ⇒ Object
Terminates all Listen actors and kill the adapter.
-
#unpause ⇒ Object
Unpauses listening callback.
Constructor Details
#initialize(*args) {|modified, added, removed| ... } ⇒ Listener
Initializes the directories listener.
23 24 25 26 27 28 29 |
# File 'lib/listen/listener.rb', line 23 def initialize(*args, &block) @options = (args.last.is_a?(Hash) ? args.pop : {}) @directories = args.flatten.map { |path| Pathname.new(path).realpath } @changes = [] @block = block _init_debug end |
Instance Attribute Details
#block ⇒ Object
Returns the value of attribute block.
9 10 11 |
# File 'lib/listen/listener.rb', line 9 def block @block end |
#changes ⇒ Object
Returns the value of attribute changes.
9 10 11 |
# File 'lib/listen/listener.rb', line 9 def changes @changes end |
#directories ⇒ Object
Returns the value of attribute directories.
9 10 11 |
# File 'lib/listen/listener.rb', line 9 def directories @directories end |
#options ⇒ Object
Returns the value of attribute options.
9 10 11 |
# File 'lib/listen/listener.rb', line 9 def @options end |
#paused ⇒ Object
Returns the value of attribute paused.
9 10 11 |
# File 'lib/listen/listener.rb', line 9 def paused @paused end |
#thread ⇒ Object
Returns the value of attribute thread.
9 10 11 |
# File 'lib/listen/listener.rb', line 9 def thread @thread end |
Instance Method Details
#_init_actors ⇒ Object (private)
115 116 117 118 119 120 |
# File 'lib/listen/listener.rb', line 115 def _init_actors Celluloid::Actor[:listen_silencer] = Silencer.new(self) Celluloid::Actor[:listen_change_pool] = Change.pool(args: self) Celluloid::Actor[:listen_adapter] = Adapter.new(self) Celluloid::Actor[:listen_record] = Record.new(self) end |
#_init_debug ⇒ Object (private)
107 108 109 110 111 112 113 |
# File 'lib/listen/listener.rb', line 107 def _init_debug if [:debug] Celluloid.logger.level = Logger::INFO else Celluloid.logger = nil end end |
#_init_options(options = {}) ⇒ Object (private)
100 101 102 103 104 105 |
# File 'lib/listen/listener.rb', line 100 def ( = {}) { debug: false, latency: nil, force_polling: false, polling_fallback_message: nil }.merge() end |
#_pop_changes ⇒ Object (private)
147 148 149 150 151 152 153 154 |
# File 'lib/listen/listener.rb', line 147 def _pop_changes changes = { modified: [], added: [], removed: [] } until @changes.empty? change = @changes.pop change.each { |k, v| changes[k] << v.to_s } end changes.each { |_, v| v.uniq! } end |
#_signals_trap ⇒ Object (private)
122 123 124 125 126 127 |
# File 'lib/listen/listener.rb', line 122 def _signals_trap return if defined?(JRUBY_VERSION) if Signal.list.keys.include?('INT') Signal.trap('INT') { stop } end end |
#_terminate_actors ⇒ Object (private)
156 157 158 159 160 161 |
# File 'lib/listen/listener.rb', line 156 def _terminate_actors Celluloid::Actor[:listen_adapter].terminate Celluloid::Actor[:listen_silencer].terminate Celluloid::Actor[:listen_change_pool].terminate Celluloid::Actor[:listen_record].terminate end |
#_wait_for_changes ⇒ Object (private)
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/listen/listener.rb', line 129 def _wait_for_changes loop do break if @stopping changes = _pop_changes unless changes.all? { |_,v| v.empty? } block.call(changes[:modified], changes[:added], changes[:removed]) end sleep 0.1 end _terminate_actors exit rescue => ex Kernel.warn "[Listen warning]: Change block raised an exception: #{$!}" Kernel.warn "Backtrace:\n\t#{ex.backtrace.join("\n\t")}" end |
#ignore(regexps) ⇒ Object
Adds ignore patterns to the existing one (See DEFAULT_IGNORED_DIRECTORIES and DEFAULT_IGNORED_EXTENSIONS in Listen::Silencer)
83 84 85 86 |
# File 'lib/listen/listener.rb', line 83 def ignore(regexps) @options[:ignore] = [[:ignore], regexps] Celluloid::Actor[:listen_silencer] = Silencer.new(self) end |
#ignore!(regexps) ⇒ Object
Overwrites ignore patterns (See DEFAULT_IGNORED_DIRECTORIES and DEFAULT_IGNORED_EXTENSIONS in Listen::Silencer)
92 93 94 95 96 |
# File 'lib/listen/listener.rb', line 92 def ignore!(regexps) @options.delete(:ignore) @options[:ignore!] = regexps Celluloid::Actor[:listen_silencer] = Silencer.new(self) end |
#listen? ⇒ Boolean
Returns true if Listener is not paused
75 76 77 |
# File 'lib/listen/listener.rb', line 75 def listen? @paused == false end |
#pause ⇒ Object
Pauses listening callback (adapter still running)
52 53 54 |
# File 'lib/listen/listener.rb', line 52 def pause @paused = true end |
#paused? ⇒ Boolean
Returns true if Listener is paused
67 68 69 |
# File 'lib/listen/listener.rb', line 67 def paused? @paused == true end |
#start ⇒ Object
Starts the listener by initializing the adapter and building the directory record concurrently, then it starts the adapter to watch for changes. The current thread is not blocked after starting.
35 36 37 38 39 40 41 |
# File 'lib/listen/listener.rb', line 35 def start _signals_trap _init_actors unpause Celluloid::Actor[:listen_adapter].async.start @thread = Thread.new { _wait_for_changes } end |
#stop ⇒ Object
Terminates all Listen actors and kill the adapter.
45 46 47 48 |
# File 'lib/listen/listener.rb', line 45 def stop @stopping = true thread.join end |
#unpause ⇒ Object
Unpauses listening callback
58 59 60 61 |
# File 'lib/listen/listener.rb', line 58 def unpause Celluloid::Actor[:listen_record].build @paused = false end |