Skip to content

Commit

Permalink
add better typings for server code
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Apr 27, 2024
1 parent 12608bd commit 9e2dab6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
14 changes: 7 additions & 7 deletions bin/utils.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ class WSSharedDoc extends Y.Doc {
this.awareness.on('update', awarenessChangeHandler)
this.on('update', /** @type {any} */ (updateHandler))
if (isCallbackSet) {
this.on('update', debounce(
this.on('update', /** @type {any} */ (debounce(
callbackHandler,
CALLBACK_DEBOUNCE_WAIT,
{ maxWait: CALLBACK_DEBOUNCE_MAXWAIT }
))
)))
}
this.whenInitialized = contentInitializor(this)
}
Expand Down Expand Up @@ -233,15 +233,15 @@ const closeConn = (doc, conn) => {

/**
* @param {WSSharedDoc} doc
* @param {any} conn
* @param {import('ws').WebSocket} conn
* @param {Uint8Array} m
*/
const send = (doc, conn, m) => {
if (conn.readyState !== wsReadyStateConnecting && conn.readyState !== wsReadyStateOpen) {
closeConn(doc, conn)
}
try {
conn.send(m, /** @param {any} err */ err => { err != null && closeConn(doc, conn) })
conn.send(m, {}, err => { err != null && closeConn(doc, conn) })
} catch (e) {
closeConn(doc, conn)
}
Expand All @@ -250,11 +250,11 @@ const send = (doc, conn, m) => {
const pingTimeout = 30000

/**
* @param {any} conn
* @param {any} req
* @param {import('ws').WebSocket} conn
* @param {import('http').IncomingMessage} req
* @param {any} opts
*/
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'
// get doc, initialize if it does not exist yet
const doc = getYDoc(docName, gc)
Expand Down
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"y-protocols": "^1.0.5"
},
"devDependencies": {
"@types/lodash.debounce": "^4.0.9",
"@types/node": "^18.15.0",
"@types/ws": "^8.5.10",
"rollup": "^3.19.1",
Expand Down

0 comments on commit 9e2dab6

Please sign in to comment.