Difference between new Observable(…) and Rx.Observable.create(…)?

There is no difference. Observable.create calls new Observable. As the manual says, Observables are created using Rx.Observable.create or a creation operator <…> Rx.Observable.create is an alias for the Observable constructor Observable.create is conventionally used, probably because it reads better in chains and conforms with other Observable static methods that create observables, too. The difference may … Read more

Rxjs Observable.take(1) vs Subscription.unsubscribe()

The take(1) approach has a number of advantages over subscribe: Code readability (and elegance). The second approach requires that you hold and manage extra variables. The second approach will not invoke the complete handler. This is because .take(1) actually create a new observable which potentially yields a single item and completes. The second approach will … Read more

How to unsubscribe from EventEmitter in Angular 2?

EventEmitter extends Subject. When you subscribe to a subject you get a Subscription which you can later use to unsubscribe. someOutput:EventEmitter = new EventEmitter(); … this.subscription = someOutput.subscribe(…); … this.subscription.unsubscribe(); Hint Don’t use EventEmitter for anything else but @Output()s. Angular doesn’t guarantee that EventEmitter will keep extending Subject or even work similar to a Subject … Read more

How to catch error in Observable.forkJoin(…)?

You may catch the error in each of your observables that are being passed to forkJoin: // Imports that support chaining of operators in older versions of RxJS import {Observable} from ‘rxjs/Observable’; import {forkJoin} from ‘rxjs/add/observable/forkJoin’; import {of} from ‘rxjs/add/observable/of’; import {map} from ‘rxjs/add/operator/map’; import {catch} from ‘rxjs/add/operator/catch’; // Code with chaining operators in older … Read more

How to make an http call every 2 minutes with RXJS?

Since you are already using Observables, simply make full use of it 🙂 Obersvable.interval() is your good friend here: In your component, do this: Observable .interval(2*60*1000) .timeInterval() .mergeMap(() => this.notificationService.getNotifications(this.token)) .subscribe(data => { console.log(data); }); Explanation: .interval() creates an observable that emits an event every 2 minutes. .timeInterval() convert an Observable that emits items into … Read more

How to monitor number of RXJS subscriptions?

You could achieve it using defer to track subscriptions and finalize to track completions, e.g. as an operator: // a custom operator that will count number of subscribers function customOperator(onCountUpdate = noop) { return function refCountOperatorFunction(source$) { let counter = 0; return defer(()=>{ counter++; onCountUpdate(counter); return source$; }) .pipe( finalize(()=>{ counter–; onCountUpdate(counter); }) ); }; … Read more

Merge two observables, single output

Merging observables means that items emitted by both observable will be emitted successively and separately by the new merged observable, cf this page. If your observables emit just one item each and you want the merge the items by concatenating the arrays, you could use the zip operator as follows: zip(observable, this.observable) .pipe(map(x => x[0].concat(x[1]))) … Read more

RxJS 6 – Cancel / End a Pipe

You can also cancel/end a pipe by using a signal Subject and the rxjs operator: takeUntil Example httpGetSafe(path: string): Observable<Data> { const stopSignal$ = new Subject(); return this.http.get<Data>(path).pipe( map(data => { const isBad = data === null; if (isBad) { stopSignal$.next(); } return data; }), takeUntil(stopSignal$) ); } Sometimes it’s a bit simpler and more … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)