Sha256: 7caba4e1c794bd9533a1e218136dd8719bec615df809b81087db474ac7d3712f

Contents?: true

Size: 751 Bytes

Versions: 13

Compression:

Stored size: 751 Bytes

Contents

# frozen_string_literal: true
# http://spin.atomicobject.com/2014/07/07/ruby-queue-pop-timeout/
class QueueWithTimeout
  def initialize
    @mutex = Mutex.new
    @queue = []
    @recieved = ConditionVariable.new
  end

  def push(item)
    @mutex.synchronize do
      @queue << item
      @recieved.signal
    end
  end

  def <<(item)
    push(item)
  end

  def pop(non_block = false)
    pop_with_timeout(non_block ? 0 : nil)
  end

  def pop_with_timeout(timeout = nil)
    @mutex.synchronize do
      if @queue.empty?
        @recieved.wait(@mutex, timeout) if timeout != 0
        # if we're still empty after the timeout, raise exception
        raise ThreadError, 'queue empty' if @queue.empty?
      end
      @queue.shift
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
slack-ruby-client-2.4.0 spec/support/queue_with_timeout.rb
slack-ruby-client-2.3.0 spec/support/queue_with_timeout.rb
slack-ruby-client-2.2.0 spec/support/queue_with_timeout.rb
slack-ruby-client-2.1.0 spec/support/queue_with_timeout.rb
slack-ruby-client-2.0.0 spec/support/queue_with_timeout.rb
slack-ruby-client-1.1.0 spec/support/queue_with_timeout.rb
slack-ruby-client-1.0.0 spec/support/queue_with_timeout.rb
slack-ruby-client-0.17.0 spec/support/queue_with_timeout.rb
slack-ruby-client-0.16.0 spec/support/queue_with_timeout.rb
slack-ruby-client-0.15.1 spec/support/queue_with_timeout.rb
slack-ruby-client-0.15.0 spec/support/queue_with_timeout.rb
slack-ruby-client-0.14.6 spec/support/queue_with_timeout.rb
slack-ruby-client-0.14.5 spec/support/queue_with_timeout.rb