The solution below involves keeping the state of the relevant event data in the previous ten minutes in a list using Observable.Scan
. State is maintained as a list of tuples with an int
(money) and a DateTime
as values.
var events = Observable.FromEvent<YourEventArgs>(
h => SomeEvent += h, h => SomeEvent -= h);
var runningSums =
events.Scan(new List<Tuple<int, DateTime>>(),
(l, e) =>
{
var now = DateTime.Now;
// Add last event data to list.
l.Add(Tuple.Create(e.EventArgs.Money, now));
// Return the correct part of the list (everything
// from the last ten minutes).
return l.Where(t => (now - t.Item2) <
TimeSpan.FromMinutes(10)).ToList();
})
.Select(l => l.Sum(t => t.Item1));
runningSums.Subscribe(sum => Console.WriteLine(sum));
EDIT: Example that doesn't return a new list for every event:
var events = Observable.FromEvent<YourEventArgs>(
h => SomeEvent += h, h => SomeEvent -= h);
var runningSums =
events.Scan(Tuple.Create(new List<Tuple<int, DateTime>>(),
DateTime.Now - TimeSpan.FromMinutes(10)),
(l, e) =>
{
var now = DateTime.Now;
l.Item1.Add(Tuple.Create(e.EventArgs.Nr, now));
// if (trimming-condition) then trim front of list...
return Tuple.Create(l.Item1, now - TimeSpan.FromMinutes(10));
})
.Select(l => l.Item1.Where(t => t.Item2 > l.Item2).Sum(t => t.Item1));
runningSums.Subscribe(sum => Console.WriteLine(sum));