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.
14 15 16 17 18 |
# File 'app/models/turbo/thread_debouncer.rb', line 14 def initialize(key, thread, delay: ) @key = key @debouncer = debouncer_class.new(delay: delay) @thread = thread end |
Class Method Details
.for(key, delay: Turbo::Debouncer::DEFAULT_DELAY) ⇒ Object
8 9 10 |
# File 'app/models/turbo/thread_debouncer.rb', line 8 def self.for(key, delay: Turbo::Debouncer::DEFAULT_DELAY) Thread.current[key] ||= new(key, Thread.current, delay: delay) end |
Instance Method Details
#debounce ⇒ Object
20 21 22 23 24 25 26 |
# File 'app/models/turbo/thread_debouncer.rb', line 20 def debounce debouncer.debounce do yield.tap do thread[key] = nil end end end |