lib/lounger.rb in lounger-0.3.1 vs lib/lounger.rb in lounger-0.3.4

- old
+ new

@@ -1,47 +1,48 @@ -require "lounger/version" +# frozen_string_literal: true +require 'lounger/version' + +# The Lounger class is the main class which will allow to handle traps +# in simple way. class Lounger - SIGNALS = ["INT", "TERM", "EXIT", "USR1", "QUIT"] + SIGNALS = %w[INT TERM EXIT USR1 QUIT].freeze def initialize(include_signals: [], exclude_signals: []) @lock = Mutex.new @condition = ConditionVariable.new @pending_signals = 0 @buffer = [] @idle = false (SIGNALS + include_signals - exclude_signals).each do |s| - prev = Signal.trap(s) do - @condition.signal - prev.call unless prev.is_a?(String) - end + Signal.trap(s) { @condition.signal } end end def idle(ignore_pending: false) result = nil @lock.synchronize do @pending_signals = 0 if ignore_pending @idle = true - if @pending_signals > 0 + if @pending_signals.positive? @pending_signals -= 1 else @condition.wait(@lock) @pending_signals -= 1 end @idle = false result = @buffer.shift end - return result + result end def idle? - return @idle + @idle end def wakeup!(value = nil) @lock.synchronize do @pending_signals += 1 @@ -55,6 +56,6 @@ end alias_method :waiting?, :idle? alias_method :wait, :idle alias_method :signal, :wakeup! -end \ No newline at end of file +end