Class: Philiprehberger::Debounce::KeyedDebouncer
- Inherits:
-
Object
- Object
- Philiprehberger::Debounce::KeyedDebouncer
- Defined in:
- lib/philiprehberger/debounce/keyed_debouncer.rb
Overview
Manages per-key debouncer instances, allowing independent debouncing for different keys using the same configuration.
Instance Method Summary collapse
-
#call(key, *args) ⇒ void
Invoke the debouncer for the given key.
-
#cancel(key) ⇒ void
Cancel the pending execution for a specific key.
-
#cancel_all ⇒ void
Cancel all pending executions.
-
#flush(key) ⇒ void
Flush the pending execution for a specific key immediately.
-
#flush_all ⇒ void
Flush all pending keyed debouncers immediately.
-
#initialize(wait:, leading: false, trailing: true, max_wait: nil, on_execute: nil, on_cancel: nil, on_flush: nil, on_error: nil, &block) ⇒ KeyedDebouncer
constructor
A new instance of KeyedDebouncer.
-
#pending_keys ⇒ Array
List keys that have pending executions.
-
#size ⇒ Integer
Number of active keyed debouncers currently held internally.
Constructor Details
#initialize(wait:, leading: false, trailing: true, max_wait: nil, on_execute: nil, on_cancel: nil, on_flush: nil, on_error: nil, &block) ⇒ KeyedDebouncer
Returns a new instance of KeyedDebouncer.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/philiprehberger/debounce/keyed_debouncer.rb', line 21 def initialize(wait:, leading: false, trailing: true, max_wait: nil, on_execute: nil, on_cancel: nil, on_flush: nil, on_error: nil, &block) raise ArgumentError, 'block is required' unless block @wait = wait @leading = leading @trailing = trailing @max_wait = max_wait @on_execute = on_execute @on_cancel = on_cancel @on_flush = on_flush @on_error = on_error @block = block @debouncers = {} @mutex = Mutex.new end |
Instance Method Details
#call(key, *args) ⇒ void
This method returns an undefined value.
Invoke the debouncer for the given key.
42 43 44 |
# File 'lib/philiprehberger/debounce/keyed_debouncer.rb', line 42 def call(key, *args) debouncer_for(key).call(*args) end |
#cancel(key) ⇒ void
This method returns an undefined value.
Cancel the pending execution for a specific key.
50 51 52 53 54 55 |
# File 'lib/philiprehberger/debounce/keyed_debouncer.rb', line 50 def cancel(key) @mutex.synchronize do debouncer = @debouncers.delete(key) debouncer&.cancel end end |
#cancel_all ⇒ void
This method returns an undefined value.
Cancel all pending executions.
81 82 83 84 85 86 |
# File 'lib/philiprehberger/debounce/keyed_debouncer.rb', line 81 def cancel_all @mutex.synchronize do @debouncers.each_value(&:cancel) @debouncers.clear end end |
#flush(key) ⇒ void
This method returns an undefined value.
Flush the pending execution for a specific key immediately.
61 62 63 64 65 66 |
# File 'lib/philiprehberger/debounce/keyed_debouncer.rb', line 61 def flush(key) @mutex.synchronize do debouncer = @debouncers.delete(key) debouncer&.flush end end |
#flush_all ⇒ void
This method returns an undefined value.
Flush all pending keyed debouncers immediately.
71 72 73 74 75 76 |
# File 'lib/philiprehberger/debounce/keyed_debouncer.rb', line 71 def flush_all @mutex.synchronize do @debouncers.each_value(&:flush) @debouncers.clear end end |
#pending_keys ⇒ Array
List keys that have pending executions.
91 92 93 94 95 |
# File 'lib/philiprehberger/debounce/keyed_debouncer.rb', line 91 def pending_keys @mutex.synchronize do @debouncers.select { |_key, debouncer| debouncer.pending? }.keys end end |
#size ⇒ Integer
Number of active keyed debouncers currently held internally.
100 101 102 103 104 |
# File 'lib/philiprehberger/debounce/keyed_debouncer.rb', line 100 def size @mutex.synchronize do @debouncers.size end end |