Lev
Lev

Reputation: 15684

have Subject emit a value when it is subscribed to

How can I make a Subject emit a value when it is subscribed to ?

let mySubject = new Subject<string>
// specify somehow that when you subscribe to mySubject, it emits 'foo'

mySubject.subscribe(value => {
  // get 'foo' here
});

Upvotes: 5

Views: 2916

Answers (3)

Ced
Ced

Reputation: 17337

I believe your description of what you wish to have is actually not what you wish to have..

For instance if you want your subject to emit foo every time someone subscribes to it, then you can extend the subject.

https://jsfiddle.net/v11rndmt/

class MySubject extends Rx.Subject{
  constructor(){ super(); }
  subscribe(oOrOnNext, onError, onCompleted){
     const subs = super.subscribe(oOrOnNext, onError, onCompleted);
     this.next("foo")
     return subs;
  }
}

let sub = new MySubject()
sub.subscribe(v => console.log(v));
sub.subscribe(v => console.log(v));
sub.subscribe(v => console.log(v));

This will emit foo 3 times. The first time there is only 1 subscriber, so it will print once. The second time there is two so it will print twice. The third there is 3 so it will print 3 times. Thus foo will be printed 6 times.

I'm not figuring out any use case for this however. Thus maybe you should provide us with more information.

Upvotes: 0

asmmahmud
asmmahmud

Reputation: 5044

Not only you can emit stream/value from a Subject actually you can emit stream/value to multiple Observers. That means you can attach multiple observers to a Subject. Every Subject can contains a collection of Observer. When you'll subscribe to that Subject then it will emit stream/value to every Observer in its collection.

const subject =  new Rx.Subject()
// add an observer to the list of observers of the subject
const sub1 = subject.subscribe(function(val){
       console.log("observer 1", val);
});
// add another observer to the list of observers of the subject
const sub2 = subject.subscribe(function(val){
       console.log("observer 2", val);
});
// notify all observers in the list with "hi there"
subject.next('hi there');

// remove observer1 from the list
sub1.unsubscribe();

Not only this, you can use a Subject as an Observer and supply it to an Observable. That means you can use a Subject to “multicast” an Observable to multiple Observers.

// To "share" the observable tick$ with two observers,
// this way, we can pipe all notifications
// through a Subject, like so

const tick$ = Rx.Observable.interval(1000);
const subject = new Rx.Subject();
subject.subscribe(function(val){
    console.log('from observer 1: '+ val);
});
subject.subscribe(function(val){
    console.log('from observer 2: '+ val);
});
tick$.subscribe(subject);

In order to understand RxJs you've to understand the Observer Pattern prescribed by “Gang Of Four” . I will suggest you to try to understand the Observer Pattern and I hope you'll be clear about what the RxJs library is doing.

There is another wonderful reference from the book of Learning JavaScript Design Patterns by Addy Osmani.

Upvotes: 1

Corim Bretinson
Corim Bretinson

Reputation: 66

You just need to push data to the stream now by using .next

The code below should work:

const mySubject = new Subject()

mySubject.subscribe(value => {
  console.log(value)
});

mySubject.next("foo")

Upvotes: 0

Related Questions