Skip to content

Commit fc82264

Browse files
committedMar 25, 2024·
add initializer to server/utils
1 parent 244889f commit fc82264

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed
 

‎bin/utils.cjs

+17-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ if (typeof persistenceDir === 'string') {
4242
ldb.storeUpdate(docName, update)
4343
})
4444
},
45-
writeState: async (docName, ydoc) => {}
45+
writeState: async (_docName, _ydoc) => {}
4646
}
4747
}
4848

@@ -85,6 +85,21 @@ const updateHandler = (update, _origin, doc, _tr) => {
8585
doc.conns.forEach((_, conn) => send(doc, conn, message))
8686
}
8787

88+
/**
89+
* @type {(ydoc: Y.Doc) => Promise<void>}
90+
*/
91+
let contentInitializor = _ydoc => Promise.resolve()
92+
93+
/**
94+
* This function is called once every time a Yjs document is created. You can
95+
* use it to pull data from an external source or initialize content.
96+
*
97+
* @param {(ydoc: Y.Doc) => Promise<void>} f
98+
*/
99+
export const setContentInitializor = (f) => {
100+
contentInitializor = f
101+
}
102+
88103
class WSSharedDoc extends Y.Doc {
89104
/**
90105
* @param {string} name
@@ -133,6 +148,7 @@ class WSSharedDoc extends Y.Doc {
133148
{ maxWait: CALLBACK_DEBOUNCE_MAXWAIT }
134149
))
135150
}
151+
this.whenInitialized = contentInitializor(this)
136152
}
137153
}
138154

0 commit comments

Comments
 (0)
Please sign in to comment.