Skip to content

Commit 09c45f9

Browse files
committed
Use event listener changes for (un)watching downloads.
1 parent c3f7153 commit 09c45f9

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

README.md

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,6 @@ See the [Hypercore docs](https://github.com/hypercore-protocol/hypercore) for mo
205205

206206
Append a block or array of blocks to the hypercore
207207

208-
#### `await feed.watchDownloads()`
209-
210-
Emit `download` events for all blocks downloaded from now.
211-
212-
#### `await feed.unwatchDownloads()`
213-
214-
Stop emitting `download` events.
215-
216208
#### `feed.peers`
217209

218210
A list of peers this feed is connected to.
@@ -231,7 +223,7 @@ Emitted when the feed is appended to, either locally or remotely.
231223

232224
#### `feed.on('download', seq)`
233225

234-
Emitted when block is downloaded. Has to be enabled with `watchDownloads()` first.
226+
Emitted when a block is downloaded.
235227

236228
# License
237229

index.js

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,21 @@ class RemoteHypercore extends Nanoresource {
363363
this._extensions = new Map()
364364
this._onwaits = new FreeMap(1)
365365

366+
// Track listeners for the download event, and enable/disable download watching.
367+
this._downloadListeners = 0
368+
this.on('newListener', (event, _listener) => {
369+
if (event === 'download') {
370+
if (!this._downloadListeners) this._watchDownloads()
371+
this._downloadListeners += 1
372+
}
373+
})
374+
this.on('removeListener', (event, _listener) => {
375+
if (event === 'download') {
376+
this._downloadListeners -= 1
377+
if (!this._downloadListeners) this._unwatchDownloads()
378+
}
379+
})
380+
366381
if (!this.lazy) this.ready(() => {})
367382
}
368383

@@ -579,13 +594,19 @@ class RemoteHypercore extends Nanoresource {
579594
}
580595

581596
async _watchDownloads () {
582-
if (!this.opened) await this.open()
583-
if (this.closed) throw new Error('Feed is closed')
584-
return this._client.hypercore.watchDownloads({ id: this._id })
597+
try {
598+
if (!this.opened) await this.open()
599+
if (this.closed) return
600+
await this._client.hypercore.watchDownloads({ id: this._id })
601+
} catch (_) {}
585602
}
586603

587604
async _unwatchDownloads () {
588-
return this._client.hypercore.unwatchDownloads({ id: this._id })
605+
try {
606+
if (!this.opened) await this.open()
607+
if (this.closed) return
608+
await this._client.hypercore.unwatchDownloads({ id: this._id })
609+
} catch (_) {}
589610
}
590611

591612
// Public Methods
@@ -710,14 +731,6 @@ class RemoteHypercore extends Nanoresource {
710731
return prom.then(() => () => this._client.hypercore.releaseLockNoReply({ id: this._id }))
711732
}
712733

713-
watchDownloads (cb) {
714-
return maybe(cb, this._watchDownloads())
715-
}
716-
717-
unwatchDownloads (cb) {
718-
return maybe(cb, this._unwatchDownloads())
719-
}
720-
721734
// TODO: Unimplemented methods
722735

723736
registerExtension (name, opts) {

0 commit comments

Comments
 (0)