RxJS unsubscribe from inner observable when the source completes

痴心易碎 提交于 2021-02-19 08:40:14

问题


I have a function that returns observable that automatically unsubscribe when some internal thing happens using a Subject. Here is a simple version that shows the issue:

const subject = new Subject();

const source = fromEvent(document.querySelector("h1"), "click").pipe(
  takeUntil(subject)
);

fromEvent(document.querySelector("p"), "click").subscribe(() => {
  subject.next();
});

The issue is that if someone subscribes source and uses higher-order observable, it will not subscribe from the inner observable when the Subject emits:

source.pipe(mergeMap(() => interval(1000))).subscribe(x => console.log(x));

There is a way to solve this issue without forcing the consumer to call unsubscribe?


回答1:


The issue is that source completing doesn't mean the currently running interval(s) end. If you want to kill the inner observables thing when source closes, you can do that, you'll just want to multicast source to avoid creating more h1 event listeners.

const subject = new Subject();

const source = fromEvent(document.querySelector("h1"), "click").pipe(
  takeUntil(subject)
);

const sharedSource = source.pipe(share());
const emitOnComplete = last(null, null);
const sourceIsDone = sharedSource.pipe(emitOnComplete);

sharedSource.pipe(
  mergeMap(() => interval(1000)),
  takeUntil(sourceIsDone)
);


来源:https://stackoverflow.com/questions/61462806/rxjs-unsubscribe-from-inner-observable-when-the-source-completes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!