add initializer to server/utils

This commit is contained in:
Kevin Jahns 2024-03-25 18:39:39 +01:00
parent 244889fd24
commit fc822645b7

View File

@ -42,7 +42,7 @@ if (typeof persistenceDir === 'string') {
ldb.storeUpdate(docName, update) 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)) doc.conns.forEach((_, conn) => send(doc, conn, message))
} }
/**
* @type {(ydoc: Y.Doc) => Promise<void>}
*/
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<void>} f
*/
export const setContentInitializor = (f) => {
contentInitializor = f
}
class WSSharedDoc extends Y.Doc { class WSSharedDoc extends Y.Doc {
/** /**
* @param {string} name * @param {string} name
@ -133,6 +148,7 @@ class WSSharedDoc extends Y.Doc {
{ maxWait: CALLBACK_DEBOUNCE_MAXWAIT } { maxWait: CALLBACK_DEBOUNCE_MAXWAIT }
)) ))
} }
this.whenInitialized = contentInitializor(this)
} }
} }