From 708c266de483564de2e7b7fe71d89bc0ab59392c Mon Sep 17 00:00:00 2001 From: Kalle <38327916+Sendouc@users.noreply.github.com> Date: Mon, 2 Sep 2024 17:07:18 +0300 Subject: [PATCH] Tournament SSE tweaks --- .../core/emitters.server.ts | 2 + .../routes/to.$id.matches.$mid.tsx | 50 +++++++++++++++---- 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/app/features/tournament-bracket/core/emitters.server.ts b/app/features/tournament-bracket/core/emitters.server.ts index 60bc49a34..4eac0bd6e 100644 --- a/app/features/tournament-bracket/core/emitters.server.ts +++ b/app/features/tournament-bracket/core/emitters.server.ts @@ -5,5 +5,7 @@ const globalForEmitter = global as unknown as { }; export const emitter = globalForEmitter.emitter ?? new EventEmitter(); +// the default of 10 is not relevant for us because we use it for server-sent events +emitter.setMaxListeners(0); globalForEmitter.emitter = emitter; diff --git a/app/features/tournament-bracket/routes/to.$id.matches.$mid.tsx b/app/features/tournament-bracket/routes/to.$id.matches.$mid.tsx index e5231139f..bf730558b 100644 --- a/app/features/tournament-bracket/routes/to.$id.matches.$mid.tsx +++ b/app/features/tournament-bracket/routes/to.$id.matches.$mid.tsx @@ -121,6 +121,8 @@ export const action: ActionFunction = async ({ params, request }) => { }) : null; + let emitMatchUpdate = false; + let emitBracketUpdate = false; switch (data._action) { case "REPORT_SCORE": { // they are trying to report score that was already reported @@ -215,6 +217,9 @@ export const action: ActionFunction = async ({ params, request }) => { } })(); + emitMatchUpdate = true; + emitBracketUpdate = true; + break; } case "SET_ACTIVE_ROSTER": { @@ -243,6 +248,8 @@ export const action: ActionFunction = async ({ params, request }) => { activeRosterUserIds: data.roster, }); + emitMatchUpdate = true; + break; } case "UNDO_REPORT_SCORE": { @@ -310,6 +317,9 @@ export const action: ActionFunction = async ({ params, request }) => { } })(); + emitMatchUpdate = true; + emitBracketUpdate = true; + break; } case "UPDATE_REPORTED_SCORE": { @@ -373,6 +383,9 @@ export const action: ActionFunction = async ({ params, request }) => { } })(); + emitMatchUpdate = true; + emitBracketUpdate = true; + break; } case "BAN_PICK": { @@ -433,6 +446,8 @@ export const action: ActionFunction = async ({ params, request }) => { type: match.roundMaps.pickBan === "BAN_2" ? "BAN" : "PICK", }); + emitMatchUpdate = true; + break; } case "REOPEN_MATCH": { @@ -481,6 +496,9 @@ export const action: ActionFunction = async ({ params, request }) => { }); })(); + emitMatchUpdate = true; + emitBracketUpdate = true; + break; } case "SET_AS_CASTED": { @@ -492,6 +510,8 @@ export const action: ActionFunction = async ({ params, request }) => { twitchAccount: data.twitchAccount, }); + emitBracketUpdate = true; + break; } case "LOCK": { @@ -507,6 +527,8 @@ export const action: ActionFunction = async ({ params, request }) => { tournamentId: tournament.ctx.id, }); + emitMatchUpdate = true; + break; } case "UNLOCK": { @@ -517,6 +539,8 @@ export const action: ActionFunction = async ({ params, request }) => { tournamentId: tournament.ctx.id, }); + emitMatchUpdate = true; + break; } default: { @@ -524,17 +548,21 @@ export const action: ActionFunction = async ({ params, request }) => { } } - emitter.emit(matchSubscriptionKey(match.id), { - eventId: nanoid(), - userId: user.id, - }); - emitter.emit(bracketSubscriptionKey(tournament.ctx.id), { - matchId: match.id, - scores, - isOver: - scores[0] === Math.ceil(match.bestOf / 2) || - scores[1] === Math.ceil(match.bestOf / 2), - }); + if (emitMatchUpdate) { + emitter.emit(matchSubscriptionKey(match.id), { + eventId: nanoid(), + userId: user.id, + }); + } + if (emitBracketUpdate) { + emitter.emit(bracketSubscriptionKey(tournament.ctx.id), { + matchId: match.id, + scores, + isOver: + scores[0] === Math.ceil(match.bestOf / 2) || + scores[1] === Math.ceil(match.bestOf / 2), + }); + } clearTournamentDataCache(tournamentId);