Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Prevent joining a channel multiple times #146

Merged
merged 2 commits into from
Dec 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions src/stores/NetworkStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ export default class NetworkStore {
@observable
channels = {}

// Keeps track of the channel join promises
// This helps us to prevent joining same channel multiple times in quick succession
_joiningChannelPromises = {}

@observable
swarmPeers = []

Expand Down Expand Up @@ -174,10 +178,14 @@ export default class NetworkStore {
async joinChannel (channelName) {
if (typeof channelName !== 'string') return
if (!this.isOnline) throw new Error('Network is not online')
if (!this.channelNames.includes(channelName)) {
await this.workerProxy.joinChannel(channelName)
if (
!(channelName in this._joiningChannelPromises) &&
!this.channelNames.includes(channelName)) {
this._joiningChannelPromises[channelName] = this.workerProxy.joinChannel(channelName)
.then(() => this._onJoinedChannel(channelName))
}
return this._onJoinedChannel(channelName)

return this._joiningChannelPromises[channelName]
}

async leaveChannel (channelName) {
Expand Down