<commit_before>import { Component, ElementRef, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

import { ModalDirective } from 'ng2-bootstrap/components/modal';
import { MetaService } from 'ng2-meta';
import * as videojs from 'video.js';

import { Video, VideoService } from '../shared';
import { WebTorrentService } from './webtorrent.service';

@Component({
  selector: 'my-video-watch',
  templateUrl: './video-watch.component.html',
  styleUrls: [ './video-watch.component.scss' ]
})

export class VideoWatchComponent implements OnInit, OnDestroy {
  private static LOADTIME_TOO_LONG: number = 30000;

  @ViewChild('magnetUriModal') magnetUriModal: ModalDirective;
  @ViewChild('shareModal') shareModal: ModalDirective;

  downloadSpeed: number;
  error: boolean = false;
  loading: boolean = false;
  numPeers: number;
  player: VideoJSPlayer;
  uploadSpeed: number;
  video: Video = null;

  private errorTimer: NodeJS.Timer;
  private sub: any;
  private torrentInfosInterval: NodeJS.Timer;

  constructor(
    private elementRef: ElementRef,
    private ngZone: NgZone,
    private route: ActivatedRoute,
    private videoService: VideoService,
    private metaService: MetaService,
    private webTorrentService: WebTorrentService
  ) {}

  ngOnInit() {
    this.sub = this.route.params.subscribe(routeParams => {
      let id = routeParams['id'];
      this.videoService.getVideo(id).subscribe(
        video => {
          this.video = video;
          this.setOpenGraphTags();
          this.loadVideo();
        },
        error => alert(error.text)
      );
    });

    const videojsOptions = {
      controls: true,
      autoplay: false
    };

    const self = this;
    videojs('video-container', videojsOptions, function () {
      self.player = this;
    });
  }

  ngOnDestroy() {
    console.log('Removing video from webtorrent.');
    clearInterval(this.torrentInfosInterval);
    clearTimeout(this.errorTimer);
    this.webTorrentService.remove(this.video.magnetUri);

    this.sub.unsubscribe();
  }

  loadVideo() {
    // Reset the error
    this.error = false;
    // We are loading the video
    this.loading = true;

    console.log('Adding ' + this.video.magnetUri + '.');

    // The callback might never return if there are network issues
    // So we create a timer to inform the user the load is abnormally long
    this.errorTimer = setTimeout(() => this.loadTooLong(), VideoWatchComponent.LOADTIME_TOO_LONG);

    this.webTorrentService.add(this.video.magnetUri, (torrent) => {
      // Clear the error timer
      clearTimeout(this.errorTimer);
      // Maybe the error was fired by the timer, so reset it
      this.error = false;

      // We are not loading the video anymore
      this.loading = false;

      console.log('Added ' + this.video.magnetUri + '.');
      torrent.files[0].renderTo('#video-container video', { autoplay: true }, (err) => {
        if (err) {
          alert('Cannot append the file.');<commit_msg>Client: remove videojs on destroy<commit_after>import { Component, ElementRef, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

import { ModalDirective } from 'ng2-bootstrap/components/modal';
import { MetaService } from 'ng2-meta';
import * as videojs from 'video.js';

import { Video, VideoService } from '../shared';
import { WebTorrentService } from './webtorrent.service';

@Component({
  selector: 'my-video-watch',
  templateUrl: './video-watch.component.html',
  styleUrls: [ './video-watch.component.scss' ]
})

export class VideoWatchComponent implements OnInit, OnDestroy {
  private static LOADTIME_TOO_LONG: number = 30000;

  @ViewChild('magnetUriModal') magnetUriModal: ModalDirective;
  @ViewChild('shareModal') shareModal: ModalDirective;

  downloadSpeed: number;
  error: boolean = false;
  loading: boolean = false;
  numPeers: number;
  player: VideoJSPlayer;
  playerElement: Element;
  uploadSpeed: number;
  video: Video = null;

  private errorTimer: NodeJS.Timer;
  private sub: any;
  private torrentInfosInterval: NodeJS.Timer;

  constructor(
    private elementRef: ElementRef,
    private ngZone: NgZone,
    private route: ActivatedRoute,
    private videoService: VideoService,
    private metaService: MetaService,
    private webTorrentService: WebTorrentService
  ) {}

  ngOnInit() {
    this.sub = this.route.params.subscribe(routeParams => {
      let id = routeParams['id'];
      this.videoService.getVideo(id).subscribe(
        video => {
          this.video = video;
          this.setOpenGraphTags();
          this.loadVideo();
        },
        error => alert(error.text)
      );
    });

    this.playerElement = this.elementRef.nativeElement.querySelector('#video-container');

    const videojsOptions = {
      controls: true,
      autoplay: false
    };

    const self = this;
    videojs(this.playerElement, videojsOptions, function () {
      self.player = this;
    });
  }

  ngOnDestroy() {
    // Remove WebTorrent stuff
    console.log('Removing video from webtorrent.');
    clearInterval(this.torrentInfosInterval);
    clearTimeout(this.errorTimer);
    this.webTorrentService.remove(this.video.magnetUri);

    // Remove player
    videojs(this.playerElement).dispose();

    // Unsubscribe route subscription
    this.sub.unsubscribe();
  }

  loadVideo() {
    // Reset the error
    this.error = false;
    // We are loading the video
    this.loading = true;

    console.log('Adding ' + this.video.magnetUri + '.');

    // The callback might never return if there are network issues
    // So we create a timer to inform the user the load is abnormally long
    this.errorTimer = setTimeout(() => this.loadTooLong(), VideoWatchComponent.LOADTIME_TOO_LONG);

    this.webTorrentService.add(this.video.magnetUri, (torrent) => {
      // Clear the error timer
      clearTimeout(this.errorTimer);
      // Maybe the error was fired by the timer, so reset it
      this.error = false;

      // We are not loading the video anymore
      this.loading = false;

      console.log('Added ' + this.video.magnetUri + '.');
      torrent.files[0].renderTo(this.playerElement, { autoplay: true }, (err) => {
        if (err) {
          alert('Cannot append the file.');
{'fraction_non_alphanumeric': 0.1193109987357775, 'fraction_numerical': 0.0025284450063211127, 'mean_word_length': 3.241957104557641, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6269404', 'n_tokens_mistral': 1885, 'n_tokens_neox': 1849, 'n_words': 467}