Meteor-rxjs ObservableCursor as observable unsubscribe

时间:2017-11-28 03:23:45

标签: angular meteor rxjs observable

设置

目前,我正在使用angular2 + meteor和meteor-rxjs包。

描述

带有RXJS可观察的meteor-rxjs包裹ObservableCursor,我可以轻松订阅该集合并将数据集绑定到组件中的items变量。

在我想要取消订阅当前数据集并使用Collection.find()中的不同mongo选择器查询另一组数据之前,一切都很好。预期结果只是来自当前订阅数据的更新将影响items的值,但现在之前的订阅还会更新items的值,如果有任何更改。

问题

我怀疑以前的订阅未被取消订阅。如何取消订阅Observable以及ObservableCursor的基础mongo光标?

代码

DBService.ts

import { Injectable } from '@angular/core';
import { Observable, Subscription } from 'rxjs';
import { Meteor } from 'meteor/meteor';

@Injectable()
export class DBService {

  constructor() {
    MeteorObservable.subscribe('collection_name').subscribe();
  }

  public getItems(callback: (value) => void): Subscription {

    // do query
    let subscription = Emails.find({/* some selector */}).zone().subscribe(
      (items) => {
        return callback(items);
      }
    );
    return subscription;
  }
}

component.ts

import 'reflect-metadata';
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Subscription } from 'rxjs';

import { DBService } from 'db.service';

@Component({
  .....
})
export class EmailComponent implements OnInit, OnDestroy {

  private subscription: Subscription;
  private items;

  constructor(private dbService: DBService) {}

  public ngOnInit(): void {
    this.subscription = this.dbService.getItems((items) => this.items = items);
  }

  public ngOnDestroy(): void {
    // unsubscribe
    this.subscription.unsubscribe();
  }
}

参考

https://github.com/Urigo/meteor-rxjs

0 个答案:

没有答案