rxjs

RxJS 5, converting an observable to a BehaviorSubject(?)

偶尔善良 提交于 2019-12-18 14:14:42
问题 I have a parent observable that, once it has a subscriber, will do a lookup and emit a single value, then complete. I'd like to convert that into an observable (or behavior subject or whatever works) that does the following: once it has at least one subscriber, it gets the result from the parent observable (once). Then it emits that value to all of its subscribers, and also emits that single value to all future subscribers, when they subscribe. It should continue with this behavior even if

Rxjs--转换操作符

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-18 13:55:29
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 对Observable 发出的值进行: 缓存(buffer)、映射(map)、扫描累计(scan)、抽取对象属性(pluck)、分离(partition、groupBy)、组合操作(map&concat、merge、switch和scan&merge)、递归扩展(expand)、窗口限制(window) buffer public buffer(closingNotifier: Observable<any>): Observable<T[]> 将 Observable 发出的值缓冲起来直到 closingNotifier 发出数据, 在这个时候在输出 Observable 上发出该缓冲区的值并且内部开启一个新的缓冲区, 等待下一个closingNotifier的发送 bufferCount public bufferCount(bufferSize: number, startBufferEvery: number): Observable<T[]> 将过往的值收集到一个数组中,当数组数量到达设定的 bufferSize 时发出该数组 bufferTime public bufferTime(bufferTimeSpan: number, bufferCreationInterval: number,

Does Subject.complete() unsubscribe all listeners?

≯℡__Kan透↙ 提交于 2019-12-18 13:53:33
问题 I build a simple confirmation dialog service (Angular 2) with this method: confirm(body?: string, title?: string): Subject<void> { this.confirmation = new Subject<void>(); // ... show dialog here... "are you sure?" return this.confirmation; } _onYesClicked() { // ... closing the dialog this.confirmation.next(); this.confirmation.complete(); } _onNoClicked() { // ... closing the dialog this.confirmation.complete(); } Usage: confirmationService.confirm().subscribe(() => alert("CONFIRMED")); If

what triggered combineLatest?

不羁的心 提交于 2019-12-18 13:33:15
问题 I have a few observables. And I need to know which one triggered the subscribe. Observable.combineLatest( this.tournamentsService.getUpcoming(), this.favoriteService.getFavoriteTournaments(), this.teamsService.getTeamRanking(), (tournament, favorite, team) => { //what triggered combinelatest to run? }).subscribe() 回答1: Short answer is: You don't know. You could implement some workaround, however this is really ugly and I would recommend rethinking the usecase why you need this and maybe if

Cancel previous requests and only fire the latest request with redux observable

孤街浪徒 提交于 2019-12-18 12:38:42
问题 So I have use case where I update the api request when the map is moved - but it could generate several rapid fire requests with small map movements - and I want to cancel all the inflight requests except for the last one. I can use debounce to only send requests after a delay. However I still want to cancel any old requests if they happen to still be in process. const fetchNearbyStoresEpic = action$ => action$.ofType(FETCH_NEARBY_STORES) .debounceTime(500) .switchMap(action => db.collection(

I dont get rxjs 6 with angular 6 with interval, switchMap, and map

只谈情不闲聊 提交于 2019-12-18 12:12:51
问题 I want to update my rxjs code to 6 got I don't get it. Before I had the below that wouth poll for new data every 5 seconds: import { Observable, interval } from 'rxjs'; import { switchMap, map } from 'rxjs/operators'; var result = interval(5000).switchMap(() => this._authHttp.get(url)).map(res => res.json().results); Now...of course, it's broken and the documentation leaves me nowhere to go. How do I write the above to conform to rxjs 6? Thanks 回答1: The code should be something like the

Angular filter Observable array

对着背影说爱祢 提交于 2019-12-18 12:08:38
问题 I have an Observable array and I want to filter/find the Project by name. When I try to use the filter option it is saying ProjectService.ts import { Injectable } from '@angular/core'; import { Project } from "../classes/project"; import { Observable } from 'rxjs/Observable'; import 'rxjs/add/observable/of'; import { Http } from '@angular/http'; @Injectable() export class ProjectService { private projects: Observable<Project[]>; constructor(private http: Http) { this.loadFromServer(); }

Angular 2 observable subscription not triggering

心已入冬 提交于 2019-12-18 11:47:34
问题 I have an issue with a subscription to an observable not triggering. I have a layout using the side nav layout directive looking like this: <md-sidenav-layout class="nav-bar"> <md-sidenav #start> <nav> <a [routerLink]="['/home']">Home</a> </nav> <button md-button (click)="start.close()">Close</button> </md-sidenav> <router-outlet></router-outlet> </md-sidenav-layout> What I need to do is open the side nav from the component that the router-outlet is displaying. One such component might look

Observable.throw replacement in rxjs 5.5.2

旧巷老猫 提交于 2019-12-18 11:41:09
问题 I'm migrating to rxjs@5.5.2 and using lettable operators... I also update Observable static methods. I wonder what is the counterpart of Observable.throw and import 'rxjs/add/observable/throw'; . Should I import ugly _throw ? import { _throw } from 'rxjs/observable/throw'; Or there's a better way. Honestly I liked static methods on Observable , and now it seems that all static creating methods like of , from should be imported from rxjs/observable/<methodName> ? 回答1: I'm still getting my head

RxJS - .subscribe() vs .publish().connect()

筅森魡賤 提交于 2019-12-18 11:09:12
问题 This is mainly an RxJs best practice/approach question, since my POC code works but I'm brand new to RxJs. The question boils down to .subscribe() vs .publish().connect() , since they both appear to do the same thing. In my angular2 app, I have a button that calls a function to log the user out, which calls a function in my service that performs some server side actions and returns me a URL to redirect the user to. In order to initiate the request I call .subscribe() to cause the observable