Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
35 lines (26 sloc) 640 Bytes
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.