Sha256: 77f51f7db43d2dc7e37cc562775d1a0c3fa08cbe115ddf9fd376a130c05c6ada

Contents?: true

Size: 758 Bytes

Versions: 6

Compression:

Stored size: 758 Bytes

Contents

# frozen_string_literal: true

module HTTPX
  module Callbacks
    def on(type, &action)
      callbacks(type) << action
    end

    def once(type, &block)
      on(type) do |*args, &callback|
        block.call(*args, &callback)
        :delete
      end
    end

    def only(type, &block)
      callbacks(type).clear
      on(type, &block)
    end

    def emit(type, *args)
      callbacks(type).delete_if { |pr| :delete == pr.call(*args) } # rubocop:disable Style/YodaCondition
    end

    def callbacks_for?(type)
      @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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
httpx-0.23.4 lib/httpx/callbacks.rb
httpx-0.23.3 lib/httpx/callbacks.rb
httpx-0.23.2 lib/httpx/callbacks.rb
httpx-0.23.1 lib/httpx/callbacks.rb
httpx-0.23.0 lib/httpx/callbacks.rb
httpx-0.22.5 lib/httpx/callbacks.rb