Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 Observable - How to call next from outside of the Observable's constructor

Tags:

angular

rxjs

I am building a service which exposes an Observable. In this service I receive external function calls which should trigger a next call on the Observable so that various consumers get the subscribe event. During Observer constructor I can call next and everything works great, but how can I access this outside of the constructor so that external triggers can fire next calls?

private myObservable$: Observable<any>; 

During service init I do

this.myObservable$ = new Observable(observer => {     observer.next("initial message"); } 

Then in other methods of the same service I want to be able to execute something like

this.myObservable$.observer.next("next message"); 

The above obviously doesn't work, but how can I accomplish this goal?

I'm assuming I'm missing something basic since there must be a way to emit further messages outside of the Observable's initial constructor

like image 964
Joshua Ohana Avatar asked Jan 25 '17 18:01

Joshua Ohana


People also ask

Does observable have next?

Executing Observableslink There are three types of values an Observable Execution can deliver: "Next" notification: sends a value such as a Number, a String, an Object, etc. "Error" notification: sends a JavaScript Error or exception. "Complete" notification: does not send a value.

How do you call an observable?

To execute the observable you have created and begin receiving notifications, you call its subscribe() method, passing an observer. This is a JavaScript object that defines the handlers for the notifications you receive.

Is observable next async?

A common misconception in Angular development is regarding whether observables are synchronous or asynchronous. A lot of (even experienced Angular developers) think that observables are async, but the truth is that they can be… Both synchronous and asynchronous.

What is subject in RxJS?

A subject in RxJS is a special hybrid that can act as both an observable and an observer at the same time. This way, data can be pushed into a subject, and the subject's subscribers will, in turn, receive that pushed data.


2 Answers

Actually Subject is used for both publisher and subscriber, and here I think you need only to publish your value, so simply use Observable.

By using observable, assign Subscriber to class level variable and then use it, like below code

subscriber: Subscriber<boolean>;  public observe(): Observable<boolean> {      return new Observable<boolean>(subs => {       this.subscriber = subs;     });   }  public callNext() {      if (this.subscriber) {       this.subscriber.next();       this.subscriber.complete();     }   } 
like image 36
Ravi Sevta Avatar answered Sep 19 '22 11:09

Ravi Sevta


You should create a Subject for that

this.myObservable$ = new Subject(); 

And then you can call at any point:

this.myObservable$.next(...); 

Or use subscribe:

this.myObservable$.subscribe(...) 
like image 174
olsn Avatar answered Sep 22 '22 11:09

olsn