|
1 | 1 | // chat.ts |
2 | 2 | import cbws from './websocket'; |
3 | | -import { EventEmitter } from 'events'; |
4 | 3 | import { ChatMessage, UserMessage } from '@codebolt/types' |
5 | 4 |
|
6 | 5 | 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() |
14 | 6 | /** |
15 | 7 | * Chat module to interact with the WebSocket server. |
16 | 8 | */ |
@@ -62,36 +54,7 @@ const cbchat = { |
62 | 54 | } |
63 | 55 | waitForConnection(); |
64 | 56 | }, |
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 | | - }; |
91 | 57 |
|
92 | | - waitForConnection(); |
93 | | - return eventEmitter; |
94 | | - }, |
95 | 58 | /** |
96 | 59 | * Sends a message through the WebSocket connection. |
97 | 60 | * @param {string} message - The message to be sent. |
@@ -123,26 +86,28 @@ const cbchat = { |
123 | 86 | }); |
124 | 87 | }, |
125 | 88 | /** |
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. |
128 | 92 | */ |
129 | | - processStarted: () => { |
| 93 | + processStarted: (onStopClicked?: (message: any) => void) => { |
130 | 94 | // Send the process started message |
131 | 95 | cbws.getWebsocket.send(JSON.stringify({ |
132 | 96 | "type": "processStarted" |
133 | 97 | })); |
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 | + } |
142 | 108 |
|
143 | | - // Return an object that includes the event emitter and the stopProcess method |
| 109 | + // Return an object that includes the stopProcess method |
144 | 110 | return { |
145 | | - event: eventEmitter, |
146 | 111 | stopProcess: () => { |
147 | 112 | // Implement the logic to stop the process here |
148 | 113 | // For example, you might want to send a specific message to the server to stop the process |
|
0 commit comments