Module: YrbLite::Sync::ClassMethods

Defined in:
lib/yrb_lite/sync.rb

Instance Method Summary collapse

Instance Method Details

#on_change(callable = nil, &block) ⇒ Object

Record every document change durably before it is applied or distributed (authoritative audit mode). Called synchronously with (key, update), where update is the exact CRDT delta, serialized per document so the recorded order is the apply order. If the block raises, the change is rejected: neither applied to the shared document nor broadcast to other subscribers.

Registering an on_change switches that channel onto the strict path (record, apply, broadcast). Without it, the default fast path applies and broadcasts, with an optional on_save snapshot.



79
80
81
82
# File 'lib/yrb_lite/sync.rb', line 79

def on_change(callable = nil, &block)
  @on_change = callable || block if callable || block
  @on_change
end

#on_load(callable = nil, &block) ⇒ Object

Load persisted document state. Called once per key with (key); return a binary Y.js update (or nil for a fresh document).



57
58
59
60
# File 'lib/yrb_lite/sync.rb', line 57

def on_load(callable = nil, &block)
  @on_load = callable || block if callable || block
  @on_load
end

#on_save(callable = nil, &block) ⇒ Object

Persist document state. Called with (key, update) after every message that modified the document.



64
65
66
67
# File 'lib/yrb_lite/sync.rb', line 64

def on_save(callable = nil, &block)
  @on_save = callable || block if callable || block
  @on_save
end

#sync_backend(mode = nil) ⇒ Object

Select the document backend:

:memory (default): keep a warm in-memory replica per process and keep
  it current via a custom stream_from callback. Fast, but it assumes
  classic ActionCable (the callback runs in Ruby) and
  process<->document affinity.
:store: stateless per message, with no warm replica and no custom
  stream callback. Handshakes and reads are served from the durable
  store (`on_load`); changes are recorded (`on_change`) and relayed.
  Works under AnyCable (broadcasts handled outside Ruby, no worker
  affinity) and across processes. Requires `on_load` and `on_change`.


94
95
96
97
# File 'lib/yrb_lite/sync.rb', line 94

def sync_backend(mode = nil)
  @sync_backend = mode if mode
  @sync_backend || :memory
end