rxjs5

How to get “Observable.of([]);” to work?

Deadly 提交于 2019-11-27 07:46:42
问题 What is the correct expression and import for Observable.of([]); . import { of } from 'rxjs'; does not work for me. 回答1: Since RxJS 6 you should import all "creation" observables directly from 'rxjs' (assuming you have path maps set when bundling your app). More detailed explanation: https://github.com/ReactiveX/rxjs/blob/master/MIGRATION.md#import-paths import { of } from 'rxjs'; of(1).subscribe(console.log); See demo: https://stackblitz.com/edit/typescript-e3lxkb?file=index.ts 回答2: No need

How would I use `do` as an RxJS lettable operator?

帅比萌擦擦* 提交于 2019-11-27 07:42:07
问题 RxJS 5.5 allows grabbing lettable operators and piping them like so: import { ajax } from 'rxjs/observable/dom/ajax' import { catchError, map, retry } from 'rxjs/operators' ajax.getJSON('https://example.com/api/test') .pipe( retry(3, 1000), map(fetchUserFulfilled), catchError(console.error) ) How would I use the do operator between these commands? 回答1: The do operator was renamed in RxJS 5.5 to tap because it collided with the JavaScript do keyword. For more info see: https://github.com

RxJS: takeUntil() Angular component's ngOnDestroy()

 ̄綄美尐妖づ 提交于 2019-11-27 07:26:35
tl;dr: Basically I want to marry Angular's ngOnDestroy with the Rxjs takeUntil() operator. -- is that possible? I have an Angular component that opens several Rxjs subscriptions. These need to be closed when the component is destroyed. A simple solution for this would be: class myComponent { private subscriptionA; private subscriptionB; private subscriptionC; constructor( private serviceA: ServiceA, private serviceB: ServiceB, private serviceC: ServiceC) {} ngOnInit() { this.subscriptionA = this.serviceA.subscribe(...); this.subscriptionB = this.serviceB.subscribe(...); this.subscriptionC =

Difference between .unsubscribe to .take(1)

拈花ヽ惹草 提交于 2019-11-27 07:23:39
I wonder, if there is any difference in performance between using .take(1) and .unsubscribe when unsubscribe is used right after the subscription: var observable = Rx.Observable.interval(100); First: var subscription = observable.subscribe(function(value) { console.log(value); }).unsubscribe(); Second: var subscription = observable.take(1).subscribe(function(value) { console.log(value); }); Any ideas of it makes any different regard the performance? Each serves a different purpose so it's hard to compare them. In general if you take this source: const source = range(1,3); ... and consume it

Angular 2 router resolve with Observable

不问归期 提交于 2019-11-27 07:15:51
After release of Angular 2 RC.5 there was introduced router resolve. Here demonstrated example with Promise, how to do the same if I make request to server with Observable? search.service.ts ... searchFields(id: number) { return this.http.get(`http://url.to.api/${id}`).map(res => res.json()); } ... search-resolve.service.ts import { Injectable } from '@angular/core'; import { Router, Resolve, ActivatedRouteSnapshot } from '@angular/router'; import { Observable } from 'rxjs/Observable'; import { SearchService } from '../shared'; @Injectable() export class SearchResolveService implements Resolve

SystemJS loads many files for rxjs

孤人 提交于 2019-11-27 06:50:49
问题 I have the following systemjs.config.js (based on some example I found on the internet): (function (global) { System.config({ paths: { // paths serve as alias 'js:': 'js/', }, // map tells the System loader where to look for things map: { // our app is within the app folder app: 'app', // angular bundles '@angular/core': 'js:angular2/core.umd.js', '@angular/common': 'js:angular2/common.umd.js', '@angular/compiler': 'js:angular2/compiler.umd.js', '@angular/platform-browser': 'js:angular2

When to use asObservable() in rxjs?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 06:29:29
I am wondering what is the use of asObservable : As per docs: An observable sequence that hides the identity of the source sequence. But why would you need to hide the sequence? When you don't want to leak the "observer-side" of a Subject out of your API. (Basically to prevent leaky abstraction). var myAPI = { getData: () => { var subject = new Subject(); var source = new SomeWeirdDataSource(); source.onMessage = (data) => subject.next({ type: 'message', data }); source.onOtherMessage = (data) => subject.next({ type: 'othermessage', data }); return subject.asObservable(); } }; Now when someone

Rx.Subject loses events

北战南征 提交于 2019-11-27 04:51:24
问题 Can anybody explain what the differents between these 3 variants? http://jsfiddle.net/8vx2g3fr/2/ First works as excpect, all events are processed. But second loses last event (3) Third loses second event (2) Could you please help me to understand what the issue is and how to make the third variant process all events? 1 let bs = new Rx.Subject(); bs .subscribe(v=>{ console.log("in", v); if (v % 2 == 0) { setTimeout(()=>{ console.log(" out", v, "->" , v + 1); bs.next(v+1); }, 0); } }); bs.next

Angular 2 router resolve with Observable

和自甴很熟 提交于 2019-11-27 03:59:29
问题 After release of Angular 2 RC.5 there was introduced router resolve. Here demonstrated example with Promise, how to do the same if I make request to server with Observable? search.service.ts ... searchFields(id: number) { return this.http.get(`http://url.to.api/${id}`).map(res => res.json()); } ... search-resolve.service.ts import { Injectable } from '@angular/core'; import { Router, Resolve, ActivatedRouteSnapshot } from '@angular/router'; import { Observable } from 'rxjs/Observable'; import

RxJS takeWhile but include the last value

笑着哭i 提交于 2019-11-27 03:11:02
问题 I have a RxJS5 pipeline looks like this Rx.Observable.from([2, 3, 4, 5, 6]) .takeWhile((v) => { v !== 4 }) I want to keep the subscription until I see 4, but I want to last element 4 also to be included in the result. So the example above should be 2, 3, 4 However, according to official document, takeWhile operator is not inclusive. Which means when it encounters the element which doesn't match predicate we gave, it completes the stream immediately without the last element. As a result, the