diff --git a/src/Constants.ts b/src/Constants.ts index 93826d1..dfdbc30 100644 --- a/src/Constants.ts +++ b/src/Constants.ts @@ -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 }; diff --git a/src/FileChangeManager.ts b/src/FileChangeManager.ts index 85df3fb..930cd86 100644 --- a/src/FileChangeManager.ts +++ b/src/FileChangeManager.ts @@ -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'; @@ -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; @@ -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)); diff --git a/src/utilites.ts b/src/utilites.ts index 5be1c09..5d56dbb 100644 --- a/src/utilites.ts +++ b/src/utilites.ts @@ -7,6 +7,18 @@ import * as os from 'os'; import * as fs from 'fs'; +export function debounce void>(func: T, wait: number): (...args: Parameters) => void { + let timeout: NodeJS.Timeout | null; + return function (...args: Parameters) { + if (timeout) { + clearTimeout(timeout); + } + timeout = setTimeout(() => { + func(...args); + }, wait); + }; +} + export async function writeToFile(filePath: string, exprText: string): Promise {