From d3ef341abe8fb890a085189f5e4d0a5bd38d0172 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 31 May 2016 22:39:36 +0200 Subject: Move webtorrent inside a service --- client/app/videos/video-watch/index.ts | 1 + .../videos/video-watch/video-watch.component.ts | 19 +++++++--------- .../app/videos/video-watch/webtorrent.service.ts | 26 ++++++++++++++++++++++ 3 files changed, 35 insertions(+), 11 deletions(-) create mode 100644 client/app/videos/video-watch/webtorrent.service.ts (limited to 'client/app/videos/video-watch') diff --git a/client/app/videos/video-watch/index.ts b/client/app/videos/video-watch/index.ts index 2228b6ed7..b17aaacf2 100644 --- a/client/app/videos/video-watch/index.ts +++ b/client/app/videos/video-watch/index.ts @@ -1 +1,2 @@ export * from './video-watch.component'; +export * from './webtorrent.service'; diff --git a/client/app/videos/video-watch/video-watch.component.ts b/client/app/videos/video-watch/video-watch.component.ts index 137db8f0b..71fb4f634 100644 --- a/client/app/videos/video-watch/video-watch.component.ts +++ b/client/app/videos/video-watch/video-watch.component.ts @@ -4,14 +4,13 @@ import { CanDeactivate, ComponentInstruction, RouteParams } from '@angular/route import { BytesPipe } from 'angular-pipes/src/math/bytes.pipe'; import { LoaderComponent, Video, VideoService } from '../shared/index'; - -// TODO import it with systemjs -declare var WebTorrent: any; +import { WebTorrentService } from './webtorrent.service'; @Component({ selector: 'my-video-watch', templateUrl: 'client/app/videos/video-watch/video-watch.component.html', styleUrls: [ 'client/app/videos/video-watch/video-watch.component.css' ], + providers: [ WebTorrentService ], directives: [ LoaderComponent ], pipes: [ BytesPipe ] }) @@ -23,23 +22,21 @@ export class VideoWatchComponent implements OnInit, CanDeactivate { uploadSpeed: number; video: Video; - private client: any; private interval: NodeJS.Timer; constructor( private elementRef: ElementRef, private routeParams: RouteParams, - private videoService: VideoService - ) { - // TODO: use a service - this.client = new WebTorrent({ dht: false }); - } + private videoService: VideoService, + private webTorrentService: WebTorrentService + ) {} loadVideo(video: Video) { this.loading = true; this.video = video; console.log('Adding ' + this.video.magnetUri + '.'); - this.client.add(this.video.magnetUri, (torrent) => { + + this.webTorrentService.add(this.video.magnetUri, (torrent) => { this.loading = false; console.log('Added ' + this.video.magnetUri + '.'); torrent.files[0].appendTo(this.elementRef.nativeElement.querySelector('.embed-responsive'), (err) => { @@ -69,7 +66,7 @@ export class VideoWatchComponent implements OnInit, CanDeactivate { routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction) { console.log('Removing video from webtorrent.'); clearInterval(this.interval); - this.client.remove(this.video.magnetUri); + this.webTorrentService.remove(this.video.magnetUri); return true; } } diff --git a/client/app/videos/video-watch/webtorrent.service.ts b/client/app/videos/video-watch/webtorrent.service.ts new file mode 100644 index 000000000..0c73ab4f4 --- /dev/null +++ b/client/app/videos/video-watch/webtorrent.service.ts @@ -0,0 +1,26 @@ +// Don't use webtorrent typings for now +// It misses some little things I'll fix later +// + +import { Injectable } from '@angular/core'; + +// import WebTorrent = require('webtorrent'); +declare var WebTorrent: any; + +@Injectable() +export class WebTorrentService { + // private client: WebTorrent.Client; + private client: any; + + constructor() { + this.client = new WebTorrent({ dht: false }); + } + + add(magnetUri: string, callback: Function) { + return this.client.add(magnetUri, callback); + } + + remove(magnetUri: string) { + return this.client.remove(magnetUri); + } +} -- cgit v1.2.3