I have a subject that is responsible for subscriptions to certain observable:
var timer$ = Rx.Observable.timer(1000, 2000);
When the subject is linked to the subject like that
var timerSubject = new Rx.Subject;
timer$.subscribe(timerSubject);
var subscription1 = timerSubject.subscribe(n => console.log(n));
var subscription2 = timerSubject.subscribe(n => console.log(n));
setTimeout(() => timerSubject.unsubscribe(), 4000);
everything is fine, timerSubject.unsubscribe()
can be called once and the subscriptions shouldn't be unsubscribed one by one.
When the subject is created with Subject.create
like that (a plunk)
var timerSubject = Rx.Subject.create(null, timer$);
var subscription1 = timerSubject.subscribe(n => console.log(n));
var subscription2 = timerSubject.subscribe(n => console.log(n));
setTimeout(() => timerSubject.unsubscribe(), 4000);
timerSubject.unsubscribe()
does nothing, while I would expect to behave it the same as in the first snippet.
If Subject.create
creates a subject that can't even unsubscribe, what's the purpose of Subject.create
then?
Why does this happen? Is this a bug?
How can the subject should be created to reach the desired behaviour?
It is reproducible with RxJS 5 RC1.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…