Class: Legion::Chat::NotificationQueue
- Inherits:
-
Object
- Object
- Legion::Chat::NotificationQueue
- Defined in:
- lib/legion/chat/notification_queue.rb
Constant Summary collapse
- PRIORITIES =
{ critical: 0, info: 1, debug: 2 }.freeze
Instance Method Summary collapse
- #clear ⇒ Object
-
#has_critical? ⇒ Boolean
rubocop:disable Naming/PredicatePrefix.
-
#initialize(max_size: 50) ⇒ NotificationQueue
constructor
A new instance of NotificationQueue.
- #pop_all(max_priority: :info) ⇒ Object
- #push(message:, priority: :info, source: nil) ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(max_size: 50) ⇒ NotificationQueue
Returns a new instance of NotificationQueue.
8 9 10 11 12 |
# File 'lib/legion/chat/notification_queue.rb', line 8 def initialize(max_size: 50) @queue = [] @mutex = Mutex.new @max_size = max_size end |
Instance Method Details
#clear ⇒ Object
38 39 40 |
# File 'lib/legion/chat/notification_queue.rb', line 38 def clear @mutex.synchronize { @queue.clear } end |
#has_critical? ⇒ Boolean
rubocop:disable Naming/PredicatePrefix
30 31 32 |
# File 'lib/legion/chat/notification_queue.rb', line 30 def has_critical? # rubocop:disable Naming/PredicatePrefix @mutex.synchronize { @queue.any? { |n| n[:priority] == :critical } } end |
#pop_all(max_priority: :info) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/legion/chat/notification_queue.rb', line 21 def pop_all(max_priority: :info) @mutex.synchronize do threshold = PRIORITIES[max_priority] || 1 pending = @queue.select { |n| PRIORITIES[n[:priority]] <= threshold } @queue -= pending pending.sort_by { |n| PRIORITIES[n[:priority]] } end end |
#push(message:, priority: :info, source: nil) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/legion/chat/notification_queue.rb', line 14 def push(message:, priority: :info, source: nil) @mutex.synchronize do @queue << { message: , priority: priority, source: source, at: Time.now } @queue.shift if @queue.size > @max_size end end |
#size ⇒ Object
34 35 36 |
# File 'lib/legion/chat/notification_queue.rb', line 34 def size @mutex.synchronize { @queue.size } end |