Skip to content

Commit e30e89b

Browse files
committed
implement debounce timer for update the tests if document changed
1 parent 2d5d5ae commit e30e89b

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

vscode-client/testcontrollermanager.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class TestControllerManager {
5151
public readonly debugProfile: vscode.TestRunProfile;
5252
private readonly refreshMutex = new Mutex();
5353
private readonly debugSessions = new Set<vscode.DebugSession>();
54-
54+
private readonly didChangedTimer = new Map<vscode.TextDocument, number>();
5555
constructor(
5656
public readonly extensionContext: vscode.ExtensionContext,
5757
public readonly languageClientsManager: LanguageClientsManager,
@@ -81,21 +81,43 @@ export class TestControllerManager {
8181

8282
this._disposables = vscode.Disposable.from(
8383
fileWatcher,
84+
vscode.workspace.onDidCloseTextDocument((document) => {
85+
if (document.languageId !== "robotframework") return;
86+
87+
if (this.didChangedTimer.has(document)) {
88+
clearTimeout(this.didChangedTimer.get(document));
89+
this.didChangedTimer.delete(document);
90+
}
91+
}),
8492
vscode.workspace.onDidSaveTextDocument(async (document) => {
8593
if (document.languageId !== "robotframework") return;
8694

95+
if (this.didChangedTimer.has(document)) {
96+
clearTimeout(this.didChangedTimer.get(document));
97+
}
98+
8799
await this.refresh(this.findTestItemForDocument(document));
88100
}),
89101
vscode.workspace.onDidOpenTextDocument(async (document) => {
90102
if (document.languageId !== "robotframework") return;
91103

92104
await this.refresh(this.findTestItemForDocument(document));
93105
}),
94-
vscode.workspace.onDidChangeTextDocument(async (event) => {
106+
vscode.workspace.onDidChangeTextDocument((event) => {
95107
if (event.document.languageId !== "robotframework") return;
96-
// TODO: refresh only once if several short changes are made
97108

98-
await this.refresh(this.findTestItemForDocument(event.document));
109+
if (this.didChangedTimer.has(event.document)) {
110+
clearTimeout(this.didChangedTimer.get(event.document));
111+
}
112+
this.didChangedTimer.set(
113+
event.document,
114+
setTimeout((_) => {
115+
this.refresh(this.findTestItemForDocument(event.document)).then(
116+
() => undefined,
117+
() => undefined
118+
);
119+
}, 1000)
120+
);
99121
}),
100122
vscode.workspace.onDidChangeWorkspaceFolders(async (event) => {
101123
for (const r of event.removed) {

0 commit comments

Comments
 (0)