Module: Y::Sync

Defined in:
lib/y/sync.rb

Constant Summary collapse

MESSAGE_YJS_SYNC_STEP_1 =
0
MESSAGE_YJS_SYNC_STEP_2 =
1
MESSAGE_YJS_UPDATE =
2

Class Method Summary collapse

Class Method Details

.read_sync_message(decoder, encoder, doc, transaction_origin) ⇒ Object

TODO: y-rb sync does not support transaction origins

Parameters:



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/y/sync.rb', line 60

def self.read_sync_message(decoder, encoder, doc, transaction_origin) # rubocop:disable Metrics/MethodLength
  message_type = Y::Lib0::Decoding.read_var_uint(decoder)

  case message_type
  when MESSAGE_YJS_SYNC_STEP_1
    read_sync_step1(decoder, encoder, doc)
  when MESSAGE_YJS_SYNC_STEP_2
    read_sync_step2(decoder, doc, transaction_origin)
  when MESSAGE_YJS_UPDATE
    read_update(decoder, doc, transaction_origin)
  else
    raise "unknown message type"
  end

  message_type
end

.read_sync_step1(decoder, encoder, doc) ⇒ Object

Parameters:



28
29
30
# File 'lib/y/sync.rb', line 28

def self.read_sync_step1(decoder, encoder, doc)
  write_sync_step2(encoder, doc, Y::Lib0::Decoding.read_var_uint8_array(decoder))
end

.read_sync_step2(decoder, doc, _transaction_origin) ⇒ Object

TODO: y-rb sync does not support transaction origins

Parameters:



36
37
38
39
# File 'lib/y/sync.rb', line 36

def self.read_sync_step2(decoder, doc, _transaction_origin)
  update = Y::Lib0::Decoding.read_var_uint8_array(decoder)
  doc.sync(update)
end

.read_update(decoder, doc, transaction_origin) ⇒ Object

Parameters:



51
52
53
# File 'lib/y/sync.rb', line 51

def self.read_update(decoder, doc, transaction_origin)
  read_sync_step2(decoder, doc, transaction_origin)
end

.write_sync_step1(encoder, doc) ⇒ Object

Parameters:



11
12
13
14
15
# File 'lib/y/sync.rb', line 11

def self.write_sync_step1(encoder, doc)
  Y::Lib0::Encoding.write_var_uint(encoder, MESSAGE_YJS_SYNC_STEP_1)
  state_vector = doc.state
  Y::Lib0::Encoding.write_var_uint8_array(encoder, state_vector)
end

.write_sync_step2(encoder, doc, encoded_state_vector) ⇒ Object

Parameters:



20
21
22
23
# File 'lib/y/sync.rb', line 20

def self.write_sync_step2(encoder, doc, encoded_state_vector)
  Y::Lib0::Encoding.write_var_uint(encoder, MESSAGE_YJS_SYNC_STEP_2)
  Y::Lib0::Encoding.write_var_uint8_array(encoder, doc.diff(encoded_state_vector))
end

.write_update(encoder, update) ⇒ Object

Parameters:



43
44
45
46
# File 'lib/y/sync.rb', line 43

def self.write_update(encoder, update)
  Y::Lib0::Encoding.write_var_uint(encoder, MESSAGE_YJS_UPDATE)
  Y::Lib0::Encoding.write_var_uint8_array(encoder, update)
end