File tree Expand file tree Collapse file tree 3 files changed +33
-0
lines changed
src/Api/PubnubApi/EventEngine/Core Expand file tree Collapse file tree 3 files changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -8,13 +8,17 @@ public class EffectDispatcher {
8
8
private readonly Dictionary < System . Type , IEffectHandler > effectInvocationHandlerMap =
9
9
new Dictionary < System . Type , IEffectHandler > ( ) ;
10
10
11
+ public event System . Action < IEffectInvocation > OnEffectDispatch ;
12
+
11
13
/// <summary>
12
14
/// Dispatch an invocation i.e. call a registered effect handler.
13
15
/// </summary>
14
16
public async Task Dispatch < T > ( T invocation ) where T : IEffectInvocation {
15
17
if ( ! effectInvocationHandlerMap . ContainsKey ( invocation . GetType ( ) ) ) {
16
18
throw new ArgumentException ( $ "No handler for { invocation . GetType ( ) . Name } found.") ;
17
19
}
20
+
21
+ OnEffectDispatch ? . Invoke ( invocation ) ;
18
22
19
23
if ( invocation is IEffectCancelInvocation ) {
20
24
await effectInvocationHandlerMap [ invocation . GetType ( ) ] . Cancel ( ) ;
Original file line number Diff line number Diff line change @@ -11,6 +11,25 @@ public abstract class Engine {
11
11
private Task currentTransitionLoop = Utils . EmptyTask ;
12
12
13
13
private readonly IEffectInvocation [ ] emptyInvocationList = new IEffectInvocation [ 0 ] ;
14
+
15
+ /// <summary>
16
+ /// Subscribe to receive notification on effect dispatch
17
+ /// </summary>
18
+ public event System . Action < IEffectInvocation > OnEffectDispatch
19
+ {
20
+ add => dispatcher . OnEffectDispatch += value ;
21
+ remove => dispatcher . OnEffectDispatch -= value ;
22
+ }
23
+
24
+ /// <summary>
25
+ /// Subscribe to receive notification on state transition
26
+ /// </summary>
27
+ public event System . Action < TransitionResult > OnStateTransition ;
28
+
29
+ /// <summary>
30
+ /// Subscribe to receive notification on event being queued
31
+ /// </summary>
32
+ public event System . Action < IEvent > OnEventQueued ;
14
33
15
34
public Engine ( ) {
16
35
eventQueue . OnEventQueued += OnEvent ;
@@ -22,12 +41,14 @@ public Engine() {
22
41
23
42
private async void OnEvent ( EventQueue q )
24
43
{
44
+ OnEventQueued ? . Invoke ( q . Peek ( ) ) ;
25
45
await currentTransitionLoop ;
26
46
currentTransitionLoop = eventQueue . Loop ( async e => currentState = await Transition ( e ) ) ;
27
47
}
28
48
29
49
private async Task < State > Transition ( IEvent e ) {
30
50
var stateInvocationPair = currentState . Transition ( e ) ;
51
+ OnStateTransition ? . Invoke ( stateInvocationPair ) ;
31
52
32
53
if ( stateInvocationPair is null ) {
33
54
return currentState ;
Original file line number Diff line number Diff line change @@ -33,6 +33,14 @@ private IEvent Dequeue()
33
33
}
34
34
}
35
35
36
+ public IEvent Peek ( )
37
+ {
38
+ lock ( lockObj )
39
+ {
40
+ return eventQueue . Peek ( ) ;
41
+ }
42
+ }
43
+
36
44
public async Task Loop < T > ( System . Func < IEvent , Task < T > > function )
37
45
{
38
46
while ( Count > 0 )
You can’t perform that action at this time.
0 commit comments