mirror of
https://github.com/HoneyryderChuck/httpx.git
synced 2025-10-06 00:02:08 -04:00
33 lines
628 B
Ruby
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
|