]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Move webtorrent inside a service
authorChocobozzz <florian.bigard@gmail.com>
Tue, 31 May 2016 20:39:36 +0000 (22:39 +0200)
committerChocobozzz <florian.bigard@gmail.com>
Tue, 31 May 2016 20:39:36 +0000 (22:39 +0200)
client/app/videos/video-add/video-add.component.ts
client/app/videos/video-watch/index.ts
client/app/videos/video-watch/video-watch.component.ts
client/app/videos/video-watch/webtorrent.service.ts [new file with mode: 0644]
client/tsconfig.json

index eb10aedeb2bc41c11de77e1f85d66febb7daa19a..619a4f4d868ee330b9622a6e0103531768633d89 100644 (file)
@@ -1,3 +1,6 @@
+/// <reference path="../../../typings/globals/jquery/index.d.ts" />
+/// <reference path="../../../typings/globals/jquery.fileupload/index.d.ts" />
+
 import { Component, ElementRef, OnInit } from '@angular/core';
 import { Router } from '@angular/router-deprecated';
 
 import { Component, ElementRef, OnInit } from '@angular/core';
 import { Router } from '@angular/router-deprecated';
 
@@ -6,9 +9,6 @@ import { PROGRESSBAR_DIRECTIVES } from 'ng2-bootstrap/components/progressbar';
 
 import { AuthService, User } from '../../users/index';
 
 
 import { AuthService, User } from '../../users/index';
 
-// TODO: import it with systemjs
-declare var jQuery: any;
-
 @Component({
   selector: 'my-videos-add',
   styleUrls: [ 'client/app/videos/video-add/video-add.component.css' ],
 @Component({
   selector: 'my-videos-add',
   styleUrls: [ 'client/app/videos/video-add/video-add.component.css' ],
@@ -37,7 +37,7 @@ export class VideoAddComponent implements OnInit {
       dataType: 'json',
       singleFileUploads: true,
       multipart: true,
       dataType: 'json',
       singleFileUploads: true,
       multipart: true,
-      autoupload: false,
+      autoUpload: false,
 
       add: (e, data) => {
         this.form = data;
 
       add: (e, data) => {
         this.form = data;
index 2228b6ed7aeab71971ce0a1c5e188fb154370ecd..b17aaacf22d8af70f6abcc4435cefab7ed9bccdb 100644 (file)
@@ -1 +1,2 @@
 export * from './video-watch.component';
 export * from './video-watch.component';
+export * from './webtorrent.service';
index 137db8f0b23987a064370ddf4ec0be68e6dc73b6..71fb4f6342867156bf36c0c0192a16d9de91cd84 100644 (file)
@@ -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';
 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' ],
 
 @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 ]
 })
   directives: [ LoaderComponent ],
   pipes: [ BytesPipe ]
 })
@@ -23,23 +22,21 @@ export class VideoWatchComponent implements OnInit, CanDeactivate {
   uploadSpeed: number;
   video: Video;
 
   uploadSpeed: number;
   video: Video;
 
-  private client: any;
   private interval: NodeJS.Timer;
 
   constructor(
     private elementRef: ElementRef,
     private routeParams: RouteParams,
   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 + '.');
 
   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) => {
       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);
   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;
   }
 }
     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 (file)
index 0000000..0c73ab4
--- /dev/null
@@ -0,0 +1,26 @@
+// Don't use webtorrent typings for now
+// It misses some little things I'll fix later
+// <reference path="../../../typings/globals/webtorrent/index.d.ts" />
+
+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);
+  }
+}
index d48bd392009b206313a4e51e2cd6b41a422cd91d..24583f6c3e57168a8c54da3178a5c8f5c553aa02 100644 (file)
     "app/videos/video-list/video-sort.component.ts",
     "app/videos/video-watch/index.ts",
     "app/videos/video-watch/video-watch.component.ts",
     "app/videos/video-list/video-sort.component.ts",
     "app/videos/video-watch/index.ts",
     "app/videos/video-watch/video-watch.component.ts",
+    "app/videos/video-watch/webtorrent.service.ts",
     "main.ts",
     "typings/globals/es6-shim/index.d.ts",
     "typings/globals/jasmine/index.d.ts",
     "main.ts",
     "typings/globals/es6-shim/index.d.ts",
     "typings/globals/jasmine/index.d.ts",
+    "typings/globals/jquery.fileupload/index.d.ts",
+    "typings/globals/jquery/index.d.ts",
     "typings/globals/node/index.d.ts",
     "typings/index.d.ts"
   ],
     "typings/globals/node/index.d.ts",
     "typings/index.d.ts"
   ],