From c3f7153861064d7dad82577610469e14a2adda3b Mon Sep 17 00:00:00 2001 From: "Franz Heinzmann (Frando)" Date: Tue, 11 Aug 2020 01:43:23 +0200 Subject: [PATCH 1/4] Watch downloads --- README.md | 12 ++++++++++++ index.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/README.md b/README.md index 78e74dc..42a2476 100644 --- a/README.md +++ b/README.md @@ -205,6 +205,14 @@ See the [Hypercore docs](https://github.com/hypercore-protocol/hypercore) for mo Append a block or array of blocks to the hypercore +#### `await feed.watchDownloads()` + +Emit `download` events for all blocks downloaded from now. + +#### `await feed.unwatchDownloads()` + +Stop emitting `download` events. + #### `feed.peers` A list of peers this feed is connected to. @@ -221,6 +229,10 @@ Emitted when a peer is removed. Emitted when the feed is appended to, either locally or remotely. +#### `feed.on('download', seq)` + +Emitted when block is downloaded. Has to be enabled with `watchDownloads()` first. + # License MIT diff --git a/index.js b/index.js index 4f0cda3..e348dc8 100644 --- a/index.js +++ b/index.js @@ -73,6 +73,11 @@ class RemoteCorestore extends EventEmitter { const remoteCore = this._sessions.get(id) if (!remoteCore) throw new Error('Invalid RemoteHypercore ID.') remoteCore._onwait(onWaitId, seq) + }, + onDownload ({ id, seq }) { + const remoteCore = this._sessions.get(id) + if (!remoteCore) throw new Error('Invalid RemoteHypercore ID.') + remoteCore._ondownload({ seq }) } }) this._client.corestore.onRequest(this, { @@ -446,6 +451,11 @@ class RemoteHypercore extends Nanoresource { ext.onmessage(data, remotePeer) } + _ondownload (rsp) { + // TODO: Add to local bitfield? + this.emit('download', rsp.seq) + } + // Private Methods _indexOfPeer (remotePublicKey) { @@ -568,6 +578,16 @@ class RemoteHypercore extends Nanoresource { return rsp.bytes } + async _watchDownloads () { + if (!this.opened) await this.open() + if (this.closed) throw new Error('Feed is closed') + return this._client.hypercore.watchDownloads({ id: this._id }) + } + + async _unwatchDownloads () { + return this._client.hypercore.unwatchDownloads({ id: this._id }) + } + // Public Methods append (blocks, cb) { @@ -690,6 +710,14 @@ class RemoteHypercore extends Nanoresource { return prom.then(() => () => this._client.hypercore.releaseLockNoReply({ id: this._id })) } + watchDownloads (cb) { + return maybe(cb, this._watchDownloads()) + } + + unwatchDownloads (cb) { + return maybe(cb, this._unwatchDownloads()) + } + // TODO: Unimplemented methods registerExtension (name, opts) { From fbff4688ea1627fea58fc8b0c8b2ba297a950574 Mon Sep 17 00:00:00 2001 From: "Franz Heinzmann (Frando)" Date: Mon, 7 Sep 2020 13:59:14 +0200 Subject: [PATCH 2/4] Use event listener changes for (un)watching downloads. --- README.md | 10 +--------- index.js | 34 ++++++++++++++++++++++------------ 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 42a2476..f7c408e 100644 --- a/README.md +++ b/README.md @@ -205,14 +205,6 @@ See the [Hypercore docs](https://github.com/hypercore-protocol/hypercore) for mo Append a block or array of blocks to the hypercore -#### `await feed.watchDownloads()` - -Emit `download` events for all blocks downloaded from now. - -#### `await feed.unwatchDownloads()` - -Stop emitting `download` events. - #### `feed.peers` A list of peers this feed is connected to. @@ -231,7 +223,7 @@ Emitted when the feed is appended to, either locally or remotely. #### `feed.on('download', seq)` -Emitted when block is downloaded. Has to be enabled with `watchDownloads()` first. +Emitted when a block is downloaded. # License diff --git a/index.js b/index.js index e348dc8..0317b46 100644 --- a/index.js +++ b/index.js @@ -363,6 +363,18 @@ class RemoteHypercore extends Nanoresource { this._extensions = new Map() this._onwaits = new FreeMap(1) + // Track listeners for the download event, and enable/disable download watching. + this.on('newListener', (event, _listener) => { + if (event === 'download' && !this.listenerCount(event)) { + this._watchDownloads() + } + }) + this.on('removeListener', (event, _listener) => { + if (event === 'download' && !this.listenerCount(event)) { + this._unwatchDownloads() + } + }) + if (!this.lazy) this.ready(() => {}) } @@ -579,13 +591,19 @@ class RemoteHypercore extends Nanoresource { } async _watchDownloads () { - if (!this.opened) await this.open() - if (this.closed) throw new Error('Feed is closed') - return this._client.hypercore.watchDownloads({ id: this._id }) + try { + if (!this.opened) await this.open() + if (this.closed) return + await this._client.hypercore.watchDownloads({ id: this._id }) + } catch (_) {} } async _unwatchDownloads () { - return this._client.hypercore.unwatchDownloads({ id: this._id }) + try { + if (!this.opened) await this.open() + if (this.closed) return + await this._client.hypercore.unwatchDownloads({ id: this._id }) + } catch (_) {} } // Public Methods @@ -710,14 +728,6 @@ class RemoteHypercore extends Nanoresource { return prom.then(() => () => this._client.hypercore.releaseLockNoReply({ id: this._id })) } - watchDownloads (cb) { - return maybe(cb, this._watchDownloads()) - } - - unwatchDownloads (cb) { - return maybe(cb, this._unwatchDownloads()) - } - // TODO: Unimplemented methods registerExtension (name, opts) { From 3adbf7148f9120d1950a857fdb52f5d47abfb723 Mon Sep 17 00:00:00 2001 From: "Franz Heinzmann (Frando)" Date: Wed, 16 Sep 2020 11:45:52 +0200 Subject: [PATCH 3/4] Remove unused argument --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 0317b46..ca7789a 100644 --- a/index.js +++ b/index.js @@ -364,12 +364,12 @@ class RemoteHypercore extends Nanoresource { this._onwaits = new FreeMap(1) // Track listeners for the download event, and enable/disable download watching. - this.on('newListener', (event, _listener) => { + this.on('newListener', (event) => { if (event === 'download' && !this.listenerCount(event)) { this._watchDownloads() } }) - this.on('removeListener', (event, _listener) => { + this.on('removeListener', (event) => { if (event === 'download' && !this.listenerCount(event)) { this._unwatchDownloads() } From 3c9b51ae893e2230fe6eeef4e5d534f82358ef69 Mon Sep 17 00:00:00 2001 From: "Franz Heinzmann (Frando)" Date: Wed, 16 Sep 2020 11:56:39 +0200 Subject: [PATCH 4/4] Use noReply for (un)watching downloads --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index ca7789a..c17f7c9 100644 --- a/index.js +++ b/index.js @@ -594,7 +594,7 @@ class RemoteHypercore extends Nanoresource { try { if (!this.opened) await this.open() if (this.closed) return - await this._client.hypercore.watchDownloads({ id: this._id }) + this._client.hypercore.watchDownloadsNoReply({ id: this._id }) } catch (_) {} } @@ -602,7 +602,7 @@ class RemoteHypercore extends Nanoresource { try { if (!this.opened) await this.open() if (this.closed) return - await this._client.hypercore.unwatchDownloads({ id: this._id }) + this._client.hypercore.unwatchDownloadsNoReply({ id: this._id }) } catch (_) {} }