Skip to content

Commit

Permalink
Allow queue to be used for task finished.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Jul 11, 2024
1 parent bed3499 commit 5a40346
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions lib/async/queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
module Async
# A queue which allows items to be processed in order.
# @public Since `stable-v1`.
class Queue < Notification
def initialize(parent: nil)
class Queue
def initialize(parent: nil, available: Notification.new)
super()

@items = []
@parent = parent
@available = available
end

attr :items
Expand All @@ -31,18 +32,18 @@ def empty?
def <<(item)
@items << item

self.signal unless self.empty?
@available.signal unless self.empty?
end

def enqueue(*items)
@items.concat(items)

self.signal unless self.empty?
@available.signal unless self.empty?
end

def dequeue
while @items.empty?
self.wait
@available.wait
end

@items.shift
Expand All @@ -59,16 +60,23 @@ def each
yield item
end
end

def signal(value)
self.enqueue(value)
end

def wait
self.dequeue
end
end

# @public Since `stable-v1`.
class LimitedQueue < Queue
def initialize(limit = 1, **options)
def initialize(limit = 1, full: Notification.new, **options)
super(**options)

@limit = limit

@full = Notification.new
@full = full
end

attr :limit
Expand Down

0 comments on commit 5a40346

Please sign in to comment.