rxjs

How to start second observable *only* after first is *completely* done in rxjs

倖福魔咒の 提交于 2019-12-17 16:54:23
问题 I was under impression that Observable.[prototype.]concat makes sure first operation is fully finished before second operation starts. But in following code: Observable .concat( Observable.fromNodeCallback(rimraf)(path.resolve('./some_dir')), Observable.fromNodeCallback(mkdir)(path.resolve('./some_dir')), writeToSomeDir$ ) mkdir attempts (and fails) to create ./some_dir before rimraf is finished deleting the dir. At the end (of throwing) however, ./some_dir ends up getting deleted. Why is

return boolean instead of subscribe to canActivate

一曲冷凌霜 提交于 2019-12-17 16:48:06
问题 I have a component protected with canActivate guard. The Authguard in the checkLogin function subscribes from an observable but I do not know how to return a boolean value from it to canActivate. guard.service.ts canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { let url: string = state.url; return this.checkLogin(url); } public checkService:boolean; checkLogin(url: string):boolean { return this.loadAppSettings().subscribe(res=>{ console.log(this.checkservice);

How to cancel/unsubscribe all pending HTTP requests angular 4+

廉价感情. 提交于 2019-12-17 15:42:07
问题 How to cancel/abort all pending HTTP requests angular 4+. There is an unsubscribe method to cancel HTTP Requests but how to cancel all pending requests all at once. Especially while route change. There is one thing I did ngOnDestroy() { this.subscription.unsubscribe(); } but how to achieve this globally Any Ideas? 回答1: Checkout the takeUntil() operator from RxJS to globally drop your subscriptions : - RxJS 6+ (using the pipe syntax) import { takeUntil } from 'rxjs/operators'; export class

How do I force a refresh on an Observable service in Angular2?

北城以北 提交于 2019-12-17 15:34:25
问题 In ngOnInit , my component obtains a list of users like so: this.userService.getUsers().subscribe(users => { this.users = users; }); And the implementation of userService.getUsers() looks like this: getUsers() : Observable<UserModel[]> { return this.http.get('http://localhost:3000/api/user') .map((res: Response) => <UserModel[]>res.json().result) .catch((error: any) => Observable.throw(error.json().error || 'Internal error occurred')); } Now, in another component, I have a form that can

How do I test a function that returns an observable using timed intervals in rxjs 5?

≯℡__Kan透↙ 提交于 2019-12-17 13:54:25
问题 For example, I have a function like this: export function timeRange(start: number, end: number): Rx.Observable<number> { return Rx.Observable.interval(1000) .map(n => n + start) .take(end - start + 1) } And I had a unit test that ran timeRange(10, 100) and asserted values against it. The problem is the time intervals would make my test too long to run. How would you reduce the time interval without touching the function itself? I tried reading the docs on schedulers but I didn't get it. 回答1:

Behaviour subject initial value null?

亡梦爱人 提交于 2019-12-17 10:52:35
问题 private customer: Subject<Object> = new BehaviorSubject<Object>(null); setCustomer(id, accountClassCode) { this.customer.next({'id': id, 'accountClassCode': accountClassCode}); } getCustomer() { return this.customer.asObservable(); } I'm using this part of code but I'm getting an error that can not find id of null. Is there any solution to get initial value that is not null? 回答1: The purpose of BehaviorSubject is to provide initial value. It can be null or anything else. If no valid initial

Property 'catch' does not exist on type 'Observable<any>'

自作多情 提交于 2019-12-17 10:22:28
问题 On the Angular 2 documentation page for using the Http service, there is an example. getHeroes (): Observable<Stuff[]> { return this.http.get(this.url) .map(this.extractData) .catch(this.handleError); } I cloned the angular2-webpack-starter project and added the above code myself. I imported Observable using import {Observable} from 'rxjs/Observable'; I'm assuming the properties Observable are imported as well ( .map works). Looked at the changelog for rxjs.beta-6 and nothing is mentioned

Difference between .unsubscribe to .take(1)

こ雲淡風輕ζ 提交于 2019-12-17 08:52:27
问题 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? 回答1: Each serves a different purpose so it

RxJs Observables nested subscriptions?

六月ゝ 毕业季﹏ 提交于 2019-12-17 06:45:14
问题 Whats the way to simplify something like the following code example? I can't find the right operator.. could anyone give a short example? this.returnsObservable1(...) .subscribe( success => { this.returnsObservable2(...) .subscribe( success => { this.returnsObservable3(...) .subscribe( success => { ... }, 回答1: As mentioned in comments, you are looking for the flatMap operator. You can find more details in previous answers : How to do the chain sequence in rxjs Why do we need to use flatMap?

RxJs Observables nested subscriptions?

佐手、 提交于 2019-12-17 06:45:10
问题 Whats the way to simplify something like the following code example? I can't find the right operator.. could anyone give a short example? this.returnsObservable1(...) .subscribe( success => { this.returnsObservable2(...) .subscribe( success => { this.returnsObservable3(...) .subscribe( success => { ... }, 回答1: As mentioned in comments, you are looking for the flatMap operator. You can find more details in previous answers : How to do the chain sequence in rxjs Why do we need to use flatMap?