From 1d36f89cfc85c89bd59e1f27f6c302af90285e74 Mon Sep 17 00:00:00 2001 From: Lucas Vieira Date: Fri, 6 Dec 2024 13:13:26 -0300 Subject: [PATCH] merge yjs updates before persisting --- apps/api/src/yjs/v2/index.ts | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/apps/api/src/yjs/v2/index.ts b/apps/api/src/yjs/v2/index.ts index 454d16e..171f092 100644 --- a/apps/api/src/yjs/v2/index.ts +++ b/apps/api/src/yjs/v2/index.ts @@ -682,15 +682,30 @@ export class WSSharedDocV2 { 'Enqueuing Yjs update for publishing' ) await this.serialUpdatesQueue.add(async () => { - const updates = this.pendingUpdates + let updates = this.pendingUpdates this.pendingUpdates = [] if (updates.length === 0) { return } + try { + const update = Y.mergeUpdates(updates) + updates = [update] + } catch (err) { + logger().error( + { + id: this.id, + documentId: this.documentId, + workspaceId: this.workspaceId, + err, + }, + 'Failed to merge Yjs updates, persisting individually' + ) + } + const pubsubChannel = this.getPubSubChannel() try { - const updateIds = await this.persistor.persistUpdates(this, updates) + const updateIds = await this.persistor.persistUpdates(this, [update]) for (const updateId of updateIds) { await publish( pubsubChannel,