Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
chore(refactor): include version in steps for WebsocketPolyfill
Signed-off-by: Max <[email protected]>
  • Loading branch information
max-nextcloud committed Oct 12, 2025
commit 2e47554d794585fca801bc50654e2ea0e65c6a68
31 changes: 31 additions & 0 deletions src/helpers/steps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import type { FlatStep, Session, Step } from '../services/SyncService'
import { COLLABORATOR_DISCONNECT_TIME } from '../services/SyncService'

/**
* Get the recent awareness messages as steps
* @param sessions to process.
*/
export function awarenessSteps(sessions: Session[]): FlatStep[] {
const lastContactThreshold = Math.floor(Date.now() / 1000) - COLLABORATOR_DISCONNECT_TIME
return sessions
.filter((s) => s.lastContact > lastContactThreshold)
.filter((s) => Boolean(s.lastAwarenessMessage))
.map((s) => ({ step: s.lastAwarenessMessage }))
}

/**
* Flatten the provided steps and assign the version to each of them.
* @param steps to process.
*/
export function flatSteps(steps: Step[]): FlatStep[] {
return steps.flatMap(
(s) => s.data.map(
(step) => ({ step, version: s.version }),
),
)
}
52 changes: 20 additions & 32 deletions src/services/SyncService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import mitt from 'mitt'
import debounce from 'debounce'

import PollingBackend from './PollingBackend.js'
import Outbox from './Outbox.js'
import Outbox from './Outbox'
import SessionApi, { Connection } from './SessionApi.js'
import { documentStateToStep } from '../helpers/yjs.js'
import { logger } from '../helpers/logger.js'

import { awarenessSteps, flatSteps } from '../helpers/steps'
/**
* Timeout after which the editor will consider a document without changes being synced as idle
* The session will be terminated and the document will stay open in read-only mode with a button to reconnect if needed
Expand Down Expand Up @@ -64,6 +64,14 @@ export interface Step {
sessionId: number
}

/*
* Step as what we process it in the WebsocketPolyfill
*/
export interface FlatStep {
step: string
version?: number
}

export interface UserSession {
id: number
userId: string
Expand Down Expand Up @@ -301,40 +309,20 @@ class SyncService {
steps,
document,
sessions = [],
} : {
steps: Step[],
document?: Document,
sessions?: Session[],
}: {
steps: Step[]
document?: object
sessions?: Session[]
}) {
const awareness = sessions
.filter(s => s.lastContact > (Math.floor(Date.now() / 1000) - COLLABORATOR_DISCONNECT_TIME))
.filter(s => s.lastAwarenessMessage)
.map(s => {
return { step: s.lastAwarenessMessage }
})
const newSteps = [...awareness]
for (let i = 0; i < steps.length; i++) {
const singleSteps = steps[i].data
if (this.version < steps[i].version) {
this.version = steps[i].version
}
if (!Array.isArray(singleSteps)) {
logger.error('Invalid step data, skipping step', { step: steps[i] })
// TODO: recover
continue
}
singleSteps.forEach(step => {
newSteps.push({
step,
})
})
}
this.lastStepPush = Date.now()
this.bus.emit('sync', {
steps: newSteps,
steps: [
...awarenessSteps(sessions),
...flatSteps(steps),
],
document,
version: this.version,
version: Math.max(this.version, ...steps.map((s) => s.version)),
})
this.lastStepPush = Date.now()
}

checkIdle() {
Expand Down