Skip to content

Commit

Permalink
Doi Processing class added and visitedFiles functionality implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
yaad96 committed Mar 2, 2024
1 parent e0a9d72 commit 2ae0ab2
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 20 deletions.
77 changes: 77 additions & 0 deletions src/DoiProcessing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import * as vscode from 'vscode';
import WebSocket from 'ws';
import { exec } from 'child_process';
import { promisify } from 'util';
import * as fs from 'fs';
import * as path from 'path';
import { readFile } from 'fs/promises';
import { WebSocketConstants } from './WebSocketConstants';
import { buildFolderHierarchy } from './utilites';

export class DoiProcessing{
private ws: WebSocket | null;
private projectPath:string;
private static instance: DoiProcessing|null = null;

private timedVisitedFiles: { timeStamp: string, filePath: string }[] = [];

public constructor(projectPath:string,ws:WebSocket|null){
this.projectPath=projectPath;
this.ws = ws;
this.monitorWorkSpaceBehavior();

}

public static getInstance(projectPath:string = "", ws: WebSocket | null = null): DoiProcessing {
if (!DoiProcessing.instance) {
DoiProcessing.instance = new DoiProcessing(projectPath,ws);
}
return DoiProcessing.instance;
}

public updateProjectWs(projectPath: string, ws: WebSocket): void {
this.projectPath = projectPath;
this.ws = ws;
this.monitorWorkSpaceBehavior();
//this.learningDRPath = path.join(this.currentProjectPath, Constants.LEARNING_DR_DIRECTORY);

}

private monitorWorkSpaceBehavior(){
// Listening for file opening events in VSCode
vscode.workspace.onDidOpenTextDocument(this.newVisitedFile.bind(this));

}

public getVisitedFiles(): { timeStamp: string, filePath: string }[] {
// Directly return the array of visited files
return this.timedVisitedFiles;
}


private newVisitedFile(document: vscode.TextDocument): void {
const filePath = document.uri.fsPath;
this.updateVisitedFiles(filePath);
// Assuming updateSearchHistory() functionality is handled elsewhere or omitted
//this.currentFilePath = filePath;
}

private updateVisitedFiles(newFilePath: string): void {
const currentTime = new Date().getTime();
const timeStamp = currentTime.toString();

if (this.timedVisitedFiles.length > 0) {
const lastVisitedFile = this.timedVisitedFiles[this.timedVisitedFiles.length - 1];
const lastTimeStamp = parseInt(lastVisitedFile.timeStamp);

// If the new visit occurs within 1 second of the last, remove the last entry
if (currentTime - lastTimeStamp < 1000) {
this.timedVisitedFiles.pop();
}
}

this.timedVisitedFiles.push({ timeStamp, filePath: newFilePath });
}


}
29 changes: 26 additions & 3 deletions src/FileChangeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { buildFolderHierarchy } from './utilites';
//import { MessageProcessor } from './MessageProcessor';
import { FollowAndAuthorRulesProcessor } from './FollowAndAuthorRulesProcessor';
import { MiningRulesProcessor } from './MiningRulesProcessor';
import { DoiProcessing } from './DoiProcessing';

const readFileAsync = promisify(fs.readFile);

Expand Down Expand Up @@ -72,21 +73,43 @@ export class FileChangeManager {
// Handle or log the error appropriately
}
}
try {
const ins = DoiProcessing.getInstance();
if (!ins) {
// If getInstance() returned null, we create a new instance
// But since our getInstance never returns null (creates a new instance if null),
// This check is just for demonstrating a similar approach to catching NullPointerException in Java.
throw new Error('Instance is null'); // Simulating a scenario to create a new instance
}
ins.updateProjectWs(this.projectPath, this.ws);
} catch (error) {
if (error instanceof Error && error.message === 'Instance is null') {
// This block is for handling the specific error thrown above,
// simulating the catch for NullPointerException in Java.
// In practical TypeScript, this pattern is rarely needed due to the dynamic nature of JS and the way we handle nulls/undefined values.
new DoiProcessing(projectPath, this.ws); // Assuming this will set the instance internally or perform necessary actions
} else {
console.error("An unexpected error occurred:", error);
// Handle or log the error appropriately
}
}


}
}

/*public void checkChangedProject(){
}*/

public static getInstance(projectPath:string,ws:WebSocket): FileChangeManager {
if (!FileChangeManager.instance) {
FileChangeManager.instance = new FileChangeManager(projectPath,ws);
}
return FileChangeManager.instance;
}

public setWebSocket(ws: WebSocket) {
this.ws = ws;
}


private watchWorkspaceChanges() {

Expand Down
30 changes: 13 additions & 17 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { WebSocketConstants } from './WebSocketConstants';
import * as path from 'path';
import { FollowAndAuthorRulesProcessor } from './FollowAndAuthorRulesProcessor';
import { MiningRulesProcessor } from './MiningRulesProcessor';
import { DoiProcessing } from './DoiProcessing';



Expand All @@ -30,7 +31,7 @@ export function activate(context: vscode.ExtensionContext) {
server.on('connection', (ws) => {

console.log('Client connected');
//fileChangeManager.setWebSocket(ws);


(async () => { // Immediately Invoked Function Expression (IIFE) for async
if (vscode.workspace.workspaceFolders) {
Expand Down Expand Up @@ -137,31 +138,26 @@ export function activate(context: vscode.ExtensionContext) {
text: word
};

/*ws.send(MessageProcessor.encodeData({
command: WebSocketConstants.SEND_ELEMENT_INFO_FOR_MINE_RULES,
data: minigDataInfo
}));*/

ws.send(JSON.stringify({
command: WebSocketConstants.SEND_ELEMENT_INFO_FOR_MINE_RULES,
data: minigDataInfo
}));

const doiProcessing = DoiProcessing.getInstance();

const doiData = {
recentVisitedFiles:doiProcessing.getVisitedFiles(),
recentSearches:[],
recentElements:[]
};

/* doi processing has to be imitated -
ws.send(JSON.stringify({
command:WebSocketConstants.SEND_DOI_INFORMATION,
data:doiData
}));

DoiProcessing doiClass = DoiProcessing.getInstance();
FileChangeManager.getInstance().sendMessage(MessageProcessor.encodeData(new Object[]{
WebSocketConstants.SEND_DOI_INFORMATION, MessageProcessor.encodeDoiInformation(
new Object[]{doiClass.getVisitedFiles(), doiClass.getSearchHistory(),
doiClass.getVisitedElements()}
)}).toString());
*/

/*ws.send(MessageProcessor.encodeData({
command: WebSocketConstants.SEND_REQUEST_MINE_RULES_FOR_ELEMENT,
data: ""
}));*/
ws.send(JSON.stringify({
command: WebSocketConstants.SEND_REQUEST_MINE_RULES_FOR_ELEMENT,
data: ""
Expand Down

0 comments on commit 2ae0ab2

Please sign in to comment.