rxjs

Cancel an observable created with “fromEvent” and switch to a new one

…衆ロ難τιáo~ 提交于 2019-12-13 15:35:25
问题 My goal is to emit "hi again" in 1 second interval until a key is pressed and continue whenever there is a mouse click. Here is my code: import { of, fromEvent, interval } from 'rxjs'; import { map, tap, takeUntil,take, repeatWhen, shareReplay, switchMap , takeLast} from 'rxjs/operators'; const fromKeyUp$ = fromEvent(window, 'keyup').pipe(tap(_=> console.log('keyup'))); const fromMouseUp$ = fromEvent(window, 'mouseup').pipe(tap(_=> console.log('mouseup'))); const source = interval(1000).pipe

Import of lettable operators and observable creation methods

徘徊边缘 提交于 2019-12-13 15:23:11
问题 I'm upgrading to Angular 5 and RxJS 5.5.2 and trying to import Observable.of operator. Before lettable operators, we did it like this: import 'rxjs/add/observable/of'; // Usage Observable.of(...) But now importing from paths containing add is discouraged. So what is the proper way of importing and using lettable static operators now? 回答1: The operators that have now a lettable version are the instance operators. Since before 5.5.x of and any other observable creation methods can be used as in

Observable and ngFor in Angular 2

橙三吉。 提交于 2019-12-13 15:19:31
问题 Below is a simple sample Angular 2 component which uses rxjs Observables. import { Component , OnInit } from '@angular/core'; import { Observable } from 'rxjs/Observable'; import 'rxjs/add/observable/of'; @Component({ selector:'ob', template: ` <ul> <li *ngFor="let x of obj | async"> {{x}} </li> </ul> ` }) export class ObComponent implements OnInit{ obj:Observable<number>; ngOnInit() { this.obj = Observable.of(1,2,3,4); } } This component throws below error inline template:15:12 caused by:

How to delay event emission with rxpy/rxjs?

不羁岁月 提交于 2019-12-13 14:59:39
问题 I've got two event streams. One is from an inductance loop, the other is an IP camera. Cars will drive over the loop and then hit the camera. I want to combine them if the events are within N milliseconds of each other (car will always hit the loop first), but I also want the unmatched events from each stream (either hardware can fail) all merged into a single stream. Something like this: ---> (only unmatched a's, None) / \ stream_a (loop) \ \ \ --> (a, b) ---------------------------> (Maybe

Angular 2 - What to do when an Http request depends on result of another Http request

时光毁灭记忆、已成空白 提交于 2019-12-13 14:13:45
问题 I'm having trouble figuring out how to use the result of an Http request to make another Http request. I have a Service that requests and receives JSON Web Tokens from a backend API that looks like: @Injectable() export class JwtAuthorizationService { constructor(private http: Http) {} public requestToken(): Observable<string> { // Set dummy credentials. let body = this.setBody(); let headers = this.setHeaders(); let token = this.http.post(tokenUrl, body, { headers: headers }) .map(this

How to use RxJS observables in sequential order?

ぐ巨炮叔叔 提交于 2019-12-13 13:25:37
问题 Here's the deal: I have a HTTP get request that returns a JSON list of objects. Using RxJS I subscribe to receive the data of that list. Now, for each of the objects in that list I want to perform another HTTP request and then place the results of that request in an Array. So far I've been able to do this but I can't seem to figure out how to maintain the order of the initial list with data. This probably has to do with the fact that the whole Observable mechanism is asynchronous. Here's my

RXJS - Angular - unsubscribe from Subjects

僤鯓⒐⒋嵵緔 提交于 2019-12-13 13:22:40
问题 As described in this thread, 'official' solution to unsubscribe from Observables in Angular 5+ in general is using takeUntil. So far, so good. My question is, does this also apply if the Observable I am subscribed to is actually a Subject? 回答1: Once you call .subscribe() on anything (Subjects too), something needs to make sure the subscription gets unsubscribed. Dealing with finite Observables : If you subscribe to a finite observable (meaning an observable that has a finite/limited sequence)

ng2-translate customLoader & multiple files per language

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-13 12:42:41
问题 In an Ionic2 app I'm using ng2-translate the strings in my app. Now I need to split the translation file into several files per language, for example "de.json" and "de_gs1ais.json". As ng2-translate is limited to one file per language I've tried to implement a custom loader: class CustomLoader implements TranslateLoader { private http: Http; public getTranslation(lang: String): Observable<any> { return this.http.get("assets/i18n" + "/" + lang + ".json").map((res) => res.json()); } } This

When to use Promise over observable?

纵饮孤独 提交于 2019-12-13 12:04:17
问题 Is there any case, where Promise is more powerful as compare to observable? I know a lot of benefits of observables over promises. But Is there any case, I should use only promises over observables. I found this link, promises vs observables. But this always shows me the benefits of observables over promises. I want to know the benefits of promise over observables. 回答1: An observable does everything that a promise does and more. It can always be switched to a promise with toPromise() method

RXJS: Aggregated debounce

只谈情不闲聊 提交于 2019-12-13 11:33:14
问题 My use case is as following: I get events, which sometimes happen in bursts. If a burst occurs, I only need to handle it once though. Debounce does this. However, debounce only gives me the last element of a burst, but I need to know about all elements in a burst to aggregate on them (using flatmap). This could be done by a timed window or buffer, however, these are fixed intervals, so a buffer/window timeout could occur in the middle of a burst, therefore splitting the burst in 2 parts to