Skip to content

Commit

Permalink
fixup! refactor(#141): [wip] enable multiple action receivers
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyckahn committed Nov 25, 2024
1 parent 6541734 commit 36b370e
Showing 1 changed file with 47 additions and 28 deletions.
75 changes: 47 additions & 28 deletions src/lib/PeerRoom/PeerRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
import { sleep } from 'lib/sleep'
import { StreamType } from 'models/chat'
import { PeerAction } from 'models/network'
import memoize from 'fast-memoize'

export enum PeerHookType {
NEW_PEER = 'NEW_PEER',
Expand All @@ -35,6 +34,13 @@ export enum PeerConnectionType {

const streamQueueAddDelay = 1000

type PeerRoomAction<T extends DataPayload> = [
ActionSender<T>,
ActionReceiver<T>,
ActionProgress,
() => void,
]

export class PeerRoom {
private room: Room

Expand Down Expand Up @@ -71,6 +77,8 @@ export class PeerRoom {
this.isProcessingPendingStreams = false
}

private actions: Partial<Record<string, PeerRoomAction<any>>> = {}

constructor(config: RelayConfig & BaseRoomConfig, roomId: string) {
this.roomConfig = config
this.room = joinRoom(this.roomConfig, roomId)
Expand Down Expand Up @@ -178,44 +186,55 @@ export class PeerRoom {
return peerConnections
}

makeAction = memoize(
<T extends DataPayload>(
peerAction: PeerAction,
namespace?: string
): [ActionSender<T>, ActionReceiver<T>, ActionProgress, () => void] => {
const [sender, receiver, progress] = this.room.makeAction<T>(
`${namespace ?? '_'}.${peerAction}`
)
makeAction = <T extends DataPayload>(
peerAction: PeerAction,
namespace?: string
): PeerRoomAction<T> => {
const actionName = `${namespace ?? '_'}.${peerAction}`

const eventName = `peerRoomAction.${namespace ?? '_'}.${peerAction}`
const eventTarget = new EventTarget()
if (actionName in this.actions) {
return this.actions[actionName] as PeerRoomAction<T>
}

const [sender, receiver, progress] = this.room.makeAction<T>(actionName)

let handler: EventListenerOrEventListenerObject | null = null
const eventName = `peerRoomAction.${namespace ?? '_'}.${peerAction}`
const eventTarget = new EventTarget()

const dispatchReceiver: ActionReceiver<T> = callback => {
handler = (event: Event): void => {
// @ts-expect-error
callback(...event.detail)
}
let handler: EventListenerOrEventListenerObject | null = null

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

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

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

const detatchDispatchReceiver = () => {
eventTarget.removeEventListener(eventName, handler)
}
eventTarget.dispatchEvent(customEvent)
})

return [sender, dispatchReceiver, progress, detatchDispatchReceiver]
const detatchDispatchReceiver = () => {
eventTarget.removeEventListener(eventName, handler)
}
)

const action: PeerRoomAction<T> = [
sender,
dispatchReceiver,
progress,
detatchDispatchReceiver,
]

this.actions[actionName] = action

return action
}

addStream = (
stream: Parameters<Room['addStream']>[0],
Expand Down

0 comments on commit 36b370e

Please sign in to comment.