diff --git a/src/utils/Doc.js b/src/utils/Doc.js index 91d7de71..197801fa 100644 --- a/src/utils/Doc.js +++ b/src/utils/Doc.js @@ -17,6 +17,7 @@ import { Observable } from 'lib0/observable' import * as random from 'lib0/random' import * as map from 'lib0/map' import * as array from 'lib0/array' +import * as promise from 'lib0/promise' export const generateNewClientId = random.uint32 @@ -71,6 +72,13 @@ export class Doc extends Observable { this.shouldLoad = shouldLoad this.autoLoad = autoLoad this.meta = meta + this.isLoaded = false + this.whenLoaded = promise.create(resolve => { + this.on('load', () => { + this.isLoaded = true + resolve(this) + }) + }) } /** diff --git a/tests/doc.tests.js b/tests/doc.tests.js index 66ffb454..994ecaeb 100644 --- a/tests/doc.tests.js +++ b/tests/doc.tests.js @@ -228,3 +228,19 @@ export const testSubdocsUndo = tc => { undoManager.redo() t.assert(elems.length === 1) } + +/** + * @param {t.TestCase} tc + */ +export const testLoadDocs = async tc => { + const ydoc = new Y.Doc() + t.assert(ydoc.isLoaded === false) + let loadedEvent = false + ydoc.on('load', () => { + loadedEvent = true + }) + ydoc.emit('load', [ydoc]) + await ydoc.whenLoaded + t.assert(loadedEvent) + t.assert(ydoc.isLoaded) +}