rxjs

Should rxjs subjects be public in the class?

不羁的心 提交于 2019-12-05 04:22:46
Let's say I have two classes, where you can observe over some observables. First example, with public subject: class EventsPub { public readonly onEnd = new Subject<void>(); } Second example, with private subject and registering method: class EventsPriv { private readonly endEvent = new Subject<void>(); public onEnd(cb: () => void): Subscription { return this.endEvent.subscribe(cb); } } The first example is somehow unsafe because anyone can call eventsPub.endEvent.next() from outside the class and introduce side effects, however, comparing to example 2 It allows for pipes, which is a big plus

is there any difference between import { Observable } from 'rxjs/Observable' and import { Observable } from 'rxjs'?

橙三吉。 提交于 2019-12-05 04:12:54
When using rxjs in angular 2, is there any difference between import { Observable } from 'rxjs/Observable' and import { Observable } from 'rxjs' ? Yes there is a slight difference which is the bundle size. If you aren't using tree shaking library like rollup.js which remove all unnecessary codes, your bundle will be large when importing from 'rxjs' as you are importing everything even if you are using only the Observable. On the other hand if you import from 'rxjs/Observable' you are only importing what you need and the bundle will be smaller. Import only what you need and patch Observable

Subject.next not firing in ngOnInit

给你一囗甜甜゛ 提交于 2019-12-05 03:57:21
Does anyone why this code (initializing a value from Subject) does not work? Is there a bug or by design? What am I doing wrong? ts import { Component, OnInit } from '@angular/core'; import { Subject } from "rxjs"; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.styl'] }) export class AppComponent implements OnInit { itemSupplier$: Subject<any[]> = new Subject<any[]>(); items: any[] = [ {name: 'Item 1', value: 'item1'}, {name: 'Item 2', value: 'item2'}, ]; ngOnInit(){ this.itemSupplier$.next(this.items); } } html <ul> <li *ngFor="let item of

TypeError: rxjs__WEBPACK_IMPORTED_MODULE_2__.Observable.throw is not a function

筅森魡賤 提交于 2019-12-05 03:52:32
Here is my file userdata.service.ts : import { Injectable } from '@angular/core'; import { Http, Response } from '@angular/http'; import {Observable} from 'rxjs'; import { map } from "rxjs/operators"; import { catchError } from 'rxjs/operators'; import { Data } from './userdata'; @Injectable() export class UserDataService { url : "http://localhost:4200/assets/data/userdata.json"; constructor(private http:Http) { } getDataWithObservable() : Observable<any>{ return this.http.get(this.url) .pipe( map(this.extractData), catchError(this.handleErrorObservable) ); } private extractData(res: Response)

Angular2 + ngrx/store for handling failure HTTP requests

两盒软妹~` 提交于 2019-12-05 03:36:37
I want to have a simple code path for creating and dispatching HTTP actions. What I would like to do is something like: this.http.request(...) .map((res: Response) => res.json()) .catch((err: any) => err.json()) .map((payload: any) => { type: 'SUCCESS', payload }) .catch((payload: any) => { type: 'FAILURE', payload}) .subscribe((action: Action) => this.store.dispatch(action)); That way both the success and failure responses are converted to JSON and then based upon the success/fail criteria assign the correct reduction type so that the store can be operated on properly. (think user login

Angular 4 Subscribe method call multiple times

≡放荡痞女 提交于 2019-12-05 02:54:27
问题 im creating a global modal component. My problem is when i call the subscribe method it call multiple times base on the number of the modal was called. How can i prevent mutiple calls on observable subscribe method? Please check my code below. Thanks in advance. modal.model.ts export class Modal { title: string; message: string; visible: boolean = false; } modal.service import { Injectable } from '@angular/core'; import { Modal } from './modal.model'; import { Observable } from 'rxjs'; import

How to Map Response object returned by Http Service to a TypeScript Objects using Observable Map function in Angular2

*爱你&永不变心* 提交于 2019-12-05 02:35:27
Hi I have created an angular2 service whose task is to call a webapi which returns data in a json object structure as follows: //Result of the webapi service call. { "Total":11, "Data":[{"Id":1,"Name":"A","StartDate":"2016-01-01T00:00:00"}, {"Id":2,"Name":"B","StartDate":"2016-02-01T00:00:00"}] } Here is my angular2 service. The getMileStones methods work perfectly and I am able to cast the response back to MileStone[] . But In order to get the paged data I have created a function getPagedMileStones(int,int) which call a webapi method and return the result as mentioned above structure .I want

Chaining RxJs Observables in Angular 2

孤者浪人 提交于 2019-12-05 02:29:35
问题 I've got a TypeScript function in my Angular 2 app that returns an Observable, to push web API data back to the consumer, something like this: public getApiData(): Observable { let readySource = Observable.from('no', 'no', 'ready'); let dataSource = http.get('api/getData'); // ... magic here return chainedObservable; } However, rather than returning the http.get Observable as normal, I need to chain this HTTP call to another readySource Observable that indicates whether the API is ready to

Angular2 Observable with interval

狂风中的少年 提交于 2019-12-05 02:10:34
问题 I have a function that needs to be called about every 500ms. The way I am looking at doing it with angular2 is using intervals and observables. I have tried this function to create the observable: counter() { return Observable.create(observer => { setInterval(() => { return this.media.getCurrentPosition(); }, 500) }) } With this code for the subscriber: test() { this.playerService.initUrl(xxxx) // This works this.playerService.counter().subscribe(data => { res => { console.log(data); } }) } I

How to “react” on data changes with RxJS?

穿精又带淫゛_ 提交于 2019-12-05 01:39:01
RxJS beginner here: I have problems with saving and tracking data changes using RxJS. Say I structure my app in small views/widgets and every view/widget has its own state and should do things on data changes. How do I do that? More concrete example. Let's say I have a widget called Widget and Widget has a title and button. The state should contain the title and the information if the button was already clicked. From reading the docs of RxJS it seems this would be a good starting point: var widgetState = new Rx.Subject().startWith({ wasClicked: false, title: 'foo' }); Now I want to be notified