Skip to content

Commit 8adc8a8

Browse files
committed
Make the Networker a Nanoresource
1 parent 5052d9f commit 8adc8a8

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

index.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const crypto = require('crypto')
22
const { EventEmitter } = require('events')
33
const { promisify } = require('util')
44

5+
const { NanoresourcePromise: Nanoresource } = require('nanoresource-promise/emitter')
56
const HypercoreProtocol = require('hypercore-protocol')
67
const hyperswarm = require('hyperswarm')
78
const codecs = require('codecs')
@@ -13,7 +14,7 @@ const log = require('debug')('corestore:network')
1314
const OUTER_STREAM = Symbol('networker-outer-stream')
1415
const STREAM_PEER = Symbol('networker-stream-peer')
1516

16-
class CorestoreNetworker extends EventEmitter {
17+
class CorestoreNetworker extends Nanoresource {
1718
constructor (corestore, opts = {}) {
1819
super()
1920
this.corestore = corestore
@@ -145,7 +146,7 @@ class CorestoreNetworker extends EventEmitter {
145146
}
146147
}
147148

148-
listen () {
149+
_open () {
149150
const self = this
150151
if (this.swarm) return
151152

@@ -196,6 +197,10 @@ class CorestoreNetworker extends EventEmitter {
196197
})
197198
}
198199

200+
listen () {
201+
return this.open()
202+
}
203+
199204
status (discoveryKey) {
200205
if (Buffer.isBuffer(discoveryKey)) discoveryKey = discoveryKey.toString('hex')
201206
return this._configurations.get(discoveryKey)
@@ -250,12 +255,9 @@ class CorestoreNetworker extends EventEmitter {
250255
return ext
251256
}
252257

253-
async close () {
258+
async _close () {
254259
if (!this.swarm) return null
255260

256-
const leaving = [...this._joined].map(dkey => this._leave(dkey))
257-
await Promise.all(leaving)
258-
259261
for (const ext of this._extensions) {
260262
ext.destroy()
261263
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"end-of-stream": "^1.4.4",
99
"hypercore-protocol": "^8.0.0",
1010
"hyperswarm": "^2.14.1",
11+
"nanoresource-promise": "^1.2.2",
1112
"pump": "^3.0.0"
1213
},
1314
"devDependencies": {

0 commit comments

Comments
 (0)