hyperswarm
Advanced tools
Comparing version 4.3.0 to 4.3.1
@@ -27,10 +27,10 @@ const safetyCatch = require('safety-catch') | ||
this._firstAnnounce = true | ||
this._needsUnannounce = false | ||
this._refreshes = 0 | ||
this.refresh().catch(this._onerror) | ||
} | ||
session (opts) { | ||
session ({ server = true, client = true, onerror = safetyCatch }) { | ||
if (this.destroyed) throw new Error('PeerDiscovery is destroyed') | ||
const session = new PeerDiscoverySession(this, opts) | ||
const session = new PeerDiscoverySession(this) | ||
session.refresh({ server, client }).catch(onerror) | ||
this._sessions.push(session) | ||
@@ -53,3 +53,3 @@ return session | ||
if (overdue) this._refreshLater(true) | ||
else this.refresh().catch(this._onerror) | ||
else this._refreshMaybe().catch(this._onerror) | ||
}, delay) | ||
@@ -74,7 +74,11 @@ } | ||
if (clock !== this._refreshes) return | ||
this._needsUnannounce = true | ||
} | ||
const query = this._activeQuery = this.isServer | ||
const announcing = this.isServer | ||
const query = this._activeQuery = announcing | ||
? this.swarm.dht.announce(this.topic, this.swarm.keyPair, this.swarm.server.relayAddresses, opts) | ||
: this.swarm.dht.lookup(this.topic, opts) | ||
: this._needsUnannounce | ||
? this.swarm.dht.lookupAndUnannounce(this.topic, this.swarm.keyPair, opts) | ||
: this.swarm.dht.lookup(this.topic, opts) | ||
@@ -97,5 +101,10 @@ try { | ||
this._closestNodes = query.closestNodes | ||
if (clock !== this._refreshes) return | ||
// In this is the latest query, unannounce has been fulfilled as well | ||
if (!announcing) this._needsUnannounce = false | ||
} | ||
async refresh () { | ||
async _refreshMaybe () { | ||
if (this.destroyed) throw new Error('PeerDiscovery is destroyed') | ||
@@ -141,2 +150,3 @@ | ||
if (this._sessions.length === 0) await this.destroy() | ||
else if (this._serverSessions === 0 && this._needsUnannounce) await this._refreshMaybe() | ||
} | ||
@@ -165,4 +175,6 @@ | ||
if (!this.isServer) return | ||
return this.swarm.dht.unannounce(this.topic, this.swarm.keyPair) | ||
if (this._needsUnannounce) { | ||
await this.swarm.dht.unannounce(this.topic, this.swarm.keyPair) | ||
this._needsUnannounce = false | ||
} | ||
} | ||
@@ -172,14 +184,7 @@ } | ||
class PeerDiscoverySession { | ||
constructor (discovery, { server = true, client = true, onerror = safetyCatch }) { | ||
constructor (discovery) { | ||
this.discovery = discovery | ||
this.isClient = client | ||
this.isServer = server | ||
this.isClient = false | ||
this.isServer = false | ||
this.destroyed = false | ||
this._onerror = onerror | ||
if (client) this.discovery._clientSessions++ | ||
if (server) this.discovery._serverSessions++ | ||
this.refresh().catch(this._onerror) | ||
} | ||
@@ -209,3 +214,3 @@ | ||
return this.discovery.refresh() | ||
return this.discovery._refreshMaybe() | ||
} | ||
@@ -212,0 +217,0 @@ |
{ | ||
"name": "hyperswarm", | ||
"version": "4.3.0", | ||
"version": "4.3.1", | ||
"description": "A distributed networking stack for connecting peers", | ||
@@ -5,0 +5,0 @@ "dependencies": { |
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
63041
1568