Architecture choice of non-promise-based getters

Hey Kevin,

I also wanted to know more about the architectural choice of the getters of Yjs shared types not being promised based. I was experimenting around with the chaining of functions and I noticed that the getters are unable to return the correct output right after setting them. However, if I view the object, I see that it has been set properly, and the object does exist in _prelimContent. The length property, however, still returns 0.

I’m stuck in understanding how this works, so could you give out your insights?

Thanks, and regards,

Mansehej.

Hi @Mansehej,

Please always post code examples. This makes it easier for me understand your approach and figure out what is going wrong.

I guess you are doing the following:

const yarray = new Y.Array()

yarray.insert(0, ['some content'])
yarray.toArray() // => []

But instead you should get the type from the Yjs document:

const ydoc = new Y.Doc()
const yarray = ydoc.getArray('my array')

yarray.insert(0, ['some content'])
yarray.toArray() // => ['some content']

Every type needs to be bound to a Yjs document. You can initialize a Yjs type before inserting it into another type. Until then it is empty:

const ydoc = new Y.Doc()
const yarray = new Y.Array

yarray.insert(0, ['some content'])
yarray.toArray() // => []

ydoc.getMap('my map').set('my array', yarray)

yarray.toArray() // => ['some content']

At some point the types might also work independently without a Yjs document. But that would require me to completely mock the Yjs event system, and mock also the other Yjs methods for a feature that is probably utterly useless. Just use the types with the Yjs document - we want build collaborative documents anyway :wink: