RXJS管道行为主体

RXJS管道行为主体,rxjs,Rxjs,我有一个默认为空对象的流。随着时间的推移,此对象将填充其键 const RXSubject = new BehaviorSubject({}); RXSubject.pipe( filter((frame): frame is InstDecodedFrame => frame.type === FrameType.INST), scan<InstDecodedFrame, InstantDataDictionnary>( (acc, fr

我有一个默认为空对象的流。随着时间的推移,此对象将填充其键

  const RXSubject = new BehaviorSubject({});

  RXSubject.pipe(
    filter((frame): frame is InstDecodedFrame => frame.type === FrameType.INST),
    scan<InstDecodedFrame, InstantDataDictionnary>(
      (acc, frame) => ({ ...acc, ...frame.dataList }),
      {},
    ),
  );
constrxsubject=newbehaviorsubject({});
RXSubject.pipe(
过滤器((帧):帧为instdeceddframe=>frame.type===FrameType.INST),
扫描(
(acc,frame)=>({…acc,…frame.dataList}),
{},
),
);
现在我在应用程序的某个部分订阅了过滤器,但是如果我在其他地方订阅了,并且最后一个值没有触发过滤条件。我的新观察者什么也得不到

是否有任何方法可以从管道的任何订阅服务器中获取最新的“有效”值


谢谢

您可以在
filter()之后使用
shareReplay(1)
并订阅可观察到的内容:

const obs$ = RXSubject.pipe(
    filter((frame): frame is InstDecodedFrame => frame.type === FrameType.INST),
    scan<InstDecodedFrame, InstantDataDictionnary>(
      (acc, frame) => ({ ...acc, ...frame.dataList }),
      {},
    ),
    shareReplay(1),
  );
const obs$=RXSubject.pipe(
过滤器((帧):帧为instdeceddframe=>frame.type===FrameType.INST),
扫描(
(acc,frame)=>({…acc,…frame.dataList}),
{},
),
共享重播(1),
);
然后您将订阅
obs$
,而不是
RXSubject

,也许这个答案会有帮助: