rxjs

What is switchMap equivalent to in unflattened form?

扶醉桌前 提交于 2019-12-24 02:26:03
问题 If flatMap() is equivalent to map().mergeAll() , then what is switchMap() equivalent to ? 回答1: The switchMap() operator is equivalent to using map().switch() . Since the switch() operator works only with higher-order Observables the preceding map() has to return an Observable. There's no switchAll() operator because the switch() operator already works with higher-order Observables(just like all *all() operators). 来源: https://stackoverflow.com/questions/43165848/what-is-switchmap-equivalent-to

Dynamically filtering rxjs stream

泄露秘密 提交于 2019-12-24 01:27:29
问题 I'm using RXJS and I'm looking to dynamically filter the data, but I'm having problems: let numberSource: ReplaySubject<Number> = new ReplaySubject<Number>(); let numberFilter: BehaviorSubject<Number> = new BehaviorSubject<Number>(5); let filteredData = numberSource.filter(n => n < numberFilter.value); numberFilter.subscribe(newFilter => { filteredData = numberSource.filter(n => n < newFilter); filteredData.subscribe(console.log); // <- I think this is wrong }); console.log("A"); filteredData

How to flatten Array of Observables in an Observable

无人久伴 提交于 2019-12-24 01:19:17
问题 I'm trying to flatten a nested Observable, but I'm not getting it to work: this.af.object('test/father') .map(res => { res.namedKeys = []; for (let el in res.keys) { res.namedKeys.push(this.af.object(`test/keys/${el}`)) } return res }) .flatMap(res => Observable.combineLatest(res)) .subscribe(res => { console.log('The final response:', res); }); I want to get the actual data of the Observables in my Array. I've tried a lot of different things the last 2 days, but in the end, I receive either

Angular2 components fail to communicate via observable in a service

余生颓废 提交于 2019-12-24 01:16:01
问题 I'm trying to make components send messages via a shared service, like in a cookbook's "Parent and children communicate via a service" recipe. I have a Sender component that calls a service's method, in which an event is fired. And there's a Receiver component, which's just listens to events. The problem is the Receiver doesn't get an event. Here's the code: import {bootstrap} from 'angular2/platform/browser' import {Injectable, Component} from 'angular2/core'; import 'rxjs/Rx'; import

Angular 2 - Inject authorization token before each Http request

僤鯓⒐⒋嵵緔 提交于 2019-12-24 01:14:29
问题 I need to check that my token is not expired before I send every HTTP request. I'm trying to make injection using Http Interceptor like this: class HttpInterceptor extends Http { constructor(backend: ConnectionBackend, defaultOptions: RequestOptions, private _router: Router) { super(backend, defaultOptions); } get(url: string, options?: RequestOptionsArgs): Observable<Response> { const keycloak$: Observable = KeycloakService.updateToken(); return keycloak$.map((options) => { return super.get

How to check if multiple subscriptions reached onComplete()?

╄→гoц情女王★ 提交于 2019-12-24 01:06:42
问题 I have two subscriptions in an Angular 6 component. Now I want to start a method as soon as these two subscriptions reached onComplete. How can I do this the easiest way? 回答1: Use forkJoin. It will emit as soon as all given Observables completed. 回答2: You can use forkJoin to wait for both once both are done, then you subscribe to it as you would normally do with an observable. Like so import { Component } from '@angular/core'; import { forkJoin, Observable } from 'rxjs' @Component({ selector:

Debounce and buffer an rxjs subscription

自作多情 提交于 2019-12-24 01:01:36
问题 I have a message queue processor that feeds messages to a service... q.on("message", (m) => { service.create(m) .then(() => m.ack()) .catch(() => n.nack()) }) The service uses an RxJS Observable and subscription to debounceTime() those requests. class Service { constructor() { this.subject = new Subject() this.subject.debounceTime(1000) .subscribe(({ req, resolve, reject }) => someOtherService.doWork(req) .then(() => resolve()) .catch(() => reject()) ) } create(req) { return new Promise(

Angular 2 recursive http.gets of unknown depth

与世无争的帅哥 提交于 2019-12-24 00:29:47
问题 I've been using a pattern like the below to chain together http.gets in Angular2 to retrieve information from a hierarchical structure, two layers deep, of folders (all pseudotypescript): myObservable = this.myService.getSubFolders(topFolderUrl) .switchMap(subFolders=> this.myService.getInfoFromSubFolders(subFolders)) .map(subFolders=> => { ...do stuff with subFolders... return subFolders; } ); Where myService looks something like this: getSubFolders(topFolderUrl): Observable<Folder[]> {

Is there an more ideomatic way to split a stream by some predicate?

非 Y 不嫁゛ 提交于 2019-12-24 00:17:52
问题 I have a stream of events, which I want to split into multiple streams, based on some property the event may have. Something like the following let streams = new rx.Subject() let stream = new rx.Subject() input.subscribe((x) => { stream.onNext(x) if (!x.splitHere) return streams.onNext(stream) stream = new rx.Subject() }) EDIT Thank you for the hints about partition and if . While they do split one stream into multiple, they only provide two result streams. Clarification What I need to do is

Which merge operator to use to listen to single source and not 2nd stream source

瘦欲@ 提交于 2019-12-23 23:17:44
问题 So I know the question may seem simple, but it's not that simple. All the operators I have tried such as combineLatest , concat , and switchMap result in diff issues. So here is the challenge: var campaignSelected$ = this.store.select(store => store.appDb.uiState.campaign.campaignSelected) var campaignsList$ = this.store.select(store => store.msDatabase.sdk.table_campaigns); return campaignSelected$.switchMap(i_campaignSelected => campaignsList$, (campaignId, campaigns) => { return 'foo'; })