Sha256: 4eec164f143b070faefb8ec80985864a9b8771ddefbb2325d89027e728b4a9e7

Contents?: true

Size: 761 Bytes

Versions: 33

Compression:

Stored size: 761 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

    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

33 entries across 33 versions & 1 rubygems

Version Path
httpx-0.22.4 lib/httpx/callbacks.rb
httpx-0.22.3 lib/httpx/callbacks.rb
httpx-0.22.2 lib/httpx/callbacks.rb
httpx-0.22.1 lib/httpx/callbacks.rb
httpx-0.22.0 lib/httpx/callbacks.rb
httpx-0.21.1 lib/httpx/callbacks.rb
httpx-0.21.0 lib/httpx/callbacks.rb
httpx-0.20.5 lib/httpx/callbacks.rb
httpx-0.20.4 lib/httpx/callbacks.rb
httpx-0.20.3 lib/httpx/callbacks.rb
httpx-0.20.2 lib/httpx/callbacks.rb
httpx-0.20.1 lib/httpx/callbacks.rb
httpx-0.20.0 lib/httpx/callbacks.rb
httpx-0.19.8 lib/httpx/callbacks.rb
httpx-0.19.7 lib/httpx/callbacks.rb
httpx-0.19.6 lib/httpx/callbacks.rb
httpx-0.19.5 lib/httpx/callbacks.rb
httpx-0.19.4 lib/httpx/callbacks.rb
httpx-0.19.3 lib/httpx/callbacks.rb
httpx-0.19.2 lib/httpx/callbacks.rb