Add optional iterable param to Y.Map(), matching Map()
This commit is contained in:
parent
fa58ce53cd
commit
868dd5f0a5
@ -45,13 +45,23 @@ export class YMapEvent extends YEvent {
|
|||||||
* @implements {Iterable<T>}
|
* @implements {Iterable<T>}
|
||||||
*/
|
*/
|
||||||
export class YMap extends AbstractType {
|
export class YMap extends AbstractType {
|
||||||
constructor () {
|
/**
|
||||||
|
*
|
||||||
|
* @param {Iterable<readonly [string, any]>=} entries - an optional iterable to initialize the YMap
|
||||||
|
*/
|
||||||
|
constructor (entries) {
|
||||||
super()
|
super()
|
||||||
/**
|
/**
|
||||||
* @type {Map<string,any>?}
|
* @type {Map<string,any>?}
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
this._prelimContent = new Map()
|
this._prelimContent = null
|
||||||
|
|
||||||
|
if (entries === undefined) {
|
||||||
|
this._prelimContent = new Map()
|
||||||
|
} else {
|
||||||
|
this._prelimContent = new Map(entries)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -8,6 +8,33 @@ import * as Y from '../src/index.js'
|
|||||||
import * as t from 'lib0/testing.js'
|
import * as t from 'lib0/testing.js'
|
||||||
import * as prng from 'lib0/prng.js'
|
import * as prng from 'lib0/prng.js'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {t.TestCase} tc
|
||||||
|
*/
|
||||||
|
export const testMapHavingIterableAsConstructorParamTests = tc => {
|
||||||
|
const { map0 } = init(tc, { users: 1 })
|
||||||
|
|
||||||
|
const m1 = new Y.Map(Object.entries({ number: 1, string: 'hello' }))
|
||||||
|
map0.set('m1', m1)
|
||||||
|
t.assert(m1.get('number') === 1)
|
||||||
|
t.assert(m1.get('string') === 'hello')
|
||||||
|
|
||||||
|
const m2 = new Y.Map([
|
||||||
|
['object', { x: 1 }],
|
||||||
|
['boolean', true]
|
||||||
|
])
|
||||||
|
map0.set('m2', m2)
|
||||||
|
t.assert(m2.get('object').x === 1)
|
||||||
|
t.assert(m2.get('boolean') === true)
|
||||||
|
|
||||||
|
const m3 = new Y.Map([...m1, ...m2])
|
||||||
|
map0.set('m3', m3)
|
||||||
|
t.assert(m3.get('number') === 1)
|
||||||
|
t.assert(m3.get('string') === 'hello')
|
||||||
|
t.assert(m3.get('object').x === 1)
|
||||||
|
t.assert(m3.get('boolean') === true)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {t.TestCase} tc
|
* @param {t.TestCase} tc
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user