1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
# frozen_string_literal: true
module HTTPX
module Callbacks
def on(type, &action)
callbacks(type) << action
action
end
def once(type, &block)
on(type) do |*args, &callback|
block.call(*args, &callback)
:delete
end
end
def emit(type, *args)
log { "emit #{type.inspect} callbacks" } if respond_to?(:log)
callbacks(type).delete_if { |pr| :delete == pr.call(*args) } # rubocop:disable Style/YodaCondition
end
def callbacks_for?(type)
@callbacks && @callbacks.key?(type) && @callbacks[type].any?
end
protected
def callbacks(type = nil)
return @callbacks unless type
@callbacks ||= Hash.new { |h, k| h[k] = [] }
@callbacks[type]
end
end
end
|