Skip to content

Commit

Permalink
refactor(yjs): move queue handling into helper
Browse files Browse the repository at this point in the history
Signed-off-by: Max <max@nextcloud.com>
  • Loading branch information
max-nextcloud committed Jul 2, 2024
1 parent e214f6f commit 13c580c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
20 changes: 20 additions & 0 deletions src/helpers/yjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,26 @@ export function applyUpdateMessage(ydoc, updateMessage, origin = 'origin') {
)
}

/**
* Get the steps for sending to the server
*
* @param {object[]} queue - queue for the outgoing steps
*/
export function getSteps(queue) {
return queue.map(s => encodeArrayBuffer(s))
.filter(s => s < 'AQ')
}

/**
* Encode the latest awareness message for sending
*
* @param {object[]} queue - queue for the outgoing steps
*/
export function getAwareness(queue) {
return queue.map(s => encodeArrayBuffer(s))
.findLast(s => s > 'AQ') || ''
}

/**
* Log y.js messages with their type and initiator call stack
*
Expand Down
8 changes: 3 additions & 5 deletions src/services/SyncService.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import debounce from 'debounce'

import PollingBackend from './PollingBackend.js'
import SessionApi, { Connection } from './SessionApi.js'
import { encodeArrayBuffer } from '../helpers/base64.ts'
import { getSteps, getAwareness } from '../helpers/yjs.js'
import { logger } from '../helpers/logger.js'

/**
Expand Down Expand Up @@ -276,10 +276,8 @@ class SyncService {
return
}
let outbox = []
const steps = queue.map(s => encodeArrayBuffer(s))
.filter(s => s < 'AQ')
const awareness = queue.map(s => encodeArrayBuffer(s))
.findLast(s => s > 'AQ') || ''
const steps = getSteps(queue)
const awareness = getAwareness(queue)
return this.sendStepsNow(() => {
const data = { steps, awareness, version: this.version }
outbox = [...queue]
Expand Down
17 changes: 4 additions & 13 deletions src/services/WebSocketPolyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
*/

import { logger } from '../helpers/logger.js'
import { encodeArrayBuffer, decodeArrayBuffer } from '../helpers/base64.ts'
import { decodeArrayBuffer } from '../helpers/base64.ts'
import { getSteps, getAwareness } from '../helpers/yjs.js'

/**
*
Expand Down Expand Up @@ -69,8 +70,8 @@ export default function initWebSocketPolyfill(syncService, fileId, initialSessio
let outbox = []
return syncService.sendSteps(() => {
const data = {
steps: this.#steps,
awareness: this.#awareness,
steps: getSteps(queue),
awareness: getAwareness(queue),
version: this.#version,
}
outbox = [...queue]
Expand All @@ -86,16 +87,6 @@ export default function initWebSocketPolyfill(syncService, fileId, initialSessio
}, err => logger.error(err))
}

get #steps() {
return queue.map(s => encodeArrayBuffer(s))
.filter(s => s < 'AQ')
}

get #awareness() {
return queue.map(s => encodeArrayBuffer(s))
.findLast(s => s > 'AQ') || ''
}

async close() {
Object.entries(this.#handlers)
.forEach(([key, value]) => syncService.off(key, value))
Expand Down

0 comments on commit 13c580c

Please sign in to comment.