Class: Bowline::Watcher

Inherits:
Object show all
Defined in:
lib/bowline/watcher.rb

Defined Under Namespace

Modules: Base Classes: Callback

Instance Method Summary collapse

Constructor Details

#initializeWatcher

Returns a new instance of Watcher.



90
91
92
# File 'lib/bowline/watcher.rb', line 90

def initialize
  @listeners = {}
end

Instance Method Details

#append(event, method = nil, oneshot = false, &block) ⇒ Object

Add new method/proc to a specific event.



95
96
97
98
99
# File 'lib/bowline/watcher.rb', line 95

def append(event, method = nil, oneshot = false, &block)
  callback = Callback.new(self, event, method||block, oneshot)
  (@listeners[event] ||= []) << callback
  callback
end

#call(event, *args) ⇒ Object

Call an event’s callbacks with provided arguments.



102
103
104
105
106
107
# File 'lib/bowline/watcher.rb', line 102

def call(event, *args)
  return unless @listeners[event]
  @listeners[event].each do |callback|
    callback.call(*args)
  end
end

#clearObject

Clear all events and callbacks.



124
125
126
# File 'lib/bowline/watcher.rb', line 124

def clear
  @listeners = {}
end

#remove(event, value = nil) ⇒ Object

Remove an specific callback on an event, or all an event’s callbacks.



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/bowline/watcher.rb', line 111

def remove(event, value=nil)
  return unless @listeners[event]
  if value
    @listeners[event].delete(value)
    if @listeners[event].empty?
      @listeners.delete(event)
    end
  else
    @listeners.delete(event)
  end
end