0

Suppose I have an Observable that has been created like this

let observable = of(mockData).pipe(delay(5000));

How can I at an later point in time, emit another value to the observers currently subscribing to this observable?

I have read this RxJS: How would I "manually" update an Observable? and BehaviorSubject vs Observable? but I can not seems to grasps why I am holding an object of type Observable in my hand, and can not access any easy way to emit data to observers without defining an emitter independently at the observable creation time like this

var emitter;
var observable = Rx.Observable.create(e => emitter = e);
var observer = {
  next: function(next) {
    console.log(next);
  },
  error: function(error) {
    console.log(error);
  },
  complete: function() {
    console.log("done");
  }
}
observable.subscribe(observer);
emitter.next('foo');
emitter.next('bar');
emitter.next('baz');
emitter.complete();

//console output
//"foo"
//"bar"
//"baz"
//"done"
qkhanhpro
  • 4,371
  • 2
  • 33
  • 45
  • 2
    [Observables are specialized functions](https://benlesh.com/posts/learning-observable-by-building-observable/). Once defined you can just subscribe to them. `Subject`s on the other hand are also Observers, and therefore they offer the `next` method which you can call and that will notify whatever value is passed to their subscribers. – Picci Feb 27 '22 at 09:05

2 Answers2

4

Observables are not Emmitables (not official interface name;P) thus you cannot emmit using Observable

What you have to do is to combine that existing stream with subject that you can use to emmit new values, eg using merge

const subject=new Subject():
const yourObservableThatYouCannotChange;

const combinedObservable=merge(yourObservableThatYouCannotChange,subject);

now you have to return combinedObservable for others to subscribe to. Then if you want to inject emition into this stream, you simly emit it via subject.next(yourVal).

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • 1
    Thanks @Antoniossss . At the moment, I do use Subject to emit data like you suggested. I am just a bit puzzled that I will not be able to modify the existing yourObservableThatYouCannotChange in any meaningful way, thus I can not emit to the observers that already subscribed to the yourObservableThatYouCannotChange. Maybe this is for the best but I do not understand why – qkhanhpro Feb 27 '22 at 08:00
  • 1
    I think this is because it simplifies things if you can use Observable only for "reading" the data and not "writing" especially that your requirement can be fullfilled anyway using operators. It is similar to InputStream and OutputStream. None of those allows you to read/write at the same time. – Antoniossss Feb 28 '22 at 08:36
0

I am not sure I understood your goal correctly, but why not try it like this:

private subject = new Subject();
public observable = this.subject.asObservable();

This way you change your subject with internal methods, the observable will emit every new change, and the subject itself will be protected while you can only subscribe to the observable.

Julian
  • 11
  • 2