The Easiest Way to Save and Share Code Snippets on the web

Untitled

csharp

posted: Nov, 18th 2009 | jump to bottom

public static IObservable<RoutedEventArgs> GetMultiClickScan(
  this ButtonBase button, int count, int multiClickSpeedInMilliSeconds)
{
  if (count < 1)
    throw new ArgumentOutOfRangeException("count");
 
  var span = TimeSpan.FromMilliseconds(multiClickSpeedInMilliSeconds);
 
  return Observable
    .FromEvent<RoutedEventArgs>(button, "Click")
    .Select(x => x.EventArgs)
    .TimeInterval()
    .Scan(new { Value = default(RoutedEventArgs), Count = 0 },
      (acc, e) => new { e.Value, Count =
        acc.Count < count && e.Interval <= span ? acc.Count + 1 : 1})
    .Where(x => x.Count == count)
    .Select(x => x.Value);
}
139 views