brazier/presence/sync

Types

What changed on a topic. An update is a leave of the old meta and a join of the new one.

pub type Diff(meta) {
  Diff(
    topic: String,
    joins: List(Entry(meta)),
    leaves: List(Entry(meta)),
  )
}

Constructors

  • Diff(
      topic: String,
      joins: List(Entry(meta)),
      leaves: List(Entry(meta)),
    )

Everything tracked under one key on a topic: one meta per tracking process (several when the same key is tracked from several places).

pub type Entry(meta) {
  Entry(key: String, metas: List(Meta(meta)))
}

Constructors

  • Entry(key: String, metas: List(Meta(meta)))

One tracked entry’s meta, from any node.

pub type Meta(meta) {
  Meta(
    value: meta,
    ref: String,
    ref_prev: option.Option(String),
    node: String,
  )
}

Constructors

  • Meta(
      value: meta,
      ref: String,
      ref_prev: option.Option(String),
      node: String,
    )

Values

pub fn merge(
  entries: List(Entry(meta)),
  diff: Diff(meta),
) -> List(Entry(meta))

Apply diff to entries (Phoenix.Presence’s syncDiff): joins add the metas whose refs are not there yet, then leaves remove the metas with their refs, and keys left without metas are dropped. Applying a diff twice changes nothing more. The result is ordered by key, like presence.list.

OnlineChanged(diff) ->
  Model(..model, online: sync.merge(model.online, diff))
✨ Search Document