Skip to content

Commit

Permalink
fixup! refactor(#141): [wip] centralize room actions
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyckahn committed Dec 2, 2024
1 parent f0e0b93 commit 1ad316f
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions src/lib/PeerRoom/PeerRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ import {
Room,
BaseRoomConfig,
DataPayload,
ActionSender,
ActionReceiver,
RelayConfig,
} from 'trystero/torrent'
import { RelayConfig } from 'trystero/torrent'

import { sleep } from 'lib/sleep'
import {
Expand All @@ -18,6 +16,7 @@ import {
UnsentMessage,
} from 'models/chat'
import { PeerAction } from 'models/network'
import { ActionSender, ActionReceiver, ActionProgress } from 'trystero'

interface UserMetadata extends Record<string, any> {
userId: string
Expand Down Expand Up @@ -216,8 +215,33 @@ export class PeerRoom {
return peerConnections
}

makeAction = <T extends DataPayload>(peerAction: PeerAction) => {
return this.room.makeAction<T>(`${peerAction}`)
// FIXME: This is subscribing duplicate handlers
makeAction = <T extends DataPayload>(
peerAction: PeerAction
): [ActionSender<T>, ActionReceiver<T>, ActionProgress] => {
const [sender, receiver, progress] = this.room.makeAction<T>(
`${peerAction}`
)

const eventName = `peerRoomAction.${peerAction}`
const eventTarget = new EventTarget()

const dispatchReceiver: ActionReceiver<T> = callback => {
eventTarget.addEventListener(eventName, event => {
// @ts-expect-error
callback(...event.detail)
})
}

receiver((...args) => {
const customEvent = new CustomEvent(eventName, {
detail: args,
})

eventTarget.dispatchEvent(customEvent)
})

return [sender, dispatchReceiver, progress]
}

addStream = (
Expand Down

0 comments on commit 1ad316f

Please sign in to comment.