Sha256: a41bcd8c93b18d325eaa55b82dacda7fdf8f5446bb4c547fd81d51b8dc275f32

Contents?: true

Size: 780 Bytes

Versions: 28

Compression:

Stored size: 780 Bytes

Contents

# frozen_string_literal: true

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

    def once(type, &block)
      on(type) do |*args, &callback|
        block.call(*args, &callback)
        :delete
      end
      self
    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

28 entries across 28 versions & 1 rubygems

Version Path
httpx-1.3.4 lib/httpx/callbacks.rb
httpx-1.3.3 lib/httpx/callbacks.rb
httpx-1.3.2 lib/httpx/callbacks.rb
httpx-1.3.1 lib/httpx/callbacks.rb
httpx-1.3.0 lib/httpx/callbacks.rb
httpx-1.2.6 lib/httpx/callbacks.rb
httpx-1.2.4 lib/httpx/callbacks.rb
httpx-1.2.3 lib/httpx/callbacks.rb
httpx-1.2.2 lib/httpx/callbacks.rb
httpx-1.2.1 lib/httpx/callbacks.rb
httpx-1.2.0 lib/httpx/callbacks.rb
httpx-1.1.5 lib/httpx/callbacks.rb
httpx-1.1.4 lib/httpx/callbacks.rb
httpx-1.1.3 lib/httpx/callbacks.rb
httpx-1.1.2 lib/httpx/callbacks.rb
httpx-1.1.1 lib/httpx/callbacks.rb
httpx-1.1.0 lib/httpx/callbacks.rb
httpx-1.0.2 lib/httpx/callbacks.rb
httpx-0.24.7 lib/httpx/callbacks.rb
httpx-1.0.1 lib/httpx/callbacks.rb