constellation/domains/dispatcher

Pure, demand-aware event dispatch strategies.

Types

Events assigned to one subscriber by a dispatch operation.

pub type Delivery(event) {
  Delivery(
    subscription_id: subscription_id.SubscriptionId,
    events: List(event),
  )
}

Constructors

The result of dispatching, including updated demand and undelivered events.

pub type DispatchResult(event) {
  DispatchResult(
    targets: List(Target),
    deliveries: List(Delivery(event)),
    remaining: List(event),
  )
}

Constructors

  • DispatchResult(
      targets: List(Target),
      deliveries: List(Delivery(event)),
      remaining: List(event),
    )

A pure policy for assigning events to subscribers.

pub opaque type Strategy(event)

A subscriber and the capacity currently available to it.

pub opaque type Target

Errors returned while constructing dispatch targets.

pub type TargetError {
  InvalidDemand(Int)
}

Constructors

  • InvalidDemand(Int)

Values

pub fn broadcast_strategy() -> Strategy(event)

Creates a strict broadcast strategy.

pub fn custom_strategy(
  select: fn(List(Target), event) -> option.Option(
    subscription_id.SubscriptionId,
  ),
) -> Strategy(event)

Creates a single-target strategy from a user-defined pure selector.

Returning an unknown target, a target without demand, or None keeps the event buffered. The library retains ownership of demand and delivery state.

pub fn demand(target: Target) -> Int

Returns the remaining capacity represented by a target.

pub fn demand_strategy() -> Strategy(event)

Creates a demand-based round-robin strategy.

pub fn dispatch(
  strategy: Strategy(event),
  targets: List(Target),
  events: List(event),
) -> DispatchResult(event)

Dispatches events with the selected strategy without performing side effects.

Demand is consumed in the returned targets. Events in remaining were not delivered and must be retained by the core buffer. Demand and partition strategies preserve FIFO per subscription. They do not promise a global processing order across different subscribers.

pub fn has_capacity(
  strategy: Strategy(event),
  targets: List(Target),
) -> Bool

Reports whether the strategy can currently deliver at least one event.

pub fn partition(target: Target) -> Int

Returns the partition assigned to a target.

pub fn partition_strategy(
  key: fn(event) -> Int,
) -> Strategy(event)

Creates a partition strategy using an event key function.

pub fn subscription_id(
  target: Target,
) -> subscription_id.SubscriptionId

Returns the subscription represented by a target.

pub fn target(
  subscription_id: subscription_id.SubscriptionId,
  partition: Int,
) -> Target

Creates a dispatch target with no available demand.

pub fn with_demand(
  value: Target,
  demand: Int,
) -> Result(Target, TargetError)

Updates a target’s demand, rejecting negative capacity.

Search Document