Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix levelDb error #182

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
dist
node_modules
tmp
tmp
# Level db database
*dbDir*
35 changes: 30 additions & 5 deletions bin/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,13 @@ class WSSharedDoc extends Y.Doc {
*/
const getYDoc = (docname, gc = true) => map.setIfUndefined(docs, docname, () => {
const doc = new WSSharedDoc(docname)
let docLoadedPromise = null
doc.gc = gc
if (persistence !== null) {
persistence.bindState(docname, doc)
docLoadedPromise = persistence.bindState(docname, doc)
}
docs.set(docname, doc)
return doc
return { doc, docLoadedPromise }
})

exports.getYDoc = getYDoc
Expand Down Expand Up @@ -237,17 +238,28 @@ const pingTimeout = 30000
exports.setupWSConnection = (conn, req, { docName = req.url.slice(1).split('?')[0], gc = true } = {}) => {
conn.binaryType = 'arraybuffer'
// get doc, initialize if it does not exist yet
const doc = getYDoc(docName, gc)
const { doc, docLoadedPromise } = getYDoc(docName, gc)
doc.conns.set(conn, new Set())

// it might take some time to load the doc from leveldb
// but before then we still need to listen for websocket events
let isDocLoaded = docLoadedPromise ? false : true
let queuedMessages = []
let isConnectionAlive = true

// 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 => {
if (isDocLoaded) messageListener(conn, doc, new Uint8Array(message))
else queuedMessages.push(new Uint8Array(message))
})

// Check if connection is still alive
let pongReceived = true
const pingInterval = setInterval(() => {
if (!pongReceived) {
if (doc.conns.has(conn)) {
closeConn(doc, conn)
isConnectionAlive = false
}
clearInterval(pingInterval)
} else if (doc.conns.has(conn)) {
Expand All @@ -256,20 +268,22 @@ exports.setupWSConnection = (conn, req, { docName = req.url.slice(1).split('?')[
conn.ping()
} catch (e) {
closeConn(doc, conn)
isConnectionAlive = false
clearInterval(pingInterval)
}
}
}, pingTimeout)
conn.on('close', () => {
closeConn(doc, conn)
isConnectionAlive = false
clearInterval(pingInterval)
})
conn.on('pong', () => {
pongReceived = true
})
// put the following in a variables in a block so the interval handlers don't keep in in
// scope
{
const sendSyncStep1 = () =>{
// send sync step 1
const encoder = encoding.createEncoder()
encoding.writeVarUint(encoder, messageSync)
Expand All @@ -283,4 +297,15 @@ exports.setupWSConnection = (conn, req, { docName = req.url.slice(1).split('?')[
send(doc, conn, encoding.toUint8Array(encoder))
}
}

if (docLoadedPromise) {
docLoadedPromise.then(() => {
if (!isConnectionAlive) return

isDocLoaded = true
queuedMessages.forEach(message => messageListener(conn, doc, message))
queuedMessages = null
sendSyncStep1()
})
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"url": "https://github.com/sponsors/dmonad"
},
"scripts": {
"start": "node ./bin/server.js",
"start": "HOST=localhost PORT=1234 YPERSISTENCE=./dbDir node ./bin/server.js",
"dist": "rm -rf dist && rollup -c && tsc",
"lint": "standard && tsc",
"test": "npm run lint",
Expand Down