Skip to content

Commit

Permalink
chore: Add helpful comments and code bits for y.js debugging
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliusknorr committed Jun 19, 2023
1 parent 2df404c commit 3dfaa7f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/components/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,11 @@ export default {
},
created() {
this.$ydoc = new Doc()
// The following can be useful for debugging ydoc updates
// this.$ydoc.on('update', function(update, origin, doc, tr) {
// console.debug('ydoc update', update, origin, doc, tr)
// Y.logUpdate(update)
// });
this.$providers = []
this.$editor = null
this.$syncService = null
Expand Down
29 changes: 29 additions & 0 deletions src/helpers/yjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import { encodeArrayBuffer, decodeArrayBuffer } from '../helpers/base64.js'
import { Doc, encodeStateAsUpdate, applyUpdate } from 'yjs'
import * as decoding from 'lib0/decoding.js'

/**
*
Expand All @@ -41,3 +42,31 @@ export function applyDocumentState(ydoc, documentState) {
const update = decodeArrayBuffer(documentState)
applyUpdate(ydoc, update)
}

/**
* Log y.js messages with their type and initiator call stack
*
* @param {string} step - Y.js message
*/
export function logStep(step) {
// Create error for stack trace
const err = new Error()

const decoder = decoding.createDecoder(step)

const messageType = decoding.readVarUint(decoder)
const subType = decoding.readVarUint(decoder)

const encodedStep = encodeArrayBuffer(step)
switch (messageType) {
case 0:
console.debug('y.js message sync', subType, encodedStep, err.stack)
break
case 3:
console.debug('y.js message awareness_query', encodedStep, err.stack)
break
case 1:
console.debug('y.js message awareness', encodedStep, err.stack)
break
}
}
3 changes: 3 additions & 0 deletions src/services/WebSocketPolyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ export default function initWebSocketPolyfill(syncService, fileId, initialSessio
}

send(...data) {
// Useful for debugging what steps are sent and how they were initiated
// data.forEach(logStep)

const sending = this.#queue.length > 0
this.#queue.push(...data)
if (!sending) {
Expand Down

0 comments on commit 3dfaa7f

Please sign in to comment.