-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8331391
commit e2e35a1
Showing
7 changed files
with
166 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import type { RealtimeMessage } from "@supabase/supabase-js" | ||
import { queryClient } from "~/root" | ||
import type { RoomLoaderData } from "~/routes/rooms.$roomId" | ||
|
||
export const handlePlayerUpdate = | ||
(roomId: string, serverLoader: () => Promise<RoomLoaderData>) => | ||
async (payload: RealtimeMessage["payload"]) => { | ||
const newItem = payload.new as { | ||
player_id: string | ||
isActive: boolean | ||
score: number | ||
id: string | ||
scrollPosition: { | ||
x: number | ||
y: number | ||
} | ||
} | ||
// biome-ignore lint/style/noNonNullAssertion: This will be there | ||
const data = queryClient.getQueryData<RoomLoaderData>(["room", roomId])! | ||
if (!data.players.find((p) => p.playerId === newItem.player_id)) { | ||
const data = await serverLoader() | ||
queryClient.setQueryData(["room", roomId], data) | ||
} | ||
queryClient.setQueryData<typeof data>(["room", roomId], { | ||
...data, | ||
players: data.players.map((player) => { | ||
if (player.playerId === newItem.player_id) { | ||
return { | ||
...player, | ||
isActive: newItem.isActive, | ||
score: newItem.score, | ||
scrollPosition: newItem.scrollPosition, | ||
} | ||
} | ||
return player | ||
}), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type { RealtimeMessage } from "@supabase/supabase-js" | ||
import { queryClient } from "~/root" | ||
import type { RoomLoaderData } from "~/routes/rooms.$roomId" | ||
|
||
export const handleRoomUpdate = (roomId: string) => (payload: RealtimeMessage["payload"]) => { | ||
const newRoomInfo = payload.new | ||
// biome-ignore lint/style/noNonNullAssertion: This will be there | ||
const data = queryClient.getQueryData<RoomLoaderData>(["room", roomId])! | ||
if(payload.new.id !== data.room.id) return | ||
queryClient.setQueryData<typeof data>(["room", data.room.id], { | ||
...data, | ||
room: { | ||
...data.room, | ||
...newRoomInfo, | ||
flippedIndices: newRoomInfo.flippedIndices ?? data.room.flippedIndices, | ||
matchedPairs: newRoomInfo.matchedPairs ?? data.room.matchedPairs, | ||
currentTurn: newRoomInfo.current_turn, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.