Skip to content
Merged
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
28 changes: 28 additions & 0 deletions playwright/e2e/synchronize-content/untouched.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { expect, mergeTests } from '@playwright/test'
import { test as editorTest } from '../../support/fixtures/editor.ts'
import { test as uploadFileTest } from '../../support/fixtures/upload-file.ts'

const test = mergeTests(editorTest, uploadFileTest)

test.use({ fileContent: '## Hello world\n' })

test('does not save an untouched file', async ({ close, editor, open, page }) => {
const saves: string[] = []
page.on('request', (request) => {
if (/\/session\/.*\/save/.test(request.url())) {
saves.push(request.url())
}
})
const push = page.waitForResponse(/\/session\/.*\/push/)
await open()
await expect(editor.getHeading({ name: 'Hello world' })).toBeVisible()
await push
await expect(editor.saveIndicator).not.toHaveAccessibleName(/Unsaved changes/)
await close()
expect(saves).toEqual([])
})
5 changes: 5 additions & 0 deletions src/components/CollaborativeEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -651,8 +651,13 @@ export default defineComponent({
content,
isRichEditor: this.isRichEditor,
})
// Every client generates the same initial state from the file content.
// Mark it as received from the server so it is neither pushed nor saved.
// The first local change pushes it along, as the provider sends
// everything the server does not have yet.
setInitialYjsState(this.ydoc, content, {
isRichEditor: this.isRichEditor,
origin: this.syncProvider,
})
}
})
Expand Down
21 changes: 19 additions & 2 deletions src/composables/useSaveService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ export function provideSaveService(
*/
function getSaveData(): SaveData {
return {
version: syncService.version,
// No steps were received yet on a document without stored state
version: Math.max(syncService.version, 0),
autosaveContent: serialize(),
documentState: getDocumentState(ydoc),
}
Expand All @@ -51,6 +52,7 @@ export function provideSaveService(
const saveService = new SaveService({
connection,
getSaveData,
pushChanges: () => syncService.sendRemainingSteps(),
})

const autosaveOnUpdate = ({ transaction }: { transaction: Transaction }) => {
Expand Down Expand Up @@ -99,13 +101,28 @@ export function provideSaveService(
*/
function updateVersionWithChanges(event: { version: number }) {
versionWithChanges.value = Math.max(event.version, versionWithChanges.value)
// Retried pushes are not preceded by an editor update that triggers the autosave
if ((document.value?.lastSavedVersion ?? 0) < event.version && !saveService.autosave.isPending) {
saveService.autosave()
}
}
syncService.bus.on('changesPushed', updateVersionWithChanges)
onUnmounted(() => {
syncService.bus.off('changesPushed', updateVersionWithChanges)
})

const dirty = computed(() => (document.value?.lastSavedVersion ?? 0) < versionWithChanges.value)
const hasPendingChanges = ref(false)
// Track if local changes are waiting to be pushed
function updatePendingChanges(event: { pending: boolean }) {
hasPendingChanges.value = event.pending
}
syncService.bus.on('pendingChanges', updatePendingChanges)
onUnmounted(() => {
syncService.bus.off('pendingChanges', updatePendingChanges)
})

const dirty = computed(() => hasPendingChanges.value
|| (document.value?.lastSavedVersion ?? 0) < versionWithChanges.value)
watch(dirty, setDirty)

provide(saveServiceKey, saveService)
Expand Down
5 changes: 3 additions & 2 deletions src/helpers/setInitialYjsState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ import markdownit from '../markdownit/index.ts'
* @param content desired content of the final document
* @param options options
* @param options.isRichEditor use a rich editor for the content
* @param options.origin origin of the update, e.g. the sync provider
*/
export function setInitialYjsState(
ydoc: Doc,
content: string,
{ isRichEditor }: { isRichEditor: boolean },
{ isRichEditor, origin }: { isRichEditor: boolean, origin?: unknown },
) {
const html = isRichEditor
? markdownit.render(content) + '<p/>'
Expand Down Expand Up @@ -54,5 +55,5 @@ export function setInitialYjsState(
}

const baseUpdate = encodeStateAsUpdate(getBaseDoc(node))
applyUpdate(ydoc, baseUpdate)
applyUpdate(ydoc, baseUpdate, origin)
}
21 changes: 19 additions & 2 deletions src/services/Outbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ export default class Outbox {
#syncQuery = ''
#recoveryAttemptCounter = 0
#isRecoveringSync = false
#onChange

/**
* @param options options
* @param options.onChange called when `hasUpdate` changes
*/
constructor({ onChange = () => { } }: { onChange?: (hasUpdate: boolean) => void } = {}) {
this.#onChange = onChange
}

storeStep(step: Uint8Array<ArrayBufferLike>) {
const encoded = encodeArrayBuffer(step)
Expand All @@ -29,7 +38,7 @@ export default class Outbox {
return
}
if (encoded < 'AQ') {
this.#syncUpdate = encoded
this.#setSyncUpdate(encoded)
return
}
this.#awarenessUpdate = encoded
Expand Down Expand Up @@ -58,6 +67,14 @@ export default class Outbox {
return !!this.#syncUpdate
}

#setSyncUpdate(update: string) {
const hadUpdate = this.hasUpdate
this.#syncUpdate = update
if (this.hasUpdate !== hadUpdate) {
this.#onChange(this.hasUpdate)
}
}

/*
* Clear data that has just been sent.
*
Expand All @@ -66,7 +83,7 @@ export default class Outbox {
*/
clearSentData({ steps, awareness }: Sendable) {
if (steps.includes(this.#syncUpdate)) {
this.#syncUpdate = ''
this.#setSyncUpdate('')
}
if (steps.includes(this.#syncQuery)) {
this.#syncQuery = ''
Expand Down
6 changes: 6 additions & 0 deletions src/services/SaveService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,22 @@ class SaveService {
lastSaveAttempt = 0
pendingAutosave = 0
getSaveData
pushChanges
autosave
clear

constructor({
connection,
getSaveData,
pushChanges = async () => {},
}: {
connection: ShallowRef<Connection | undefined>
getSaveData: () => SaveData
pushChanges?: () => Promise<void>
}) {
this.connection = connection
this.getSaveData = getSaveData
this.pushChanges = pushChanges
this.autosave = debounce(this._autosave.bind(this), AUTOSAVE_DEBOUNCE * 1000)
this.clear = this.clearAutosave.bind(this)
}
Expand All @@ -67,6 +71,8 @@ class SaveService {
logger.warn('Could not save due to missing connection')
return
}
// Save the version that includes the local changes
await this.pushChanges()
const data = this.getSaveData()
try {
this.lastSaveAttempt = Date.now()
Expand Down
8 changes: 7 additions & 1 deletion src/services/SyncService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ export declare type EventTypes = {
/* local yjs changes have been pushed to the server */
changesPushed: { version: number }

/* local yjs changes are waiting to be pushed to the server */
pendingChanges: { pending: boolean }

/* error */
error: { type: ErrorType, data?: object }

Expand All @@ -85,7 +88,10 @@ class SyncService {
pushError = 0
backend?: PollingBackend
#sendIntervalId?: NodeJS.Timeout
#outbox = new Outbox()
#outbox = new Outbox({
onChange: (hasUpdate) => this.bus.emit('pendingChanges', { pending: hasUpdate }),
})

bus = mitt<EventTypes>()
#openConnection: () => Promise<OpenData>
#lastStepPush = Date.now()
Expand Down
105 changes: 105 additions & 0 deletions src/tests/composables/useSaveService.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import type { Editor } from '@tiptap/core'
import type { SaveService } from '../../services/SaveService.ts'
import type { EventTypes, SyncService } from '../../services/SyncService.ts'
import type { Connection } from '../../types/Connection.ts'
import type { Document } from '../../types/Document.ts'

import { mount } from '@vue/test-utils'
import mitt from 'mitt'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { defineComponent, nextTick, shallowRef } from 'vue'
import { Doc } from 'yjs'
import * as saveApi from '../../apis/save.ts'
import { provideSaveService } from '../../composables/useSaveService.ts'

vi.mock('../../apis/save.ts')

const connection = shallowRef<Connection | undefined>({
documentId: '123',
sessionId: 345,
sessionToken: 'sessionToken',
filePath: './',
baseVersionEtag: 'etag',
})

/**
* Mount a component providing the save service with a stubbed sync service.
*
* @param options options
* @param options.version version on the stupped sync service
*/
function setup({ version = 2 } = {}) {
const syncService = {
bus: mitt<EventTypes>(),
version,
sendRemainingSteps: vi.fn(async () => {}),
}
const editor = { on: vi.fn(), off: vi.fn() }
const setDirty = vi.fn<(val: boolean) => Promise<void>>(async () => {})
let saveService: SaveService | undefined
mount(defineComponent({
template: '<div />',
setup() {
({ saveService } = provideSaveService(
connection,
editor as unknown as Editor,
syncService as unknown as SyncService,
() => 'content',
new Doc(),
setDirty as unknown as (val: boolean) => Promise<never>,
))
},
}))
const document = { lastSavedVersion: 1, lastSavedVersionTime: 0 } as Document
syncService.bus.emit('change', { document, sessions: [] })
return { syncService, setDirty, saveService: saveService! }
}

describe('provideSaveService', () => {
beforeEach(() => {
vi.useFakeTimers()
vi.mocked(saveApi.save).mockResolvedValue({
data: { document: { lastSavedVersion: 2, lastSavedVersionTime: 0 } },
} as Awaited<ReturnType<typeof saveApi.save>>)
})

afterEach(() => {
vi.useRealTimers()
vi.clearAllMocks()
})

it('is dirty while local changes are pending', async () => {
const { syncService, setDirty } = setup()
syncService.bus.emit('pendingChanges', { pending: true })
await nextTick()
expect(setDirty.mock.lastCall?.[0]).toBe(true)
syncService.bus.emit('pendingChanges', { pending: false })
await nextTick()
expect(setDirty.mock.lastCall?.[0]).toBe(false)
})

it('autosaves changes pushed without an editor update', async () => {
const { syncService } = setup()
syncService.bus.emit('changesPushed', { version: 2 })
await vi.advanceTimersByTimeAsync(1000)
expect(saveApi.save).toHaveBeenCalledWith(connection.value, expect.objectContaining({ version: 2 }))
})

it('does not autosave pushed changes that are already saved', async () => {
const { syncService } = setup()
syncService.bus.emit('changesPushed', { version: 1 })
await vi.advanceTimersByTimeAsync(1000)
expect(saveApi.save).not.toHaveBeenCalled()
})

it('saves version 0 before any steps were received', async () => {
const { saveService } = setup({ version: -1 })
await saveService.save()
expect(saveApi.save).toHaveBeenCalledWith(connection.value, expect.objectContaining({ version: 0 }))
})
})
73 changes: 73 additions & 0 deletions src/tests/helpers/setInitialYjsState.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import type { SyncService } from '../../services/SyncService.ts'

import * as decoding from 'lib0/decoding'
import mitt from 'mitt'
import { describe, expect, it, vi } from 'vitest'
import { applyUpdate, Doc, XmlText } from 'yjs'
import { setInitialYjsState } from '../../helpers/setInitialYjsState.ts'
import initWebSocketPolyfill from '../../services/WebSocketPolyfill.ts'
import { WebsocketProvider } from '../../services/y-websocket.js'

const content = '# Hello world\n\nold line :)\n'

/**
* Create a provider connected to a sync service that records the sent steps.
*/
async function setupProvider() {
const ydoc = new Doc()
const syncService = {
bus: mitt(),
open: vi.fn(async () => {}),
hasActiveConnection: () => true,
sendStep: vi.fn(),
}
const WebSocketPolyfill = initWebSocketPolyfill(syncService as unknown as SyncService)
const provider = new WebsocketProvider('ws://localhost:1234', 'file:123', ydoc, {
WebSocketPolyfill: WebSocketPolyfill as unknown as typeof WebSocket,
disableBc: true,
})
await vi.waitUntil(() => provider.wsconnected)
const sentUpdates = () => syncService.sendStep.mock.calls
.map(([step]) => decoding.createDecoder(step))
// sync message containing a sync step 2 or update
.filter((decoder) => decoding.readVarUint(decoder) === 0 && decoding.readVarUint(decoder) > 0)
.map((decoder) => decoding.readVarUint8Array(decoder))
return { ydoc, provider, sentUpdates }
}

describe('setInitialYjsState', () => {
it('applies the content with the given origin', () => {
const ydoc = new Doc()
const origin = {}
const onUpdate = vi.fn()
ydoc.on('update', onUpdate)
setInitialYjsState(ydoc, content, { isRichEditor: true, origin })
expect(onUpdate).toHaveBeenCalledOnce()
expect(onUpdate.mock.calls[0][1]).toBe(origin)
expect(ydoc.getXmlFragment('default').toString()).toContain('old line :)')
})

it('is not sent with the sync provider as origin', async () => {
const { ydoc, provider, sentUpdates } = await setupProvider()
setInitialYjsState(ydoc, content, { isRichEditor: true, origin: provider })
expect(sentUpdates()).toHaveLength(0)
provider.destroy()
})

it('is sent along with the first local change', async () => {
const { ydoc, provider, sentUpdates } = await setupProvider()
setInitialYjsState(ydoc, content, { isRichEditor: true, origin: provider })
ydoc.getXmlFragment('default').insert(0, [new XmlText('edit')])
expect(sentUpdates()).toHaveLength(1)
const remote = new Doc()
applyUpdate(remote, sentUpdates()[0])
expect(remote.getXmlFragment('default').toString())
.toBe(ydoc.getXmlFragment('default').toString())
provider.destroy()
})
})
Loading
Loading