hyperswarm
Advanced tools
Comparing version 4.3.4 to 4.3.5
21
index.js
@@ -327,6 +327,11 @@ const { EventEmitter } = require('events') | ||
const topicString = b4a.toString(topic, 'hex') | ||
if (this._discovery.has(topicString)) { | ||
return this._discovery.get(topicString).session(opts) | ||
let discovery = this._discovery.get(topicString) | ||
if (discovery && !discovery.destroyed) { | ||
return discovery.session(opts) | ||
} | ||
const discovery = new PeerDiscovery(this, topic, { | ||
discovery = new PeerDiscovery(this, topic, { | ||
wait: discovery ? discovery.destroy() : null, | ||
onpeer: peer => this._handlePeer(peer, topic) | ||
@@ -339,9 +344,13 @@ }) | ||
// Returns a promise | ||
leave (topic) { | ||
async leave (topic) { | ||
if (!topic) throw new Error(ERR_MISSING_TOPIC) | ||
const topicString = b4a.toString(topic, 'hex') | ||
if (!this._discovery.has(topicString)) return Promise.resolve() | ||
const discovery = this._discovery.get(topicString) | ||
this._discovery.delete(topicString) | ||
return discovery.destroy() | ||
await discovery.destroy() | ||
if (this._discovery.get(topicString) === discovery) { | ||
this._discovery.delete(topicString) | ||
} | ||
} | ||
@@ -348,0 +357,0 @@ |
@@ -8,3 +8,3 @@ const safetyCatch = require('safety-catch') | ||
module.exports = class PeerDiscovery { | ||
constructor (swarm, topic, { onpeer = noop, onerror = safetyCatch }) { | ||
constructor (swarm, topic, { wait = null, onpeer = noop, onerror = safetyCatch }) { | ||
this.swarm = swarm | ||
@@ -15,2 +15,3 @@ this.topic = topic | ||
this.destroyed = false | ||
this.destroying = null | ||
@@ -31,2 +32,3 @@ this._sessions = [] | ||
this._refreshes = 0 | ||
this._wait = wait | ||
} | ||
@@ -63,2 +65,9 @@ | ||
const clock = ++this._refreshes | ||
if (this._wait) { | ||
await this._wait | ||
this._wait = null | ||
if (clock !== this._refreshes) return | ||
} | ||
const clear = this.isServer && this._firstAnnounce | ||
@@ -149,2 +158,3 @@ if (clear) this._firstAnnounce = false | ||
async _destroyMaybe () { | ||
if (this.destroyed) return | ||
if (this._sessions.length === 0) await this.swarm.leave(this.topic) | ||
@@ -154,6 +164,14 @@ else if (this._serverSessions === 0 && this._needsUnannounce) await this.refresh() | ||
async destroy () { | ||
destroy () { | ||
if (this.destroying) return this.destroying | ||
this.destroying = this._destroy() | ||
return this.destroying | ||
} | ||
async _destroy () { | ||
if (this.destroyed) return | ||
this.destroyed = true | ||
if (this._wait) await this._wait | ||
if (this._activeQuery) { | ||
@@ -160,0 +178,0 @@ this._activeQuery.destroy() |
{ | ||
"name": "hyperswarm", | ||
"version": "4.3.4", | ||
"version": "4.3.5", | ||
"description": "A distributed networking stack for connecting peers", | ||
@@ -5,0 +5,0 @@ "files": [ |
@@ -5,8 +5,2 @@ # hyperswarm | ||
## NOTE: v3 | ||
Note that this is the README for v3 which is tagged under next. To see the v2 documentation/code go to https://github.com/hyperswarm/hyperswarm/tree/v2 | ||
As v3 fully matures over the next month it will be shifted to npm latest. | ||
## Installation | ||
@@ -13,0 +7,0 @@ ``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33704
745
153