import { SingleConnection } from "../client/connection" ; import { MAX_OUTGOING_COMMAND_LENGTH } from "../constants" ; import { splitIntoChunks } from "../utils/split-into-chunks" ; import { awaitJoinResponse } from "./join" ; export async function joinAll ( conn , channelNames ) { channelNames . forEach ( ( channelName ) => conn . wantedChannels . add ( channelName ) ) ; const channelChunks = splitIntoChunks ( channelNames . map ( ( e ) => `#${ e }` ) , "," , MAX_OUTGOING_COMMAND_LENGTH - "JOIN " . length ) ; const resultsMap = { } ; for ( const chunk of channelChunks ) { conn . sendRaw ( `JOIN ${ chunk . join ( "," ) }` ) ; const chunkNames = chunk . map ( ( s ) => s . slice ( 1 ) ) ; const chunkPromises = [ ] ; for ( const channelName of chunkNames ) { chunkPromises . push ( awaitJoinResponse ( conn , channelName ) . then ( ( ) => { conn . joinedChannels . add ( channelName ) ; resultsMap [ channelName ] = undefined ; } , ( error ) => { resultsMap [ channelName ] = error ; } ) ) ; } await Promise . all ( chunkPromises ) ; } return resultsMap ; }