搜索

首页  >  问答  >  正文

javascript - rxjs中publishReplay().refCount()的作用

我找了很多资料都看的不是很明白,希望大家可以简单的说一说或者提供点资料,谢谢!

天蓬老师天蓬老师2788 天前1023

全部回复(1)我来回复

  • PHPz

    PHPz2017-04-11 12:16:56

    var subject$$ = new Rx.ReplaySubject();
    subject$$.subscribe( x => console.log('sub',x) );
    
    var observableA$ = Rx.Observable.of('test');
    observableA$.subscribe(subject$$) ;
    

    =======省略的写法========

    var connectableObservable$ = Rx.Observable.of('test').multicast( new Rx.ReplaySubject())
    

    //需要手动的去connect

    var sub = connectableObservable$.connect();
    connectableObservable$.subscribe( x => console.log('sub',x) );
    

    //需要手动去unsubscribe unconnect

    sub.unsubscribe()
    
    =====这样很麻烦,使用引用计数 来自动connect unconnect====
    var sourceA$ = connectableObservable$.refCount(); //stands for referenceCounting
    var subA = sourceA$.subscribe( x => console.log('sub',x) )  
    //ref 0->1    then  automatic connect
    subA.unsubscribe()  
    //ref 1-> 0 then  automatic unconnect
    
    Rx.Observable.of('test').multicast( new Rx.ReplaySubject() ).refCount()   

    ->进一步省略

    Rx.Observable.of('test').publishReplay().refCount() 
    
    
    
    

    回复
    0
  • 取消回复