import { TypedEmitter } from 'tiny-typed-emitter' ; import type { Awaited } from '../util/util' ; /**
 * The known data for a user in a Discord voice connection.
 */ export interface VoiceUserData { /**
     * The SSRC of the user's audio stream.
     */ audioSSRC : number ; /**
     * The SSRC of the user's video stream (if one exists)
     * Cannot be 0. If undefined, the user has no video stream.
     */ videoSSRC ? : number ; /**
     * The Discord user id of the user.
     */ userId : string ; } /**
 * The events that an SSRCMap may emit.
 */ export interface SSRCMapEvents { create : ( newData : VoiceUserData ) => Awaited < void > ; update : ( oldData : VoiceUserData | undefined , newData : VoiceUserData ) => Awaited < void > ; delete : ( deletedData : VoiceUserData ) => Awaited < void > ; } /**
 * Maps audio SSRCs to data of users in voice connections.
 */ export class SSRCMap extends TypedEmitter < SSRCMapEvents > { /**
     * The underlying map.
     */ private readonly map : Map < number , VoiceUserData > ; public constructor ( ) { super ( ) ; this . map = new Map ( ) ; } param data public update ( data ) { const existing = this . map . get ( data . audioSSRC ) ; const newValue = { ... this . map . get ( data . audioSSRC ) , ... data , } ; this . map . set ( data . audioSSRC , newValue ) ; if ( ! existing ) this . emit ( 'create' , newValue ) ; this . emit ( 'update' , existing , newValue ) ; } param target public get ( target ) { if ( typeof target === 'number' ) { return this . map . get ( target ) ; } for ( const data of this . map . values ( ) ) { if ( data . userId === target ) { return data ; } } return undefined ; } param target returns public delete ( target ) { if ( typeof target === 'number' ) { const existing = this . map . get ( target ) ; if ( existing ) { this . map . delete ( target ) ; this . emit ( 'delete' , existing ) ; } return existing ; } for ( const [ audioSSRC , data ] of this . map . entries ( ) ) { if ( data . userId === target ) { this . map . delete ( audioSSRC ) ; this . emit ( 'delete' , data ) ; return data ; } } return undefined ; } }