rxjs

RxJs/NgRx - is there a way to “Cancel” a stream after the delay operator

余生长醉 提交于 2020-03-20 19:35:25
问题 I am using a polling scheme in my Angular application using NgRx. To simplify things, I have something like the following... public stopPolling$ = createEffect(() => this.actions$.pipe( ofType(actions.stopPolling), tap(_ => this.isPollingActive = false), map(_ => actions.stopPolling()) ), { dispatch: false }); public continuePolling$ = createEffect(() => this.actions$.pipe( ofType(actions.getData), tap(_ => this.logger.debug('continue polling')), delay(8000), switchMap(_ => this.pollData()) )

Angular 7 testing retryWhen with mock http requests fails to actually retry

做~自己de王妃 提交于 2020-03-20 16:03:05
问题 I have the following interceptor that tries to use a OAuth refresh_token whenever any 401 (error) response is obtained. Basically a refresh token is obtained on the first 401 request and after it is obtained, the code waits 2,5 seconds. In most cases the second request will not trigger an error, but if it does (token couldn't be refreshed or whatever), the user is redirect to the login page. export class RefreshAuthenticationInterceptor implements HttpInterceptor { constructor( private router

RxJS 中的创建操作符

僤鯓⒐⒋嵵緔 提交于 2020-03-19 19:15:20
RxJs 中创建操作符是创建数据流的起点,这些操作符可以凭空创建一个流或者是根据其它数据形式创建一个流。 Observable的构造函数可以直接创建一个数据流,比如: const $source=new Observable(observer=>{ observer.next(1); observer.next(2); observer.next(3); }) 但是在真正的使用过程中很少使用这种方式去创建,RxJx 提供了大量的创建操作符供我们在开发中去使用。创建型操作符打不风都是静态操作符。 一、创建同步数据流 同步数据流,或者说同步Observable对象,需要关⼼的就是: 1.产⽣哪些数据 2. 数据之间的先后顺序如何。 对于同步数据流,数据之间的时间间隔不存在,所以不需要考虑时间 ⽅⾯的问题。 1、create 最简单的操作符 它的使用像这样: import {Observable} from 'rxjs/Observable'; const onSubscribe=observer=>{ observer.next(1); observer.next(2); observer.next(3); } const source$=Observable.create(onSubscribe); var theObserver={ next:item=>console.log

再读《深入浅出 rxjs》的小收获

巧了我就是萌 提交于 2020-03-18 18:29:29
3 月,跳不动了?>>> Observbale 对象可以看作一个数据集合,但这个数据集合可以不是一次产生,而是在一个时间段上逐个产生每个数据,因为这,一个 observable 对象即使产生超庞大的数据,依然不会消耗很多内存,因为每一次只产生一个,吐出来之后再产生另一个,不会挤压; 每一个操作符都需要考虑: 返回一个全新的 observable 对象、对上游和下游的订阅及退订处理、处理异常情况,及时释放资源; 为什么使用 pipe 来组合操作数?好处:1. 它通过删除操作符来清除 Observable.prototype 2. 让 rxjs 的库更加容易被摇树优化 3. 更易写和使用第三方操作数,因为不需要给OBservable.prototype打补丁; Throttle 和 debounce, 节流和去抖动; throttleTime 的作用是限制在 duration 时间范围内,从上游传递给下游数据的 个数 ,debounceTime 的作用是让传递给下游的时间间隔不能小于给定 时间 dueTime; combineLatest: 取得各个 observable 最后送出的值,再合并在一起输出一个observable;zip 会取每个 observable 相同顺位的元素组合成一个 observable;( 平时没事不要用 zip, 除非真的需要,因为zip必须cache

Rxjs: Difference between Observable.First vs Single vs Filter

谁说胖子不能爱 提交于 2020-03-18 12:30:15
问题 I am exploring RxJS library and really a fan of using Observable instead of Promise. However, can someone provide any detailed information about the difference between using Observable.First Observable.Single Apply Filter in such a way that it returns only single item What is the need for Single specifically in this library? 回答1: If by filter you mean something like: let emitted = false; obs = obs.filter(x => { if(emitted) { return false; } else { emitted = true; return true; } }); Filter (in

Property 'switchMap' does not exist on type 'Observable<User>'

匆匆过客 提交于 2020-03-18 04:01:07
问题 I have been getting the following error message when trying to apply the switchMap operator to my Observable: Property 'switchMap' does not exist on type 'Observable'. I'm currently using rxjs version 5.5.2, and in my component, I have it imported like so: import 'rxjs/operator/switchMap'; However, I still get a compilation error. I have looked at similar questions and have not found a proper solution to this, any suggestions on what could be the issue here? get appUser$() : Observable

理解 rxjs 中的 subject

烂漫一生 提交于 2020-03-17 12:52:32
某厂面试归来,发现自己落伍了!>>> 已经学习了很久的 angular 了,rxjs 中的操作数也有很多都打过了照面,subject 也已经可以仿着别人的栗子,能写个大概的轮廓了,可是我真的理解了、掌握了 rxjs 中的 subject 吗? 以前有人告诉过我:在生活中学到的所有东西中,真正让人难以忘怀的就是我们教给其他人的东西。所以现在就把它慢慢整理出来,如果没有理解透彻 subject 的你看到了,很开心你会有所收获; 背景:为什么我们需要 subject ? 理解 hot Observable 和 cold observable hot Observable: 每次被 subscribe 都会产生一个全新的数据序列数据流;(创建类操作符、interval、range);也就是虽然对一个 Observable 做了多次 subscribe, 但是对于每一个 Observer, 其实都有一个独立的数据流在喂着,数据并不是真正来自于同一个源头;也就是不同订阅者的订阅实则是相互独立,互不干扰的;比如下面这个例子: const source$ = interval(500).pipe(take(3)); const observerA = { next: value => console.log('A' + value), error: error => console.log('A'

What is the difference between Reactjs and Rxjs?

天涯浪子 提交于 2020-03-17 04:43:45
问题 Basically I am start learning Rxjs and I am a little bit confused between React and Rxjs. I was supposing that Reactjs and Rxjs is same. Questions: If Reactjs and Rxjs is the same then why are we using Reactjs over Rxjs or vise versa? If Reactjs and Rxjs not same, then please briefly differentiate both the languages. 回答1: This is really different, React is view library and Rxjs is reactive programming library for javascript. You can use Rxjs inside react view but in reactjs, people usually

Angular subscribe elegant

。_饼干妹妹 提交于 2020-03-15 13:23:11
问题 I'd like to ask about my subscribe method. I want to get my objects(employees) from firebase and do a loot of operations on my objects, like avg salaries, avg missed days... In my opinion it looks so horrible. My service void: getEmployees(): Observable<Employee[]> { return this.db.list<Employee>(this.Api_url).snapshotChanges() .pipe(map(response => response.map(car => this.assignKey(car)))); } And my ngOnInit: ngOnInit() { this.subscribtion = this.mainservice.getEmployees().subscribe((emps)=

angular--Observable总结

a 夏天 提交于 2020-03-12 22:19:55
Observable的通俗理解 Observable在消息发布者和观察者Observer之间起到一个媒体中介的作用,Observer是真正需要接受信息的人。Observable的实例提供了一个订阅函数,subscribe()。当Observable的实例接收到信息时,将Observer作为参数传递给subscribe(),也就是通过subscribe()函数通知Observer,Observer接到通知后,使用next()方法再通知Observable()表示可以传数据了,然后Observer可以一直接收到数据,直到发生error(),或是数据接收完成,complete(); 创建Observable对象的几种情形 通过promise创建 import {fromPromise} from 'rxjs'; const data = fromPromise(fetch('api/endpoint')); data.subscribe({ next(response){ console.log(response); }, error(err){ console.error('Error' + err); }, complete(){ console.log('Completed'); } }); 通过counter创建 import {interval} from 'rxjs'; /