@@ -363,6 +363,21 @@ class RemoteHypercore extends Nanoresource {
363
363
this . _extensions = new Map ( )
364
364
this . _onwaits = new FreeMap ( 1 )
365
365
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
+
366
381
if ( ! this . lazy ) this . ready ( ( ) => { } )
367
382
}
368
383
@@ -579,13 +594,19 @@ class RemoteHypercore extends Nanoresource {
579
594
}
580
595
581
596
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 ( _ ) { }
585
602
}
586
603
587
604
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 ( _ ) { }
589
610
}
590
611
591
612
// Public Methods
@@ -710,14 +731,6 @@ class RemoteHypercore extends Nanoresource {
710
731
return prom . then ( ( ) => ( ) => this . _client . hypercore . releaseLockNoReply ( { id : this . _id } ) )
711
732
}
712
733
713
- watchDownloads ( cb ) {
714
- return maybe ( cb , this . _watchDownloads ( ) )
715
- }
716
-
717
- unwatchDownloads ( cb ) {
718
- return maybe ( cb , this . _unwatchDownloads ( ) )
719
- }
720
-
721
734
// TODO: Unimplemented methods
722
735
723
736
registerExtension ( name , opts ) {
0 commit comments