@@ -101,6 +101,13 @@ export class TorrentService {
101
101
* @param wasCrash
102
102
*/
103
103
private setupWebTorrent ( wasCrash = false ) {
104
+ // If there is already a instance we don't need to do anything
105
+ if ( this . webTorrent !== null ) {
106
+ return
107
+ }
108
+
109
+ this . logger . log ( 'Creating new WebTorrent client' )
110
+
104
111
this . webTorrent = new WebTorrent ( {
105
112
maxConns : 55 // Is the default
106
113
} )
@@ -119,12 +126,31 @@ export class TorrentService {
119
126
}
120
127
}
121
128
129
+ /**
130
+ * Cleanup / destroy WebTorrent when there are not downloads / torrents left
131
+ */
132
+ private cleanupWebTorrent ( ) : void {
133
+ // Check if we have a client, downloads and torrents if not then destroy the client
134
+ if ( this . webTorrent && this . connectingTorrents . length === 0 && this . torrents . length === 0 && this . downloads . length === 0 ) {
135
+ this . webTorrent ?. destroy ( ( err ) => {
136
+ if ( err ) {
137
+ this . logger . error ( 'Error cleaning up WebTorrent' , err . toString ( ) )
138
+
139
+ } else {
140
+ this . logger . log ( 'No torrents left, WebTorrent client destroyed' )
141
+ }
142
+ } )
143
+
144
+ this . webTorrent = null
145
+ }
146
+ }
147
+
122
148
/**
123
149
* Starts the streaming process of one item
124
150
*
125
151
* @param download
126
152
*/
127
- public startStreaming ( download : Model < Download > ) {
153
+ public startStreaming ( download : Model < Download > ) : void {
128
154
this . logger . log ( `[${ download . _id } ]: Start streaming` )
129
155
130
156
this . download ( download )
@@ -204,6 +230,12 @@ export class TorrentService {
204
230
*/
205
231
public async startDownloads ( ) : Promise < void > {
206
232
if ( this . backgroundDownloading || this . downloads . length === 0 ) {
233
+ // If start downloads is called and we have no downlaods then also cleanup
234
+ // WebTorrent client to be sure
235
+ if ( this . downloads . length === 0 ) {
236
+ this . cleanupWebTorrent ( )
237
+ }
238
+
207
239
return
208
240
}
209
241
@@ -222,6 +254,8 @@ export class TorrentService {
222
254
223
255
// We are no longer downloading to disable
224
256
this . backgroundDownloading = false
257
+ // Cleanup WebTorrent if needed
258
+ this . cleanupWebTorrent ( )
225
259
}
226
260
227
261
/**
@@ -250,7 +284,10 @@ export class TorrentService {
250
284
*
251
285
* @param {Download } download - Item to download
252
286
*/
253
- private async download ( download : Download ) {
287
+ private async download ( download : Download ) : Promise < void > {
288
+ // Make sure WebTorrent is setup
289
+ this . setupWebTorrent ( )
290
+
254
291
return new Promise ( ( async ( resolve ) => {
255
292
this . logger . log ( `[${ download . _id } ]: Start download` )
256
293
@@ -317,7 +354,7 @@ export class TorrentService {
317
354
maxWebConns : 5 ,
318
355
announce : this . trackers
319
356
} ,
320
- this . handleTorrent ( resolve , item , download , magnet )
357
+ this . handleTorrent ( resolve , item , download )
321
358
)
322
359
323
360
// Add to active torrents array
@@ -331,13 +368,8 @@ export class TorrentService {
331
368
332
369
/**
333
370
* Handles the torrent and resolves when the torrent is done
334
- *
335
- * @param resolve
336
- * @param item
337
- * @param download
338
- * @param magnet
339
371
*/
340
- private handleTorrent ( resolve , item , download , magnet ) {
372
+ private handleTorrent ( resolve , item , download ) {
341
373
return ( torrent : Torrent ) => {
342
374
// Let's make sure all the not needed files are deselected
343
375
const { file } = torrent . files . reduce ( ( previous , current , index ) => {
@@ -434,7 +466,7 @@ export class TorrentService {
434
466
const now = Date . now ( )
435
467
// Only update every 1 second
436
468
if ( lastUpdate === null || ( lastUpdate + 1000 ) < now ) {
437
- this . logger . debug ( `[${ download . _id } ]: Progress ${ newProgress . toFixed ( 1 ) } % at ${ formatKbToString ( torrent . downloadSpeed ) } ` )
469
+ this . logger . debug ( `[${ download . _id } ]: Progress ${ newProgress . toFixed ( 2 ) } % at ${ formatKbToString ( torrent . downloadSpeed ) } ( ${ torrent . timeRemaining } ) ` )
438
470
439
471
lastUpdate = now
440
472
@@ -485,6 +517,9 @@ export class TorrentService {
485
517
// Remove from the queue as the item is downloaded
486
518
this . removeFromDownloads ( download )
487
519
520
+ // Cleanup WebTorrent if there is nothing left anymore
521
+ this . cleanupWebTorrent ( )
522
+
488
523
// Wait at-least 0,5 second before updating, this is to prevent
489
524
// a double save happening
490
525
setTimeout ( async ( ) => {
0 commit comments