Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/eventengine-poc' into eventengin…
Browse files Browse the repository at this point in the history
…e/transition-unit-tests
  • Loading branch information
budgetpreneur committed Aug 2, 2023
2 parents 51d8738 + 57f4fb2 commit af2633b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Api/PubnubApi/EventEngine/Core/EffectDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@ public class EffectDispatcher {
private readonly Dictionary<System.Type, IEffectHandler> effectInvocationHandlerMap =
new Dictionary<System.Type, IEffectHandler>();

public event System.Action<IEffectInvocation> OnEffectDispatch;

/// <summary>
/// Dispatch an invocation i.e. call a registered effect handler.
/// </summary>
public async Task Dispatch<T>(T invocation) where T : IEffectInvocation {
public async Task Dispatch(IEffectInvocation invocation) {
if (!effectInvocationHandlerMap.ContainsKey(invocation.GetType())) {
throw new ArgumentException($"No handler for {invocation.GetType().Name} found.");
}

OnEffectDispatch?.Invoke(invocation);

if (invocation is IEffectCancelInvocation) {
await effectInvocationHandlerMap[invocation.GetType()].Cancel();
} else
{
var handler = ((IEffectHandler<T>)effectInvocationHandlerMap[invocation.GetType()]);
var handler = effectInvocationHandlerMap[invocation.GetType()];
if (handler.IsBackground(invocation))
handler.Run(invocation).Start();
else
Expand Down
21 changes: 21 additions & 0 deletions src/Api/PubnubApi/EventEngine/Core/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@ public abstract class Engine {
private Task currentTransitionLoop = Utils.EmptyTask;

private readonly IEffectInvocation[] emptyInvocationList = new IEffectInvocation[0];

/// <summary>
/// Subscribe to receive notification on effect dispatch
/// </summary>
public event System.Action<IEffectInvocation> OnEffectDispatch
{
add => dispatcher.OnEffectDispatch += value;
remove => dispatcher.OnEffectDispatch -= value;
}

/// <summary>
/// Subscribe to receive notification on state transition
/// </summary>
public event System.Action<TransitionResult> OnStateTransition;

/// <summary>
/// Subscribe to receive notification on event being queued
/// </summary>
public event System.Action<IEvent> OnEventQueued;

public Engine() {
eventQueue.OnEventQueued += OnEvent;
Expand All @@ -22,12 +41,14 @@ public Engine() {

private async void OnEvent(EventQueue q)
{
OnEventQueued?.Invoke(q.Peek());
await currentTransitionLoop;
currentTransitionLoop = eventQueue.Loop(async e => currentState = await Transition(e));
}

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

if (stateInvocationPair is null) {
return currentState;
Expand Down
8 changes: 8 additions & 0 deletions src/Api/PubnubApi/EventEngine/Core/EventQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ private IEvent Dequeue()
}
}

public IEvent Peek()
{
lock (lockObj)
{
return eventQueue.Peek();
}
}

public async Task Loop<T>(System.Func<IEvent, Task<T>> function)
{
while (Count > 0)
Expand Down
15 changes: 15 additions & 0 deletions src/Api/PubnubApi/EventEngine/Core/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Reflection;

namespace PubnubApi.EventEngine.Core
{
Expand All @@ -17,6 +18,20 @@ internal static IEffectInvocation[] AsArray(this IEffectInvocation invocation)
{
return new IEffectInvocation[] { invocation };
}

internal static bool IsBackground(this IEffectHandler handler, IEffectInvocation invocation)
{
return (bool)handler.GetType()
.GetMethod("IsBackground")
.Invoke(handler, new object[] { invocation });
}

internal static Task Run(this IEffectHandler handler, IEffectInvocation invocation)
{
return (Task)handler.GetType()
.GetMethod("Run")
.Invoke(handler, new object[] { invocation });
}
}

public class TransitionResult
Expand Down

0 comments on commit af2633b

Please sign in to comment.