Skip to content

Commit 510a269

Browse files
committed
Add id to streaming comparison
This avoids a bug where comparisons could potentially overlap if the user opens a new comparison while the previous one has not yet finished loading.
1 parent a69ef15 commit 510a269

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

extensions/ql-vscode/src/common/interface-types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,8 @@ export type InterpretedQueryCompareResult = {
424424

425425
export interface StreamingComparisonSetupMessage {
426426
readonly t: "streamingComparisonSetup";
427+
// The id of this streaming comparison
428+
readonly id: string;
427429
readonly currentResultSetName: string;
428430
readonly message: string | undefined;
429431
// The from and to fields will only contain a chunk of the results
@@ -432,12 +434,14 @@ export interface StreamingComparisonSetupMessage {
432434

433435
interface StreamingComparisonAddResultsMessage {
434436
readonly t: "streamingComparisonAddResults";
437+
readonly id: string;
435438
// The from and to fields will only contain a chunk of the results
436439
readonly result: QueryCompareResult;
437440
}
438441

439442
interface StreamingComparisonCompleteMessage {
440443
readonly t: "streamingComparisonComplete";
444+
readonly id: string;
441445
}
442446

443447
/**

extensions/ql-vscode/src/compare/compare-view.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
} from "./result-set-names";
3535
import { compareInterpretedResults } from "./interpreted-results";
3636
import { isCanary } from "../config";
37+
import { nanoid } from "nanoid";
3738

3839
interface ComparePair {
3940
from: CompletedLocalQueryInfo;
@@ -206,13 +207,16 @@ export class CompareView extends AbstractWebview<
206207
return;
207208
}
208209

210+
const id = nanoid();
211+
209212
// Streaming itself is implemented like this:
210213
// - 1 setup message which contains the first 1,000 results
211214
// - n "add results" messages which contain 1,000 results each
212215
// - 1 complete message which just tells the webview that we're done
213216

214217
await this.postMessage({
215218
t: "streamingComparisonSetup",
219+
id,
216220
result: this.chunkResults(result, 0, 1000),
217221
currentResultSetName,
218222
message,
@@ -226,12 +230,14 @@ export class CompareView extends AbstractWebview<
226230

227231
await this.postMessage({
228232
t: "streamingComparisonAddResults",
233+
id,
229234
result: chunk,
230235
});
231236
}
232237

233238
await this.postMessage({
234239
t: "streamingComparisonComplete",
240+
id,
235241
});
236242
}
237243

extensions/ql-vscode/src/view/compare/Compare.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ export function Compare(_: Record<string, never>): React.JSX.Element {
7474
break;
7575
}
7676

77+
if (prev.id !== msg.id) {
78+
console.warn(
79+
'Received "streamingComparisonAddResults" with different id, ignoring',
80+
);
81+
break;
82+
}
83+
7784
let result: QueryCompareResult;
7885
switch (prev.result.kind) {
7986
case "raw":
@@ -121,6 +128,14 @@ export function Compare(_: Record<string, never>): React.JSX.Element {
121128
setComparison(null);
122129
break;
123130
}
131+
132+
if (streamingComparisonRef.current.id !== msg.id) {
133+
console.warn(
134+
'Received "streamingComparisonComplete" with different id, ignoring',
135+
);
136+
break;
137+
}
138+
124139
setComparison({
125140
...streamingComparisonRef.current,
126141
t: "setComparisons",

0 commit comments

Comments
 (0)