Class: Turbo::Debouncer

Inherits:
Object
  • Object
show all
Defined in:
app/models/turbo/debouncer.rb

Constant Summary collapse

DEFAULT_DELAY =
0.5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(delay: DEFAULT_DELAY) ⇒ Debouncer

Returns a new instance of Debouncer.



6
7
8
9
# File 'app/models/turbo/debouncer.rb', line 6

def initialize(delay: DEFAULT_DELAY)
  @delay = delay
  @scheduled_task = nil
end

Instance Attribute Details

#delayObject (readonly)

Returns the value of attribute delay.



2
3
4
# File 'app/models/turbo/debouncer.rb', line 2

def delay
  @delay
end

#scheduled_taskObject (readonly)

Returns the value of attribute scheduled_task.



2
3
4
# File 'app/models/turbo/debouncer.rb', line 2

def scheduled_task
  @scheduled_task
end

Instance Method Details

#debounce(&block) ⇒ Object



11
12
13
14
# File 'app/models/turbo/debouncer.rb', line 11

def debounce(&block)
  scheduled_task&.cancel unless scheduled_task&.complete?
  @scheduled_task = Concurrent::ScheduledTask.execute(delay, &block)
end

#waitObject



16
17
18
# File 'app/models/turbo/debouncer.rb', line 16

def wait
  scheduled_task&.wait(wait_timeout)
end