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.

A block recorder runs in the *channel instance’s* context, so it can call the channel’s own methods (current_user, params, a per-connection Current.* accessor) directly, with no thread-local plumbing. (A non-Proc callable is invoked with #call instead, since it carries its own context.) on_change always fires from within sync_receive, unlike on_load/on_save, which can run context-free in the shared registry.

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.



90
91
92
93
# File 'lib/yrb_lite/sync.rb', line 90

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).



61
62
63
64
# File 'lib/yrb_lite/sync.rb', line 61

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.



68
69
70
71
# File 'lib/yrb_lite/sync.rb', line 68

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`.


105
106
107
108
# File 'lib/yrb_lite/sync.rb', line 105

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