Class: Turbo::ThreadDebouncer
- Inherits:
-
Object
- Object
- Turbo::ThreadDebouncer
- 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
- #debounce ⇒ Object
-
#initialize(key, thread, delay:) ⇒ ThreadDebouncer
constructor
A new instance of ThreadDebouncer.
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
#debounce ⇒ Object
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 |