httpx/lib/httpx/callbacks.rb
2018-01-13 17:49:40 +00:00

33 lines
628 B
Ruby

# frozen_string_literal: true
module HTTPX
module Callbacks
def on(type, &action)
callbacks(type) << action
end
def once(event, &block)
on(event) do |*args, &callback|
block.call(*args, &callback)
:delete
end
end
def emit(type, *args)
callbacks(type).delete_if { |pr| pr[*args] == :delete }
end
protected
def inherit_callbacks(callbackable)
@callbacks = callbackable.callbacks
end
def callbacks(type=nil)
return @callbacks unless type
@callbacks ||= Hash.new { |h, k| h[k] = [] }
@callbacks[type]
end
end
end