merge pending structs in v1 format
This commit is contained in:
		
							parent
							
								
									097b9e8208
								
							
						
					
					
						commit
						0c34216ed0
					
				@ -32,9 +32,11 @@ import {
 | 
				
			|||||||
  DSEncoderV2,
 | 
					  DSEncoderV2,
 | 
				
			||||||
  DSDecoderV1,
 | 
					  DSDecoderV1,
 | 
				
			||||||
  DSEncoderV1,
 | 
					  DSEncoderV1,
 | 
				
			||||||
 | 
					  mergeUpdates,
 | 
				
			||||||
  mergeUpdatesV2,
 | 
					  mergeUpdatesV2,
 | 
				
			||||||
  Skip,
 | 
					  Skip,
 | 
				
			||||||
  diffUpdateV2,
 | 
					  diffUpdateV2,
 | 
				
			||||||
 | 
					  convertUpdateFormatV2ToV1,
 | 
				
			||||||
  DSDecoderV2, Doc, Transaction, GC, Item, StructStore // eslint-disable-line
 | 
					  DSDecoderV2, Doc, Transaction, GC, Item, StructStore // eslint-disable-line
 | 
				
			||||||
} from '../internals.js'
 | 
					} from '../internals.js'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -523,8 +525,6 @@ export const encodeStateAsUpdateV2 = (doc, encodedTargetStateVector = new Uint8A
 | 
				
			|||||||
  writeStateAsUpdate(encoder, doc, targetStateVector)
 | 
					  writeStateAsUpdate(encoder, doc, targetStateVector)
 | 
				
			||||||
  const updates = [encoder.toUint8Array()]
 | 
					  const updates = [encoder.toUint8Array()]
 | 
				
			||||||
  // also add the pending updates (if there are any)
 | 
					  // also add the pending updates (if there are any)
 | 
				
			||||||
  // @todo support diffirent encoders
 | 
					 | 
				
			||||||
  if (encoder.constructor === UpdateEncoderV2) {
 | 
					 | 
				
			||||||
  if (doc.store.pendingDs) {
 | 
					  if (doc.store.pendingDs) {
 | 
				
			||||||
    updates.push(doc.store.pendingDs)
 | 
					    updates.push(doc.store.pendingDs)
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
@ -532,6 +532,9 @@ export const encodeStateAsUpdateV2 = (doc, encodedTargetStateVector = new Uint8A
 | 
				
			|||||||
    updates.push(diffUpdateV2(doc.store.pendingStructs.update, encodedTargetStateVector))
 | 
					    updates.push(diffUpdateV2(doc.store.pendingStructs.update, encodedTargetStateVector))
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  if (updates.length > 1) {
 | 
					  if (updates.length > 1) {
 | 
				
			||||||
 | 
					    if (encoder.constructor === UpdateEncoderV1) {
 | 
				
			||||||
 | 
					      return mergeUpdates(updates.map((update, i) => i === 0 ? update : convertUpdateFormatV2ToV1(update)))
 | 
				
			||||||
 | 
					    } else if (encoder.constructor === UpdateEncoderV2) {
 | 
				
			||||||
      return mergeUpdatesV2(updates)
 | 
					      return mergeUpdatesV2(updates)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
				
			|||||||
@ -514,3 +514,33 @@ const finishLazyStructWriting = (lazyWriter) => {
 | 
				
			|||||||
    encoding.writeUint8Array(restEncoder, partStructs.restEncoder)
 | 
					    encoding.writeUint8Array(restEncoder, partStructs.restEncoder)
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @param {Uint8Array} update
 | 
				
			||||||
 | 
					 * @param {typeof UpdateDecoderV2 | typeof UpdateDecoderV1} YDecoder
 | 
				
			||||||
 | 
					 * @param {typeof UpdateEncoderV2 | typeof UpdateEncoderV1 } YEncoder
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					export const convertUpdateFormat = (update, YDecoder, YEncoder) => {
 | 
				
			||||||
 | 
					  const updateDecoder = new YDecoder(decoding.createDecoder(update))
 | 
				
			||||||
 | 
					  const lazyDecoder = new LazyStructReader(updateDecoder, false)
 | 
				
			||||||
 | 
					  const updateEncoder = new YEncoder()
 | 
				
			||||||
 | 
					  const lazyWriter = new LazyStructWriter(updateEncoder)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  for (let curr = lazyDecoder.curr; curr !== null; curr = lazyDecoder.next()) {
 | 
				
			||||||
 | 
					    writeStructToLazyStructWriter(lazyWriter, curr, 0)
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  finishLazyStructWriting(lazyWriter)
 | 
				
			||||||
 | 
					  const ds = readDeleteSet(updateDecoder)
 | 
				
			||||||
 | 
					  writeDeleteSet(updateEncoder, ds)
 | 
				
			||||||
 | 
					  return updateEncoder.toUint8Array()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @param {Uint8Array} update
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					export const convertUpdateFormatV1ToV2 = update => convertUpdateFormat(update, UpdateDecoderV1, UpdateEncoderV2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @param {Uint8Array} update
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					export const convertUpdateFormatV2ToV1 = update => convertUpdateFormat(update, UpdateDecoderV2, UpdateEncoderV1)
 | 
				
			||||||
 | 
				
			|||||||
@ -240,5 +240,49 @@ export const testMergeUpdates2 = tc => {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * @todo be able to apply Skip structs to Yjs docs
 | 
					 * @param {t.TestCase} tc
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					export const testMergePendingUpdates = tc => {
 | 
				
			||||||
 | 
					  const yDoc = new Y.Doc()
 | 
				
			||||||
 | 
					  /**
 | 
				
			||||||
 | 
					   * @type {Array<Uint8Array>}
 | 
				
			||||||
 | 
					   */
 | 
				
			||||||
 | 
					  const serverUpdates = []
 | 
				
			||||||
 | 
					  yDoc.on('update', (update, origin, c) => {
 | 
				
			||||||
 | 
					    serverUpdates.splice(serverUpdates.length, 0, update)
 | 
				
			||||||
 | 
					  })
 | 
				
			||||||
 | 
					  const yText = yDoc.getText('textBlock')
 | 
				
			||||||
 | 
					  yText.applyDelta([{ insert: 'r' }])
 | 
				
			||||||
 | 
					  yText.applyDelta([{ insert: 'o' }])
 | 
				
			||||||
 | 
					  yText.applyDelta([{ insert: 'n' }])
 | 
				
			||||||
 | 
					  yText.applyDelta([{ insert: 'e' }])
 | 
				
			||||||
 | 
					  yText.applyDelta([{ insert: 'n' }])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const yDoc1 = new Y.Doc()
 | 
				
			||||||
 | 
					  Y.applyUpdate(yDoc1, serverUpdates[0])
 | 
				
			||||||
 | 
					  const update1 = Y.encodeStateAsUpdate(yDoc1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const yDoc2 = new Y.Doc()
 | 
				
			||||||
 | 
					  Y.applyUpdate(yDoc2, update1)
 | 
				
			||||||
 | 
					  Y.applyUpdate(yDoc2, serverUpdates[1])
 | 
				
			||||||
 | 
					  const update2 = Y.encodeStateAsUpdate(yDoc2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const yDoc3 = new Y.Doc()
 | 
				
			||||||
 | 
					  Y.applyUpdate(yDoc3, update2)
 | 
				
			||||||
 | 
					  Y.applyUpdate(yDoc3, serverUpdates[3])
 | 
				
			||||||
 | 
					  const update3 = Y.encodeStateAsUpdate(yDoc3)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const yDoc4 = new Y.Doc()
 | 
				
			||||||
 | 
					  Y.applyUpdate(yDoc4, update3)
 | 
				
			||||||
 | 
					  Y.applyUpdate(yDoc4, serverUpdates[2])
 | 
				
			||||||
 | 
					  const update4 = Y.encodeStateAsUpdate(yDoc4)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const yDoc5 = new Y.Doc()
 | 
				
			||||||
 | 
					  Y.applyUpdate(yDoc5, update4)
 | 
				
			||||||
 | 
					  Y.applyUpdate(yDoc5, serverUpdates[4])
 | 
				
			||||||
 | 
					  // @ts-ignore
 | 
				
			||||||
 | 
					  const update5 = Y.encodeStateAsUpdate(yDoc5) // eslint-disable-line
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const yText5 = yDoc5.getText('textBlock')
 | 
				
			||||||
 | 
					  t.compareStrings(yText5.toString(), 'nenor')
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user