Skip to content

Commit ab5941f

Browse files
committed
fix: debounce
1 parent f03cee9 commit ab5941f

3 files changed

Lines changed: 12 additions & 31 deletions

File tree

src/visualBuilder/eventManager/useRecalculateVariantDataCSLPValues.ts

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@ import { isValidCslp } from "../../cslp/cslpdata";
77
import { setHighlightVariantFields } from "./useVariantsPostMessageEvent";
88
import visualBuilderPostMessage from "../utils/visualBuilderPostMessage";
99
import { VisualBuilderPostMessageEvents } from "../utils/types/postMessage.types";
10+
import { debounce } from "lodash-es";
1011

1112
const VARIANT_UPDATE_DELAY_MS: Readonly<number> = 8000;
12-
// Debounce window after the last observed mutation before asking the visual
13-
// editor to re-send discussion highlights. Short enough to feel responsive;
14-
// long enough to coalesce a burst of mutations into a single request.
15-
const DISCUSSION_HIGHLIGHTS_DEBOUNCE_MS: Readonly<number> = 200;
13+
14+
// Coalesce a burst of data-cslp mutations into a single request to the
15+
// visual editor.
16+
const requestDiscussionHighlights = debounce(() => {
17+
visualBuilderPostMessage?.send(
18+
VisualBuilderPostMessageEvents.REQUEST_DISCUSSION_HIGHLIGHTS
19+
);
20+
}, 200);
1621

1722
type OnAudienceModeVariantPatchUpdate = {
1823
highlightVariantFields: boolean;
@@ -38,21 +43,6 @@ export function updateVariantClasses(): void {
3843
const variant = VisualBuilder.VisualBuilderGlobalState.value.variant;
3944
const observers: MutationObserver[] = [];
4045

41-
// Ask the visual editor to re-send discussion highlights once the current
42-
// burst of data-cslp mutations has settled. The VB owns the field-path list
43-
// (it can be per-variant) so the iframe cannot re-mount comment icons on
44-
// its own — it can only request a refresh.
45-
let highlightsRequestTimer: ReturnType<typeof setTimeout> | null = null;
46-
const requestDiscussionHighlights = () => {
47-
if (highlightsRequestTimer !== null) clearTimeout(highlightsRequestTimer);
48-
highlightsRequestTimer = setTimeout(() => {
49-
highlightsRequestTimer = null;
50-
visualBuilderPostMessage?.send(
51-
VisualBuilderPostMessageEvents.REQUEST_DISCUSSION_HIGHLIGHTS
52-
);
53-
}, DISCUSSION_HIGHLIGHTS_DEBOUNCE_MS);
54-
};
55-
5646
// Helper function to update element classes
5747
const updateElementClasses = (
5848
element: HTMLElement,

src/visualBuilder/eventManager/useVariantsPostMessageEvent.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,18 +167,12 @@ export function useVariantFieldsPostMessageEvent({ isSSR }: { isSSR: boolean }):
167167
if (selectedVariant) {
168168
addVariantFieldClass(selectedVariant);
169169
}
170-
// SSR DOM is already in its final state (no async re-render) —
171-
// the MutationObserver in updateVariantClasses will never fire,
172-
// so ask the visual editor to re-send discussion highlights now
173-
// that the classes are applied against the final CSLP values.
170+
// SSR DOM is final; observer never fires, request directly.
174171
visualBuilderPostMessage?.send(
175172
VisualBuilderPostMessageEvents.REQUEST_DISCUSSION_HIGHLIGHTS
176173
);
177174
} else {
178-
// For CSR apps the framework re-renders asynchronously after
179-
// receiving the new variant, mutating data-cslp attributes.
180-
// updateVariantClasses installs a MutationObserver that emits
181-
// REQUEST_DISCUSSION_HIGHLIGHTS once those mutations settle.
175+
// CSR: observer in updateVariantClasses requests on settle.
182176
updateVariantClasses();
183177
}
184178
}

src/visualBuilder/generators/generateHighlightedComment.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,7 @@ export function updateHighlightedCommentIconPosition() {
9696
icon.style.top = `${top - highlighCommentOffset}px`; // Adjust based on the target element's top
9797
icon.style.left = `${left - highlighCommentOffset}px`; // Adjust based on the target element's left
9898
} else {
99-
// Target element is gone (e.g. data-cslp mutated after a
100-
// variant switch). Remove the orphaned icon instead of
101-
// silently leaving it drifted — the visual editor will
102-
// re-mount a fresh set via REQUEST_DISCUSSION_HIGHLIGHTS.
99+
// Target gone — drop the orphan icon.
103100
icon.remove();
104101
}
105102
}

0 commit comments

Comments
 (0)