Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
tree: 80e044c262
Fetching contributors…

Cannot retrieve contributors at this time

35 lines (26 sloc) 0.64 kB
module Watchable
def watchers
@watchers ||= Hash.new { |h, k| h[k] = [] }
end
def fire event, *args
watchers[event].each { |w| w.call *args }
watchers[:all].each { |w| w.call event, *args }
self
end
def on event, callable = Proc.new
watchers[event] << callable
self
end
def once event, callable = Proc.new
wrapper = lambda do |*args|
off event, wrapper
callable.call *args
end
on event, wrapper
end
def off event, callable = nil, &block
watcher = callable || block
watcher ? watchers[event].delete(watcher) : watchers[event].clear
self
end
end
Jump to Line
Something went wrong with that request. Please try again.