Sha256: d03c561978708be4fd94ae14e42b0f03d4ee778af64bcb76fa428b0a23eafe22

Contents?: true

Size: 897 Bytes

Versions: 12

Compression:

Stored size: 897 Bytes

Contents

module Steep
  module Server
    class DelayQueue
      attr_reader :delay, :thread, :queue, :last_task

      def initialize(delay:)
        @delay = delay

        @queue = Thread::Queue.new

        @thread = Thread.new do
          while (scheduled_at, proc = queue.pop)
            # @type var scheduled_at: Time
            # @type var proc: ^() -> void

            diff = scheduled_at - Time.now
            case
            when diff > 0.1
              sleep diff
            when diff > 0
              while Time.now < scheduled_at
                # nop
                sleep 0
              end
            end

            if proc.equal?(last_task)
              proc[]
            end
          end
        end
      end

      def execute(&block)
        @last_task = block
        scheduled_at = Time.now + delay
        queue << [scheduled_at, block]
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
steep-1.8.0.dev.2 lib/steep/server/delay_queue.rb
steep-1.8.0.dev.1 lib/steep/server/delay_queue.rb
steep-1.7.1 lib/steep/server/delay_queue.rb
steep-1.7.0 lib/steep/server/delay_queue.rb
steep-1.7.0.dev.3 lib/steep/server/delay_queue.rb
steep-1.7.0.dev.2 lib/steep/server/delay_queue.rb
steep-1.7.0.dev.1 lib/steep/server/delay_queue.rb
steep-1.6.0 lib/steep/server/delay_queue.rb
steep-1.6.0.pre.4 lib/steep/server/delay_queue.rb
steep-1.6.0.pre.3 lib/steep/server/delay_queue.rb
steep-1.6.0.pre.2 lib/steep/server/delay_queue.rb
steep-1.6.0.pre.1 lib/steep/server/delay_queue.rb