]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/video-watch/video-watch.component.ts
Client: clear timeout error timer for video watch
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / video-watch / video-watch.component.ts
1 import { Component, ElementRef, OnDestroy, OnInit } from '@angular/core';
2 import { ActivatedRoute } from '@angular/router';
3
4 import { BytesPipe } from 'angular-pipes/src/math/bytes.pipe';
5
6 import { LoaderComponent, Video, VideoService } from '../shared';
7 import { WebTorrentService } from './webtorrent.service';
8
9 @Component({
10 selector: 'my-video-watch',
11 template: require('./video-watch.component.html'),
12 styles: [ require('./video-watch.component.scss') ],
13 providers: [ WebTorrentService ],
14 directives: [ LoaderComponent ],
15 pipes: [ BytesPipe ]
16 })
17
18 export class VideoWatchComponent implements OnInit, OnDestroy {
19 private static LOADTIME_TOO_LONG: number = 30000;
20
21 downloadSpeed: number;
22 error: boolean = false;
23 loading: boolean = false;
24 numPeers: number;
25 uploadSpeed: number;
26 video: Video;
27
28 private errorTimer: NodeJS.Timer;
29 private sub: any;
30 private torrentInfosInterval: NodeJS.Timer;
31
32 constructor(
33 private elementRef: ElementRef,
34 private route: ActivatedRoute,
35 private videoService: VideoService,
36 private webTorrentService: WebTorrentService
37 ) {}
38
39 loadVideo() {
40 // Reset the error
41 this.error = false;
42 // We are loading the video
43 this.loading = true;
44
45 console.log('Adding ' + this.video.magnetUri + '.');
46
47 // The callback might never return if there are network issues
48 // So we create a timer to inform the user the load is abnormally long
49 this.errorTimer = setTimeout(() => this.loadTooLong(), VideoWatchComponent.LOADTIME_TOO_LONG);
50
51 this.webTorrentService.add(this.video.magnetUri, (torrent) => {
52 // Clear the error timer
53 clearTimeout(this.errorTimer);
54 // Maybe the error was fired by the timer, so reset it
55 this.error = false;
56
57 // We are not loading the video anymore
58 this.loading = false;
59
60 console.log('Added ' + this.video.magnetUri + '.');
61 torrent.files[0].appendTo(this.elementRef.nativeElement.querySelector('.embed-responsive'), (err) => {
62 if (err) {
63 alert('Cannot append the file.');
64 console.error(err);
65 }
66 });
67
68 // Refresh each second
69 this.torrentInfosInterval = setInterval(() => {
70 this.downloadSpeed = torrent.downloadSpeed;
71 this.numPeers = torrent.numPeers;
72 this.uploadSpeed = torrent.uploadSpeed;
73 }, 1000);
74 });
75 }
76
77 ngOnDestroy() {
78 console.log('Removing video from webtorrent.');
79 clearInterval(this.torrentInfosInterval);
80 clearTimeout(this.errorTimer);
81 this.webTorrentService.remove(this.video.magnetUri);
82
83 this.sub.unsubscribe();
84 }
85
86 ngOnInit() {
87 this.sub = this.route.params.subscribe(routeParams => {
88 let id = routeParams['id'];
89 this.videoService.getVideo(id).subscribe(
90 video => {
91 this.video = video;
92 this.loadVideo();
93 },
94 error => alert(error)
95 );
96 });
97 }
98
99 private loadTooLong() {
100 this.error = true;
101 console.error('The video load seems to be abnormally long.');
102 }
103 }