forked from RolandPheasant/Dynamic.Trader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathThrottlingDispatcher.cs
56 lines (44 loc) · 1.33 KB
/
ThrottlingDispatcher.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using ObservableComputations;
namespace Trader.Domain.Infrastucture
{
public class ThrottlingDispatcher : IOcDispatcher, IDisposable
{
public IOcDispatcher DestinationOcDispatcher => _destinationOcDispatcher;
public TimeSpan TimeSpan => _timeSpan;
Subject<Action> _actions;
private IDisposable _cleanUp;
private readonly IOcDispatcher _destinationOcDispatcher;
private readonly TimeSpan _timeSpan;
public ThrottlingDispatcher(TimeSpan timeSpan, IOcDispatcher destinationOcDispatcher)
{
_timeSpan = timeSpan;
_destinationOcDispatcher = destinationOcDispatcher;
_actions = new Subject<Action>();
_cleanUp = _actions.Throttle(timeSpan).Subscribe(action =>
{
_destinationOcDispatcher.Invoke(action, 0, null, this);
});
}
#region Implementation of IOcDispatcher
public void Invoke(Action action, int priority, object parameter, object context)
{
_actions.OnNext(action);
}
#endregion
#region Implementation of IOcDisposable
public void Dispose()
{
_cleanUp.Dispose();
}
#endregion
}
public class UserInputThrottlingOcDispatcher : ThrottlingDispatcher
{
public UserInputThrottlingOcDispatcher(IOcDispatcher destinationOcDispatcher) : base(TimeSpan.FromMilliseconds(250), destinationOcDispatcher)
{
}
}
}