Skip to content

Commit

Permalink
add initializer to server/utils
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Mar 25, 2024
1 parent 244889f commit fc82264
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion bin/utils.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ if (typeof persistenceDir === 'string') {
ldb.storeUpdate(docName, update)
})
},
writeState: async (docName, ydoc) => {}
writeState: async (_docName, _ydoc) => {}
}
}

Expand Down Expand Up @@ -85,6 +85,21 @@ const updateHandler = (update, _origin, doc, _tr) => {
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 {
/**
* @param {string} name
Expand Down Expand Up @@ -133,6 +148,7 @@ class WSSharedDoc extends Y.Doc {
{ maxWait: CALLBACK_DEBOUNCE_MAXWAIT }
))
}
this.whenInitialized = contentInitializor(this)
}
}

Expand Down

0 comments on commit fc82264

Please sign in to comment.