allow callback-able objects to share callbacks

This commit is contained in:
HoneyryderChuck 2018-01-13 17:49:40 +00:00
parent 539bb3c7d0
commit 0e6cbee92e
4 changed files with 15 additions and 10 deletions

View File

@ -17,9 +17,14 @@ module HTTPX
callbacks(type).delete_if { |pr| pr[*args] == :delete } callbacks(type).delete_if { |pr| pr[*args] == :delete }
end end
private protected
def callbacks(type) def inherit_callbacks(callbackable)
@callbacks = callbackable.callbacks
end
def callbacks(type=nil)
return @callbacks unless type
@callbacks ||= Hash.new { |h, k| h[k] = [] } @callbacks ||= Hash.new { |h, k| h[k] = [] }
@callbacks[type] @callbacks[type]
end end

View File

@ -32,6 +32,7 @@ module HTTPX
extend Forwardable extend Forwardable
include Registry include Registry
include Loggable include Loggable
include Callbacks
require "httpx/channel/http2" require "httpx/channel/http2"
require "httpx/channel/http1" require "httpx/channel/http1"
@ -39,7 +40,7 @@ module HTTPX
BUFFER_SIZE = 1 << 14 BUFFER_SIZE = 1 << 14
class << self class << self
def by(uri, options, &blk) def by(uri, options)
io = case uri.scheme io = case uri.scheme
when "http" when "http"
IO.registry("tcp").new(uri.host, uri.port, options) IO.registry("tcp").new(uri.host, uri.port, options)
@ -48,7 +49,7 @@ module HTTPX
else else
raise Error, "#{uri.scheme}: unrecognized channel" raise Error, "#{uri.scheme}: unrecognized channel"
end end
new(io, options, &blk) new(io, options)
end end
end end
@ -56,14 +57,13 @@ module HTTPX
def_delegator :@write_buffer, :empty? def_delegator :@write_buffer, :empty?
def initialize(io, options, &on_response) def initialize(io, options)
@io = io @io = io
@options = Options.new(options) @options = Options.new(options)
@window_size = @options.window_size @window_size = @options.window_size
@read_buffer = "".b @read_buffer = "".b
@write_buffer = Buffer.new(BUFFER_SIZE) @write_buffer = Buffer.new(BUFFER_SIZE)
@pending = [] @pending = []
@on_response = on_response
end end
def match?(uri) def match?(uri)
@ -161,7 +161,7 @@ module HTTPX
def build_parser(protocol=@io.protocol) def build_parser(protocol=@io.protocol)
parser = registry(protocol).new(@write_buffer, @options) parser = registry(protocol).new(@write_buffer, @options)
parser.on(:response, &@on_response) parser.inherit_callbacks(self)
parser.on(:close) { throw(:close, self) } parser.on(:close) { throw(:close, self) }
parser parser
end end

View File

@ -38,8 +38,8 @@ module HTTPX
end end
end end
def build_channel(uri, &on_response) def build_channel(uri)
channel = Channel.by(uri, @options, &on_response) channel = Channel.by(uri, @options)
register_channel(channel) register_channel(channel)
channel channel
end end

View File

@ -44,7 +44,7 @@ module HTTPX
@parser = nil @parser = nil
when :idle when :idle
@parser = ProxyParser.new(@write_buffer, @options) @parser = ProxyParser.new(@write_buffer, @options)
@parser.on(:response, &@on_response) @parser.inherit_callbacks(self)
@parser.on(:close) { throw(:close, self) } @parser.on(:close) { throw(:close, self) }
else else
return return