Module: Philiprehberger::Debounce

Defined in:
lib/philiprehberger/debounce.rb,
lib/philiprehberger/debounce/mixin.rb,
lib/philiprehberger/debounce/version.rb,
lib/philiprehberger/debounce/debouncer.rb,
lib/philiprehberger/debounce/throttler.rb

Overview

Debounce and throttle decorators for Ruby method calls

Defined Under Namespace

Modules: Mixin Classes: Debouncer, Error, Throttler

Constant Summary collapse

VERSION =
'0.1.0'

Class Method Summary collapse

Class Method Details

.debounce(wait:, leading: false, trailing: true) {|*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)

Yields:

  • (*args)

    the block to execute after the debounce period

Returns:



21
22
23
# File 'lib/philiprehberger/debounce.rb', line 21

def self.debounce(wait:, leading: false, trailing: true, &block)
  Debouncer.new(wait: wait, leading: leading, trailing: trailing, &block)
end

.throttle(interval:, leading: true, trailing: false) {|*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)

Yields:

  • (*args)

    the block to execute

Returns:



32
33
34
# File 'lib/philiprehberger/debounce.rb', line 32

def self.throttle(interval:, leading: true, trailing: false, &block)
  Throttler.new(interval: interval, leading: leading, trailing: trailing, &block)
end