Merge branch 'provide-synced-ydoc-accessors' of git://github.com/canadaduane/y-websocket into canadaduane-provide-synced-ydoc-accessors

This commit is contained in:
Kevin Jahns 2020-10-18 18:00:50 +02:00
commit 9d298f3255
2 changed files with 56 additions and 17 deletions

View File

@ -23,6 +23,16 @@ const wsReadyStateClosed = 3 // eslint-disable-line
// disable gc when using snapshots! // disable gc when using snapshots!
const gcEnabled = process.env.GC !== 'false' && process.env.GC !== '0' const gcEnabled = process.env.GC !== 'false' && process.env.GC !== '0'
const persistenceDir = process.env.YPERSISTENCE const persistenceDir = process.env.YPERSISTENCE
/**
* If persistence is enabled, performs two-way sync between memory and disk stores.
* If persistence is not enabled, does nothing.
*
* @param {string} docName the Y.Doc's name in disk store
* @param {WSSharedDoc} ydoc the in-memory Y.Doc
*/
let mutualSync = async (docName, ydoc) => {}
/** /**
* @type {{bindState: function(string,WSSharedDoc):void, writeState:function(string,WSSharedDoc):Promise<any>}|null} * @type {{bindState: function(string,WSSharedDoc):void, writeState:function(string,WSSharedDoc):Promise<any>}|null}
*/ */
@ -32,17 +42,24 @@ if (typeof persistenceDir === 'string') {
// @ts-ignore // @ts-ignore
const LeveldbPersistence = require('y-leveldb').LeveldbPersistence const LeveldbPersistence = require('y-leveldb').LeveldbPersistence
const ldb = new LeveldbPersistence(persistenceDir) const ldb = new LeveldbPersistence(persistenceDir)
// Sync in-memory and ldb state
mutualSync = async (docName, ydoc) => {
const persistedYdoc = await ldb.getYDoc(docName)
const newUpdates = Y.encodeStateAsUpdate(ydoc)
ldb.storeUpdate(docName, newUpdates)
Y.applyUpdate(ydoc, Y.encodeStateAsUpdate(persistedYdoc))
}
persistence = { persistence = {
bindState: async (docName, ydoc) => { bindState: async (docName, ydoc) => {
const persistedYdoc = await ldb.getYDoc(docName) await mutualSync(docName, ydoc)
const newUpdates = Y.encodeStateAsUpdate(ydoc) ydoc.on('update', (update) => {
ldb.storeUpdate(docName, newUpdates)
Y.applyUpdate(ydoc, Y.encodeStateAsUpdate(persistedYdoc))
ydoc.on('update', update => {
ldb.storeUpdate(docName, update) ldb.storeUpdate(docName, update)
}) })
}, },
writeState: async (docName, ydoc) => {}
writeState: async (docName, ydoc) => {},
} }
} }
@ -130,6 +147,35 @@ class WSSharedDoc extends Y.Doc {
} }
} }
/**
* @param {string} docName - the name of the Y.Doc to find or initialize
* @param {boolean} gc - whether to allow gc on the doc (applies only when created)
*/
function getOrInitDoc(docName, gc = true) {
// get doc, create if it does not exist yet
return map.setIfUndefined(docs, docName, () => {
const doc = new WSSharedDoc(docName)
doc.gc = gc
if (persistence !== null) {
persistence.bindState(docName, doc)
}
docs.set(docName, doc)
return doc
})
}
/**
* Gets a Y.Doc by name, whether in memory or on disk
*
* @param {string} docName - the name of the Y.Doc to find or create
*/
async function getOrCreateDoc(docName, gc = true) {
const ydoc = getOrInitDoc(docName, gc)
await mutualSync(docName, ydoc)
return ydoc
}
exports.getOrCreateDoc = getOrCreateDoc
/** /**
* @param {any} conn * @param {any} conn
* @param {WSSharedDoc} doc * @param {WSSharedDoc} doc
@ -196,6 +242,7 @@ const send = (doc, conn, m) => {
const pingTimeout = 30000 const pingTimeout = 30000
/** /**
* @param {any} conn * @param {any} conn
* @param {any} req * @param {any} req
@ -203,16 +250,8 @@ const pingTimeout = 30000
*/ */
exports.setupWSConnection = (conn, req, { docName = req.url.slice(1).split('?')[0], gc = true } = {}) => { exports.setupWSConnection = (conn, req, { docName = req.url.slice(1).split('?')[0], gc = true } = {}) => {
conn.binaryType = 'arraybuffer' conn.binaryType = 'arraybuffer'
// get doc, create if it does not exist yet // get doc, initialize if it does not exist yet
const doc = map.setIfUndefined(docs, docName, () => { const doc = getOrInitDoc(docName, gc)
const doc = new WSSharedDoc(docName)
doc.gc = gc
if (persistence !== null) {
persistence.bindState(docName, doc)
}
docs.set(docName, doc)
return doc
})
doc.conns.set(conn, new Set()) doc.conns.set(conn, new Set())
// listen and reply to events // listen and reply to events
conn.on('message', /** @param {ArrayBuffer} message */ message => messageListener(conn, doc, new Uint8Array(message))) conn.on('message', /** @param {ArrayBuffer} message */ message => messageListener(conn, doc, new Uint8Array(message)))

View File

@ -55,7 +55,7 @@
"rollup-cli": "^1.0.9", "rollup-cli": "^1.0.9",
"standard": "^12.0.1", "standard": "^12.0.1",
"typescript": "^3.9.6", "typescript": "^3.9.6",
"yjs": "13.0.5" "yjs": "13.4.1"
}, },
"peerDependenies": { "peerDependenies": {
"yjs": "^13.0.0" "yjs": "^13.0.0"