Subdocuments
Embedding Yjs documents into Yjs documents
Yjs documents can be embedded into shared types. This allows you to manage vast amounts of Yjs documents as part of a root document.
1
// Client One
2
const rootDoc = new Y.Doc()
3
const folder = rootDoc.getMap()
4
​
5
const subDoc = new Y.Doc()
6
subDoc.getText().insert(0, 'some initial content')
7
folder.set('my-document.txt', subDoc)
Copied!
An obvious use-case is to manage documents in a folder structure. Each note could be represented as a subdocument that is lazily loaded to memory when needed. By default, subdocuments are empty until they are explicitly loaded.
1
// Client Two
2
const subDoc = rootDoc.getMap().get('my-document.txt')
3
const subDocText = subDoc.getText()
4
subDocText.toString() // => "" - content is empty
5
​
6
// Data needs to be loaded first..
7
subDoc.load()
8
​
9
// Then the providers will fetch data from the database / network
10
// and eventually fill the content
11
subDoc.on('synced', () => {
12
subDocText.toString() // => "some initial content"
13
})
14
​
15
// It is hard to determine when data was actually synced.
16
// The synced event is still helpful to show the user that this user synced
17
// with other users.
18
// It is safer to observe the data types and don't listen
19
// to an explicit sync event.
20
subDocText.observe(() => {
21
// data changed..
22
})
Copied!
Subdocuments are lazily loaded after they have been explicitly loaded to memory. A subdocument can be destroyed doc.destroy() to free all used memory and destroy existing data bindings. The document can be accessed again to force the provider to load the content again.
1
const subDoc = rootDoc.getMap().get('my-document.text')
2
subDoc.load()
3
​
4
// After some time you might not need to render the document anymore.
5
// You want to destroy the existing data bindings and load a different document
6
...
7
​
8
subDoc.destroy() // free all memory and destroy all data bindings to this document.
9
​
10
// You should not access the destroyed document again.
11
// Instead you can create a new one by requesting the document again.
12
const subDocReloaded = rootDoc.getMap().get('my-document.text')
13
subDocReloaded.load()
Copied!
It is up to the providers to sync subdocuments. It is possible to create a very efficient sync mechanism using sub-documents. The providers can sync collections of documents in one flush instead of having multiple requests. It is also possible to handle authorization over the folder structure. But all official Yjs providers currently think of sub-documents as separate entities. A new feature that was introduced in [email protected] is that all documents are given a GUID. The documents are identified with GUIDs and used as a room-name to sync documents. This allows you to duplicate data in the document structure.
1
const rootDoc = new Y.Doc()
2
​
3
const doc = new Y.Doc()
4
doc.guid // => "123e4567-e89b-12d3-a456-426614174000" - A random UUIDv4
5
​
6
rootDoc.getMap().set('file.txt', doc)
7
​
8
// we can include a copy of a subdocument by giving it the same UUIDv4
9
const copy = new Y.Doc({ guid: doc.guid })
10
rootDoc.getMap().set('copy.txt', copy)
11
​
12
// `doc` and `copy` will automatically sync because they have the same guid
Copied!
By default, all subdocuments must be explicitly loaded before they are filled with content. It is possible to define Y.Doc({ autoLoad: true }) to specify that all peers should automatically load the document.
Providers listen to subdocs events to get notified when subdocuments are added, removed, or loaded.
1
doc.on('subdocs', ({ added: Set<Y.Doc>, removed: Set<Y.Doc>, loaded: Set<Y.Doc> }) => {
2
// use added / removed to sync documents in the background
3
// use loaded to fill document content
4
})
Copied!
Providers (e.g. y-websocket, y-indexeddb) are responsible for syncing subdocuments. Not all providers support subdocuments yet. A simple method to implement lazy-loading documents is to create a provider instance to the doc.guid-room once a document is loaded:
1
doc.on('subdocs', ({ loaded }) => {
2
loaded.forEach(subdoc => {
3
new WebrtcProvider(subdoc.guid, subdoc)
4
})
5
})
6
doc.getSubdocs() // Get the Set<Y.Doc> of all subdocuments
Copied!
Last modified 19d ago
Copy link
Edit on GitHub