Fix YPERSISTENCE -- allowing y-leveldb persistence

This commit is contained in:
Kevin Jahns
2020-07-10 02:39:44 +02:00
parent 5b758e2691
commit f6943f33af
5 changed files with 252 additions and 8 deletions

View File

@@ -20,9 +20,22 @@ const persistenceDir = process.env.YPERSISTENCE
*/
let persistence = null
if (typeof persistenceDir === 'string') {
console.info('Persisting documents to "' + persistenceDir + '"')
// @ts-ignore
const LevelDbPersistence = require('y-leveldb').LevelDbPersistence
persistence = new LevelDbPersistence(persistenceDir)
const LeveldbPersistence = require('y-leveldb').LeveldbPersistence
const ldb = new LeveldbPersistence(persistenceDir)
persistence = {
bindState: async (docName, ydoc) => {
const persistedYdoc = await ldb.getYDoc(docName)
const newUpdates = Y.encodeStateAsUpdate(ydoc)
ldb.storeUpdate(docName, newUpdates)
Y.applyUpdate(ydoc, Y.encodeStateAsUpdate(persistedYdoc))
ydoc.on('update', update => {
ldb.storeUpdate(docName, update)
})
},
writeState: async (docName, ydoc) => {}
}
}
/**