最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

typescript - Signal that an Observable with shareReplay is "stale" - Stack Overflow

programmeradmin4浏览0评论

I have a situation with shareReplay(1) . Note that "recalculating" result$ is expensive so I don't want to do it unless required hence I'm using shareReplay(1) in the first place.

refreshSubject = new BehaviorSubject<void>(undefined);

// Assume this has many subscriptions.
obs$ = refreshSubject.pipe(
  switchMap(() => timer(1000)), // My actual logic here is more complicated but the important part is that there's some time delay before result$ emits)
  switchMap(() => result$),
  shareReplay(1),
);

Now if I call refreshSubject.next() (to "reset" the value of obs$ after some logic that would change the value of result$) and want to immediately use a new/"up to date" value of obs$ like so

refreshSubject.next();

obs$.pipe(take(1)).subscribe(value => {
  ...
});

The subscription seems to receive the old value, presumably because result$ hasn't emitted yet (due to the time delay) so shareReplay(1) hasn't yet "realised" that its value is "stale".

Is there an elegant way to "tell" the shareReplay Observable that its stored value is no longer relevant (so any subscriptions after the refresh will use the next emitted value whether it occurs before/after they subscribe)?

I have a situation with shareReplay(1) . Note that "recalculating" result$ is expensive so I don't want to do it unless required hence I'm using shareReplay(1) in the first place.

refreshSubject = new BehaviorSubject<void>(undefined);

// Assume this has many subscriptions.
obs$ = refreshSubject.pipe(
  switchMap(() => timer(1000)), // My actual logic here is more complicated but the important part is that there's some time delay before result$ emits)
  switchMap(() => result$),
  shareReplay(1),
);

Now if I call refreshSubject.next() (to "reset" the value of obs$ after some logic that would change the value of result$) and want to immediately use a new/"up to date" value of obs$ like so

refreshSubject.next();

obs$.pipe(take(1)).subscribe(value => {
  ...
});

The subscription seems to receive the old value, presumably because result$ hasn't emitted yet (due to the time delay) so shareReplay(1) hasn't yet "realised" that its value is "stale".

Is there an elegant way to "tell" the shareReplay Observable that its stored value is no longer relevant (so any subscriptions after the refresh will use the next emitted value whether it occurs before/after they subscribe)?

Share Improve this question edited Mar 31 at 7:14 jonrsharpe 122k30 gold badges268 silver badges475 bronze badges asked Mar 31 at 7:00 Christopher MattiskeChristopher Mattiske 212 bronze badges New contributor Christopher Mattiske is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Add a comment  | 

1 Answer 1

Reset to default 1

just change shareReplay to share, it fits exactly what u need.

https://rxjs.dev/api/operators/share

share is similar to shareReplay that it multicast an obs to multiple subscriber, but share does not store or replay previous emit.

Edit: The comment reminded me, I fot to mention.

After changing it to share, need to swap the position of subscribe and next. like:

obs$.pipe(take(1)).subscribe(value => {
  ...
});

refreshSubject.next();
发布评论

评论列表(0)

  1. 暂无评论