This question builds upon this one, where it is shown how to feed an Observable
into a Subject
. My question is similar, but I want to avoid making the Observable
hot unless it is necessary, so that it's .pipe()
doesn't run needlessly. For example:
const subject$ = new Subject();
const mouseMove$ = Observable.fromEvent(document, 'mousemove')
.pipe(map(it => superExpensiveComputation(it)));
mouseMove$.subscribe(n => subject$.next(n));
Because of the subscription, this will make mouseMove$
hot, and superExpensiveComputation
will be run for every mouse move, whether someone is listening for it on subject$
or not.
How can I feed the result of mouseMove$
into subject$
without running superExpensiveComputation
unneccessarily?