lib/consumer.rb in gitmqueue-0.1.0 vs lib/consumer.rb in gitmqueue-0.1.1
- old
+ new
@@ -1,22 +1,38 @@
# frozen_string_literal: true
+require 'listen'
+
module GitMQueue
class Consumer
def initialize(storage:, name:, branch:)
@storage = storage
@branch = branch
@name = name
- @label = "#{branch}.#{name}"
end
+ def branch_file
+ @branch_file ||= File.join(@storage.path, 'refs', 'heads')
+ end
+
def consume(&block)
- @storage.wait_branch @branch
+ @block = block
+ listener.start
+ end
- loop do
- commit = @storage.poll(@branch, @label)
- block.call commit.message
- @storage.tag(@label, commit)
+ def stop
+ listener.stop
+ end
+
+ private
+
+ def listener
+ @listener ||= Listen.to(branch_file, only: /#{Regexp.escape(@branch)}/) do
+ commits = @storage.commits(@branch, @name)
+ commits.each do |commit|
+ @block.call commit.message
+ @storage.tag(@name, commit)
+ end
end
end
end
end