working flat observers over arrays + test

This commit is contained in:
Bartosz Sypytkowski 2023-06-09 16:04:20 +02:00
parent 2c60c4a130
commit 5dcf2d7610

View File

@ -122,12 +122,12 @@ export const testObserveMapUpdate = tc => {
const { testConnector, users, map0, map1 } = init(tc, { users: 2 }) const { testConnector, users, map0, map1 } = init(tc, { users: 2 })
map0.set('a', 'value') map0.set('a', 'value')
const link0 = /** @type {Y.WeakLink<String>} */ (map0.link('a')) const link0 = /** @type {Y.WeakLink<String>} */ (map0.link('a'))
map0.set('b', link0)
/** /**
* @type {any} * @type {any}
*/ */
let target0 let target0
link0.observe((e) => target0 = e.target) link0.observe((e) => target0 = e.target)
map0.set('b', link0)
testConnector.flushAllMessages() testConnector.flushAllMessages()
@ -153,12 +153,12 @@ export const testObserveMapDelete = tc => {
const { testConnector, users, map0, map1 } = init(tc, { users: 2 }) const { testConnector, users, map0, map1 } = init(tc, { users: 2 })
map0.set('a', 'value') map0.set('a', 'value')
const link0 = /** @type {Y.WeakLink<String>} */ (map0.link('a')) const link0 = /** @type {Y.WeakLink<String>} */ (map0.link('a'))
map0.set('b', link0)
/** /**
* @type {any} * @type {any}
*/ */
let target0 let target0
link0.observe((e) => target0 = e.target) link0.observe((e) => target0 = e.target)
map0.set('b', link0)
testConnector.flushAllMessages() testConnector.flushAllMessages()
@ -176,74 +176,35 @@ export const testObserveMapDelete = tc => {
testConnector.flushAllMessages() testConnector.flushAllMessages()
t.compare(target1.deref(), undefined) t.compare(target1.deref(), undefined)
} }
/** /**
* @param {t.TestCase} tc * @param {t.TestCase} tc
*/ */
const testObserveMapLinkMapRemove = tc => { export const testObserveArray = tc => {
const doc = new Y.Doc() const { testConnector, array0, array1 } = init(tc, { users: 2 })
const map1 = doc.getMap('map1') array0.insert(0, ['A','B','C'])
const map2 = doc.getMap('map2') const link0 = /** @type {Y.WeakLink<String>} */ (array0.link(1))
array0.insert(0, [link0])
/** /**
* @type {Map<string, { action: 'add' | 'update' | 'delete', oldValue: any, newValue: any }>} * @type {any}
*/ */
let keys let target0
map1.observe((e) => keys = e.keys) link0.observe((e) => target0 = e.target)
map2.set('key', 'value1') testConnector.flushAllMessages()
const link = map2.link('key')
map1.set('other-key', link)
keys = /** @type {any} */ (null) let link1 = /** @type {Y.WeakLink<String>} */ (array1.get(0))
map2.delete('key') t.compare(link1.deref(), 'B')
t.compare(keys.get('key'), { action:'delete', oldValue: 'value1', newValue: null })
}
/**
* @param {t.TestCase} tc
*/
const testObserveArrayLinkMapRemove = tc => {
const doc = new Y.Doc()
const array = doc.getArray('array')
const map = doc.getMap('map')
/** /**
* @type {Array<any>} * @type {any}
*/ */
let delta let target1
array.observe((e) => delta = e.delta) link1.observe((e) => target1 = e.target)
map.set('key', 'value1') array0.delete(2)
const link = map.link('key') t.compare(target0.deref(), undefined)
array.insert(0, [link])
delta = /** @type {any} */ (null) testConnector.flushAllMessages()
map.delete('key') t.compare(target1.deref(), undefined)
t.compare(delta, [{ delete: 1 }])
}
/**
* @param {t.TestCase} tc
*/
const testObserveArrayLinkMapUpdate = tc => {
const doc = new Y.Doc()
const array = doc.getArray('array')
const map = doc.getMap('map')
/**
* @type {Array<any>}
*/
let delta
array.observe((e) => delta = e.delta)
map.set('key', 'value1')
const link = map.link('key')
array.insert(0, [link])
delta = /** @type {any} */ (null)
map.set('key', 'value2')
t.compare(delta, [{ delete: 1 }, { insert: 'value2' }])
} }
/** /**