Class: Turbo::ThreadDebouncer

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

Overview

A decorated debouncer that will store instances in the current thread clearing them after the debounced logic triggers.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, thread, delay:) ⇒ ThreadDebouncer

Returns a new instance of ThreadDebouncer.



12
13
14
15
16
# File 'app/models/turbo/thread_debouncer.rb', line 12

def initialize(key, thread, delay: )
  @key = key
  @debouncer = Turbo::Debouncer.new(delay: delay)
  @thread = thread
end

Class Method Details

.for(key, delay: Turbo::Debouncer::DEFAULT_DELAY) ⇒ Object



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

def self.for(key, delay: Turbo::Debouncer::DEFAULT_DELAY)
  Thread.current[key] ||= new(key, Thread.current, delay: delay)
end

Instance Method Details

#debounceObject



18
19
20
21
22
23
24
# File 'app/models/turbo/thread_debouncer.rb', line 18

def debounce
  debouncer.debounce do
    yield.tap do
      thread[key] = nil
    end
  end
end