Skip to content

Commit

Permalink
Added a debouncer function to delay the constant firing of "UPDATE_XM…
Browse files Browse the repository at this point in the history
…L" command on cursor movement
  • Loading branch information
yaad96 committed May 22, 2024
1 parent 6d37d6f commit 77d6685
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ export const Constants = {
SRCML_PATH_WINDOWS: "C:\\Program Files\\srcML 0.9.5\\bin\\srcml",
SRCML_PATH_MAC: "/usr/local/bin/srcml",
SRCML_PATH_LINUX: "/usr/bin/srcml",
JUST_FOR_THE_KICKS:"hello"
DEBOUNCER_DELAY:3000

};
8 changes: 6 additions & 2 deletions src/FileChangeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import * as fs from 'fs';
import * as path from 'path';
import { readFile } from 'fs/promises';
import { WebSocketConstants } from './WebSocketConstants';
import { buildFolderHierarchy } from './utilites';
import { Constants } from './Constants';
import { buildFolderHierarchy,debounce } from './utilites';
//import { MessageProcessor } from './MessageProcessor';
import { FollowAndAuthorRulesProcessor } from './FollowAndAuthorRulesProcessor';
import { MiningRulesProcessor } from './MiningRulesProcessor';
Expand All @@ -23,6 +24,8 @@ export class FileChangeManager {
private constructor(projectPath:string,ws:WebSocket) {
this.projectPath = projectPath;
this.ws = ws;
//second argument to the debounce function sets the delay timer
this.debouncedHandleChangeTextDocument = debounce(this.handleChangeTextDocument.bind(this), Constants.DEBOUNCER_DELAY);
this.watchWorkspaceChanges();
if (vscode.workspace.workspaceFolders) {
//const projectPath = vscode.workspace.workspaceFolders[0].uri.fsPath;
Expand Down Expand Up @@ -109,13 +112,14 @@ export class FileChangeManager {
return FileChangeManager.instance;
}

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


private watchWorkspaceChanges() {

this.handleActiveTextEditorChange();

vscode.workspace.onDidChangeTextDocument(this.handleChangeTextDocument.bind(this));
vscode.workspace.onDidChangeTextDocument(this.debouncedHandleChangeTextDocument);
vscode.workspace.onDidCreateFiles(this.handleCreateFile.bind(this));
vscode.workspace.onDidDeleteFiles(this.handleDeleteFile.bind(this));
vscode.workspace.onDidRenameFiles(this.handleRenameFile.bind(this));
Expand Down
12 changes: 12 additions & 0 deletions src/utilites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ import * as os from 'os';
import * as fs from 'fs';


export function debounce<T extends (...args: any[]) => void>(func: T, wait: number): (...args: Parameters<T>) => void {
let timeout: NodeJS.Timeout | null;
return function (...args: Parameters<T>) {
if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(() => {
func(...args);
}, wait);
};
}



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

0 comments on commit 77d6685

Please sign in to comment.