Skip to content

Commit 4bf1cd0

Browse files
changes
1 parent b7f9358 commit 4bf1cd0

File tree

2 files changed

+63
-115
lines changed

2 files changed

+63
-115
lines changed

src/modules/chat.ts

Lines changed: 62 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,180 +1,128 @@
1-
// chat.ts
21
import cbws from './websocket';
32
import { EventEmitter } from 'events';
4-
import { ChatMessage, UserMessage } from '@codebolt/types'
3+
import { ChatMessage, UserMessage } from '@codebolt/types';
54

5+
class CustomEventEmitter extends EventEmitter {}
66

7+
class Chat {
8+
private wsManager: cbws;
9+
private ws: any;
10+
private eventEmitter: CustomEventEmitter;
711

8-
/**
9-
* CustomEventEmitter class that extends the Node.js EventEmitter class.
10-
*/
11-
class CustomEventEmitter extends EventEmitter { }
12-
let eventEmitter = new CustomEventEmitter()
13-
/**
14-
* Chat module to interact with the WebSocket server.
15-
*/
16-
const cbchat = {
12+
constructor(wsManager: cbws) {
13+
this.wsManager = wsManager;
14+
this.ws = this.wsManager.getWebsocket() as any;
15+
this.eventEmitter = new CustomEventEmitter();
16+
}
1717

18-
/**
19-
* Retrieves the chat history from the server.
20-
* @returns {Promise<ChatMessage[]>} A promise that resolves with an array of ChatMessage objects representing the chat history.
21-
*/
22-
getChatHistory: (): Promise<ChatMessage[]> => {
18+
getChatHistory = (): Promise<ChatMessage[]> => {
2319
return new Promise((resolve, reject) => {
24-
cbws.getWebsocket.send(JSON.stringify({
20+
this.ws.send(JSON.stringify({
2521
"type": "getChatHistory"
2622
}));
27-
cbws.getWebsocket.on('message', (data: string) => {
23+
this.ws.on('message', (data: string) => {
2824
const response = JSON.parse(data);
2925
if (response.type === "getChatHistoryResponse") {
30-
resolve(response); // Resolve the Promise with the response data
26+
resolve(response);
3127
}
32-
})
33-
})
34-
},
28+
});
29+
});
30+
}
3531

36-
/**
37-
* Sets up a listener for incoming WebSocket messages and emits a custom event when a message is received.
38-
* @returns {EventEmitter} The event emitter used for emitting custom events.
39-
*/
40-
onActionMessage: () => {
41-
if (!cbws.getWebsocket) return;
42-
cbws.getWebsocket.on('message', (data: string) => {
32+
onActionMessage = () => {
33+
if (!this.ws) return;
34+
this.ws.on('message', (data: string) => {
4335
const response = JSON.parse(data);
4436
if (response.type === "messageResponse") {
45-
// Pass a callback function as an argument to the emit method
46-
eventEmitter.emit("userMessage", response, (message: string) => {
37+
this.eventEmitter.emit("userMessage", response, (message: string) => {
4738
console.log("Callback function invoked with message:", message);
48-
cbws.getWebsocket.send(JSON.stringify({
39+
this.ws.send(JSON.stringify({
4940
"type": "processStoped"
5041
}));
5142
});
5243
}
5344
});
54-
return eventEmitter;
55-
},
45+
return this.eventEmitter;
46+
}
5647

57-
/**
58-
* Sends a message through the WebSocket connection.
59-
* @param {string} message - The message to be sent.
60-
*/
61-
sendMessage: (message: string) => {
48+
sendMessage = (message: string) => {
6249
console.log(message);
63-
cbws.getWebsocket.send(JSON.stringify({
50+
this.ws.send(JSON.stringify({
6451
"type": "sendMessage",
6552
"message": message
6653
}));
67-
},
54+
}
6855

69-
/**
70-
* Waits for a reply to a sent message.
71-
* @param {string} message - The message for which a reply is expected.
72-
* @returns {Promise<UserMessage>} A promise that resolves with the reply.
73-
*/
74-
waitforReply: (message: string): Promise<UserMessage> => {
56+
waitforReply = (message: string): Promise<UserMessage> => {
7557
return new Promise((resolve, reject) => {
76-
cbws.getWebsocket.send(JSON.stringify({
58+
this.ws.send(JSON.stringify({
7759
"type": "waitforReply",
7860
"message": message
7961
}));
80-
cbws.getWebsocket.on('message', (data: string) => {
62+
this.ws.on('message', (data: string) => {
8163
const response = JSON.parse(data);
8264
if (response.type === "waitFormessageResponse") {
83-
resolve(response); // Resolve the Promise with the response data
65+
resolve(response);
8466
}
8567
});
8668
});
87-
},
69+
}
8870

89-
/**
90-
* Notifies the server that a process has started and sets up an event listener for stopProcessClicked events.
91-
* @returns An object containing the event emitter and a stopProcess method.
92-
*/
93-
processStarted: () => {
94-
// Send the process started message
95-
cbws.getWebsocket.send(JSON.stringify({
71+
processStarted = () => {
72+
this.ws.send(JSON.stringify({
9673
"type": "processStarted"
9774
}));
98-
// Register event listener for WebSocket messages
99-
cbws.getWebsocket.on('message', (data: string) => {
75+
this.ws.on('message', (data: string) => {
10076
const message = JSON.parse(data);
10177
console.log("Received message:", message);
102-
if (message.type === 'stopProcessClicked')
103-
104-
// Emit a custom event based on the message type
105-
eventEmitter.emit("stopProcessClicked", message);
78+
if (message.type === 'stopProcessClicked') {
79+
this.eventEmitter.emit("stopProcessClicked", message);
80+
}
10681
});
10782

108-
// Return an object that includes the event emitter and the stopProcess method
10983
return {
110-
event: eventEmitter,
111-
stopProcess: () => {
112-
// Implement the logic to stop the process here
113-
console.log("Stopping process...");
114-
// For example, you might want to send a specific message to the server to stop the process
115-
cbws.getWebsocket.send(JSON.stringify({
116-
"type": "processStoped"
117-
}));
118-
}
84+
event: this.eventEmitter,
85+
stopProcess: this.stopProcess
11986
};
120-
},
121-
/**
122-
* Stops the ongoing process.
123-
* Sends a specific message to the server to stop the process.
124-
*/
125-
stopProcess: () => {
126-
// Implement the logic to stop the process here
87+
}
88+
89+
stopProcess = () => {
12790
console.log("Stopping process...");
128-
// For example, you might want to send a specific message to the server to stop the process
129-
cbws.getWebsocket.send(JSON.stringify({
91+
this.ws.send(JSON.stringify({
13092
"type": "processStoped"
13193
}));
132-
},
133-
/**
134-
* Stops the ongoing process.
135-
* Sends a specific message to the server to stop the process.
136-
*/
137-
processFinished: () => {
138-
// Implement the logic to stop the process here
94+
}
95+
96+
processFinished = () => {
13997
console.log("Process Finished ...");
140-
// For example, you might want to send a specific message to the server to stop the process
141-
cbws.getWebsocket.send(JSON.stringify({
98+
this.ws.send(JSON.stringify({
14299
"type": "processFinished"
143100
}));
144-
},
101+
}
145102

146-
/**
147-
* Sends a confirmation request to the server with two options: Yes or No.
148-
* @returns {Promise<string>} A promise that resolves with the server's response.
149-
*/
150-
sendConfirmationRequest: (confirmationMessage: string, buttons: string[] = []): Promise<string> => {
103+
sendConfirmationRequest = (confirmationMessage: string, buttons: string[] = []): Promise<string> => {
151104
return new Promise((resolve, reject) => {
152-
cbws.getWebsocket.send(JSON.stringify({
105+
this.ws.send(JSON.stringify({
153106
"type": "confirmationRequest",
154107
"message": confirmationMessage,
155108
buttons: buttons
156-
157109
}));
158-
cbws.getWebsocket.on('message', (data: string) => {
110+
this.ws.on('message', (data: string) => {
159111
const response = JSON.parse(data);
160112
if (response.type === "confirmationResponse") {
161-
resolve(response); // Resolve the Promise with the server's response
113+
resolve(response);
162114
}
163115
});
164116
});
165-
},
166-
/**
167-
* Sends a notification event to the server.
168-
* @param {string} notificationMessage - The message to be sent in the notification.
169-
*/
170-
sendNotificationEvent: (notificationMessage: string, type: 'debug' | 'git' | 'planner' | 'browser' | 'editor' | 'terminal' | 'preview'): void => {
171-
cbws.getWebsocket.send(JSON.stringify({
117+
}
118+
119+
sendNotificationEvent = (notificationMessage: string, type: 'debug' | 'git' | 'planner' | 'browser' | 'editor' | 'terminal' | 'preview'): void => {
120+
this.ws.send(JSON.stringify({
172121
"type": "notificationEvent",
173122
"message": notificationMessage,
174-
"eventType":type
123+
"eventType": type
175124
}));
176-
},
177-
178-
};
125+
}
126+
}
179127

180-
export default cbchat;
128+
export default Chat;

src/modules/websocket.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class cbws {
1919
const uniqueConnectionId = this.getUniqueConnectionId();
2020
const initialMessage = this.getInitialMessage();
2121
this.websocket = new WebSocket(`ws://localhost:${process.env.SOCKET_PORT}/codebolt?id=${uniqueConnectionId}`);
22-
return this.initializeWebSocket(initialMessage);
22+
return await this.initializeWebSocket(initialMessage);
2323
}
2424

2525
async disconnect(): Promise<void> {

0 commit comments

Comments
 (0)