Anyone have example Rx code that shows how to execute an action due to a delayed event that is cancelled by another event? Such as, displaying a tooltip when hovering over a button for a certain period of time?
The action is “display tooltip”
The duration is the “certain period of time”
And the cancellation event would be “mouse out” if the mouse stops hovering over the button.
Thanks!
Always start by breaking down the problem into what you have and what you want, turn that into a function signature, and see if that reveals an implementation.
so you have a trigger source, a delay duration, and a cancellation source
The desired behavior you describe is:
Now that we have the spec, we can make a signature and consider an implementation
Depending on the nature of the cancellation source, you could need to pass
Func<T, IObservable<TCancel>>, but it sounds like this will work in your case.The first two lines of the spec suggest
SelectMany(each item in the source makes another observable that will be combined back to a single observable). To get the cancellation, we just need to wait for the delay or until a cancellation, which we can do withTakeUntil. Since the final items will be the source items, the return type will be the same as the source.