@@ -24,18 +24,22 @@ public class EventCapturer : IEventSubscriber
24
24
{
25
25
private readonly Queue < object > _capturedEvents ;
26
26
private readonly IEventSubscriber _subscriber ;
27
- private readonly List < Type > _eventsToCapture ;
27
+ private readonly Dictionary < Type , Func < object , bool > > _eventsToCapture ;
28
28
29
29
public EventCapturer ( )
30
30
{
31
31
_capturedEvents = new Queue < object > ( ) ;
32
32
_subscriber = new ReflectionEventSubscriber ( new CommandCapturer ( this ) ) ;
33
- _eventsToCapture = new List < Type > ( ) ;
33
+ _eventsToCapture = new Dictionary < Type , Func < object , bool > > ( ) ;
34
34
}
35
35
36
- public EventCapturer Capture < TEvent > ( )
36
+ public EventCapturer Capture < TEvent > ( Func < TEvent , bool > predicate = null )
37
37
{
38
- _eventsToCapture . Add ( typeof ( TEvent ) ) ;
38
+ if ( predicate == null )
39
+ {
40
+ predicate = o => true ;
41
+ }
42
+ _eventsToCapture . Add ( typeof ( TEvent ) , o => predicate ( ( TEvent ) o ) ) ;
39
43
return this ;
40
44
}
41
45
@@ -61,22 +65,28 @@ public bool Any()
61
65
62
66
public bool TryGetEventHandler < TEvent > ( out Action < TEvent > handler )
63
67
{
64
- if ( _eventsToCapture . Count > 0 && ! _eventsToCapture . Contains ( typeof ( TEvent ) ) )
68
+ if ( _eventsToCapture . Count > 0 && ! _eventsToCapture . ContainsKey ( typeof ( TEvent ) ) )
65
69
{
66
70
handler = null ;
67
71
return false ;
68
72
}
69
73
70
- if ( ! _subscriber . TryGetEventHandler < TEvent > ( out handler ) )
74
+ if ( ! _subscriber . TryGetEventHandler ( out handler ) )
71
75
{
72
76
handler = e => Capture ( e ) ;
73
77
}
74
78
75
79
return true ;
76
80
}
77
81
78
- private void Capture ( object @event )
82
+ private void Capture < TEvent > ( TEvent @event )
79
83
{
84
+ Func < object , bool > predicate ;
85
+ if ( _eventsToCapture . TryGetValue ( typeof ( TEvent ) , out predicate ) && ! predicate ( @event ) )
86
+ {
87
+ return ;
88
+ }
89
+
80
90
_capturedEvents . Enqueue ( @event ) ;
81
91
}
82
92
0 commit comments