MCPcopy
hub / github.com/yjs/yjs / compare

Function compare

tests/testHelper.js:450–499  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

448 * @param {Array<TestYInstance>} users
449 */
450export const compare = users => {
451 users.forEach(u => u.connect())
452 while (users[0].tc.flushAllMessages()) {} // eslint-disable-line
453 // For each document, merge all received document updates with Y.mergeUpdates and create a new document which will be added to the list of "users"
454 // This ensures that mergeUpdates works correctly
455 const mergedDocs = users.map(user => {
456 const ydoc = new Y.Doc()
457 enc.applyUpdate(ydoc, enc.mergeUpdates(user.updates))
458 return ydoc
459 })
460 users.push(.../** @type {any} */(mergedDocs))
461 const userArrayValues = users.map(u => u.get('array').toJSON().children ?? [])
462 const userMapValues = users.map(u => u.get('map').toJSON().attrs ?? {})
463 // @todo fix type error here
464 // @ts-ignore
465 const userXmlValues = users.map(u => u.get('xml').toString())
466 const userTextValues = users.map(u => u.get('text').toDeltaDeep())
467 for (const u of users) {
468 t.assert(u.store.pendingDs === null)
469 t.assert(u.store.pendingStructs === null)
470 }
471 // Test Map iterator
472 const ymapkeys = Array.from(users[0].get('map').attrKeys())
473 t.assert(ymapkeys.length === Object.keys(userMapValues[0]).length)
474 ymapkeys.forEach(key => t.assert(object.hasProperty(userMapValues[0], key)))
475 // Compare all users
476 for (let i = 0; i < users.length - 1; i++) {
477 t.compare(userArrayValues[i].length, users[i].get('array').length)
478 t.compare(userArrayValues[i], userArrayValues[i + 1])
479 t.compare(userMapValues[i], userMapValues[i + 1])
480 t.compare(userXmlValues[i], userXmlValues[i + 1])
481 t.compare(list.toArray(userTextValues[i].children).map(a => (delta.$textOp.check(a) || delta.$insertOp.check(a)) ? a.insert.length : 0).reduce((a, b) => a + b, 0), users[i].get('text').length)
482 t.compare(userTextValues[i], userTextValues[i + 1], '', (_constructor, a, b) => {
483 if (a instanceof Y.Type) {
484 t.compare(a.toJSON(), b.toJSON())
485 } else if (a !== b) {
486 t.fail('Deltas dont match')
487 }
488 return true
489 })
490 t.compare(Y.encodeStateVector(users[i]), Y.encodeStateVector(users[i + 1]))
491 Y.equalIdSets(Y.createDeleteSetFromStructStore(users[i].store), Y.createDeleteSetFromStructStore(users[i + 1].store))
492 compareStructStores(users[i].store, users[i + 1].store)
493 t.compare(Y.encodeSnapshot(Y.snapshot(users[i])), Y.encodeSnapshot(Y.snapshot(users[i + 1])))
494 }
495 users.forEach(user => {
496 compareIdSets(user.store.ds, Y.createDeleteSetFromStructStore(user.store))
497 })
498 users.map(u => u.destroy())
499}
500
501/**
502 * @param {Y.Item?} a

Calls 15

compareStructStoresFunction · 0.85
compareIdSetsFunction · 0.85
connectMethod · 0.80
flushAllMessagesMethod · 0.80
mapMethod · 0.80
pushMethod · 0.80
toStringMethod · 0.80
toDeltaDeepMethod · 0.80
fromMethod · 0.80
attrKeysMethod · 0.80
toArrayMethod · 0.80
forEachMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…