Skip to content

Commit

Permalink
Merge pull request #252 from hassnian/issue-pr-9439
Browse files Browse the repository at this point in the history
  • Loading branch information
vikiival authored Feb 17, 2024
2 parents cb2c510 + 235c534 commit bb30af0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
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 }

0 comments on commit bb30af0

Please sign in to comment.