Skip to content

Commit 9243618

Browse files
committed
updated structure
1 parent 9600fe9 commit 9243618

File tree

2 files changed

+56
-50
lines changed

2 files changed

+56
-50
lines changed

src/index.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,47 @@ class Codebolt { // Extend EventEmitter
100100
agent = cbagent;
101101
utils = cbutils;
102102

103+
/**
104+
* Sets up a listener for incoming user messages with a direct handler function.
105+
* @param {Function} handler - The handler function to call when a user message is received.
106+
* @returns {void}
107+
*/
108+
onUserMessage(handler: (userMessage: any) => void | Promise<void> | any | Promise<any>) {
109+
const waitForConnection = () => {
110+
if (cbws.getWebsocket) {
111+
cbws.getWebsocket.on('message', async (data: string) => {
112+
const response = JSON.parse(data);
113+
if (response.type === "messageResponse") {
114+
try {
115+
const result = await handler(response);
116+
// Send processStoped with optional message
117+
const message: any = {
118+
"type": "processStoped"
119+
};
120+
121+
// If handler returned data, include it as message
122+
if (result !== undefined && result !== null) {
123+
message.message = result;
124+
}
125+
126+
cbws.getWebsocket.send(JSON.stringify(message));
127+
} catch (error) {
128+
console.error('Error in user message handler:', error);
129+
// Send processStoped even if there's an error
130+
cbws.getWebsocket.send(JSON.stringify({
131+
"type": "processStoped",
132+
"error": error instanceof Error ? error.message : "Unknown error occurred"
133+
}));
134+
}
135+
}
136+
});
137+
} else {
138+
setTimeout(waitForConnection, 100);
139+
}
140+
};
141+
142+
waitForConnection();
143+
}
103144
}
104145

105146
export default new Codebolt();

src/modules/chat.ts

Lines changed: 15 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
// chat.ts
22
import cbws from './websocket';
3-
import { EventEmitter } from 'events';
43
import { ChatMessage, UserMessage } from '@codebolt/types'
54

65
type RequestHandler = (request: any, response: (data: any) => void) => Promise<void> | void;
7-
8-
9-
/**
10-
* CustomEventEmitter class that extends the Node.js EventEmitter class.
11-
*/
12-
class CustomEventEmitter extends EventEmitter { }
13-
let eventEmitter = new CustomEventEmitter()
146
/**
157
* Chat module to interact with the WebSocket server.
168
*/
@@ -62,36 +54,7 @@ const cbchat = {
6254
}
6355
waitForConnection();
6456
},
65-
/**
66-
* Sets up a listener for incoming WebSocket messages and emits a custom event when a message is received.
67-
* @returns {EventEmitter} The event emitter used for emitting custom events.
68-
*/
69-
/**
70-
* Sets up a listener for incoming WebSocket messages and emits a custom event when a message is received.
71-
* @returns {EventEmitter} The event emitter used for emitting custom events.
72-
*/
73-
onActionMessage: () => {
74-
const waitForConnection = () => {
75-
if (cbws.getWebsocket) {
76-
cbws.getWebsocket.on('message', (data: string) => {
77-
const response = JSON.parse(data);
78-
if (response.type === "messageResponse") {
79-
eventEmitter.emit("userMessage", response, (message: string) => {
80-
cbws.getWebsocket.send(JSON.stringify({
81-
"type": "processStoped",
82-
"message": message
83-
}));
84-
});
85-
}
86-
});
87-
} else {
88-
setTimeout(waitForConnection, 100);
89-
}
90-
};
9157

92-
waitForConnection();
93-
return eventEmitter;
94-
},
9558
/**
9659
* Sends a message through the WebSocket connection.
9760
* @param {string} message - The message to be sent.
@@ -123,26 +86,28 @@ const cbchat = {
12386
});
12487
},
12588
/**
126-
* Notifies the server that a process has started and sets up an event listener for stopProcessClicked events.
127-
* @returns An object containing the event emitter and a stopProcess method.
89+
* Notifies the server that a process has started and sets up a listener for stopProcessClicked events.
90+
* @param {Function} onStopClicked - Callback function to handle stop process events.
91+
* @returns An object containing a stopProcess method.
12892
*/
129-
processStarted: () => {
93+
processStarted: (onStopClicked?: (message: any) => void) => {
13094
// Send the process started message
13195
cbws.getWebsocket.send(JSON.stringify({
13296
"type": "processStarted"
13397
}));
134-
// Register event listener for WebSocket messages
135-
cbws.getWebsocket.on('message', (data: string) => {
136-
const message = JSON.parse(data);
137-
if (message.type === 'stopProcessClicked')
138-
139-
// Emit a custom event based on the message type
140-
eventEmitter.emit("stopProcessClicked", message);
141-
});
98+
99+
// Register event listener for WebSocket messages if callback provided
100+
if (onStopClicked) {
101+
cbws.getWebsocket.on('message', (data: string) => {
102+
const message = JSON.parse(data);
103+
if (message.type === 'stopProcessClicked') {
104+
onStopClicked(message);
105+
}
106+
});
107+
}
142108

143-
// Return an object that includes the event emitter and the stopProcess method
109+
// Return an object that includes the stopProcess method
144110
return {
145-
event: eventEmitter,
146111
stopProcess: () => {
147112
// Implement the logic to stop the process here
148113
// For example, you might want to send a specific message to the server to stop the process

0 commit comments

Comments
 (0)