| | class MatchingQueue { |
| | constructor() { |
| | this.queue = []; |
| | } |
| |
|
| | add(socketId, user) { |
| | this.queue.push({ socketId, user, joinedAt: Date.now() }); |
| | } |
| |
|
| | remove(socketId) { |
| | this.queue = this.queue.filter(client => client.socketId !== socketId); |
| | } |
| |
|
| | findMatch(currentUser) { |
| | |
| | const candidates = this.queue.filter(c => c.user._id !== currentUser._id); |
| | |
| | if (candidates.length === 0) return null; |
| |
|
| | |
| | const oppositeGender = candidates.find( |
| | c => c.user.profile.gender !== currentUser.profile.gender |
| | ); |
| |
|
| | if (oppositeGender) return oppositeGender; |
| |
|
| | |
| | return candidates[0]; |
| | } |
| | } |
| |
|
| | export default new MatchingQueue(); |
| |
|