lib/cotton_tail/queue/reader.rb in cotton-tail-0.2.1 vs lib/cotton_tail/queue/reader.rb in cotton-tail-0.3.0

- old
+ new

@@ -8,25 +8,33 @@ class Reader def self.spawn(queue, **kwargs) Thread.new { new(queue, **kwargs).start } end - def initialize(queue, on_message:) + def initialize(queue, app:) @queue = queue - @on_message = on_message + @app = app end def fiber @fiber ||= Fiber.new do Fiber.yield @queue.pop until @queue.empty? && @queue.closed? end end def start - while fiber.alive? - args = fiber.resume - @on_message.call(*args) if args - end + call_next while fiber.alive? + end + + private + + def call_next + args = fiber.resume + middleware.call([@app, *args]) if args + end + + def middleware + @app.config.middleware end end end end