Skip to content

Commit 727bf44

Browse files
Merge pull request #238 from abdeljalil-salhi/237-add-shared-and-priority-queues
Add shared and priority queues logic
2 parents dd9959d + db92a25 commit 727bf44

File tree

9 files changed

+616
-25
lines changed

9 files changed

+616
-25
lines changed

backend-nestjs/package-lock.json

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend-nestjs/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
"qrcode": "^1.5.3",
4747
"reflect-metadata": "^0.2.1",
4848
"rxjs": "^7.8.1",
49-
"socket-io": "^1.0.0"
49+
"socket-io": "^1.0.0",
50+
"uuid": "^9.0.1"
5051
},
5152
"devDependencies": {
5253
"@nestjs/cli": "^10.0.0",
@@ -59,6 +60,7 @@
5960
"@types/nodemailer": "^6.4.15",
6061
"@types/passport-jwt": "^4.0.1",
6162
"@types/supertest": "^6.0.2",
63+
"@types/uuid": "^9.0.8",
6264
"@typescript-eslint/eslint-plugin": "^7.2.0",
6365
"@typescript-eslint/parser": "^7.2.0",
6466
"eslint": "^8.42.0",
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Dependencies
2+
import { Socket } from 'socket.io';
3+
4+
/**
5+
* Type of the SocketIO middleware function.
6+
*
7+
* @export
8+
* @type {SocketIOMiddleware}
9+
*/
10+
export type SocketIOMiddleware = {
11+
(client: Socket, next: (err?: Error) => void): void;
12+
};

backend-nestjs/src/auth/middlewares/websocket.middleware.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,8 @@ import { Socket } from 'socket.io';
33

44
// Guards
55
import { WebsocketGuard } from '../guards/websocket.guard';
6-
7-
/**
8-
* Type of the SocketIO middleware function.
9-
*
10-
* @export
11-
* @type {SocketIOMiddleware}
12-
*/
13-
export type SocketIOMiddleware = {
14-
(client: Socket, next: (err?: Error) => void): void;
15-
};
6+
// Middlewares
7+
import { SocketIOMiddleware } from './socket-io.middleware';
168

179
/**
1810
* Middleware to protect websockets by checking the validity of the access token.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Dependencies
2+
import { Socket } from 'socket.io';
3+
4+
/**
5+
* Interface of the queue member.
6+
*
7+
* @export
8+
* @interface QueueMember
9+
* @property {string} instanceId - The instance ID.
10+
* @property {Socket} client - The socket client.
11+
* @property {string} userId - The user ID.
12+
*/
13+
export interface QueueMember {
14+
instanceId: string;
15+
client: Socket;
16+
userId: string;
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Interface for the socket user client.
3+
*
4+
* @export
5+
* @interface SocketUserClient
6+
* @property {string} userId - The user ID
7+
* @property {string[]} socketIds - The user's socket IDs
8+
*/
9+
export interface SocketUserClient {
10+
userId: string;
11+
socketIds: string[];
12+
}

backend-nestjs/src/socket/queue.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* Queue data structure
3+
*
4+
* @export
5+
* @class Queue
6+
* @template T
7+
* @description
8+
* A queue is a data structure that follows the FIFO (First In First Out) principle.
9+
* This means that the first element added to the queue will be the first one to be removed.
10+
*
11+
* @example
12+
* const queue = new Queue<number>();
13+
* queue.enqueue(1);
14+
* queue.enqueue(2);
15+
* queue.enqueue(3);
16+
*
17+
* console.log(queue.dequeue()); // 1
18+
* console.log(queue.dequeue()); // 2
19+
* console.log(queue.dequeue()); // 3
20+
*/
21+
export class Queue<T> {
22+
/**
23+
* The items in the queue
24+
* @type {Array<T>}
25+
*/
26+
private items: Array<T> = new Array<T>();
27+
28+
/**
29+
* Creates an instance of the Queue class.
30+
*
31+
* @param {Array<T>} [content=[]] - The items to add to the queue
32+
*/
33+
constructor(content: Array<T> = new Array<T>()) {
34+
this.items = content;
35+
}
36+
37+
/**
38+
* Adds an item to the queue
39+
*
40+
* @param {T} item - The item to add
41+
* @returns {void}
42+
*/
43+
public enqueue(item: T): void {
44+
this.items.push(item);
45+
}
46+
47+
/**
48+
* Removes an item from the queue
49+
*
50+
* @returns {T | undefined} - The item removed from the queue
51+
*/
52+
public dequeue(): T | undefined {
53+
return this.items.shift();
54+
}
55+
56+
/**
57+
* Returns the size of the queue
58+
*
59+
* @returns {number} - The size of the queue
60+
*/
61+
public size(): number {
62+
return this.items.length;
63+
}
64+
65+
/**
66+
* Checks if the queue is empty
67+
*
68+
* @returns {boolean} - True if the queue is empty, false otherwise
69+
*/
70+
public isEmpty(): boolean {
71+
return this.items.length === 0;
72+
}
73+
74+
/**
75+
* Returns the first item in the queue
76+
*
77+
* @returns {T | undefined} - The first item in the queue
78+
*/
79+
public peek(): T | undefined {
80+
return this.items[0];
81+
}
82+
83+
/**
84+
* Returns the items in the queue
85+
*
86+
* @returns {T[]} - The items in the queue
87+
*/
88+
public getItems(): T[] {
89+
return this.items;
90+
}
91+
}

0 commit comments

Comments
 (0)