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, max_keys: 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, max_keys: nil, on_execute: nil, on_cancel: nil, on_flush: nil, on_error: nil, &block) ⇒ KeyedDebouncer
Returns a new instance of KeyedDebouncer.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/philiprehberger/debounce/keyed_debouncer.rb', line 22 def initialize(wait:, leading: false, trailing: true, max_wait: nil, # rubocop:disable Metrics/ParameterLists max_keys: nil, on_execute: nil, on_cancel: nil, on_flush: nil, on_error: nil, &block) raise ArgumentError, 'block is required' unless block raise ArgumentError, 'max_keys must be a positive integer' if max_keys && (!max_keys.is_a?(Integer) || max_keys < 1) @wait = wait @leading = leading @trailing = trailing @max_wait = max_wait @max_keys = max_keys @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.
47 48 49 |
# File 'lib/philiprehberger/debounce/keyed_debouncer.rb', line 47 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.
55 56 57 58 |
# File 'lib/philiprehberger/debounce/keyed_debouncer.rb', line 55 def cancel(key) debouncer = @mutex.synchronize { @debouncers.delete(key) } debouncer&.cancel end |
#cancel_all ⇒ void
This method returns an undefined value.
Cancel all pending executions.
84 85 86 87 88 89 90 91 |
# File 'lib/philiprehberger/debounce/keyed_debouncer.rb', line 84 def cancel_all debouncers = @mutex.synchronize do current = @debouncers.values @debouncers.clear current end debouncers.each(&:cancel) end |
#flush(key) ⇒ void
This method returns an undefined value.
Flush the pending execution for a specific key immediately.
64 65 66 67 |
# File 'lib/philiprehberger/debounce/keyed_debouncer.rb', line 64 def flush(key) debouncer = @mutex.synchronize { @debouncers.delete(key) } debouncer&.flush end |
#flush_all ⇒ void
This method returns an undefined value.
Flush all pending keyed debouncers immediately.
72 73 74 75 76 77 78 79 |
# File 'lib/philiprehberger/debounce/keyed_debouncer.rb', line 72 def flush_all debouncers = @mutex.synchronize do current = @debouncers.values @debouncers.clear current end debouncers.each(&:flush) end |
#pending_keys ⇒ Array
List keys that have pending executions.
96 97 98 99 100 |
# File 'lib/philiprehberger/debounce/keyed_debouncer.rb', line 96 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.
105 106 107 108 109 |
# File 'lib/philiprehberger/debounce/keyed_debouncer.rb', line 105 def size @mutex.synchronize do @debouncers.size end end |