Rxjs共享运算符未给出预期结果

Rxjs共享运算符未给出预期结果,rxjs,rxjs6,Rxjs,Rxjs6,我试图让rxjs操作符共享像中一样工作,但没有得到预期的结果。我希望“运行映射函数”只记录一次。我错过什么了吗 常数{of}=rxjs; const{map,share}=rxjs.operators; const obs$=数据的数量; const-mapped$=obs$。pipemapd=>{ console.log'Running map function'; 返回`mapped${d}`; }; const shared$=映射的$.pipeshare; 共享$.subscribed

我试图让rxjs操作符共享像中一样工作,但没有得到预期的结果。我希望“运行映射函数”只记录一次。我错过什么了吗

常数{of}=rxjs; const{map,share}=rxjs.operators; const obs$=数据的数量; const-mapped$=obs$。pipemapd=>{ console.log'Running map function'; 返回`mapped${d}`; }; const shared$=映射的$.pipeshare; 共享$.subscribed=>{console.logd;}; 共享$.subscribed=>{console.logd;};
如前所述,您的潜艇是同步的。要获得预期的行为,可以使用调度程序。例如,要从节拍开始,您可以阅读

const{of,Observable,asyncScheduler}=rxjs; const{map,share,observeOn}=rxjs.operators; const obs$=的'data'。pipeobserveOnasyncScheduler; const-mapped$=obs$。pipemapd=>{ console.log'Running map function'; 返回`mapped${d}`; }; const shared$=映射的$.pipeshare; 共享$.subscribed=>{console.logd;}; 共享$.subscribed=>{console.logd;};
问题是,您的可观察对象已完成。尝试通过添加concatnevery来防止这种情况,因为您的可观测源都是同步的。共享引用计数订阅,当引用计数从0增加到1时订阅源,当引用计数下降到0时取消订阅。因为源是同步的,所以对共享源的每个订阅都会看到引用计数的递增和递减,因此每个订阅都涉及对源的单独订阅。