diff --git a/frontend/src/app/workspace/component/visualization-panel-content/visualization-frame-content.component.spec.ts b/frontend/src/app/workspace/component/visualization-panel-content/visualization-frame-content.component.spec.ts index 9027bb36da8..e1455fc6a6b 100644 --- a/frontend/src/app/workspace/component/visualization-panel-content/visualization-frame-content.component.spec.ts +++ b/frontend/src/app/workspace/component/visualization-panel-content/visualization-frame-content.component.spec.ts @@ -114,26 +114,56 @@ describe("VisualizationFrameContentComponent", () => { }); describe("result-update stream subscription", () => { - it("redraws after UPDATE_INTERVAL_MS when a matching operatorId emits", fakeAsync(() => { + // The frame stays mounted across runs. A re-run that finishes inside the interval sends a + // single update, already carrying the new picture; held for the interval, the frame showed the + // old one for that long (mengw15 on #9021, measured against the real result service). + it("redraws at once on the first update after mounting", fakeAsync(() => { snapshotProvider = () => makeResultSnapshot("
v1
"); buildComponent("op-1"); // Initial drawChart() from ngAfterContentInit ran with v1. const iframe = fixture.nativeElement.querySelector("iframe") as HTMLIFrameElement; expect(iframe.getAttribute("srcdoc")).toContain("v1"); - // Stream emits, but auditTime(UPDATE_INTERVAL_MS) holds the redraw. snapshotProvider = () => makeResultSnapshot("
v2
"); resultUpdateStream.next({ "op-1": { fake: true } }); - - // Before the audit window elapses, the iframe still shows v1. - tick(UPDATE_INTERVAL_MS - 1); fixture.detectChanges(); - expect(iframe.getAttribute("srcdoc")).toContain("v1"); - // After the window, the redraw fires and the iframe flips to v2. - tick(1); + expect(iframe.getAttribute("srcdoc")).toContain("v2"); + })); + + // A visualiser that outputs a row per input row sends an update per batch over a slow source; + // the frame draws the first at once and the rest at most once per interval, the trailing + // redraw taking whatever is current when the window closes. + it("holds further updates inside the interval to one trailing redraw, of the latest picture", fakeAsync(() => { + snapshotProvider = () => makeResultSnapshot("
v1
"); + buildComponent("op-1"); + const iframe = fixture.nativeElement.querySelector("iframe") as HTMLIFrameElement; + const drawSpy = vi.spyOn(component, "drawChart"); + + snapshotProvider = () => makeResultSnapshot("
v2
"); + resultUpdateStream.next({ "op-1": { fake: true } }); + snapshotProvider = () => makeResultSnapshot("
v3
"); + tick(UPDATE_INTERVAL_MS / 4); + resultUpdateStream.next({ "op-1": { fake: true } }); + snapshotProvider = () => makeResultSnapshot("
v4
"); + tick(UPDATE_INTERVAL_MS / 4); + resultUpdateStream.next({ "op-1": { fake: true } }); + + // Inside the window: only the leading redraw has run. + tick(UPDATE_INTERVAL_MS / 4); fixture.detectChanges(); + expect(drawSpy).toHaveBeenCalledTimes(1); expect(iframe.getAttribute("srcdoc")).toContain("v2"); + + // The window closes: one more redraw, with the picture that is current now. + tick(UPDATE_INTERVAL_MS / 4); + fixture.detectChanges(); + expect(drawSpy).toHaveBeenCalledTimes(2); + expect(iframe.getAttribute("srcdoc")).toContain("v4"); + + // Nothing arrived in the window that followed, so nothing more is drawn. + tick(UPDATE_INTERVAL_MS); + expect(drawSpy).toHaveBeenCalledTimes(2); })); it("ignores stream emissions that don't include the current operatorId", fakeAsync(() => { @@ -151,6 +181,30 @@ describe("VisualizationFrameContentComponent", () => { expect(iframe.getAttribute("srcdoc")).toBe(initialSrcdoc); })); + // An update event carries only the operators whose result changed, so events for different + // operators interleave. With the throttle before the filter, a window that closed on another + // operator's event was dropped by the filter and this operator's pending redraw was lost. + it("does not lose this operator's pending redraw to a later emission for another operator", fakeAsync(() => { + snapshotProvider = () => makeResultSnapshot("
v1
"); + buildComponent("op-1"); + const iframe = fixture.nativeElement.querySelector("iframe") as HTMLIFrameElement; + + // The leading redraw opens the window ... + snapshotProvider = () => makeResultSnapshot("
v2
"); + resultUpdateStream.next({ "op-1": { fake: true } }); + // ... a second update of this operator is pending inside it ... + snapshotProvider = () => makeResultSnapshot("
v3
"); + tick(UPDATE_INTERVAL_MS / 4); + resultUpdateStream.next({ "op-1": { fake: true } }); + // ... and another operator's event lands before the window closes. + tick(UPDATE_INTERVAL_MS / 4); + resultUpdateStream.next({ "some-other-op": { fake: true } }); + tick(UPDATE_INTERVAL_MS / 2); + fixture.detectChanges(); + + expect(iframe.getAttribute("srcdoc")).toContain("v3"); + })); + it("ignores stream emissions when operatorId is undefined", fakeAsync(() => { buildComponent(undefined); const drawSpy = vi.spyOn(component, "drawChart"); diff --git a/frontend/src/app/workspace/component/visualization-panel-content/visualization-frame-content.component.ts b/frontend/src/app/workspace/component/visualization-panel-content/visualization-frame-content.component.ts index eb329c1c7f1..482ad576082 100644 --- a/frontend/src/app/workspace/component/visualization-panel-content/visualization-frame-content.component.ts +++ b/frontend/src/app/workspace/component/visualization-panel-content/visualization-frame-content.component.ts @@ -20,7 +20,8 @@ import { AfterContentInit, Component, Input } from "@angular/core"; import { DomSanitizer } from "@angular/platform-browser"; import { WorkflowResultService } from "../../service/workflow-result/workflow-result.service"; -import { auditTime, filter } from "rxjs/operators"; +import { asyncScheduler } from "rxjs"; +import { filter, throttleTime } from "rxjs/operators"; import { UntilDestroy, untilDestroyed } from "@ngneat/until-destroy"; @UntilDestroy() @@ -45,13 +46,24 @@ export class VisualizationFrameContentComponent implements AfterContentInit { // attempt to draw chart immediately this.drawChart(); - // setup an event lister that re-draws the chart content every (n) milliseconds - // auditTime makes sure the first re-draw happens after (n) milliseconds has elapsed + // Re-draw on this operator's result updates: at once on the first (the frame stays mounted + // across runs, and a re-run that finishes inside the interval sends one update, already + // carrying the new picture -- held back, it showed the old one for the whole interval), then at + // most once per UPDATE_INTERVAL_MS, with a trailing re-draw for whatever arrived inside the + // window. Filter BEFORE throttling: an update event carries only the operators whose result + // changed, so with the throttle first a window that closed on another operator's event handed + // the filter that event, and this operator's pending re-draw was lost -- the last update of a + // run showed the previous picture for good. this.workflowResultService .getResultUpdateStream() - .pipe(auditTime(VisualizationFrameContentComponent.UPDATE_INTERVAL_MS)) - .pipe(filter(rec => this.operatorId !== undefined && rec[this.operatorId] !== undefined)) - .pipe(untilDestroyed(this)) + .pipe( + filter(rec => this.operatorId !== undefined && rec[this.operatorId] !== undefined), + throttleTime(VisualizationFrameContentComponent.UPDATE_INTERVAL_MS, asyncScheduler, { + leading: true, + trailing: true, + }), + untilDestroyed(this) + ) .subscribe(() => { this.drawChart(); }); diff --git a/frontend/src/app/workspace/component/workflow-form/workflow-form.component.html b/frontend/src/app/workspace/component/workflow-form/workflow-form.component.html index a142107331f..d03474eadcb 100644 --- a/frontend/src/app/workspace/component/workflow-form/workflow-form.component.html +++ b/frontend/src/app/workspace/component/workflow-form/workflow-form.component.html @@ -607,11 +607,14 @@

Results shown here

*ngSwitchCase="isTabularResult(id)" [operatorId]="id"> - - + +

{ expect(component.resultLabel("gone")).toBe("gone"); }); - it("keeps a result's frame identity stable until its version moves", () => { - build(formViewWorkflow).ngOnInit(); - const before = component.resultKey("op-1"); - expect(component.resultKey("op-1")).toBe(before); - - (component as any).resultVersion.set("op-1", 1); - - expect(component.resultKey("op-1")).not.toBe(before); - expect(component.trackByKey(0, "k")).toBe("k"); - }); - it("resizes a result within bounds, per result, and re-fits after", () => { vi.useFakeTimers(); build(formViewWorkflow).ngOnInit(); @@ -2528,13 +2517,20 @@ describe("WorkflowFormComponent", () => { vi.useRealTimers(); }); - it("bumps the result version and re-fits on a result update", () => { + // An update to a result the form already shows reaches the frame in place (the frame redraws on + // its operator's updates); the form only re-limits the shown set and re-fits. That the frame is + // still the same one afterwards is asserted on the rendered page (workflow-form.rendered.spec). + it("re-limits the shown results and re-fits on a result update", () => { vi.useFakeTimers(); build(formViewWorkflow).ngOnInit(); + const refresh = vi.spyOn(component as any, "refreshShownResults"); const fit = vi.spyOn(component as any, "fitVisualisations").mockImplementation(() => {}); h.resultUpdateStream.next({ "op-1": {} }); - expect(component.resultKey("op-1")).toBe("op-1#1"); + h.resultUpdateStream.next({ "op-1": {} }); + + expect(refresh).toHaveBeenCalledTimes(2); + expect(fit).not.toHaveBeenCalled(); vi.advanceTimersByTime(300); expect(fit).toHaveBeenCalled(); vi.useRealTimers(); diff --git a/frontend/src/app/workspace/component/workflow-form/workflow-form.component.ts b/frontend/src/app/workspace/component/workflow-form/workflow-form.component.ts index 4c493ec71df..965641552ae 100644 --- a/frontend/src/app/workspace/component/workflow-form/workflow-form.component.ts +++ b/frontend/src/app/workspace/component/workflow-form/workflow-form.component.ts @@ -240,10 +240,6 @@ export class WorkflowFormComponent implements OnInit, OnDestroy { /** Chart height per result (0 compact / 1 default / 2 tall). Per operator so one does not resize * the others, and in memory only -- a viewing preference, not part of the workflow. */ private zoomByResult = new Map(); - /** Bumped when a result changes, used as the chart's *ngFor identity so the frame is rebuilt, not - * reused: the chart reads its content once at creation, so a stale frame showed "undefined". */ - private resultVersion = new Map(); - /** The collapsible workflow preview: closed until the reader opens it. */ public workflowOpen = false; /** The embedded canvas is built the first time the strip opens, never while collapsed. */ @@ -371,15 +367,15 @@ export class WorkflowFormComponent implements OnInit, OnDestroy { // views switch, so a step chosen there is already selected here and no event will say so. this.syncSelectionFromHighlight(); - // A result changing bumps that operator's version (so its chart frame is rebuilt, not reused), - // re-limits what the form shows to the currently-viewed set, and re-fits the visualisations. + // A result update re-limits what the form shows and re-fits the visualisations. The chart + // frame is left alone: it redraws in place on its own operator's updates. A per-operator + // version used to be bumped here to key the frame, rebuilding it on every update: a visualiser + // that outputs a row per input row (HTML Visualizer, URL Visualizer) sends several updates with + // a picture over a slow source, and each rebuild was the flicker of #8759. this.workflowResultService .getResultUpdateStream() .pipe(untilDestroyed(this)) - .subscribe(update => { - for (const operatorID of Object.keys(update ?? {})) { - this.resultVersion.set(operatorID, (this.resultVersion.get(operatorID) ?? 0) + 1); - } + .subscribe(() => { this.refreshShownResults(); // markForCheck, not detectChanges: this fires often during a run, and a synchronous pass // can be thrown out of by an unrelated component's NG0100, killing the subscription. @@ -1418,21 +1414,6 @@ export class WorkflowFormComponent implements OnInit, OnDestroy { return operator ? this.formBindingService.operatorLabel(operator) : operatorID; } - public trackByKey(_: number, key: string): string { - return key; - } - - /** - * A per-result identity that changes on each result-update for that operator, used as the chart's - * *ngFor key so the frame is rebuilt (not reused) when the result changes: the chart reads its - * content once at creation, so a reused frame kept showing the old (or "undefined") picture. A - * visualisation's result arrives as a single snapshot, so in practice this bumps about once per - * result rather than per tuple. - */ - public resultKey(operatorID: string): string { - return operatorID + "#" + (this.resultVersion.get(operatorID) ?? 0); - } - public resultZoom(operatorID: string): number { return this.zoomByResult.get(operatorID) ?? 1; } diff --git a/frontend/src/app/workspace/component/workflow-form/workflow-form.rendered.spec.ts b/frontend/src/app/workspace/component/workflow-form/workflow-form.rendered.spec.ts index 7437037ce82..43f85cb1933 100644 --- a/frontend/src/app/workspace/component/workflow-form/workflow-form.rendered.spec.ts +++ b/frontend/src/app/workspace/component/workflow-form/workflow-form.rendered.spec.ts @@ -890,6 +890,40 @@ describe("WorkflowFormComponent (rendered template)", () => { expect(names().map(e => e.getAttribute("aria-current"))).toEqual(["false", "true"]); }); + // What the result-update subscription claims: an update for a chart already on the page reaches + // the frame that is there, it does not get the frame rebuilt. A per-result key meant to do that + // used to sit on the frame; this is the assertion that would fail if one came back. + it("keeps the same chart frame across that step's result updates", () => { + const updates = new Subject>(); + const wrs: any = TestBed.inject(WorkflowResultService); + wrs.getResultUpdateStream = () => updates.asObservable(); + wrs.hasNonEmptyResult = () => true; + // A snapshot with a picture takes the visualisation branch. The frame's template is blanked + // above, so what is instantiated is the component instance alone, with no iframe to run. + wrs.getResultService = () => ({ getCurrentResultSnapshot: () => [{ "html-content": "

chart
" }] }); + // An update re-reads the shown set from the graph (refreshShownResults), so op-1 has to be a + // step that is on it: with no downstream link it is a final step, shown by default. + const actions: any = TestBed.inject(WorkflowActionService); + const graph = actions.getTexeraGraph(); + graph.getAllOperators = () => [{ operatorID: "op-1" }]; + actions.getTexeraGraph = () => graph; + fixture.detectChanges(); + finishLoad(); + vi.spyOn(fixture.componentInstance as any, "fitVisualisations").mockImplementation(() => {}); + + const frame = () => fixture.debugElement.query(By.directive(VisualizationFrameContentComponent)); + const first = frame(); + expect(first).not.toBeNull(); + + updates.next({ "op-1": {} }); + fixture.detectChanges(); + updates.next({ "op-1": {} }); + fixture.detectChanges(); + + expect(frame().componentInstance).toBe(first.componentInstance); + expect(frame().nativeElement).toBe(first.nativeElement); + }); + it("mounts the step panel from the start, hidden until a step is selected", () => { fixture.detectChanges(); finishLoad();