Class: Turbo::Debouncer
- Inherits:
-
Object
- Object
- Turbo::Debouncer
- Defined in:
- app/models/turbo/debouncer.rb
Constant Summary collapse
- DEFAULT_DELAY =
0.5
Instance Attribute Summary collapse
-
#delay ⇒ Object
readonly
Returns the value of attribute delay.
-
#scheduled_task ⇒ Object
readonly
Returns the value of attribute scheduled_task.
Instance Method Summary collapse
- #debounce(&block) ⇒ Object
-
#initialize(delay: DEFAULT_DELAY) ⇒ Debouncer
constructor
A new instance of Debouncer.
- #wait ⇒ Object
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
#delay ⇒ Object (readonly)
Returns the value of attribute delay.
2 3 4 |
# File 'app/models/turbo/debouncer.rb', line 2 def delay @delay end |
#scheduled_task ⇒ Object (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 |
#wait ⇒ Object
16 17 18 |
# File 'app/models/turbo/debouncer.rb', line 16 def wait scheduled_task&.wait(wait_timeout) end |