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

- (Watcher) initialize



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

def initialize
  @listeners = {}
end

Instance Method Details

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

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

- (Object) call(event, *args)

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

- (Object) clear

Clear all events and callbacks.



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

def clear
  @listeners = {}
end

- (Object) remove(event, value = nil)

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