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

Commit

Permalink
Merge pull request #146 from orbitdb/fix/prevent-multiple-join-calls
Browse files Browse the repository at this point in the history
Prevent joining a channel multiple times
  • Loading branch information
latenssi authored Dec 12, 2019
2 parents 7219b49 + 894194d commit d04170f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
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

0 comments on commit d04170f

Please sign in to comment.