Fixed bug in the Signalr service where it would stop updating when moving away from the index page.

This commit is contained in:
Roger Far 2021-10-09 20:24:19 -06:00
parent dbdf2acc9c
commit b8b50ca55a
3 changed files with 6 additions and 16 deletions

View file

@ -1,4 +1,4 @@
import { Component, OnDestroy, OnInit } from '@angular/core';
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Torrent } from '../models/torrent.model';
import { TorrentService } from '../torrent.service';
@ -8,7 +8,7 @@ import { TorrentService } from '../torrent.service';
templateUrl: './torrent-table.component.html',
styleUrls: ['./torrent-table.component.scss'],
})
export class TorrentTableComponent implements OnInit, OnDestroy {
export class TorrentTableComponent implements OnInit {
public torrents: Torrent[] = [];
public error: string;
@ -19,8 +19,6 @@ export class TorrentTableComponent implements OnInit, OnDestroy {
(result) => {
this.torrents = result;
this.torrentService.connect();
this.torrentService.update$.subscribe((result2) => {
this.torrents = result2;
});
@ -31,10 +29,6 @@ export class TorrentTableComponent implements OnInit, OnDestroy {
);
}
ngOnDestroy(): void {
this.torrentService.disconnect();
}
public selectTorrent(torrentId: string): void {
this.router.navigate([`/torrent/${torrentId}`]);
}

View file

@ -8,12 +8,14 @@ import { Torrent, TorrentFileAvailability } from './models/torrent.model';
providedIn: 'root',
})
export class TorrentService {
constructor(private http: HttpClient) {}
public update$: Subject<Torrent[]> = new Subject();
private connection: signalR.HubConnection;
constructor(private http: HttpClient) {
this.connect();
}
public connect(): void {
if (this.connection != null) {
return;
@ -27,10 +29,6 @@ export class TorrentService {
});
}
public disconnect(): void {
this.connection?.stop();
}
public getList(): Observable<Torrent[]> {
return this.http.get<Torrent[]>(`/Api/Torrents`);
}

View file

@ -44,8 +44,6 @@ export class TorrentComponent implements OnInit {
(torrent) => {
this.torrent = torrent;
this.torrentService.connect();
this.torrentService.update$.subscribe((result) => {
this.update(result);
});