podchat-browser
Version:
Javascript SDK to use POD's Chat Service - Browser Only
63 lines (54 loc) • 1.99 kB
JavaScript
class MidManager {
constructor(props) {
this._app = props.app;
this._mids = {};
}
pushMidToList(topic, mids) {
this._mids[topic] = mids;
}
removeTopic(topic){
if(this._mids[topic]) {
delete this._mids[topic]
}
}
getTopicsOfMids(midList) {
let topics = [];
for(let topic in this._mids) {
for(let mid of midList) {
if(this._mids[topic].includes(mid)) {
topics.push(topic);
}
}
}
return topics;
}
/**
* We must close existing topic streams and open a new stream with the new mid
*/
async reOpenExistingDuplicateMids(list) {
/**
* If mid of a new track in the list, already exists, we should open that topic again
*/
let topics = this.getTopicsOfMids(list.flatMap(item => item.mids));
let call = this._app.call.currentCall();
for(let topic of topics) {
let userId = call.users().findUserIdByTopic(topic);
let userObj = call.users().get(userId);
if(topic.startsWith('Vi') || topic.startsWith('screen')) {
await userObj.destroyVideo();
// let obj = list.find(it => it.topic == topic);
// await userObj.startVideo(topic.replace("Vi-", ""), {...obj, version: userObj.getReceiveVideoVersion()});
} else {
await userObj.destroyAudio();
// let obj = list.find(it => it.topic == topic);
// await userObj.startAudio(topic.replace("Vi-", ""), {...obj, version: userObj.getReceiveAudioVersion()});
}
delete this._mids[topic];
}
for(let i in list) {
this.pushMidToList(list[i].topic, list[i].mids);
}
call.requestReceivingMedia();
}
}
export default MidManager;