Skip to content

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

fix(cloud): skip room updates that change nothing (#1339)

Danila Poyarkov07d7da236c29
diff --git a/src/app/collab/yjs-sync.ts b/src/app/collab/yjs-sync.ts
index ebecc95ed..62824e835 100644
--- a/src/app/collab/yjs-sync.ts
+++ b/src/app/collab/yjs-sync.ts
@@ -1,3 +1,4 @@
+import { isEmptyObject, isEqual, pickBy } from 'es-toolkit'
import * as Y from 'yjs'
import type { SceneGraph } from '@open-pencil/scene-graph'
@@ -321,8 +322,14 @@ export function createYjsGraphSync({
/** Applies a layer's own fields; `applySharedTree` places it in the tree afterwards. */
function applyYnodeToGraph(graph: SceneGraph, nodeId: string, ynode: Y.Map<unknown>) {
const props = decodeNodeFromYjs(ynode)
- if (graph.getNode(nodeId)) {
- graph.updateNode(nodeId, props)
+ const node = graph.getNode(nodeId)
+ if (node) {
+ // Only what differs: joining a room replays every layer, and a layer that already reads the
+ // same is no change, so it neither marks the document unsaved nor costs a redraw. The text
+ // picture is a cache the decoder clears, so it goes along only with a real change.
+ const { textPicture, ...fields } = props
+ const changes = pickBy(fields, (value, key) => !isEqual(Reflect.get(node, key), value))
+ if (!isEmptyObject(changes)) graph.updateNode(nodeId, { ...changes, textPicture })
return
}
const type = props.type
diff --git a/tests/integration/collab/cloud-relay.test.ts b/tests/integration/collab/cloud-relay.test.ts
index 0209eb7ba..f03002448 100644
--- a/tests/integration/collab/cloud-relay.test.ts
+++ b/tests/integration/collab/cloud-relay.test.ts
@@ -313,6 +313,48 @@ describe('Cloud document rooms, another room first', () => {
})
})
+describe('Cloud document rooms, opening', () => {
+ test('a tab whose layers the room holds as they are gets no updates and stays saved', async () => {
+ const relay = await startRelay()
+ const documentId = crypto.randomUUID()
+ const roomId = `cloud:${documentId}:0`
+ const [grace, ada] = await openedTwice()
+ const first = openCloudDocumentRoom(grace, {
+ roomId,
+ transport: relay.join(documentId, 'Grace', 'edit', 'user'),
+ permission: 'edit'
+ })
+ cleanups.push(() => {
+ first.stop()
+ first.session.dispose()
+ })
+ await until(() => first.session.roomHasDocument())
+ await until(() => ada.state.preparation === null)
+ // The room replays every layer to a newcomer; layers that already read the same are no edit.
+ const updated: string[] = []
+ ada.onEditorEvent('node:updated', (id) => updated.push(id))
+ const second = openCloudDocumentRoom(ada, {
+ roomId,
+ transport: relay.join(documentId, 'Ada', 'edit', 'user'),
+ permission: 'edit'
+ })
+ // The reply is applied before onSynced fires, so the check below sees the replay.
+ let synced = false
+ cleanups.push(
+ second.session.onSynced(() => {
+ synced = true
+ })
+ )
+ cleanups.push(() => {
+ second.stop()
+ second.session.dispose()
+ })
+ await until(() => synced)
+ expect(updated).toEqual([])
+ expect(ada.hasUnsavedChanges()).toBe(false)
+ })
+})
+
/** Tabs that each opened the same stored file, which gives each its own root. */
async function openedTwice() {
await initCodec()