Skip to content

Commit 206713b

Browse files
committed
wip: event engine delegates exposed
1 parent 0fee497 commit 206713b

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

src/Api/PubnubApi/EventEngine/Core/EffectDispatcher.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ public class EffectDispatcher {
88
private readonly Dictionary<System.Type, IEffectHandler> effectInvocationHandlerMap =
99
new Dictionary<System.Type, IEffectHandler>();
1010

11+
public event System.Action<IEffectInvocation> OnEffectDispatch;
12+
1113
/// <summary>
1214
/// Dispatch an invocation i.e. call a registered effect handler.
1315
/// </summary>
1416
public async Task Dispatch<T>(T invocation) where T : IEffectInvocation {
1517
if (!effectInvocationHandlerMap.ContainsKey(invocation.GetType())) {
1618
throw new ArgumentException($"No handler for {invocation.GetType().Name} found.");
1719
}
20+
21+
OnEffectDispatch?.Invoke(invocation);
1822

1923
if (invocation is IEffectCancelInvocation) {
2024
await effectInvocationHandlerMap[invocation.GetType()].Cancel();

src/Api/PubnubApi/EventEngine/Core/Engine.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,25 @@ public abstract class Engine {
1111
private Task currentTransitionLoop = Utils.EmptyTask;
1212

1313
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;
1433

1534
public Engine() {
1635
eventQueue.OnEventQueued += OnEvent;
@@ -22,12 +41,14 @@ public Engine() {
2241

2342
private async void OnEvent(EventQueue q)
2443
{
44+
OnEventQueued?.Invoke(q.Peek());
2545
await currentTransitionLoop;
2646
currentTransitionLoop = eventQueue.Loop(async e => currentState = await Transition(e));
2747
}
2848

2949
private async Task<State> Transition(IEvent e) {
3050
var stateInvocationPair = currentState.Transition(e);
51+
OnStateTransition?.Invoke(stateInvocationPair);
3152

3253
if (stateInvocationPair is null) {
3354
return currentState;

src/Api/PubnubApi/EventEngine/Core/EventQueue.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ private IEvent Dequeue()
3333
}
3434
}
3535

36+
public IEvent Peek()
37+
{
38+
lock (lockObj)
39+
{
40+
return eventQueue.Peek();
41+
}
42+
}
43+
3644
public async Task Loop<T>(System.Func<IEvent, Task<T>> function)
3745
{
3846
while (Count > 0)

0 commit comments

Comments
 (0)