Module: Y::ActionCable::Sync::ClassMethods

Defined in:
lib/y/action_cable/sync.rb

Instance Method Summary collapse

Instance Method Details

#max_frame_bytes(bytes = :__unset__) ⇒ Object

Maximum size, in decoded bytes, of an incoming document/awareness frame. Oversized frames are dropped before base64 decode and before native parsing, so a client can't force huge allocations/CPU (a DoS vector). Defaults to DEFAULT_MAX_FRAME_BYTES; set to nil to disable the cap.



88
89
90
91
92
93
94
# File 'lib/y/action_cable/sync.rb', line 88

def max_frame_bytes(bytes = :__unset__)
  # Combined reader/writer; the sentinel keeps nil a real value (disables the cap).
  @max_frame_bytes = bytes unless bytes == :__unset__
  return @max_frame_bytes if defined?(@max_frame_bytes)

  superclass.respond_to?(:max_frame_bytes) ? superclass.max_frame_bytes : DEFAULT_MAX_FRAME_BYTES
end

#on_change(&block) ⇒ Object

Record every document change durably before it is applied or distributed. Called synchronously with (key, update), where update is the exact CRDT delta. If the block raises, the change is rejected: neither acknowledged nor broadcast to other subscribers.

Runs in the channel instance's context (instance_exec). Fires from within sync_receive.



77
78
79
80
81
82
# File 'lib/y/action_cable/sync.rb', line 77

def on_change(&block)
  @on_change = block if block
  return @on_change if defined?(@on_change) && @on_change

  superclass.respond_to?(:on_change) ? superclass.on_change : nil
end

#on_gap(&block) ⇒ Object

Optional observability hook, fired at join/serve time whenever the loaded document still holds a causal gap (a pending struct). Called with (key) in the channel instance's context (instance_exec). Use it to emit a metric (pending-document count, gap age) so an unhealed gap is visible. Errors in the hook are swallowed so observability can never break frame handling.



102
103
104
105
106
107
# File 'lib/y/action_cable/sync.rb', line 102

def on_gap(&block)
  @on_gap = block if block
  return @on_gap if defined?(@on_gap) && @on_gap

  superclass.respond_to?(:on_gap) ? superclass.on_gap : nil
end

#on_load(&block) ⇒ Object

Load persisted document state. Called once per key with (key); return a binary Y.js update (or nil for a fresh document). Runs in the channel instance's context (instance_exec).



63
64
65
66
67
68
# File 'lib/y/action_cable/sync.rb', line 63

def on_load(&block)
  @on_load = block if block
  return @on_load if defined?(@on_load) && @on_load

  superclass.respond_to?(:on_load) ? superclass.on_load : nil
end