aboutsummaryrefslogblamecommitdiffhomepage
path: root/client/src/app/videos/video-watch/webtorrent.service.ts
blob: 8819e17d4900b8d76fd6e445c49bcdc65d882620 (plain) (tree)
1
2
3
4
5
6
7
8
9

                                          
 
                                        


                                
                                        
 
                                     
 

                                                
 
                                                         

   
                                                                           
                                               

   

                                        
   
 

                                              
   
 
import { Injectable } from '@angular/core'
import { Subject } from 'rxjs/Subject'

import * as WebTorrent from 'webtorrent'

@Injectable()
export class WebTorrentService {
  errors = new Subject<string | Error>()

  private client: WebTorrent.Instance

  constructor () {
    this.client = new WebTorrent({ dht: false })

    this.client.on('error', err => this.errors.next(err))
  }

  add (magnetUri: string, callback: (torrent: WebTorrent.Torrent) => any) {
    return this.client.add(magnetUri, callback)
  }

  remove (magnetUri: string) {
    return this.client.remove(magnetUri)
  }

  has (magnetUri: string) {
    return this.client.get(magnetUri) !== null
  }
}