diff --git a/bin/utils.cjs b/bin/utils.cjs index 91b003f..6ca7804 100644 --- a/bin/utils.cjs +++ b/bin/utils.cjs @@ -42,7 +42,7 @@ if (typeof persistenceDir === 'string') { ldb.storeUpdate(docName, update) }) }, - writeState: async (docName, ydoc) => {} + writeState: async (_docName, _ydoc) => {} } } @@ -85,6 +85,21 @@ const updateHandler = (update, _origin, doc, _tr) => { doc.conns.forEach((_, conn) => send(doc, conn, message)) } +/** + * @type {(ydoc: Y.Doc) => Promise} + */ +let contentInitializor = _ydoc => Promise.resolve() + +/** + * This function is called once every time a Yjs document is created. You can + * use it to pull data from an external source or initialize content. + * + * @param {(ydoc: Y.Doc) => Promise} f + */ +export const setContentInitializor = (f) => { + contentInitializor = f +} + class WSSharedDoc extends Y.Doc { /** * @param {string} name @@ -133,6 +148,7 @@ class WSSharedDoc extends Y.Doc { { maxWait: CALLBACK_DEBOUNCE_MAXWAIT } )) } + this.whenInitialized = contentInitializor(this) } }