Skip to content

Commit

Permalink
refactor(#141): [wip] enable multiple action receivers
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyckahn committed Nov 24, 2024
1 parent 258b42c commit 29b90d9
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions src/lib/PeerRoom/PeerRoom.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { joinRoom, Room, BaseRoomConfig, DataPayload } from 'trystero/torrent'
import { RelayConfig } from 'trystero/torrent'
import {
joinRoom,
Room,
BaseRoomConfig,
DataPayload,
ActionProgress,
ActionReceiver,
ActionSender,
RelayConfig,
} from 'trystero/torrent'

import { sleep } from 'lib/sleep'
import { StreamType } from 'models/chat'
Expand Down Expand Up @@ -169,11 +177,35 @@ export class PeerRoom {
return peerConnections
}

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

const eventName = `peerRoomAction.${namespace}.${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 29b90d9

Please sign in to comment.