Sha256: d719f44c36c69ff1b6c3758ef701fec67db593b239d8fcbf7c1617cf5727d584

Contents?: true

Size: 975 Bytes

Versions: 6

Compression:

Stored size: 975 Bytes

Contents

# frozen_string_literal: true

require_relative "./buffer"

module Fusuma
  module Plugin
    module Buffers
      # manage events and generate command
      class TimerBuffer < Buffer
        DEFAULT_SOURCE = "timer_input"
        DEFAULT_SECONDS_TO_KEEP = 3

        def config_param_types
          {
            source: [String],
            seconds_to_keep: [Float, Integer]
          }
        end

        # @param event [Event]
        # @return [Buffer, NilClass]
        def buffer(event)
          return if event&.tag != source

          @events.push(event)
          self
        end

        def clear_expired(current_time: Time.now)
          @seconds_to_keep ||= (config_params(:seconds_to_keep) || DEFAULT_SECONDS_TO_KEEP)
          @events.each do |e|
            break if current_time - e.time < @seconds_to_keep

            @events.delete(e)
          end
        end

        def empty?
          @events.empty?
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
fusuma-3.3.0 lib/fusuma/plugin/buffers/timer_buffer.rb
fusuma-3.2.0 lib/fusuma/plugin/buffers/timer_buffer.rb
fusuma-3.1.0 lib/fusuma/plugin/buffers/timer_buffer.rb
fusuma-3.0.0 lib/fusuma/plugin/buffers/timer_buffer.rb
fusuma-2.5.1 lib/fusuma/plugin/buffers/timer_buffer.rb
fusuma-2.5.0 lib/fusuma/plugin/buffers/timer_buffer.rb