I have a stream of click events and I want to determine from that stream whether a single click or a double clicked was performed by the user. Currently I’m attempting to do this by buffering the values over a specified doubleClickTime
(e.g. 300ms) and taking the count of those values.
So starting with this
IObservable<Click> clicks = ...
I’m attempting to use the buffer operator with an opening selector and closing selector
IDisposable subscription = clicks.Buffer(clicks, _ => Observable.Timer(doubleClickTime))
.Subscribe(items =>
{
if(items.Count > 1)
{
Debug.WriteLine($"Double Click");
}
else
{
Debug.WriteLine($"Single Click");
}
});
However, the issue with the above is that I get the following output when double clicking
Double Click
Single Click
i.e., it registers a double click and a single click. I think this is because my opening selector is also the click event, so on the double click a new buffer window is started.
How do I compose the opening selector correctly such that it only fires when no buffering is taking place?
Is there a better way to do this?
Update
Presumably it is similar to this example written in rxjs, but can’t quite figure out how to translate the below in C#
const clickOrDoubleClick$ = click$.pipe(
buffer(click$.pipe(switchMap(() => timer(DOUBLE_CLICK_INTERVAL)))),
map((clicksArr) =>
clicksArr.length > 1 ? createDoubleClick() : createClick() ));
6