Is Rx.Subject a hot observable?

前端 未结 2 1476
北海茫月
北海茫月 2020-12-19 07:16

The code

const a = new Rx.Subject().do(x => console.log(\'a\'))
const b = a.mapTo(0)
const c = a.mapTo(1)
const d = Rx.Observable.merge(b, c)
d.subscribe(         


        
2条回答
  •  情歌与酒
    2020-12-19 07:44

    The Subject itself is hot/shared.

    However: Any(most!) operators that you append will create a new stream, with the previous stream(in this case the Subject) as source - the new stream, however, is (for most operators) not hot and will only be made hot by deriving a hot stream through appending a hot operator (like share or publish ect...)

    So when you share your do, everything should work as expected.

    const a = new Rx.Subject().do(x => console.log('a')).share();
    const b = a.mapTo(0);
    const c = a.mapTo(1);
    const d = Rx.Observable.merge(b, c)
    d.subscribe(x => console.log('d'));
    a.next(3);

提交回复
热议问题