Class: Wake::EventHandler::EM

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/wake/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.



115
116
117
118
119
120
121
# File 'lib/wake/event_handlers/em.rb', line 115

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

Instance Method Details

#add(path) ⇒ Object



180
181
182
183
184
185
186
187
188
# File 'lib/wake/event_handlers/em.rb', line 180

def add path
  # $stderr.print  "new #{path}\n"
  if false && !@monitored_paths.include?( path )
    $stderr.print "new #{path}\n"
  end
  @monitored_paths << path
  # $stderr.print "add #{path.inspect}\n"
  attach :dependence
end

#forget(connection, path) ⇒ Object



155
156
157
158
159
160
161
162
163
# File 'lib/wake/event_handlers/em.rb', line 155

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.



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

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 Wake.options.once
        Wake.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



150
151
152
153
# File 'lib/wake/event_handlers/em.rb', line 150

def refresh(monitored_paths)
  @monitored_paths = monitored_paths
  attach
end

#watch(path, event = nil) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/wake/event_handlers/em.rb', line 165

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