Skip to content

AI-native design editor. Open-source Figma alternative.

fix: have the room's saver upload before another tab names a version (#1292)

Danila Poyarkova2d1bbc25182
diff --git a/packages/cloud/src/contract/relay.ts b/packages/cloud/src/contract/relay.ts
index ea10437f6..e606979e0 100644
--- a/packages/cloud/src/contract/relay.ts
+++ b/packages/cloud/src/contract/relay.ts
@@ -16,6 +16,7 @@ export const COLLAB_ACTIONS = {
syncReply: 'sync-reply',
awareness: 'awareness',
agentPreview: 'agent-preview',
+ upload: 'upload',
/** From the server only: the document's version history changed, so open lists load it again. */
versions: 'versions'
} as const
diff --git a/packages/docs/development/cloud.md b/packages/docs/development/cloud.md
index 152f01a76..61ac0815d 100644
--- a/packages/docs/development/cloud.md
+++ b/packages/docs/development/cloud.md
@@ -68,9 +68,11 @@ keeps on its own, and versions someone named (`document_version`, one per revisi
- In the editor, File → Show version history makes the document read-only and lists its versions in
the right sidebar, as Figma does. Choosing a version shows it in a read-only editor of its own in
place of the document, whose room keeps syncing underneath; Save to version history (⌥⌘S) names
- the document once the saver's edits are uploaded. When a version is named, renamed, or turned
- back into an autosave, or a save adds a checkpoint, the server sends `versions` to the
- document's rooms and every open history loads it again; peers cannot send it.
+ the document once it is uploaded. A tab that does not save asks the room's saver on the `upload`
+ channel, after its own edits, and names it when the saver answers that its upload finished. When
+ a version is named, renamed, or turned back into an autosave, or a save adds a checkpoint, the
+ server sends `versions` to the document's rooms and every open history loads it again; peers
+ cannot send it.
- A document's address is `/cloud/documents/:id?server=…`, with `&version=…` for a version; the
editor keeps the address bar on the active tab's document and the version it shows, and a
version's Copy link builds the address on the server's advertised editor URL. The address carries
diff --git a/src/app/cloud/rooms/live.ts b/src/app/cloud/rooms/live.ts
index 39c7439c9..5a993cd4c 100644
--- a/src/app/cloud/rooms/live.ts
+++ b/src/app/cloud/rooms/live.ts
@@ -22,6 +22,7 @@ import { isCloudBinding } from '../documents/status'
import { cloudConnections } from '../sessions/connection'
import { cloudShareTarget } from '../sharing/document'
import { versionListRevision } from '../versions/history'
+import { shareRoomUploads, uploadStoredDocument } from './upload'
/** Each Cloud tab's permission in its room, for the status beside the name. */
export const cloudRoomPermissions = shallowRef<ReadonlyMap<EditorStore, 'edit' | 'view'>>(new Map())
@@ -76,7 +77,7 @@ function followSaver(
session: RoomSession,
canSave: boolean,
roomIsCurrent: () => Promise<boolean>
-): () => void {
+): { stop: () => void; saving: () => boolean } {
let saving: boolean | null = null
const documentId = () => store.getStorageBinding()?.documentId ?? null
@@ -119,10 +120,13 @@ function followSaver(
const id = documentId()
if (id && event.documentId === id) void settleConflict(id)
})
- return () => {
- stopPeers()
- stopDirty()
- stopEvents()
+ return {
+ stop() {
+ stopPeers()
+ stopDirty()
+ stopEvents()
+ },
+ saving: () => saving === true
}
}
@@ -144,6 +148,8 @@ export function openCloudDocumentRoom(
*/
roomIsCurrent?: () => Promise<boolean>
onRestored?: () => void
+ /** How the saver gets the document onto the server; the tab's own upload unless a test stands in. */
+ uploadNow?: () => Promise<boolean>
}
) {
const canEdit = options.permission === 'edit'
@@ -163,17 +169,30 @@ export function openCloudDocumentRoom(
if (canEdit && !session.roomHasDocument()) session.shareDocument()
})
// Unsure whether the document moved on, the saver waits rather than overwrite a restore.
- const stopSaver = followSaver(store, session, canSave, async () => {
+ const saver = followSaver(store, session, canSave, async () => {
const current = options.roomIsCurrent ? await options.roomIsCurrent().catch(() => null) : true
if (current === false) options.onRestored?.()
return current === true
})
+ const uploads = shareRoomUploads(session.upload, {
+ saving: saver.saving,
+ saverPeer: () => {
+ const elected = electCloudSaver(session.presence(), { clientId: session.clientId, canSave })
+ return elected === null || elected === session.clientId
+ ? null
+ : (session.peerForClient(elected) ?? null)
+ },
+ upload: options.uploadNow ?? (() => uploadStoredDocument(store))
+ })
return {
session,
+ /** Whether the server now holds the room's document with every edit this tab has seen. */
+ upload: uploads.upload,
stop() {
stopVersions()
stopSynced()
- stopSaver()
+ saver.stop()
+ uploads.stop()
setPermission(store, null)
store.setReadOnly(false)
}
@@ -204,11 +223,13 @@ export function cloudRelayTransport(
})
}
+type LiveRoom = Pick<ReturnType<typeof openCloudDocumentRoom>, 'stop' | 'upload'>
+
/** Each tab's live Cloud room, stopped when the tab closes or the room starts over. */
-const liveRooms = new Map<EditorStore, { stop(): void }>()
+const liveRooms = new Map<EditorStore, LiveRoom>()
/** Leaves the room's saving and status behind once its tab closes. */
-export function stopWithTab(store: EditorStore, room: { stop(): void }): void {
+export function stopWithTab(store: EditorStore, room: LiveRoom): void {
liveRooms.set(store, room)
const stopLeaving = watch(allTabs, () => {
if (roomForStore(store)) return
@@ -251,6 +272,14 @@ export async function startCloudRoom(store: EditorStore): Promise<void> {
}
}
+/**
+ * Gets the document of `store` onto its server as the tab shows it: through the room's saver
+ * while the tab is in a room, or by uploading it itself. Whether it got there.
+ */
+export function uploadCloudDocument(store: EditorStore): Promise<boolean> {
+ return liveRooms.get(store)?.upload() ?? uploadStoredDocument(store)
+}
+
const restarting = new WeakSet<EditorStore>()
/**
diff --git a/src/app/cloud/rooms/upload.ts b/src/app/cloud/rooms/upload.ts
new file mode 100644
index 000000000..98e6bc88a
--- /dev/null
+++ b/src/app/cloud/rooms/upload.ts
@@ -0,0 +1,105 @@
+import * as v from 'valibot'
+
+import type { CollabAction } from '@/app/collab/transport/types'
+import type { EditorStore } from '@/app/editor/session'
+import { followUpload, kickSyncEngine } from '@/app/storage/sync'
+
+/** How long a tab waits for the room's saver to say the document is on its server. */
+export const ROOM_UPLOAD_TIMEOUT_MS = 60_000
+
+const id = v.pipe(v.string(), v.minLength(1), v.maxLength(64))
+const UploadMessageSchema = v.pipe(
+ v.string(),
+ v.parseJson(),
+ v.variant('type', [
+ v.object({ type: v.literal('request'), id }),
+ v.object({ type: v.literal('reply'), id, uploaded: v.boolean() })
+ ])
+)
+type UploadMessage = v.InferOutput<typeof UploadMessageSchema>
+
+const encoder = new TextEncoder()
+const decoder = new TextDecoder()
+
+function encode(message: UploadMessage): Uint8Array {
+ return encoder.encode(JSON.stringify(message))
+}
+
+/**
+ * Saves what the tab of `store` has not saved yet, as Save does, and follows the document onto
+ * the server, an earlier save still on its way included. Whether the document there is now what
+ * the tab shows.
+ */
+export async function uploadStoredDocument(store: EditorStore): Promise<boolean> {
+ const documentId = store.getStorageBinding()?.documentId
+ if (!documentId) return false
+ if (store.hasUnsavedChanges()) {
+ await store.saveFigFile()
+ await kickSyncEngine()
+ }
+ return (await followUpload(documentId)) === 'uploaded'
+}
+
+/**
+ * Lets the tabs of a room get its document onto the server as it is now. The saver uploads when
+ * asked; anyone else asks the saver, after the edits it sent, and the saver answers once the
+ * upload is done, so the server holds everything that reached the saver before the question.
+ * Only the saver's answer counts: another peer cannot say the upload finished.
+ */
+export function shareRoomUploads(
+ [send, receive]: CollabAction,
+ options: {
+ /** Whether this tab saves the room's document. */
+ saving: () => boolean
+ /** The peer of the tab that saves for the room, when that is another tab. */
+ saverPeer: () => string | null
+ upload: () => Promise<boolean>
+ timeoutMs?: number
+ }
+) {
+ const { saving, saverPeer, upload, timeoutMs = ROOM_UPLOAD_TIMEOUT_MS } = options
+ const waiting = new Map<string, { peer: string; answer: (uploaded: boolean) => void }>()
+
+ receive((data, peerId) => {
+ const parsed = v.safeParse(UploadMessageSchema, decoder.decode(data))
+ if (!parsed.success) return
+ const message = parsed.output
+ if (message.type === 'reply') {
+ const asked = waiting.get(message.id)
+ if (asked?.peer === peerId) asked.answer(message.uploaded)
+ return
+ }
+ // A tab that stopped saving since the question was asked says so rather than leave it waiting.
+ if (!saving()) {
+ send(encode({ type: 'reply', id: message.id, uploaded: false }), peerId)
+ return
+ }
+ void upload()
+ .catch(() => false)
+ .then((uploaded) => send(encode({ type: 'reply', id: message.id, uploaded }), peerId))
+ })
+
+ function ask(): Promise<boolean> {
+ const peer = saverPeer()
+ if (!peer) return Promise.resolve(false)
+ const request = crypto.randomUUID()
+ const answer = new Promise<boolean>((resolve) => {
+ waiting.set(request, { peer, answer: resolve })
+ })
+ const timer = setTimeout(() => waiting.get(request)?.answer(false), timeoutMs)
+ // Edits made in this task reach the room in a microtask; the question has to follow them.
+ queueMicrotask(() => send(encode({ type: 'request', id: request }), peer))
+ return answer.finally(() => {
+ clearTimeout(timer)
+ waiting.delete(request)
+ })
+ }
+
+ return {
+ /** Whether the room's document is on the server with every edit this tab has seen. */
+ upload: () => (saving() ? upload() : ask()),
+ stop() {
+ for (const { answer } of waiting.values()) answer(false)
+ }
+ }
+}
diff --git a/src/app/cloud/versions/actions.ts b/src/app/cloud/versions/actions.ts
index 94a31f9ce..40fd04027 100644
--- a/src/app/cloud/versions/actions.ts
+++ b/src/app/cloud/versions/actions.ts
@@ -6,10 +6,9 @@ import type {
} from '@open-pencil/cloud/contract'
import type { EditorStore } from '@/app/editor/session'
-import { followUpload, kickSyncEngine } from '@/app/storage/sync'
import { openStorageDocumentInNewTab } from '@/app/tabs'
-import { restartCloudRoom } from '../rooms/live'
+import { restartCloudRoom, uploadCloudDocument } from '../rooms/live'
import { cloudShareTarget } from '../sharing/document'
import { closeVersionHistory, versionHistoryOf } from './history'
@@ -34,20 +33,16 @@ export async function listVersions(store: EditorStore, before?: string): Promise
}
/**
- * Names the document as it is now. The saver's unsaved edits are uploaded first, so the version
- * holds what is on screen.
+ * Names the document as it is now. Edits not on the server yet are uploaded first, by the room's
+ * saver when another tab saves for the room, so the version holds what is on screen.
*/
export async function saveVersion(
store: EditorStore,
input: SaveDocumentVersionInput
): Promise<DocumentVersion> {
const { document, target } = targetOf(store)
- if (document.state.autosaveEnabled && document.hasUnsavedChanges()) {
- await document.saveFigFile()
- await kickSyncEngine()
- if ((await followUpload(target.documentId)) !== 'uploaded') {
- throw new Error('The document could not be saved before naming the version')
- }
+ if (!(await uploadCloudDocument(document))) {
+ throw new Error('The document could not be saved before naming the version')
}
return target.client.saveDocumentVersion(target.documentId, input)
}
diff --git a/src/app/collab/room/connection.ts b/src/app/collab/room/connection.ts
index ea866dbc2..6eda5deb8 100644
--- a/src/app/collab/room/connection.ts
+++ b/src/app/collab/room/connection.ts
@@ -30,6 +30,8 @@ export type CollabRoomConnection = {
sendSyncStep1: (data: Uint8Array, peerId?: string) => void
/** What agents preview while they stream, apart from presence (`app/collab/agent-preview.ts`). */
agentPreview: CollabAction
+ /** Asking whoever saves the room's document to upload it now (`app/cloud/rooms/upload.ts`). */
+ upload: CollabAction
/** Follow peers leaving the room; returns the unsubscribe. */
onPeerLeave: (handler: (peerId: string) => void) => () => void
/** The peer whose awareness carries a presence client, so media reaches the right person. */
@@ -96,6 +98,7 @@ export function connectCollabRoom({
const [sendSyncStep1, getSyncStep1] = room.makeAction(COLLAB_ACTIONS.syncStep1)
const [sendSyncReply, getSyncReply] = room.makeAction(COLLAB_ACTIONS.syncReply)
const agentPreview = room.makeAction(COLLAB_ACTIONS.agentPreview)
+ const upload = room.makeAction(COLLAB_ACTIONS.upload)
const [, getVersions] = room.makeAction(COLLAB_ACTIONS.versions)
// A transport keeps one leave handler, so the connection shares it with its users.
const leaveHandlers = new Set<(peerId: string) => void>()
@@ -170,6 +173,7 @@ export function connectCollabRoom({
sendAwareness,
sendSyncStep1,
agentPreview,
+ upload,
onPeerLeave(handler) {
leaveHandlers.add(handler)
return () => leaveHandlers.delete(handler)
diff --git a/src/app/collab/session.ts b/src/app/collab/session.ts
index a60bd7977..062de946b 100644
--- a/src/app/collab/session.ts
+++ b/src/app/collab/session.ts
@@ -18,6 +18,7 @@ import {
type YNodes
} from '@/app/collab/shared-tree/fields'
import type { JoinCollabRoom } from '@/app/collab/transport'
+import type { CollabAction } from '@/app/collab/transport/types'
import type { RemotePeer } from '@/app/collab/types'
import { bindVariableSync, type YRecords } from '@/app/collab/variables'
import { attachRoomVoice, type RoomVoice } from '@/app/collab/voice/room'
@@ -57,6 +58,10 @@ export interface RoomSession {
presence(): ReadonlyMap<number, Record<string, unknown>>
/** This tab's awareness client. */
readonly clientId: number
+ /** The peer whose presence carries a client, to address it alone. */
+ peerForClient(clientId: number): string | undefined
+ /** The room's channel for asking whoever saves its document to upload it now. */
+ readonly upload: CollabAction
/** Leaves the room and releases its connection, presence, and saved-copy handle. */
dispose(): void
}
@@ -256,6 +261,8 @@ export function openRoomSession({
onVersionsChanged: (handler) => connection.onVersionsChanged(handler),
presence: () => awareness.getStates() as Map<number, Record<string, unknown>>,
clientId: awareness.clientID,
+ peerForClient: (clientId) => connection.peerForClient(clientId),
+ upload: connection.upload,
dispose() {
if (disposed) return
// Unbinding writes an edit still waiting to be sent, so it runs while the room is open.
diff --git a/tests/app/cloud/rooms/upload.test.ts b/tests/app/cloud/rooms/upload.test.ts
new file mode 100644
index 000000000..886a5a082
--- /dev/null
+++ b/tests/app/cloud/rooms/upload.test.ts
@@ -0,0 +1,197 @@
+import { afterEach, beforeEach, describe, expect, test } from 'bun:test'
+
+import * as v from 'valibot'
+
+import { shareRoomUploads, uploadStoredDocument } from '@/app/cloud/rooms/upload'
+import type { CollabAction, CollabActionReceiver } from '@/app/collab/transport/types'
+import type { EditorStore } from '@/app/editor/session'
+import { createMemoryLocalCanvasStore, type LocalCanvasStore } from '@/app/storage/local-store'
+import { resetLocalCanvasStoreForTests } from '@/app/storage/local-store/store'
+import { createMemoryOutbox, resetOutboxForTests } from '@/app/storage/sync'
+import { emitStorageWorkspaceEvent } from '@/app/storage/workspace/events'
+
+import { asDouble } from '#tests/helpers/doubles'
+
+/**
+ * A room of tabs whose channel delivers to everyone else, or to one tab when named; `overheard`
+ * delivers everything to everyone, as if a peer could read what was meant for another.
+ */
+function room({ overheard = false } = {}) {
+ const receivers = new Map<string, CollabActionReceiver>()
+ const tab = (peerId: string): CollabAction => [
+ (data, target) => {
+ for (const [other, receive] of receivers) {
+ if (other !== peerId && (overheard || !target || target === other)) receive(data, peerId)
+ }
+ },
+ (handler) => {
+ receivers.set(peerId, handler)
+ }
+ ]
+ return { tab }
+}
+
+const encoder = new TextEncoder()
+const decoder = new TextDecoder()
+const QuestionSchema = v.pipe(v.string(), v.parseJson(), v.object({ id: v.string() }))
+
+describe('room uploads', () => {
+ test('the saver uploads itself; anyone else hears back from the saver', async () => {
+ const { tab } = room()
+ const calls: string[] = []
+ const saver = shareRoomUploads(tab('saver'), {
+ saving: () => true,
+ saverPeer: () => null,
+ upload: async () => {
+ calls.push('saver')
+ return true
+ }
+ })
+ const other = shareRoomUploads(tab('other'), {
+ saving: () => false,
+ saverPeer: () => 'saver',
+ upload: async () => {
+ calls.push('other')
+ return true
+ }
+ })
+ expect(await saver.upload()).toBe(true)
+ expect(await other.upload()).toBe(true)
+ expect(calls).toEqual(['saver', 'saver'])
+ })
+
+ test('only the saver’s answer counts', async () => {
+ const { tab } = room({ overheard: true })
+ let finish: (uploaded: boolean) => void = () => undefined
+ shareRoomUploads(tab('saver'), {
+ saving: () => true,
+ saverPeer: () => null,
+ upload: () =>
+ new Promise((resolve) => {
+ finish = resolve
+ })
+ })
+ // Another editor overhears the question and answers it at once.
+ const [forge, listen] = tab('forger')
+ listen((data, peerId) => {
+ const question = v.safeParse(QuestionSchema, decoder.decode(data))
+ if (!question.success) return
+ const reply = { type: 'reply', id: question.output.id, uploaded: true }
+ forge(encoder.encode(JSON.stringify(reply)), peerId)
+ })
+ const asking = shareRoomUploads(tab('other'), {
+ saving: () => false,
+ saverPeer: () => 'saver',
+ upload: async () => true
+ })
+ let settled = false
+ const answer = asking.upload().finally(() => {
+ settled = true
+ })
+ await new Promise((resolve) => {
+ setTimeout(resolve, 0)
+ })
+ expect(settled).toBe(false)
+ finish(false)
+ expect(await answer).toBe(false)
+ })
+
+ test('a tab that no longer saves answers at once rather than leave the question waiting', async () => {
+ const { tab } = room()
+ shareRoomUploads(tab('saver'), {
+ saving: () => false,
+ saverPeer: () => null,
+ upload: async () => true
+ })
+ const asking = shareRoomUploads(tab('other'), {
+ saving: () => false,
+ saverPeer: () => 'saver',
+ upload: async () => true,
+ timeoutMs: 60_000
+ })
+ expect(await asking.upload()).toBe(false)
+ })
+
+ test('a failed upload, or no saver to ask, says the document is not there', async () => {
+ const failing = room()
+ shareRoomUploads(failing.tab('saver'), {
+ saving: () => true,
+ saverPeer: () => null,
+ upload: () => Promise.reject(new Error('Offline'))
+ })
+ const asking = shareRoomUploads(failing.tab('other'), {
+ saving: () => false,
+ saverPeer: () => 'saver',
+ upload: async () => true
+ })
+ expect(await asking.upload()).toBe(false)
+
+ const empty = room()
+ const alone = shareRoomUploads(empty.tab('other'), {
+ saving: () => false,
+ saverPeer: () => null,
+ upload: async () => true
+ })
+ expect(await alone.upload()).toBe(false)
+
+ const silent = shareRoomUploads(empty.tab('waiting'), {
+ saving: () => false,
+ saverPeer: () => 'gone',
+ upload: async () => true,
+ timeoutMs: 10
+ })
+ expect(await silent.upload()).toBe(false)
+ })
+
+ test('leaving the room ends what this tab was waiting for', async () => {
+ const { tab } = room()
+ const uploads = shareRoomUploads(tab('other'), {
+ saving: () => false,
+ saverPeer: () => 'saver',
+ upload: async () => true
+ })
+ const waiting = uploads.upload()
+ uploads.stop()
+ expect(await waiting).toBe(false)
+ })
+})
+
+describe('uploading the document a tab shows', () => {
+ let local: LocalCanvasStore
+
+ beforeEach(async () => {
+ local = createMemoryLocalCanvasStore()
+ resetLocalCanvasStoreForTests(local)
+ resetOutboxForTests(createMemoryOutbox())
+ await local.writeCanvas({
+ id: 'doc',
+ providerId: 'cloud',
+ name: 'Poster',
+ figBytes: new Uint8Array([1]),
+ syncStatus: 'pending'
+ })
+ })
+
+ afterEach(() => {
+ resetLocalCanvasStoreForTests()
+ resetOutboxForTests()
+ })
+
+ test('waits for a save still on its way, though nothing is left unsaved', async () => {
+ const store = asDouble<EditorStore>({
+ getStorageBinding: () => ({ documentId: 'doc' }),
+ hasUnsavedChanges: () => false
+ })
+ let settled = false
+ const uploading = uploadStoredDocument(store).finally(() => {
+ settled = true
+ })
+ await new Promise((resolve) => {
+ setTimeout(resolve, 0)
+ })
+ expect(settled).toBe(false)
+ await local.updateMeta('doc', { syncStatus: 'synced' })
+ emitStorageWorkspaceEvent({ providerId: 'cloud', documentId: 'doc', kind: 'changed' })
+ expect(await uploading).toBe(true)
+ })
+})
diff --git a/tests/integration/collab/cloud-relay.test.ts b/tests/integration/collab/cloud-relay.test.ts
index 4dffa6e19..58060172a 100644
--- a/tests/integration/collab/cloud-relay.test.ts
+++ b/tests/integration/collab/cloud-relay.test.ts
@@ -200,6 +200,42 @@ describe('Cloud document rooms', () => {
await until(() => first.session.peers.value.length > 0)
expect(compact([grace.state.autosaveEnabled, ada.state.autosaveEnabled])).toHaveLength(1)
})
+
+ test('a tab that does not save has the saver upload its latest edit', async () => {
+ const relay = await startRelay()
+ const documentId = crypto.randomUUID()
+ const roomId = `cloud:${documentId}:0`
+ const [grace, ada] = await openedTwice()
+ const uploaded: boolean[] = []
+ const open = (store: typeof grace, name: string) => {
+ const room = openCloudDocumentRoom(store, {
+ roomId,
+ transport: relay.join(documentId, name, 'edit', 'user'),
+ permission: 'edit',
+ uploadNow: async () => {
+ uploaded.push(store.graph.getNode('late:edit') !== undefined)
+ return true
+ }
+ })
+ cleanups.push(() => {
+ room.stop()
+ room.session.dispose()
+ })
+ return room
+ }
+ const first = open(grace, 'Grace')
+ await until(() => first.session.roomHasDocument())
+ const second = open(ada, 'Ada')
+ await until(() => first.session.peers.value.length > 0 && second.session.peers.value.length > 0)
+ await until(() => grace.state.autosaveEnabled !== ada.state.autosaveEnabled)
+
+ const [other, room] = grace.state.autosaveEnabled ? [ada, second] : [grace, first]
+ const page = other.graph.getPages()[0]
+ if (!page) throw new Error('Expected a page')
+ other.graph.createNode('ELLIPSE', page.id, { id: 'late:edit', name: 'Dot' })
+ expect(await room.upload()).toBe(true)
+ expect(uploaded).toEqual([true])
+ })
})
describe('Cloud document rooms, live order', () => {