From 09e3cc2b6b8f3c22f6145f1f8cd7853fb3a6fe51 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 20 Oct 2026 01:35:34 +0000 Subject: [PATCH 1/3] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Optimize=20cursor=20dec?= =?UTF-8?q?oration=20DOM=20recycling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Replaced global `decorationGeneration` with stable widget keys - Switched to using factory functions `() => HTMLElement` for `Decoration.widget` - Integrated dynamic states (`opacity`, `color`, `connectionState`) into widget keys to ensure ProseMirror correctly triggers updates instead of constant DOM recreation This optimization reduces significant garbage collection overhead and layout thrashing. Co-authored-by: dezren39 <11225574+dezren39@users.noreply.github.com> --- .jules/bolt.md | 5 ++ pkgs/id/web/src/cursors.ts | 134 ++++++++++++++++++------------------- 2 files changed, 71 insertions(+), 68 deletions(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000000..beda28217a --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,5 @@ +## 2024-05-24 - Initial Bolt Journal + +## 2024-05-24 - ProseMirror DOM Recycling Optimization +**Learning:** The codebase intentionally bypassed ProseMirror's built-in DOM node recycling by using a global `decorationGeneration` counter in the `key` for `Decoration.widget`, which caused unnecessary widget recreation on every state update and resulted in layout thrashing. +**Action:** When creating ProseMirror widgets, always use the factory function pattern `() => HTMLElement` for `Decoration.widget` and ensure `key` strings are derived from stable and relevant visual data properties (like ID, opacity, color, and connectionState) instead of using random/incremental generation values. diff --git a/pkgs/id/web/src/cursors.ts b/pkgs/id/web/src/cursors.ts index b127290321..b0e53c5b98 100644 --- a/pkgs/id/web/src/cursors.ts +++ b/pkgs/id/web/src/cursors.ts @@ -40,9 +40,6 @@ const RECONNECT_CLEANUP_DELAY_MS = 3_000; // Wait 3s after Init before cleanup ( const HOVER_RESTORE_DELAY_MS = 1_000; // 1s delay before restoring after hover const PENDING_REMOVAL_MS = 5_000; // Show fading cursor for 5s after disconnect -// Global counter to force widget recreation on every decoration call -let decorationGeneration = 0; - export interface CursorInfo { clientID: string | number; head: number; @@ -457,10 +454,6 @@ function createCursorDecorations( const now = Date.now(); const localHead = state.selection.head; - // Increment generation to force widget recreation - decorationGeneration++; - const gen = decorationGeneration; - // Normalize myClientID to number for comparison (handles string/number mismatch) const myClientIDNum = myClientID !== null ? Number(myClientID) : null; @@ -647,16 +640,21 @@ function createCursorDecorations( // Create own cursor decoration if present in cluster if (clusterHasOwnCursor && ownCursor) { - const ownCursorLine = document.createElement("span"); - ownCursorLine.className = "collab-cursor collab-cursor-own"; - ownCursorLine.style.borderColor = ownCursor.color; - ownCursorLine.setAttribute("data-client-id", String(ownCursor.clientID)); - decorations.push( - Decoration.widget(ownCursor.head, ownCursorLine, { - side: 1, - key: `cursor-own-${ownCursor.clientID}-g${gen}`, - }), + Decoration.widget( + ownCursor.head, + () => { + const ownCursorLine = document.createElement("span"); + ownCursorLine.className = "collab-cursor collab-cursor-own"; + ownCursorLine.style.borderColor = ownCursor.color; + ownCursorLine.setAttribute("data-client-id", String(ownCursor.clientID)); + return ownCursorLine; + }, + { + side: 1, + key: `cursor-own-${ownCursor.clientID}-${ownCursor.color}`, + }, + ), ); } @@ -677,73 +675,73 @@ function createCursorDecorations( const leftmostGroup = groupsForBar.find((g) => g.position === leftmostPos); let leftmostCursorLine: HTMLElement | null = null; - // Create a cluster-wide key that includes ALL client IDs AND positions in the cluster - // This ensures widgets are recreated when cluster membership OR positions change + // Create a cluster-wide key that includes ALL client IDs, positions, opacities, and connection state in the cluster + // This ensures widgets are recreated when cluster membership, positions, or dynamic states change const clusterKey = groupsForBar - .flatMap((g) => g.cursors.map((c) => `${c.clientID}@${c.head}`)) + .flatMap((g) => g.cursors.map((c) => `${c.clientID}@${c.head}-${c.opacity}-${c.color}-${connectionState}`)) .sort() .join("-"); // Create cursor lines for each position group (no attached labels) groupsForBar.forEach((group) => { - const firstCursor = group.cursors[0]; - const allClientIDs = group.cursors.map((c) => c.clientID); - const cursorLine = createCursorLine(firstCursor, true, connectionState, allClientIDs); - cursorLine.style.borderColor = group.mostRecentColor; - - // For merged cursor lines with bars, use full opacity on the line - // (the bar segments handle their own individual opacities) - if (group === leftmostGroup) { - cursorLine.style.opacity = "1"; - cursorLine.style.setProperty("--base-opacity", "1"); - cursorLine.style.animation = "none"; // No strobe on bar container - } - - // Track the leftmost cursor line for bar attachment - if (group === leftmostGroup) { - leftmostCursorLine = cursorLine; - } + decorations.push( + Decoration.widget( + group.position, + () => { + const firstCursor = group.cursors[0]; + const allClientIDs = group.cursors.map((c) => c.clientID); + const cursorLine = createCursorLine(firstCursor, true, connectionState, allClientIDs); + cursorLine.style.borderColor = group.mostRecentColor; + + // For merged cursor lines with bars, use full opacity on the line + // (the bar segments handle their own individual opacities) + if (group.position === leftmostPos) { + cursorLine.style.opacity = "1"; + cursorLine.style.setProperty("--base-opacity", "1"); + cursorLine.style.animation = "none"; // No strobe on bar container + + // Create merged tooltip bar and attach to leftmost cursor line + // (appending to cursor line ensures proper positioning via position: relative) + const mergedBar = createMergedBar(groupsForBar, handleMouseEnter, handleMouseLeave, connectionState); + cursorLine.appendChild(mergedBar); + } - // Register all cursors in this group for strobe tracking - if (viewState) { - group.cursors.forEach((cursor) => { - viewState.strobeInfos.set(cursor.clientID, { - element: cursorLine, - baseOpacity: cursor.opacity, - paused: false, - }); - }); - } + // Register all cursors in this group for strobe tracking + if (viewState) { + group.cursors.forEach((cursor) => { + viewState.strobeInfos.set(cursor.clientID, { + element: cursorLine, + baseOpacity: cursor.opacity, + paused: false, + }); + }); + } - // Use cluster-wide key + generation to force recreation on every update - decorations.push( - Decoration.widget(group.position, cursorLine, { - side: 1, - key: `cursor-cluster-${group.position}-${clusterKey}-g${gen}`, - }), + return cursorLine; + }, + { + side: 1, + key: `cursor-cluster-${group.position}-${clusterKey}`, + }, + ), ); }); - - // Create merged tooltip bar and attach to leftmost cursor line - // (appending to cursor line ensures proper positioning via position: relative) - const mergedBar = createMergedBar(groupsForBar, handleMouseEnter, handleMouseLeave, connectionState); - - if (leftmostCursorLine) { - (leftmostCursorLine as HTMLElement).appendChild(mergedBar); - } } else { // Single remote cursor, no overlap - simple standalone cursor const group = groupsForBar[0]; const cursor = group.cursors[0]; - const cursorWidget = viewState - ? createStandaloneCursor(cursor, connectionState, viewState, handleMouseEnter, handleMouseLeave) - : createCursorLine(cursor, false, connectionState); decorations.push( - Decoration.widget(cursor.head, cursorWidget, { - side: 1, - key: `cursor-${cursor.clientID}-g${gen}`, - }), + Decoration.widget( + cursor.head, + () => viewState + ? createStandaloneCursor(cursor, connectionState, viewState, handleMouseEnter, handleMouseLeave) + : createCursorLine(cursor, false, connectionState), + { + side: 1, + key: `cursor-${cursor.clientID}-${cursor.opacity}-${cursor.color}-${connectionState}`, + }, + ), ); } }); @@ -765,7 +763,7 @@ function createCursorDecorations( style: `background-color: ${cursor.color}${selectionOpacity};`, }, { - key: `selection-${cursor.clientID}-g${gen}`, + key: `selection-${cursor.clientID}-${cursor.opacity}-${cursor.color}`, }, ), ); From 47b4ed29e8bd4793c9bef8af6f5f7348ba31ff75 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 20 Oct 2026 01:39:11 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Format=20.jules/bolt.md?= =?UTF-8?q?=20and=20fix=20treefmt=20issue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Formatted the bolt.md file with Prettier to resolve the treefmt check failure in CI. Co-authored-by: dezren39 <11225574+dezren39@users.noreply.github.com> --- .jules/bolt.md | 1 + 1 file changed, 1 insertion(+) diff --git a/.jules/bolt.md b/.jules/bolt.md index beda28217a..ba804789d7 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -1,5 +1,6 @@ ## 2024-05-24 - Initial Bolt Journal ## 2024-05-24 - ProseMirror DOM Recycling Optimization + **Learning:** The codebase intentionally bypassed ProseMirror's built-in DOM node recycling by using a global `decorationGeneration` counter in the `key` for `Decoration.widget`, which caused unnecessary widget recreation on every state update and resulted in layout thrashing. **Action:** When creating ProseMirror widgets, always use the factory function pattern `() => HTMLElement` for `Decoration.widget` and ensure `key` strings are derived from stable and relevant visual data properties (like ID, opacity, color, and connectionState) instead of using random/incremental generation values. From b364fc13cdb1e8d0fbf947e22b3f69ce5abf29e7 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 20 Oct 2026 02:26:19 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Format=20cursors.ts=20w?= =?UTF-8?q?ith=20Biome?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Resolved formatting errors caught by the biome-check CI. Co-authored-by: dezren39 <11225574+dezren39@users.noreply.github.com> --- pkgs/id/web/src/cursors.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pkgs/id/web/src/cursors.ts b/pkgs/id/web/src/cursors.ts index b0e53c5b98..4afff54815 100644 --- a/pkgs/id/web/src/cursors.ts +++ b/pkgs/id/web/src/cursors.ts @@ -734,9 +734,10 @@ function createCursorDecorations( decorations.push( Decoration.widget( cursor.head, - () => viewState - ? createStandaloneCursor(cursor, connectionState, viewState, handleMouseEnter, handleMouseLeave) - : createCursorLine(cursor, false, connectionState), + () => + viewState + ? createStandaloneCursor(cursor, connectionState, viewState, handleMouseEnter, handleMouseLeave) + : createCursorLine(cursor, false, connectionState), { side: 1, key: `cursor-${cursor.clientID}-${cursor.opacity}-${cursor.color}-${connectionState}`,