Skip to content

Commit

Permalink
Merge branch 'master' of github.com:yjs/yjs
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Jun 8, 2020
2 parents 4d2369c + cebe96c commit c4d80d1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/types/YMap.js
Expand Up @@ -115,6 +115,15 @@ export class YMap extends AbstractType {
return map
}

/**
* Returns the size of the YMap (count of key/value pairs)
*
* @return {number}
*/
get size () {
return [...createMapIterator(this._map)].length
}

/**
* Returns the keys for each element in the YMap Type.
*
Expand Down Expand Up @@ -143,7 +152,7 @@ export class YMap extends AbstractType {
}

/**
* Executes a provided function on once on overy key-value pair.
* Executes a provided function on once on every key-value pair.
*
* @param {function(T,string,YMap<T>):void} f A function to execute on every element of this YArray.
*/
Expand Down
16 changes: 16 additions & 0 deletions tests/y-map.tests.js
Expand Up @@ -60,6 +60,7 @@ export const testBasicMapTests = tc => {
t.assert(map0.get('boolean1') === true, 'client 0 computed the change (boolean)')
t.compare(map0.get('object'), { key: { key2: 'value' } }, 'client 0 computed the change (object)')
t.assert(map0.get('y-map').get('y-array').get(0) === -1, 'client 0 computed the change (type)')
t.assert(map0.size === 6, 'client 0 map has correct size')

users[2].connect()
testConnector.flushAllMessages()
Expand All @@ -70,6 +71,7 @@ export const testBasicMapTests = tc => {
t.assert(map1.get('boolean1') === true, 'client 1 computed the change (boolean)')
t.compare(map1.get('object'), { key: { key2: 'value' } }, 'client 1 received the update (object)')
t.assert(map1.get('y-map').get('y-array').get(0) === -1, 'client 1 received the update (type)')
t.assert(map1.size === 6, 'client 1 map has correct size')

// compare disconnected user
t.assert(map2.get('number') === 1, 'client 2 received the update (number) - was disconnected')
Expand Down Expand Up @@ -157,6 +159,20 @@ export const testGetAndSetOfMapPropertyWithConflict = tc => {
compare(users)
}

/**
* @param {t.TestCase} tc
*/
export const testSizeAndDeleteOfMapProperty = tc => {
const { map0 } = init(tc, { users: 1 })
map0.set('stuff', 'c0')
map0.set('otherstuff', 'c1')
t.assert(map0.size === 2, `map size is ${map0.size} expected 2`)
map0.delete('stuff')
t.assert(map0.size === 1, `map size after delete is ${map0.size}, expected 1`)
map0.delete('otherstuff')
t.assert(map0.size === 0, `map size after delete is ${map0.size}, expected 0`)
}

/**
* @param {t.TestCase} tc
*/
Expand Down

0 comments on commit c4d80d1

Please sign in to comment.