|
1 | 1 | import cbws from './websocket';
|
2 | 2 |
|
3 | 3 | /**
|
4 |
| - * Fetches the list of tasks asynchronously. |
5 |
| - * @returns {Promise<any>} A promise that resolves with the list of tasks. |
| 4 | + * Manages task operations via WebSocket communication. |
6 | 5 | */
|
7 | 6 | const taskplaner = {
|
| 7 | + /** |
| 8 | + * Adds a task using a WebSocket message. |
| 9 | + * @param {string} task - The task to be added. |
| 10 | + * @returns {Promise<any>} A promise that resolves with the response from the add task event. |
| 11 | + */ |
| 12 | + addTask: async (task: string): Promise<any> => { |
| 13 | + return new Promise((resolve, reject) => { |
| 14 | + cbws.getWebsocket.send(JSON.stringify({ |
| 15 | + "type": "addTask", |
| 16 | + "task": task |
| 17 | + })); |
| 18 | + cbws.getWebsocket.on('message', (data: string) => { |
| 19 | + const response = JSON.parse(data); |
| 20 | + if (response.type === "addTaskResponse") { |
| 21 | + resolve(response); // Resolve the promise with the response data from adding the task |
| 22 | + } |
| 23 | + }); |
| 24 | + }); |
| 25 | + }, |
| 26 | + /** |
| 27 | + * Retrieves all tasks using a WebSocket message. |
| 28 | + * @returns {Promise<any>} A promise that resolves with the response from the get tasks event. |
| 29 | + */ |
8 | 30 | getTasks: async (): Promise<any> => {
|
9 | 31 | return new Promise((resolve, reject) => {
|
10 | 32 | cbws.getWebsocket.send(JSON.stringify({
|
11 |
| - "type": "getTasks", |
12 |
| - |
| 33 | + "type": "getTasks" |
13 | 34 | }));
|
14 | 35 | cbws.getWebsocket.on('message', (data: string) => {
|
15 | 36 | const response = JSON.parse(data);
|
16 | 37 | if (response.type === "getTasksResponse") {
|
17 |
| - resolve(response); // Resolve the Promise with the task data |
| 38 | + resolve(response); // Resolve the promise with the response data from retrieving tasks |
18 | 39 | }
|
19 | 40 | });
|
20 | 41 | });
|
|
0 commit comments