Setup
Currently, I'm using angular2 + meteor and meteor-rxjs package.
Description
The meteor-rxjs package wrap ObservableCursor with an RXJS observable, I can easily subscribe to the collection and bind the dataset to the items variable in the component.
Everything is fine until I want to unsubscribe the current set of data and query another set of data using different mongo selector in Collection.find(). The expected outcome is only the updates from currently subscribed data will affect the value of items, but now the previous subscription will also update the value of items when it has any changes.
Question
I suspect the previous subscription is NOT unsubscribed. How can I unsubscribe the Observable and the underlying mongo cursor of ObservableCursor?
Code
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();
}
}