Sha256: a3c26119e060a0c8d7dd59c76bdf6dc8c5c55ba226816699b601bafd73de1872

Contents?: true

Size: 539 Bytes

Versions: 28

Compression:

Stored size: 539 Bytes

Contents

# 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 callbacks(type = nil)
      return @callbacks unless type

      @callbacks ||= Hash.new { |h, k| h[k] = [] }
      @callbacks[type]
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
httpx-0.6.3 lib/httpx/callbacks.rb
httpx-0.6.2 lib/httpx/callbacks.rb
httpx-0.6.1 lib/httpx/callbacks.rb
httpx-0.6.0 lib/httpx/callbacks.rb
httpx-0.5.1 lib/httpx/callbacks.rb
httpx-0.5.0 lib/httpx/callbacks.rb
httpx-0.4.1 lib/httpx/callbacks.rb
httpx-0.4.0 lib/httpx/callbacks.rb