Sha256: 517a06922c990bd88daf2103dd73f03d0f00bc1d384d35828b2e01f4a79495bb

Contents?: true

Size: 756 Bytes

Versions: 11

Compression:

Stored size: 756 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[*args] } # rubocop:disable Style/YodaCondition
    end

    protected

    def callbacks_for?(type)
      @callbacks.key?(type) && !@callbacks[type].empty?
    end

    def callbacks(type = nil)
      return @callbacks unless type

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

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
httpx-0.15.4 lib/httpx/callbacks.rb
httpx-0.15.3 lib/httpx/callbacks.rb
httpx-0.15.2 lib/httpx/callbacks.rb
httpx-0.15.1 lib/httpx/callbacks.rb
httpx-0.15.0 lib/httpx/callbacks.rb
httpx-0.14.5 lib/httpx/callbacks.rb
httpx-0.14.4 lib/httpx/callbacks.rb
httpx-0.14.3 lib/httpx/callbacks.rb
httpx-0.14.2 lib/httpx/callbacks.rb
httpx-0.14.1 lib/httpx/callbacks.rb
httpx-0.14.0 lib/httpx/callbacks.rb