Skip to content

Commit 77d6685

Browse files
committed
Added a debouncer function to delay the constant firing of "UPDATE_XML" command on cursor movement
1 parent 6d37d6f commit 77d6685

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

src/Constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ export const Constants = {
99
SRCML_PATH_WINDOWS: "C:\\Program Files\\srcML 0.9.5\\bin\\srcml",
1010
SRCML_PATH_MAC: "/usr/local/bin/srcml",
1111
SRCML_PATH_LINUX: "/usr/bin/srcml",
12-
JUST_FOR_THE_KICKS:"hello"
12+
DEBOUNCER_DELAY:3000
1313

1414
};

src/FileChangeManager.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import * as fs from 'fs';
66
import * as path from 'path';
77
import { readFile } from 'fs/promises';
88
import { WebSocketConstants } from './WebSocketConstants';
9-
import { buildFolderHierarchy } from './utilites';
9+
import { Constants } from './Constants';
10+
import { buildFolderHierarchy,debounce } from './utilites';
1011
//import { MessageProcessor } from './MessageProcessor';
1112
import { FollowAndAuthorRulesProcessor } from './FollowAndAuthorRulesProcessor';
1213
import { MiningRulesProcessor } from './MiningRulesProcessor';
@@ -23,6 +24,8 @@ export class FileChangeManager {
2324
private constructor(projectPath:string,ws:WebSocket) {
2425
this.projectPath = projectPath;
2526
this.ws = ws;
27+
//second argument to the debounce function sets the delay timer
28+
this.debouncedHandleChangeTextDocument = debounce(this.handleChangeTextDocument.bind(this), Constants.DEBOUNCER_DELAY);
2629
this.watchWorkspaceChanges();
2730
if (vscode.workspace.workspaceFolders) {
2831
//const projectPath = vscode.workspace.workspaceFolders[0].uri.fsPath;
@@ -109,13 +112,14 @@ export class FileChangeManager {
109112
return FileChangeManager.instance;
110113
}
111114

115+
private debouncedHandleChangeTextDocument: (event: vscode.TextDocumentChangeEvent) => void;
112116

113117

114118
private watchWorkspaceChanges() {
115119

116120
this.handleActiveTextEditorChange();
117121

118-
vscode.workspace.onDidChangeTextDocument(this.handleChangeTextDocument.bind(this));
122+
vscode.workspace.onDidChangeTextDocument(this.debouncedHandleChangeTextDocument);
119123
vscode.workspace.onDidCreateFiles(this.handleCreateFile.bind(this));
120124
vscode.workspace.onDidDeleteFiles(this.handleDeleteFile.bind(this));
121125
vscode.workspace.onDidRenameFiles(this.handleRenameFile.bind(this));

src/utilites.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ import * as os from 'os';
77
import * as fs from 'fs';
88

99

10+
export function debounce<T extends (...args: any[]) => void>(func: T, wait: number): (...args: Parameters<T>) => void {
11+
let timeout: NodeJS.Timeout | null;
12+
return function (...args: Parameters<T>) {
13+
if (timeout) {
14+
clearTimeout(timeout);
15+
}
16+
timeout = setTimeout(() => {
17+
func(...args);
18+
}, wait);
19+
};
20+
}
21+
1022

1123

1224
export async function writeToFile(filePath: string, exprText: string): Promise<void> {

0 commit comments

Comments
 (0)