observable

Unit test: Mocking service that returns observables to return subjects in order to test change of values over time causes TS to throw TS2339

三世轮回 提交于 2019-12-23 19:44:59
问题 I have a service that returns the values exposed by ngrx selectors, and a component that defines this service, injects it and sets properties based on the values returned by the service. I am writing unit tests for the component using a mock of the service, and I need the mock service to return different values for each unit test. In order to do this I have defined the mock service class so it returns subjects instead of observables. The tests run, but TS throws an error saying that the

Angular2 - *ngIf and async observables

爷,独闯天下 提交于 2019-12-23 18:16:53
问题 I have problem with using *ngIf with observable variables. The thing is that when I hide element with *ngIf , and show it again, values won't load so: <div *ngIf="showDiv"> {{ someObservable$ | async }} </div> Basically when showDiv is set to true at the first place, the someObservable loads, but when I set it to false and then again to true , value won't load. What's wrong? Regards 回答1: UPDATE: Thanks to j2L4e, for his hint! BehaviorSubject is the key! See this plunker: https://plnkr.co/edit

How to recover from errors in rxjs?

烂漫一生 提交于 2019-12-23 17:58:43
问题 I'm trying to understand how to consume observable sequences and how to recover from errors. My example is a bit contrived but my real implementation is a bit too complex to show here. Anyway, I have someObservable that emits values when the user clicks in the UI. This should trigger a request to the API (GET/POST/etc). The problem is that if the API returns an error, postData isn't called anymore after that. I've make an example here that shows the problem. I've found that I can use Rx

Observable create is called twice

你离开我真会死。 提交于 2019-12-23 16:43:44
问题 I am using Ionic3 with a rxjs/Observable . I have the following function, and for some reason, even though the function is only called once, the 3rd line gets fired twice. findChats(): Observable<any[]> { return Observable.create((observer) => { this.chatSubscription2 = this.firebaseDataService.findChats().subscribe(firebaseItems => { this.localDataService.findChats().then((localItems: any[]) => { let mergedItems: any[] = []; if (localItems && localItems != null && firebaseItems &&

how do I use `Observable.bindCallback()` with typescript

北慕城南 提交于 2019-12-23 16:33:54
问题 I've got a google maps direction service I'm trying to convert to an Observable pattern. Here is the example from https://developers.google.com/maps/documentation/javascript/examples/directions-simple: function calculateAndDisplayRoute(directionsService, directionsDisplay) { directionsService.route({ origin: document.getElementById('start').value, destination: document.getElementById('end').value, travelMode: 'DRIVING' }, function(response, status) { if (status === 'OK') { directionsDisplay

how do I use `Observable.bindCallback()` with typescript

不打扰是莪最后的温柔 提交于 2019-12-23 16:33:26
问题 I've got a google maps direction service I'm trying to convert to an Observable pattern. Here is the example from https://developers.google.com/maps/documentation/javascript/examples/directions-simple: function calculateAndDisplayRoute(directionsService, directionsDisplay) { directionsService.route({ origin: document.getElementById('start').value, destination: document.getElementById('end').value, travelMode: 'DRIVING' }, function(response, status) { if (status === 'OK') { directionsDisplay

How to wait for first Observable to finish before executing others in parallel using RxJS

人走茶凉 提交于 2019-12-23 16:03:12
问题 At the moment I have all 3 requests running in parallel. Now I need to wait for the first one to finish before I fire the other 2 in parallel. This is what I have at the moment: return Observable .forkJoin(request1, request2, request3) .map((successValues: boolean[]) => { return successValues.every(x => x); }) .do(success => { if (success) { this.store.dispatch({ type: actions.UPDATE_SUCCESS }); } }); 回答1: You should use the switchMap operator. request1.switchMap(response1 => { Observable

How to use EventManager to listen to window.resize events in angular?

最后都变了- 提交于 2019-12-23 12:04:13
问题 I'm borrowing some code from this stackoverflow: Angular window resize event The author of the answer says I should be using the EventManager if I want to listen for window events from a service and not break Angular Universal. That being said, is this answer still true? If so, could someone show me why when I subscribe to the onResize$ Observable logs nothing when the window is resized? import { EventManager } from '@angular/platform-browser'; import { Observable } from 'rxjs/Observable';

FilteredList gives java.lang.ArrayIndexOutOfBoundsException on update

烂漫一生 提交于 2019-12-23 09:59:33
问题 I created a simple application to test filtered lists and their behavior when the corresponding source list changes. I'd like to test update changes also, so I created ObservableList of ObservableList s. It is faster and simpler than creating additional class like Person that have observable fields. The code looks so: ListChangeListener<ObservableList<String>> changeNotifier = new ListChangeListener<ObservableList<String>>() { @Override public void onChanged(Change<? extends ObservableList

Sequential subscription to an array of observables

心已入冬 提交于 2019-12-23 09:17:30
问题 Here, I've used forkJoin from rxjs to subscribe to an array of observables parallelly. But I want to subscribe to them one by one, What will be the best solution? Below is my code : var observables = []; Observable.forkJoin(observables) .subscribe(() => { this.msgs = []; this.msgs.push({ severity: 'success', summary: 'Saved Successfully' }); this.onSaveComplete(); }, (error: any) => this.errorMessage = <any>error); }, (error: any) => this.errorMessage = <any>error); 回答1: Alternate of forkJoin