Skip to content

Commit

Permalink
Refactor and rename getPeers -> refreshConsumers
Browse files Browse the repository at this point in the history
  • Loading branch information
keianhzo committed Feb 24, 2021
1 parent 05b7f7e commit cfe52fb
Showing 1 changed file with 43 additions and 88 deletions.
131 changes: 43 additions & 88 deletions lib/Room.js
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,44 @@ class Room extends EventEmitter
});
}

async _consumeExistingProducers(peer, joinedPeers) {
for (const joinedPeer of joinedPeers)
{
// Create Consumers for existing Producers.
for (const producer of joinedPeer.data.producers.values())
{
await this._createConsumer(
{
consumerPeer : peer,
producerPeer : joinedPeer,
producer
});
}

// Create DataConsumers for existing DataProducers.
for (const dataProducer of joinedPeer.data.dataProducers.values())
{
if (dataProducer.label === 'bot')
continue;

await this._createDataConsumer(
{
dataConsumerPeer : peer,
dataProducerPeer : joinedPeer,
dataProducer
});
}
}

// Create DataConsumers for bot DataProducer.
await this._createDataConsumer(
{
dataConsumerPeer : peer,
dataProducerPeer : null,
dataProducer : this._bot.dataProducer
});
}

/**
* Handle protoo requests from browsers.
*
Expand Down Expand Up @@ -764,41 +802,7 @@ class Room extends EventEmitter
// Mark the new Peer as joined.
peer.data.joined = true;

for (const joinedPeer of joinedPeers)
{
// Create Consumers for existing Producers.
for (const producer of joinedPeer.data.producers.values())
{
this._createConsumer(
{
consumerPeer : peer,
producerPeer : joinedPeer,
producer
});
}

// Create DataConsumers for existing DataProducers.
for (const dataProducer of joinedPeer.data.dataProducers.values())
{
if (dataProducer.label === 'bot')
continue;

this._createDataConsumer(
{
dataConsumerPeer : peer,
dataProducerPeer : joinedPeer,
dataProducer
});
}
}

// Create DataConsumers for bot DataProducer.
this._createDataConsumer(
{
dataConsumerPeer : peer,
dataProducerPeer : null,
dataProducer : this._bot.dataProducer
});
await this._consumeExistingProducers(peer, joinedPeers);

// Notify the new Peer to all other Peers.
for (const otherPeer of this._getJoinedPeers({ excludePeer: peer }))
Expand All @@ -816,70 +820,21 @@ class Room extends EventEmitter
break;
}

case 'getPeers':
case 'refreshConsumers':
{
// Ensure the Peer is already joined.
if (!peer.data.joined)
throw new Error('Peer not joined');

// Tell the new Peer about already joined Peers.
// And also create Consumers for existing Producers.
accept();

const joinedPeers =
[
...this._getJoinedPeers(),
...this._getJoinedPeers({ excludePeer: peer }),
...this._broadcasters.values()
];

// Reply now the request with the list of joined peers (all but the new one).
const peerInfos = joinedPeers
.filter((joinedPeer) => joinedPeer.id !== peer.id)
.map((joinedPeer) => ({
id : joinedPeer.id,
displayName : joinedPeer.data.displayName,
device : joinedPeer.data.device,
hasProducers : joinedPeer.data.producers.size > 0
}));

accept({ peers: peerInfos });

for (const joinedPeer of joinedPeers)
{
if (joinedPeer.id !== peer.id) {
// Create Consumers for existing Producers.
for (const producer of joinedPeer.data.producers.values())
{
this._createConsumer(
{
consumerPeer : peer,
producerPeer : joinedPeer,
producer
});
}

// Create DataConsumers for existing DataProducers.
for (const dataProducer of joinedPeer.data.dataProducers.values())
{
if (dataProducer.label === 'bot')
continue;

this._createDataConsumer(
{
dataConsumerPeer : peer,
dataProducerPeer : joinedPeer,
dataProducer
});
}
}
}

// Create DataConsumers for bot DataProducer.
this._createDataConsumer(
{
dataConsumerPeer : peer,
dataProducerPeer : null,
dataProducer : this._bot.dataProducer
});
await this._consumeExistingProducers(peer, joinedPeers);

break;
}
Expand Down

0 comments on commit cfe52fb

Please sign in to comment.