LemonChannels.Adapters.Telegram.Transport.MessageBuffer (lemon_channels v0.1.0)

View Source

Message buffering and debounce logic for the Telegram transport.

Handles the accumulation of non-command messages within a configurable debounce window so that rapid-fire user messages are coalesced into a single inbound submission.

Functions here operate on the buffers map from GenServer state but do not mutate state directly -- they return the updated buffers (or a full state map update) so the caller can apply it.

Summary

Functions

Build the coalesced inbound payload for a buffered scope without performing the submission side effect.

Drop any pending buffer for the given inbound's scope key (e.g. when a command like /new or /resume should flush pending text).

Add an inbound message to the debounce buffer for its scope key.

Collapse the buffered messages into a single inbound and submit it.

Types

buffer()

@type buffer() :: %{
  inbound: map(),
  messages: [map()],
  timer_ref: reference() | nil,
  debounce_ref: reference()
}

Functions

build_inbound(map)

@spec build_inbound(buffer()) :: map()

Build the coalesced inbound payload for a buffered scope without performing the submission side effect.

drop_buffer_for(state, inbound)

Drop any pending buffer for the given inbound's scope key (e.g. when a command like /new or /resume should flush pending text).

Returns the updated state.

enqueue_buffer(state, inbound)

Add an inbound message to the debounce buffer for its scope key.

Returns the updated state map with buffers modified.

submit_buffer(map, submit_fn)

Collapse the buffered messages into a single inbound and submit it.

This merges multiple message entries into one text block, then delegates to the submit_fn callback (which should be &submit_inbound_now/2).

Returns :ok (the submission side-effect is performed via the callback).