Module: Philiprehberger::Debounce
- Defined in:
- lib/philiprehberger/debounce.rb,
lib/philiprehberger/debounce/mixin.rb,
lib/philiprehberger/debounce/version.rb,
lib/philiprehberger/debounce/coalescer.rb,
lib/philiprehberger/debounce/debouncer.rb,
lib/philiprehberger/debounce/throttler.rb,
lib/philiprehberger/debounce/rate_limiter.rb,
lib/philiprehberger/debounce/keyed_debouncer.rb
Overview
Debounce and throttle decorators for Ruby method calls
Defined Under Namespace
Modules: Mixin Classes: Coalescer, Debouncer, Error, KeyedDebouncer, RateLimiter, Throttler
Constant Summary collapse
- VERSION =
'0.6.0'
Class Method Summary collapse
-
.coalesce(wait:, on_error: nil) {|Array| ... } ⇒ Coalescer
Create a new coalescer that batches arguments into a single invocation.
-
.debounce(wait:, leading: false, trailing: true, max_wait: nil, on_execute: nil, on_cancel: nil, on_flush: nil, on_error: nil) {|*args| ... } ⇒ Debouncer
Create a new debouncer that delays execution until the wait period elapses without new calls.
-
.keyed(wait:, leading: false, trailing: true, max_wait: nil, on_execute: nil, on_cancel: nil, on_flush: nil, on_error: nil) {|*args| ... } ⇒ KeyedDebouncer
Create a new keyed debouncer that manages per-key debouncers.
-
.rate_limiter(limit:, window:) ⇒ RateLimiter
Create a new sliding window rate limiter.
-
.throttle(interval:, leading: true, trailing: false, on_execute: nil, on_cancel: nil, on_flush: nil, on_error: nil) {|*args| ... } ⇒ Throttler
Create a new throttler that limits execution to at most once per interval.
Class Method Details
.coalesce(wait:, on_error: nil) {|Array| ... } ⇒ Coalescer
Create a new coalescer that batches arguments into a single invocation.
84 85 86 |
# File 'lib/philiprehberger/debounce.rb', line 84 def self.coalesce(wait:, on_error: nil, &block) Coalescer.new(wait: wait, on_error: on_error, &block) end |
.debounce(wait:, leading: false, trailing: true, max_wait: nil, on_execute: nil, on_cancel: nil, on_flush: nil, on_error: nil) {|*args| ... } ⇒ Debouncer
Create a new debouncer that delays execution until the wait period elapses without new calls.
28 29 30 31 32 33 |
# File 'lib/philiprehberger/debounce.rb', line 28 def self.debounce(wait:, leading: false, trailing: true, max_wait: nil, on_execute: nil, on_cancel: nil, on_flush: nil, on_error: nil, &block) Debouncer.new( 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 ) end |
.keyed(wait:, leading: false, trailing: true, max_wait: nil, on_execute: nil, on_cancel: nil, on_flush: nil, on_error: nil) {|*args| ... } ⇒ KeyedDebouncer
Create a new keyed debouncer that manages per-key debouncers.
63 64 65 66 67 68 |
# File 'lib/philiprehberger/debounce.rb', line 63 def self.keyed(wait:, leading: false, trailing: true, max_wait: nil, on_execute: nil, on_cancel: nil, on_flush: nil, on_error: nil, &block) KeyedDebouncer.new( 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 ) end |
.rate_limiter(limit:, window:) ⇒ RateLimiter
Create a new sliding window rate limiter.
75 76 77 |
# File 'lib/philiprehberger/debounce.rb', line 75 def self.rate_limiter(limit:, window:) RateLimiter.new(limit: limit, window: window) end |
.throttle(interval:, leading: true, trailing: false, on_execute: nil, on_cancel: nil, on_flush: nil, on_error: nil) {|*args| ... } ⇒ Throttler
Create a new throttler that limits execution to at most once per interval.
45 46 47 48 49 50 |
# File 'lib/philiprehberger/debounce.rb', line 45 def self.throttle(interval:, leading: true, trailing: false, on_execute: nil, on_cancel: nil, on_flush: nil, on_error: nil, &block) Throttler.new( interval: interval, leading: leading, trailing: trailing, on_execute: on_execute, on_cancel: on_cancel, on_flush: on_flush, on_error: on_error, &block ) end |