diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000000..2da87d4796 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,4 @@ +## 2024-05-14 - ProseMirror DOM Node Recycling + +**Learning:** ProseMirror's `Decoration.widget` requires using a factory function (`() => HTMLElement`) to construct the DOM node. Instantiating the node directly during the decoration generation loop bypasses ProseMirror's node recycling mechanism and causes severe layout thrashing/GC spikes, especially for dynamic decorations like cursors that update frequently. Furthermore, any visual attribute that dynamically changes the appearance of the widget (e.g. `opacity`, `connectionState`, `color`) must be explicitly embedded in the widget's `key` string. +**Action:** When implementing ProseMirror decorations, always use the factory function pattern `() => HTMLElement` for `Decoration.widget` construction and derive the `key` string from underlying data properties to enable DOM node recycling and avoid performance bottlenecks. diff --git a/pkgs/id/src/blob_gate.rs b/pkgs/id/src/blob_gate.rs index d8d207fd60..e64b3bdd34 100644 --- a/pkgs/id/src/blob_gate.rs +++ b/pkgs/id/src/blob_gate.rs @@ -125,10 +125,7 @@ pub struct FoldedWorlds { impl FoldedWorlds { /// Pairs the folded worlds with a reference index. pub const fn new(worlds: Worlds, references: HashMap>) -> Self { - Self { - worlds, - references, - } + Self { worlds, references } } } diff --git a/pkgs/id/web/src/cursors.ts b/pkgs/id/web/src/cursors.ts index b127290321..1672b1da59 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}-color-${ownCursor.color}`, + }, + ), ); } @@ -675,7 +673,6 @@ function createCursorDecorations( // Find the leftmost group (we'll attach the bar to its cursor line) 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 @@ -684,66 +681,70 @@ function createCursorDecorations( .sort() .join("-"); + // Generate a cluster hash based on opacities for the key so it recreates when opacities change + const clusterOpacityHash = groupsForBar.flatMap((g) => g.cursors.map((c) => c.opacity.toFixed(2))).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 === leftmostGroup) { + cursorLine.style.opacity = "1"; + cursorLine.style.setProperty("--base-opacity", "1"); + cursorLine.style.animation = "none"; // No strobe on bar container + + 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, + // Include connectionState, color, and cluster opacities in the key + key: `cursor-cluster-${group.position}-${clusterKey}-conn-${connectionState}-color-${group.mostRecentColor}-op-${clusterOpacityHash}`, + }, + ), ); }); - - // 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 based on dynamic properties dictating rendering + key: `cursor-${cursor.clientID}-conn-${connectionState}-op-${cursor.opacity.toFixed(2)}-color-${cursor.color}`, + }, + ), ); } }); @@ -765,7 +766,7 @@ function createCursorDecorations( style: `background-color: ${cursor.color}${selectionOpacity};`, }, { - key: `selection-${cursor.clientID}-g${gen}`, + key: `selection-${cursor.clientID}-op-${cursor.opacity.toFixed(2)}-color-${cursor.color}`, }, ), );