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

Class Method Details

.coalesce(wait:, on_error: nil) {|Array| ... } ⇒ Coalescer

Create a new coalescer that batches arguments into a single invocation.

Parameters:

  • wait (Numeric)

    delay in seconds before flushing the batch

Yields:

  • (Array)

    receives an array of argument arrays from all queued calls

Returns:



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.

Parameters:

  • wait (Float)

    delay in seconds

  • leading (Boolean) (defaults to: false)

    fire on the leading edge (default: false)

  • trailing (Boolean) (defaults to: true)

    fire on the trailing edge (default: true)

  • max_wait (Float, nil) (defaults to: nil)

    maximum time to wait before forcing execution

  • on_execute (Proc, nil) (defaults to: nil)

    callback after block executes, receives return value

  • on_cancel (Proc, nil) (defaults to: nil)

    callback when cancel is invoked

  • on_flush (Proc, nil) (defaults to: nil)

    callback when flush is invoked

Yields:

  • (*args)

    the block to execute after the debounce period

Returns:



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.

Parameters:

  • wait (Float)

    delay in seconds

  • leading (Boolean) (defaults to: false)

    fire on the leading edge (default: false)

  • trailing (Boolean) (defaults to: true)

    fire on the trailing edge (default: true)

  • max_wait (Float, nil) (defaults to: nil)

    maximum time to wait before forcing execution

  • on_execute (Proc, nil) (defaults to: nil)

    callback after block executes, receives return value

  • on_cancel (Proc, nil) (defaults to: nil)

    callback when cancel is invoked

  • on_flush (Proc, nil) (defaults to: nil)

    callback when flush is invoked

Yields:

  • (*args)

    the block to execute after the debounce period

Returns:



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.

Parameters:

  • limit (Integer)

    maximum number of requests per window

  • window (Numeric)

    window size in seconds

Returns:



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.

Parameters:

  • interval (Float)

    minimum time between executions in seconds

  • leading (Boolean) (defaults to: true)

    fire on the leading edge (default: true)

  • trailing (Boolean) (defaults to: false)

    fire on the trailing edge (default: false)

  • on_execute (Proc, nil) (defaults to: nil)

    callback after block executes, receives return value

  • on_cancel (Proc, nil) (defaults to: nil)

    callback when cancel is invoked

  • on_flush (Proc, nil) (defaults to: nil)

    callback when flush is invoked

Yields:

  • (*args)

    the block to execute

Returns:



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