diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-05-31 22:39:36 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-05-31 22:39:36 +0200 |
commit | d3ef341abe8fb890a085189f5e4d0a5bd38d0172 (patch) | |
tree | 333c058390607cac1aec22169a2feb7d9cee3a00 /client/app/videos/video-watch | |
parent | 092fbf58cca91d1a2ce0c84f47ef7856c9e56cdb (diff) | |
download | PeerTube-d3ef341abe8fb890a085189f5e4d0a5bd38d0172.tar.gz PeerTube-d3ef341abe8fb890a085189f5e4d0a5bd38d0172.tar.zst PeerTube-d3ef341abe8fb890a085189f5e4d0a5bd38d0172.zip |
Move webtorrent inside a service
Diffstat (limited to 'client/app/videos/video-watch')
-rw-r--r-- | client/app/videos/video-watch/index.ts | 1 | ||||
-rw-r--r-- | client/app/videos/video-watch/video-watch.component.ts | 19 | ||||
-rw-r--r-- | client/app/videos/video-watch/webtorrent.service.ts | 26 |
3 files changed, 35 insertions, 11 deletions
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 @@ | |||
1 | export * from './video-watch.component'; | 1 | export * from './video-watch.component'; |
2 | 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 | |||
4 | import { BytesPipe } from 'angular-pipes/src/math/bytes.pipe'; | 4 | import { BytesPipe } from 'angular-pipes/src/math/bytes.pipe'; |
5 | 5 | ||
6 | import { LoaderComponent, Video, VideoService } from '../shared/index'; | 6 | import { LoaderComponent, Video, VideoService } from '../shared/index'; |
7 | 7 | import { WebTorrentService } from './webtorrent.service'; | |
8 | // TODO import it with systemjs | ||
9 | declare var WebTorrent: any; | ||
10 | 8 | ||
11 | @Component({ | 9 | @Component({ |
12 | selector: 'my-video-watch', | 10 | selector: 'my-video-watch', |
13 | templateUrl: 'client/app/videos/video-watch/video-watch.component.html', | 11 | templateUrl: 'client/app/videos/video-watch/video-watch.component.html', |
14 | styleUrls: [ 'client/app/videos/video-watch/video-watch.component.css' ], | 12 | styleUrls: [ 'client/app/videos/video-watch/video-watch.component.css' ], |
13 | providers: [ WebTorrentService ], | ||
15 | directives: [ LoaderComponent ], | 14 | directives: [ LoaderComponent ], |
16 | pipes: [ BytesPipe ] | 15 | pipes: [ BytesPipe ] |
17 | }) | 16 | }) |
@@ -23,23 +22,21 @@ export class VideoWatchComponent implements OnInit, CanDeactivate { | |||
23 | uploadSpeed: number; | 22 | uploadSpeed: number; |
24 | video: Video; | 23 | video: Video; |
25 | 24 | ||
26 | private client: any; | ||
27 | private interval: NodeJS.Timer; | 25 | private interval: NodeJS.Timer; |
28 | 26 | ||
29 | constructor( | 27 | constructor( |
30 | private elementRef: ElementRef, | 28 | private elementRef: ElementRef, |
31 | private routeParams: RouteParams, | 29 | private routeParams: RouteParams, |
32 | private videoService: VideoService | 30 | private videoService: VideoService, |
33 | ) { | 31 | private webTorrentService: WebTorrentService |
34 | // TODO: use a service | 32 | ) {} |
35 | this.client = new WebTorrent({ dht: false }); | ||
36 | } | ||
37 | 33 | ||
38 | loadVideo(video: Video) { | 34 | loadVideo(video: Video) { |
39 | this.loading = true; | 35 | this.loading = true; |
40 | this.video = video; | 36 | this.video = video; |
41 | console.log('Adding ' + this.video.magnetUri + '.'); | 37 | console.log('Adding ' + this.video.magnetUri + '.'); |
42 | this.client.add(this.video.magnetUri, (torrent) => { | 38 | |
39 | this.webTorrentService.add(this.video.magnetUri, (torrent) => { | ||
43 | this.loading = false; | 40 | this.loading = false; |
44 | console.log('Added ' + this.video.magnetUri + '.'); | 41 | console.log('Added ' + this.video.magnetUri + '.'); |
45 | torrent.files[0].appendTo(this.elementRef.nativeElement.querySelector('.embed-responsive'), (err) => { | 42 | torrent.files[0].appendTo(this.elementRef.nativeElement.querySelector('.embed-responsive'), (err) => { |
@@ -69,7 +66,7 @@ export class VideoWatchComponent implements OnInit, CanDeactivate { | |||
69 | routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction) { | 66 | routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction) { |
70 | console.log('Removing video from webtorrent.'); | 67 | console.log('Removing video from webtorrent.'); |
71 | clearInterval(this.interval); | 68 | clearInterval(this.interval); |
72 | this.client.remove(this.video.magnetUri); | 69 | this.webTorrentService.remove(this.video.magnetUri); |
73 | return true; | 70 | return true; |
74 | } | 71 | } |
75 | } | 72 | } |
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 @@ | |||
1 | // Don't use webtorrent typings for now | ||
2 | // It misses some little things I'll fix later | ||
3 | // <reference path="../../../typings/globals/webtorrent/index.d.ts" /> | ||
4 | |||
5 | import { Injectable } from '@angular/core'; | ||
6 | |||
7 | // import WebTorrent = require('webtorrent'); | ||
8 | declare var WebTorrent: any; | ||
9 | |||
10 | @Injectable() | ||
11 | export class WebTorrentService { | ||
12 | // private client: WebTorrent.Client; | ||
13 | private client: any; | ||
14 | |||
15 | constructor() { | ||
16 | this.client = new WebTorrent({ dht: false }); | ||
17 | } | ||
18 | |||
19 | add(magnetUri: string, callback: Function) { | ||
20 | return this.client.add(magnetUri, callback); | ||
21 | } | ||
22 | |||
23 | remove(magnetUri: string) { | ||
24 | return this.client.remove(magnetUri); | ||
25 | } | ||
26 | } | ||