Class: Debouncer

Inherits:
Object
  • Object
show all
Defined in:
lib/rox/core/utils/debouncer.rb

Instance Method Summary collapse

Constructor Details

#initialize(delay_in_seconds, block) ⇒ Debouncer

Returns a new instance of Debouncer.



2
3
4
5
6
# File 'lib/rox/core/utils/debouncer.rb', line 2

def initialize(delay_in_seconds, block)
  @delay_in_seconds = delay_in_seconds
  @block = block
  @delay_until = Time.now
end

Instance Method Details

#callObject



8
9
10
11
12
13
14
15
16
17
# File 'lib/rox/core/utils/debouncer.rb', line 8

def call
  now = Time.now
  if now > @delay_until
    @delay_until = now + @delay_in_seconds
    Thread.new do
      sleep(@delay_in_seconds)
      @block.call
    end
  end
end