Skip to content

Commit 8eac328

Browse files
Defer hidden Lambda updates and debounce comparisons (#8636)
Keep the Before and After dumps unchanged while the Lambda view is hidden. Opening the view reads the latest compiler result. While Diff is open, wait 200 ms after the latest changed result before replacing its comparison; opening Diff remains immediate. Leaving Diff or unmounting cancels its timer. Review: Address cknitt's two non-blocking follow-ups. Regression tests cover hidden panels catching up, consecutive edits, cancellation on tab switches, and expanded context surviving the delayed update. Validation: All 36 developer playground tests, the native compiler suites, the browser compiler bundle tests, production Vite build, OCaml/ReScript/JS format checks, and changelog validation pass after rebasing onto b7c67d2. Signed-off-by: Florian Hammerschmidt <florianh89@gmail.com>
1 parent a2beab0 commit 8eac328

3 files changed

Lines changed: 100 additions & 16 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
- Add before/after optimization views and inline diffs to the developer playground's Lambda tab, with optimized Lambda output in playground API v9. https://github.com/rescript-lang/rescript/pull/8636
1+
- Add before/after optimization views and inline diffs to the developer playground's Lambda tab, with optimized Lambda output in playground API v9, deferred hidden-panel updates, and debounced comparisons while editing. https://github.com/rescript-lang/rescript/pull/8636

‎packages/dev-playground/scripts/lambda-diff.test.mjs‎

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,60 @@ test("Lambda view retains its diff across identical compiles and tab switches",
338338
assert.equal(diff(), changed);
339339
});
340340

