Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Partykit handle null connection state #252

Merged
merged 1 commit into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions party/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type * as Party from "partykit/server";
import type { Connection, RemoveMessage, SyncMessage, UpdateMessage, UserDetails } from "./types";
import type { Connection, MaybeUserDetails, RemoveMessage, SyncMessage, UpdateMessage, UserDetails } from "./types";

export default class Server implements Party.Server {
constructor(readonly room: Party.Room) { }
Expand All @@ -8,17 +8,17 @@ export default class Server implements Party.Server {
console.log(`Connected: id: ${conn.id} room: ${this.room.id} url: ${new URL(ctx.request.url).pathname}`
);

const connections = []
const connections = new Map<string, MaybeUserDetails>()
for (const connection of this.room.getConnections()) {
if (connection.id === conn.id) {
continue
}
connections.push(connection.state as UserDetails)
connections.set(connection.id, connection.state as MaybeUserDetails)
}

const message = <SyncMessage>{
type: 'sync',
connections: connections
connections: Object.fromEntries(connections.entries())
}

conn.send(JSON.stringify(message));
Expand Down
4 changes: 3 additions & 1 deletion party/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export type UserDetails = {
spent?: number
} & Cursor

export type MaybeUserDetails = UserDetails | null

export type UpdateMessage = {
type: 'update',
details: UserDetails
Expand All @@ -24,7 +26,7 @@ export type RemoveMessage = {

export type SyncMessage = {
type: 'sync',
connections: UserDetails[]
connections: Record<string, UserDetails | null>
}

export type Connection = PartyConnection & { state: UserDetails | undefined }
Loading