rxjs5

ObjectUnsubscribedError when trying to prevent subscribing twice

主宰稳场 提交于 2019-11-27 02:30:04
问题 I have a Service and a component that uses it: PagesService PagesListComponent In the PagesService I have an array of Pages . I notify changes in the array via a BehaviorSubject which both of them are subscribed to. The PagesService are provided at bootstrap , to have just one instance shared. That's because I need to keep the array, instead of downloading the pages everytime they are needed. The code is the following: pages.service.ts import { Injectable } from '@angular/core'; import {

Best way to “flatten” an array inside an RxJS Observable

你离开我真会死。 提交于 2019-11-27 01:28:55
问题 My backend frequently returns data as an array inside an RxJS 5 Observable (I'm using Angular 2). I often find myself wanting to process the array items individually with RxJS operators and I do so with the following code (JSBin): const dataFromBackend = Rx.Observable.of([ { name: 'item1', active: true }, { name: 'item2', active: false }, { name: 'item3', active: true } ]); dataFromBackend // At this point, the obs emits a SINGLE array of items .do(items => console.log(items)) // I flatten

How do I get around this “Subject incorrectly extends Observable” error in TypeScript 2.4 and RxJS 5.x?

核能气质少年 提交于 2019-11-26 22:49:22
问题 When I compile, I get the following compiler error in the RxJS declaration files: node_modules/rxjs/Subject.d.ts(16,22): error TS2415: Class 'Subject<T>' incorrectly extends base class 'Observable<T>'. Types of property 'lift' are incompatible. Type '<R>(operator: Operator<T, R>) => Observable<T>' is not assignable to type '<R>(operator: Operator<T, R>) => Observable<R>'. Type 'Observable<T>' is not assignable to type 'Observable<R>'. Type 'T' is not assignable to type 'R'. What's going on

Chaining Observables in RxJS

邮差的信 提交于 2019-11-26 18:47:14
I'm learning RxJS and Angular 2. Let's say I have a promise chain with multiple async function calls which depend on the previous one's result which looks like: var promiseChain = new Promise((resolve, reject) => { setTimeout(() => { resolve(1); }, 1000); }).then((result) => { console.log(result); return new Promise((resolve, reject) => { setTimeout(() => { resolve(result + 2); }, 1000); }); }).then((result) => { console.log(result); return new Promise((resolve, reject) => { setTimeout(() => { resolve(result + 3); }, 1000); }); }); promiseChain.then((finalResult) => { console.log(finalResult);

Angular 2 + rxjs - how return stream of objects fetched with several subsequent http requests

倾然丶 夕夏残阳落幕 提交于 2019-11-26 18:36:28
问题 I have data classes class Processor { frequency: number; error: booelan; } class Computer { name: string; processor: Processor[]; } I faetch it from backend using json: { "name": "Alfa Beta", "processors": [ { "url": "ASD3-455R-FRTT-ASEW" }, { "url": "AQW2-DFFFR-367K-MMKE" } ] } and single processor { "url": "ASD3-455R-FRTT-ASEW", "frequency": 2200, working: true } I need to return stream of Computer as I'd like to ask for processors status each minute. For single returned instance of

Rxjs: Observable.combineLatest vs Observable.forkJoin

ε祈祈猫儿з 提交于 2019-11-26 18:05:12
问题 Just wonder what is differences between Observable.combineLatest and Observable.forkJoin ? As far as I can see, the only difference is forkJoin expects the Observables to be completed, while combineLatest return the latest values. 回答1: Not only does forkJoin require all input observables to be completed, but it also returns an observable that produces a single value that is an array of the last values produced by the input observables. In other words, it waits until the last input observable

RxJS: takeUntil() Angular component's ngOnDestroy()

核能气质少年 提交于 2019-11-26 17:37:22
问题 tl;dr: Basically I want to marry Angular's ngOnDestroy with the Rxjs takeUntil() operator. -- is that possible? I have an Angular component that opens several Rxjs subscriptions. These need to be closed when the component is destroyed. A simple solution for this would be: class myComponent { private subscriptionA; private subscriptionB; private subscriptionC; constructor( private serviceA: ServiceA, private serviceB: ServiceB, private serviceC: ServiceC) {} ngOnInit() { this.subscriptionA =

How to force observables to execute in sequence?

浪尽此生 提交于 2019-11-26 16:43:01
I am moving from the Promise world to the Observable world. One thing I usually do with Promise is to chain a series of tasks and make them run in sequence. For example, I have three tasks: printLog1() to print 1 to the console, printLog23() to print 2 and 3 to the console, and printLog4() to print 4. When I want to print 1-2-3-4, I would write a promise chain like printLog1() .then(() => { printLog23(); }) .then(() => { printLog4(); }); Now I want the same functionality with Observable and I can rewrite the printLog() function into an Observable like printLog1 = Rx.Observabale.of(1).map((i) =

What is pipe for in rxJS

爱⌒轻易说出口 提交于 2019-11-26 16:02:57
问题 I think I have the base concept, but there are some obscurities So in general this is how I use an observable: observable.subscribe(x => { }) If I want to filter data I can use this: import { first, last, map, reduce, find, skipWhile } from 'rxjs/operators'; observable.pipe( map(x => {return x}), first() ).subscribe(x => { }) I can also do this: import 'rxjs/add/operator/map'; import 'rxjs/add/operator/first'; observable.map(x => {return x}).first().subscribe(x => { }) So my questions are:

How to achieve a debounce service on input keyup event in angular2 with rxjs

Deadly 提交于 2019-11-26 15:35:34
问题 I am trying to call to a service on input key-up event. The HTML <input placeholder="enter name" (keyup)='onKeyUp($event)'> Below is the onKeyUp() function onKeyUp(event) { let observable = Observable.fromEvent(event.target, 'keyup') .map(value => event.target.value) .debounceTime(1000) .distinctUntilChanged() .flatMap((search) => { // call the service }); observable.subscribe((data) => { // data }); } It was found from the network tab of the browser that, it is calling the key-up function on