Angular2-Rxjs主题即使在取消订阅后也会运行两次

Angular2-Rxjs主题即使在取消订阅后也会运行两次,angular,rxjs,subject-observer,Angular,Rxjs,Subject Observer,我正在使用angular2和electron编写一个桌面应用程序,并且有一个下载功能 我的下载服务是这样的吗 import {Injectable} from '@angular/core'; import {Subject} from "rxjs"; interface IQueueItem { url: string } @Injectable() export class DownloadService { private queue: Array< IQueue

我正在使用angular2和electron编写一个桌面应用程序,并且有一个下载功能

我的
下载服务是这样的吗

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
        ...
    }
}
问题是当我暂停从
下载服务传递的
主题
取消订阅的项目时,但当我再次恢复时,每个
控制台.log()
打印两次,并向
下载字节添加两次数据。
另外,如果我暂停并再次恢复,它将添加越来越多的字节和日志

我搜索了,但找不到任何解决的线索。

如果你恢复暂停一切正常,只触发了一次,你可以从创建的
问题是当您连续两次单击恢复时,因为第一个订阅将丢失在内存中,并且只有第二个订阅将存储在此订阅中。对第一个订阅的访问将丢失,您无法取消订阅。

这更像是一个应用程序问题,而不是rxjs,如果订阅未暂停,您应该无法单击“恢复”,因此您需要正确处理状态,类似于以下内容(由@yurzui-plunker编辑):

@组件({
选择器:“我的应用程序”,
模板:`
角度2系统启动
暂停
简历
开始
`
})
导出类应用程序{
构造函数(私有下载服务:下载服务){
this.addToQueue();
}
订阅
下载字节=0;
开始=错误;
暂停=错误;
暂停(){
this.paused=true;
this.subscription.unsubscripte();
}
开始(){
this.started=true;
this.addToQueue();
}
简历(){
this.pause=false;
this.addToQueue();
}
私有addToQueue(){
this.subscription=this.downloadService.addToList(this)
.订阅(evt=>{
控制台日志(11);
this.downloadedBytes+=evt.delta;
});
}
}

您可以在这个更新的

中找到一个工作示例,我猜这是因为downloadList()将组件添加到列表中。但是由于我们不知道这个列表的用途,当主题发出事件时,它的用途,pauseCourse()的作用等等,所以很难解释。提供一个完整的例子来重现这个问题。@JBNizet抱歉,我做了很多简化,结果显然不好。我添加了更多细节,我无法复制。也检查
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;
            });
    }
}
@Component({
  selector: 'my-app',
  template: `
    <h1>Angular 2 Systemjs start</h1>
    <button *ngIf="started && !paused" (click)="pause()">Pause</button>
    <button *ngIf="started && paused" (click)="resume()">Resume</button>
    <button *ngIf="!started" (click)="start()">Start</button>
  `
})
export class App {
  constructor(private downloadService: DownloadService) {
    this.addToQueue();
  }

  subscription;
  downloadedBytes = 0;
  started = false;
  paused = false;


  pause() {
    this.paused = true;  

    this.subscription.unsubscribe();
  }

  start() {
    this.started = true;  

    this.addToQueue();
  }

  resume() {
    this.paused = false;

    this.addToQueue();
  }

  private addToQueue() {
    this.subscription = this.downloadService.addToList(this)
      .subscribe(evt => {

        console.log(11);

        this.downloadedBytes += evt.delta;
      });
  }
}