341-
test("expanded unchanged regions survive edits to a changed line", () => {
342-
const { input, mode, view, html } = lambdaView();
341+
test("Lambda panels retain their text while hidden and catch up when shown", () => {
342+
const { input, visible, html } = lambdaView();
343+
assert.match(html(), /old/);
344+
Signal.set(visible, false);
345+
Signal.set(input, { before: "hidden before", after: "hidden after" });
346+
assert.doesNotMatch(html(), /hidden before|hidden after/);
347+
assert.match(html(), /old/);
348+
Signal.set(visible, true);
349+
assert.match(html(), /hidden before/);
350+
assert.match(html(), /hidden after/);
351+
});
352+
353+
test("Lambda Diff debounces edits and renders the latest comparison", t => {
354+
t.mock.timers.enable({ apis: ["setTimeout"] });
355+
const { input, mode, diff } = lambdaView();
356+
Signal.set(mode, "Diff");
357+
const initial = diff();
358+
Signal.set(input, { ...Signal.peek(input), after: "first edit" });
359+
t.mock.timers.tick(199);
360+
assert.equal(diff(), initial);
361+
Signal.set(input, { ...Signal.peek(input), after: "latest edit" });
362+
t.mock.timers.tick(199);
363+
assert.equal(diff(), initial);
364+
t.mock.timers.tick(1);
365+
assert.notEqual(diff(), initial);
366+
const html = renderToString(() => diff());
367+
assert.match(html, /latest edit/);
368+
assert.doesNotMatch(html, /first edit/);
369+
});
370+
371+
test("leaving Lambda Diff cancels pending work and reopening catches up", t => {
372+
t.mock.timers.enable({ apis: ["setTimeout"] });
373+
for (const hidePanel of [false, true]) {
374+
const { input, mode, visible, diff } = lambdaView();
375+
Signal.set(mode, "Diff");
376+
const initial = diff();
377+
Signal.set(input, { ...Signal.peek(input), after: "pending edit" });
378+
if (hidePanel) Signal.set(visible, false);
379+
else Signal.set(mode, "Before");
380+
t.mock.timers.tick(200);
381+
assert.equal(diff(), initial);
382+
if (hidePanel) Signal.set(visible, true);
383+
else Signal.set(mode, "Diff");
384+
assert.notEqual(diff(), initial);
385+
assert.match(
386+
renderToString(() => diff()),
387+
/pending edit/,
388+
);
389+
}
390+
});
391+
392+
test("expanded unchanged regions survive edits to a changed line", t => {
393+
t.mock.timers.enable({ apis: ["setTimeout"] });
394+
const { input, mode, view, html, diff } = lambdaView();
343395
Signal.set(mode, "Diff");
344396
const summary = elements(view).find(node => node.tag === "summary");
345397
const click = summary.events.find(([name]) => name === "click")[1];
@@ -353,10 +405,13 @@ test("expanded unchanged regions survive edits to a changed line", () => {
353405
const openDetails = () =>
354406
(html().match(/<details[^>]* open(?: |>|=)/g) ?? []).length;
355407
assert.equal(openDetails(), 1);
408+
const initial = diff();
356409
Signal.set(input, {
357410
...Signal.peek(input),
358411
after: Signal.peek(input).after.replace("new", "edited"),
359412
});
413+
t.mock.timers.tick(200);
414+
assert.notEqual(diff(), initial);
360415
assert.equal(openDetails(), 1);
361416
});
362417

‎packages/dev-playground/src/LambdaView.res‎

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ let renderLine = (highlights, line: LambdaDiff.line) => {
2323
}
2424
let number = number => number->Option.map(value => Int.toString(value))->Option.getOr("")
2525
<span class={className}>
26-
<span class="lambda-diff-number" ariaHidden=true> {View.text(number(line.before))} </span>
27-
<span class="lambda-diff-number" ariaHidden=true> {View.text(number(line.after))} </span>
28-
<span class="lambda-diff-marker"> {View.text(marker)} </span>
26+
<span class="lambda-diff-number" ariaHidden=true>{View.text(number(line.before))}</span>
27+
<span class="lambda-diff-number" ariaHidden=true>{View.text(number(line.after))}</span>
28+
<span class="lambda-diff-marker">{View.text(marker)}</span>
2929
<span class="lambda-diff-text">
3030
{switch LambdaInlineDiff.forLine(highlights, line) {
3131
| None => View.text(line.text)
3232
| Some(parts) =>
3333
View.fragment(
3434
parts->Array.map(part =>
3535
part.changed
36-
? <span class="lambda-diff-token"> {View.text(part.text)} </span>
36+
? <span class="lambda-diff-token">{View.text(part.text)}</span>
3737
: View.text(part.text)
3838
),
3939
)
@@ -49,9 +49,7 @@ let renderLines = (highlights, lines) =>
4949
let renderDiff = (contextState, result) =>
5050
switch result {
5151
| LambdaDiff.Identical =>
52-
<p class="lambda-notice">
53-
{View.text("No changes in the printed Lambda after optimization.")}
54-
</p>
52+
<p class="lambda-notice">{View.text("No changes in the printed Lambda after optimization.")}</p>
5553
| TooLarge =>
5654
<p class="lambda-notice">
5755
{View.text(
@@ -65,7 +63,7 @@ let renderDiff = (contextState, result) =>
6563
let occurrences = Dict.make()
6664
let sections = LambdaDiff.sections(lines)->Array.map(section =>
6765
switch section {
68-
| Visible(lines) => <pre class="lambda-diff-block"> {renderLines(highlights, lines)} </pre>
66+
| Visible(lines) => <pre class="lambda-diff-block">{renderLines(highlights, lines)}</pre>
6967
| Collapsed(lines) =>
7068
let text = lines->Array.map(line => line.text)->Array.join("\n")
7169
let occurrence = Dict.get(occurrences, text)->Option.getOr(0)
@@ -90,7 +88,7 @@ let renderDiff = (contextState, result) =>
9088
</summary>
9189
{View.tracked(() =>
9290
Signal.get(expanded)
93-
? <pre class="lambda-diff-block"> {renderLines(highlights, lines)} </pre>
91+
? <pre class="lambda-diff-block">{renderLines(highlights, lines)}</pre>
9492
: View.empty()
9593
)}
9694
</details>
@@ -117,12 +115,41 @@ let make = (~input: unit => option<input>, ~visible, ~mode: Signal.t<mode>) => {
117115
"This compiler bundle does not provide optimized Lambda. Select a compiler with playground API v9 or newer.",
118116
)}
119117
</p>
118+
let shownInput = ref(None)
119+
let panelInput = Computed.make(() => {
120+
if visible() {
121+
shownInput := input()
122+
}
123+
shownInput.contents
124+
})
125+
let diffInput = Signal.make(None)
126+
let wasActive = ref(false)
127+
Effect.run(() => {
128+
let active = visible() && Signal.get(mode) === Diff
129+
let entering = active && !wasActive.contents
130+
wasActive := active
131+
if active {
132+
let current = input()
133+
if entering {
134+
// Opening Diff is immediate; edits wait for a short pause in typing.
135+
Signal.set(diffInput, current)
136+
None
137+
} else if sameInput(Signal.peek(diffInput), current) {
138+
None
139+
} else {
140+
let timer = Window.setTimeout(() => Signal.set(diffInput, current), 200)
141+
Some(() => Window.clearTimeout(timer))
142+
}
143+
} else {
144+
None
145+
}
146+
})
120147
// Cache the complete rendered diff by text, including inline highlights and
121148
// sections. Hidden tabs keep their last result and defer new comparison work.
122149
let cached = ref((None, View.empty()))
123150
let difference = Computed.make(() => {
124151
let (previous, rendered) = cached.contents
125-
let current = visible() && Signal.get(mode) === Diff ? input() : previous
152+
let current = Signal.get(diffInput)
126153
if sameInput(previous, current) {
127154
rendered
128155
} else {
@@ -152,12 +179,14 @@ let make = (~input: unit => option<input>, ~visible, ~mode: Signal.t<mode>) => {
152179
)}
153180
</div>
154181
<pre class={() => Signal.get(mode) === Before ? "output" : "output hidden-panel"}>
155-
{View.signalText(() => input()->Option.map(value => value.before)->Option.getOr(""))}
182+
{View.signalText(() =>
183+
Signal.get(panelInput)->Option.map(value => value.before)->Option.getOr("")
184+
)}
156185
</pre>
157186
<div class={() => Signal.get(mode) === After ? "" : "hidden-panel"}>
158187
{View.tracked(() =>
159-
switch input()->Option.flatMap(value => value.after) {
160-
| Some(after) => <pre class="output"> {View.text(after)} </pre>
188+
switch Signal.get(panelInput)->Option.flatMap(value => value.after) {
189+
| Some(after) => <pre class="output">{View.text(after)}</pre>
161190
| None => unavailable()
162191
}
163192
)}

0 commit comments

Comments
 (0)