Skip to content

Commit

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

import { sleep } from 'lib/sleep'
import {
Expand All @@ -16,7 +18,6 @@ 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 @@ -215,33 +216,8 @@ export class PeerRoom {
return peerConnections
}

// 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]
makeAction = <T extends DataPayload>(peerAction: PeerAction) => {
return this.room.makeAction<T>(`${peerAction}`)
}

addStream = (
Expand Down

0 comments on commit 7f45beb

Please sign in to comment.