I'm looking to subscribe to an observable and use some methods on it to ensure that it's debounced and that previous requests are cancelled
private _allBulls = new BehaviorSubject<Sire[]>([]);
allBulls$ = this._allBulls.asObservable();
private apiChange = new Subject<ApiChange>();
apiChange$ = this.apiChange.asObservable().distinctUntilChanged().debounceTime(1000);
I've gotten to here and I can subscribe in my component and then call this method from the component
this.subscription = this.selectionService.apiChange$.subscribe(
(data) => {
this.selectionService.getSireData(data);
})
The method getSireData
getSireData(apiChange: ApiChange) {
console.log("getSireData")
this.updateApiSettings(apiChange);
const headers = new Headers({'Content-Type': 'application/json'})
options = new RequestOptions({ headers: headers});
this.http
.post(`${this.baseUrl}SireSearch/Scope/Filtered`, this.apiSettings, options)
.map(this.extractData)
.subscribe(data => this._allBulls.next(data))
}
The subscription in another component to make changes reflective of the observable
this.selectionService.allBulls$.subscribe(() => this.selectionService.applyGridSettings());
I can't use .switch or the .switchMap operator however because it expects a returned observable. So I think I need to come up with a different structure. I also would prefer to simply subscribe to the apiChange$ and not call the service method from the subscription.