class LocalDataTrack { constructor() { this.dataChannelMessages = [] this.builded = false } build(janusRoom) { this.janusRoom = janusRoom } send(data) { if (!this.janusRoom) { this.dataChannelMessages.push(data) } else { if (!this.builded) { this.janusRoom._dataChannelMessages.push(...this.dataChannelMessages) this.dataChannelMessages = [] this.builded = true } this.janusRoom._sendData(data) } } } class JanusRoom { constructor(newRoom) { if (typeof newRoom === 'undefined') { throw new Error('need to use build(token, connectOptions)') } this.janusRoom = newRoom JanusRoom.LocalVideoTrack.build(newRoom) } } room = JanusRoom() localDataTracks.forEach(localDataTrack => localDataTracks.build(room))