Class: Listen::Listener

Inherits:
Object
  • Object
show all
Includes:
Celluloid::FSM, Internals::Logging, QueueOptimizer
Defined in:
lib/listen/listener.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Internals::Logging

#_debug, #_error_exception, #_format_error, #_info, #_log, #_warn

Constructor Details

#initialize(*args) {|modified, added, removed| ... } ⇒ Listener

Initializes the directories listener.

Parameters:

  • directory (String)

    the directories to listen to

  • options (Hash)

    the listen options (see Listen::Listener::Options)

Yields:

  • (modified, added, removed)

    the changed files

Yield Parameters:

  • modified (Array<String>)

    the list of modified files

  • added (Array<String>)

    the list of added files

  • removed (Array<String>)

    the list of removed files



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/listen/listener.rb', line 40

def initialize(*args, &block)
  @options = _init_options(args.last.is_a?(Hash) ? args.pop : {})

  # Setup logging first
  if Celluloid.logger
    Celluloid.logger.level = _debug_level
    _info "Celluloid loglevel set to: #{Celluloid.logger.level}"
    _info "Listen version: #{Listen::VERSION}"
  end

  @silencer = Silencer.new
  _reconfigure_silencer({})

  @tcp_mode = nil
  if [:recipient, :broadcaster].include? args[1]
    target = args.shift
    @tcp_mode = args.shift
    _init_tcp_options(target)
  end

  @directories = args.flatten.map { |path| Pathname.new(path).realpath }
  @queue = Queue.new
  @block = block
  @registry = Celluloid::Registry.new

  transition :stopped
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



18
19
20
# File 'lib/listen/listener.rb', line 18

def block
  @block
end

#directoriesObject (readonly)

TODO: deprecate



23
24
25
# File 'lib/listen/listener.rb', line 23

def directories
  @directories
end

#hostObject (readonly)

TODO: deprecate NOTE: these are VERY confusing (broadcast + recipient modes)



28
29
30
# File 'lib/listen/listener.rb', line 28

def host
  @host
end

#optionsObject (readonly)

TODO: deprecate



23
24
25
# File 'lib/listen/listener.rb', line 23

def options
  @options
end

#portObject (readonly)

TODO: deprecate NOTE: these are VERY confusing (broadcast + recipient modes)



28
29
30
# File 'lib/listen/listener.rb', line 28

def port
  @port
end

#registryObject (readonly)

Returns the value of attribute registry.



24
25
26
# File 'lib/listen/listener.rb', line 24

def registry
  @registry
end

#silencerObject (readonly)

Returns the value of attribute silencer.



20
21
22
# File 'lib/listen/listener.rb', line 20

def silencer
  @silencer
end

#supervisorObject (readonly)

Returns the value of attribute supervisor.



24
25
26
# File 'lib/listen/listener.rb', line 24

def supervisor
  @supervisor
end

Instance Method Details

#async(type) ⇒ Object



154
155
156
157
# File 'lib/listen/listener.rb', line 154

def async(type)
  proxy = sync(type)
  proxy ? proxy.async : nil
end

#ignore(regexps) ⇒ Object

Add files and dirs to ignore on top of defaults

(@see Listen::Silencer for default ignored files and dirs)



140
141
142
# File 'lib/listen/listener.rb', line 140

def ignore(regexps)
  _reconfigure_silencer(ignore: [options[:ignore], regexps])
end

#ignore!(regexps) ⇒ Object

Replace default ignore patterns with provided regexp



145
146
147
# File 'lib/listen/listener.rb', line 145

def ignore!(regexps)
  _reconfigure_silencer(ignore: [], ignore!: regexps)
end

#only(regexps) ⇒ Object

Listen only to files and dirs matching regexp



150
151
152
# File 'lib/listen/listener.rb', line 150

def only(regexps)
  _reconfigure_silencer(only: regexps)
end

#pauseObject

Stops invoking callbacks (messages pile up)



112
113
114
# File 'lib/listen/listener.rb', line 112

def pause
  transition :paused
end

#paused=(value) ⇒ Object

TODO: deprecate



129
130
131
# File 'lib/listen/listener.rb', line 129

def paused=(value)
  transition value ? :paused : :processing
end

#paused?Boolean Also known as: paused

Returns:

  • (Boolean)


121
122
123
# File 'lib/listen/listener.rb', line 121

def paused?
  state == :paused
end

#processing?Boolean Also known as: listen?

processing means callbacks are called

Returns:

  • (Boolean)


117
118
119
# File 'lib/listen/listener.rb', line 117

def processing?
  state == :processing
end

#queue(type, change, dir, path, options = {}) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/listen/listener.rb', line 163

def queue(type, change, dir, path, options = {})
  fail "Invalid type: #{type.inspect}" unless [:dir, :file].include? type
  fail "Invalid change: #{change.inspect}" unless change.is_a?(Symbol)
  fail "Invalid path: #{path.inspect}" unless path.is_a?(String)
  if @options[:relative]
    dir = begin
            cwd = Pathname.pwd
            dir.relative_path_from(cwd)
          rescue ArgumentError
            dir
          end
  end
  @queue << [type, change, dir, path, options]

  @last_queue_event_time = Time.now.to_f
  _wakeup_wait_thread unless state == :paused

  return unless @tcp_mode == :broadcaster

  message = TCP::Message.new(type, change, dir, path, options)
  registry[:broadcaster].async.broadcast(message.payload)
end

#startObject Also known as: unpause

Starts processing events and starts adapters or resumes invoking callbacks if paused



99
100
101
# File 'lib/listen/listener.rb', line 99

def start
  transition :processing
end

#stopObject

Stops processing and terminates all actors



107
108
109
# File 'lib/listen/listener.rb', line 107

def stop
  transition :stopped
end

#sync(type) ⇒ Object



159
160
161
# File 'lib/listen/listener.rb', line 159

def sync(type)
  @registry[type]
end