From 5a403460094370a4f0fc5d8bb4d7df37537bfc66 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Wed, 30 Aug 2023 23:29:52 +1200 Subject: [PATCH] Allow queue to be used for task `finished`. --- lib/async/queue.rb | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/lib/async/queue.rb b/lib/async/queue.rb index 224b2d92..bfd2e709 100644 --- a/lib/async/queue.rb +++ b/lib/async/queue.rb @@ -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 @@ -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 @@ -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