Utilize debounceTime
to postpone emission until a specified time has elapsed without receiving a new emission, and employ takeUntil
to conclude an observable when another observable emits a value:
const mouseEnterSubscription = mouseEnterStream.pipe(
debounceTime(500),
takeUntil(mouseLeaveStream),
repeat(),
).subscribe(
() => playVideo()
);
In this code snippet, we delay emitting for 500ms but will exit if the mouseLeaveStream emits first. The use of takeUntil
ensures that your mouseLeaveSubscription does not need to handle any cleanup tasks. We utilize repeat
to "restart" the completed source (otherwise it would only emit once).
const mouseLeaveSubscription = mouseLeaveStream.subscribe(
() => pauseVideo()
);
For reference, here is a StackBlitz example.