I'm writing a desktop application using angular2 and electron and there is a download functionality.
My DownloadService
is this
import {Injectable} from '@angular/core';
import {Subject} from "rxjs";
interface IQueueItem {
url: string
}
@Injectable()
export class DownloadService {
private queue: Array< IQueueItem > = [];
private downloadSubject: Subject<any>;
constructor() {
this.downloadSubject = new Subject();
}
addToList(item: IQueueItem) {
this.queue.unshift(item);
downloadList();
return this.downloadSubject;
}
downloadList() {
// pick one item from queue and send it to electron to download and store
// do this every time a chunk of data received
this.downloadSubject.next(evt);
...
}
pauseDownload(item) {
// send an event to electron to it should stop downloading and therefore no chunk of data will receive
// also remove item from queue
...
}
}
And my ItemComponent
is this:
import {Component} from '@angular/core';
import {DownloadService} from "../services/download.service";
@Component({
selector: 'app-item',
template: `
...
`
})
export class ItemComponent {
constructor(private downloadService: DownloadService) {
this.addToQueue();
}
subscription;
downloadedBytes = 0;
fileUrl = '';
pause() {
this.downloadService.pauseDownload(this.fileUrl);
this.subscription.unsubscribe();
...
}
resume() {
this.addToQueue();
}
private addToQueue() {
this.subscription = this.downloadService.addToList(this.fileUrl)
.subscribe(evt => {
console.log(evt.delta);
this.downloadedBytes += evt.delta;
});
}
}
The problem is when I pause an item I unsubscribe from Subject
passed from DownloadService
but when I resume again, every console.log()
prints twice and add twice data to downloadedBytes
.
Also if I pause and resume again, It will add more and more bytes and logs!
I searched but I couldn't find any clue to solve.