rxjs

rxjs created observable timeout always errors

主宰稳场 提交于 2019-12-23 17:33:32
问题 ok, so now I'm really puzzled. Executing the following code const created = Rx.Observable.create(observer => { observer.next(42) }) const ofd = Rx.Observable.of(42) const createSub = name => [ val => console.log(`${name} received ${val}`), error => console.log(`${name} threw ${error.constructor.name}`) ] created .timeout(100) .subscribe( ...createSub('created') ) ofd .timeout(100) .subscribe( ...createSub('ofd') ) Prints "created received 42" "ofd received 42" "created threw TimeoutError" I

How do you prevent a BehaviourSubject from being consumed before it's defined?

久未见 提交于 2019-12-23 17:02:37
问题 I'm working on a user service that keeps track of users by their user_id. It first checks if user_id is present in their cookies, if it isn't it makes a get request to the api to create a new one and returns the id in the response. My problem is that the user_id is being consumed before the response is completed. I have two questions: The BehaviourSubject is being consumed before it's even defined as a new BehaviourService, components that consume it are calling .subscribe() on undefined as a

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 can I avoid passing the scheduler through my business logic, when writing tests for RXJS observables?

我的梦境 提交于 2019-12-23 16:34:06
问题 I am finding that the only way to make certain tests pass is to explicitly pass a scheduler to functions. For illustration, consider this function: function doStuff( stream ){ return stream.delay(100) .filter( x => x % 2 === 0 ) .map( x => x * 2 ) .flatMapLatest( x=> Rx.Observable.range( x, x+100) ) And a test: it('example test', () => { let scheduler = new Rx.TestScheduler() let xs = scheduler.createHotObservable( onNext(210, 1), onNext(220, 2), onNext(230, 3) ) let res = scheduler

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

Rx BehaviorSubject + scan pushing prior event to new subscriber?

纵饮孤独 提交于 2019-12-23 15:46:14
问题 I want to have an stream to which I can push reducer functions. Each time a reducer function is pushed, a state object should be passed to the reducer, the reducer should return a modified state value, and the updated state should be pushed to subscribers. I'm hoping my code can explain: import Rx from 'rx'; import { Map } from 'immutable'; let initialState = Map({ counter: 0 }); export let upstream = new Rx.BehaviorSubject(Rx.helpers.identity); export let downstream = upstream.scan((state,

rxjs bundle from unpkg

て烟熏妆下的殇ゞ 提交于 2019-12-23 15:35:42
问题 I have an angular 2.0.0 (same issue in 2.2.0) project. The current dev build generates > 100 http requests. This is due to it loading non-bundled versions of rxjs.. When I have the following : map: { // our app is within the app folder app: 'app', // angular bundles // snip 'rxjs': 'npm:rxjs', 'angular-in-memory-web-api': 'npm:angular-in-memory-web-api', }, The app works but we have 1000+ http requsts , so I try to load rxjs from the bundle, todo this I remove rxjs': 'npm:rxjs' and I add the

Angular jasmine test not able to trigger Observable created with fromEvent rxjs operator

浪子不回头ぞ 提交于 2019-12-23 12:44:25
问题 I have a simple case. The standard AppComponent of an Angular app contains a ChildComponent which is defined in its own module ChildModule . The template of ChildComponent is very simple <div class="child" (click)="testClick($event)"></div> ChildComponent has an even simpler testClick(event) method that just logs a message on the console. testClick(event) { console.log(event); } Now I want to build a test on AppComponent simulating a click on ChildComponent . This is the code of the test