Class: Watchr::EventHandler::EM

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/watchr/event_handlers/em.rb

Defined Under Namespace

Modules: SingleFileWatcher

Instance Method Summary collapse

Methods included from Base

#notify

Constructor Details

#initializeEM

Returns a new instance of EM.



114
115
116
117
118
119
# File 'lib/watchr/event_handlers/em.rb', line 114

def initialize
  SingleFileWatcher.handler = self
  @old_paths = []
  @first_time = true
  @watchers = {}
end

Instance Method Details

#add(path) ⇒ Object



178
179
180
181
182
# File 'lib/watchr/event_handlers/em.rb', line 178

def add path
  @monitored_paths << path
  # $stderr.print "add #{path.inspect}\n"
  attach :dependence
end

#forget(connection, path) ⇒ Object



153
154
155
156
157
158
159
160
161
# File 'lib/watchr/event_handlers/em.rb', line 153

def forget connection, path
  if @watchers[path] != connection
    $stderr.puts \
      "warning: no/wrong watcher to forget for #{path}: #{@watchers[path]} vs #{connection}"
  end
  @watchers.delete path
  raise "hell: #{path}" if !@old_paths.include? Pathname(path)
  @old_paths.delete Pathname(path)
end

#listen(monitored_paths) ⇒ Object

Enters listening loop.

Will block control flow until application is explicitly stopped/killed.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/watchr/event_handlers/em.rb', line 125

def listen(monitored_paths)
  # FIX ... make more generic (handle at a higher level ...)
  while true
    @monitored_paths = monitored_paths
    @old_paths = []
    @first_time = true
    @watchers = {}
    ::EM.run do
      attach
      if Watchr.options.once
        Watchr.batches.each do |k,v|
          k.deliver
        end
        return 
      end
    end
  end
end

#refresh(monitored_paths) ⇒ Object

Rebuilds file bindings.

will detach all current bindings, and reattach the monitored_paths



148
149
150
151
# File 'lib/watchr/event_handlers/em.rb', line 148

def refresh(monitored_paths)
  @monitored_paths = monitored_paths
  attach
end

#watch(path, event = nil) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/watchr/event_handlers/em.rb', line 163

def watch path, event = nil
  begin
    # p "watch", path, @first_time
    ::EM.watch_file path.to_s, SingleFileWatcher do |watcher|
      watcher.init @first_time, event
      @watchers[path] = watcher
    end
    @old_paths << path
  rescue Errno::ENOENT => e
    $stderr.puts e
  rescue Exception => e
    $stderr.puts e
  end
end