Electric.Shapes.EventRouter (electric v1.4.13)

View Source

Routes replication events to shapes, returning per-shape transaction fragments.

The EventRouter wraps a Filter and adds transaction-aware routing:

  • For Relation events, returns the relation for all affected shapes
  • For TransactionFragment events, returns per-shape TransactionFragments with only the changes that affect each shape.

Transaction state is tracked to ensure:

  • Each shape receives Begin only once per transaction (on first relevant operation)
  • Each shape receives Commit only if it received operations in the transaction
  • Shapes added mid-transaction are skipped for that transaction
  • Shapes removed mid-transaction stop receiving events immediately

Summary

Types

shape_id()

@type shape_id() :: any()

t()

@type t() :: %Electric.Shapes.EventRouter{
  current_xid: term(),
  filter: term(),
  in_txn: term(),
  shapes_added_mid_txn: term(),
  shapes_in_txn: term(),
  shapes_seen_begin: term()
}

Functions

active_shapes(router)

@spec active_shapes(t()) :: MapSet.t(shape_id())

add_shape(router, shape_id, shape)

@spec add_shape(t(), shape_id(), Electric.Shapes.Shape.t()) :: t()

event_by_shape_handle(router, relation)

has_shape?(router, shape_id)

@spec has_shape?(t(), shape_id()) :: boolean()

new(opts \\ [])

@spec new(keyword()) :: t()

remove_shape(router, shape_id)

@spec remove_shape(t(), shape_id()) :: t()