Class: Parse::Agent::MCPSubscriptions::Debouncer Private

Inherits:
Object
  • Object
show all
Defined in:
lib/parse/agent/mcp_subscriptions.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Trailing-debounce coalescer for one (session, uri) subscription.

The first event in a quiet period arms a one-shot timer; events that arrive before it fires are coalesced (dropped) so the timer emits a single update. After the emit the coalescer rearms on the next event. This bounds emission to at most one notification per window regardless of event rate.

The timer mechanism is injected (timer:) so tests can drive emission deterministically instead of sleeping. The default spawns a short-lived thread per burst (not per event); at most one timer thread is live per coalescer at a time.

Instance Method Summary collapse

Constructor Details

#initialize(interval:, timer: nil) { ... } ⇒ Debouncer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Debouncer.

Parameters:

  • interval (Numeric)

    debounce window in seconds. <= 0 emits synchronously on every trigger (no coalescing) — used by tests and callers that want immediate delivery.

  • timer (#call) (defaults to: nil)

    timer.call(interval) { emit } schedules a one-shot emit. Default spawns a thread.

Yields:

  • the emit action invoked once per coalesced burst.



236
237
238
239
240
241
242
# File 'lib/parse/agent/mcp_subscriptions.rb', line 236

def initialize(interval:, timer: nil, &emit)
  @interval  = interval
  @emit      = emit
  @timer     = timer || method(:default_timer)
  @armed     = false
  @mutex     = Mutex.new
end

Instance Method Details

#triggerObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Record an event; arm the timer if not already armed.



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/parse/agent/mcp_subscriptions.rb', line 245

def trigger
  if @interval <= 0
    @emit.call
    return
  end
  should_arm = @mutex.synchronize do
    next false if @armed
    @armed = true
  end
  return unless should_arm
  @timer.call(@interval) do
    @mutex.synchronize { @armed = false }
    @emit.call
  end
end