Skip to content

Commit

Permalink
wip: preliminary potential fix for dispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeDobrzan committed Aug 2, 2023
1 parent 206713b commit 57f4fb2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Api/PubnubApi/EventEngine/Core/EffectDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class EffectDispatcher {
/// <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.");
}
Expand All @@ -24,7 +24,7 @@ public async Task Dispatch<T>(T invocation) where T : IEffectInvocation {
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
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 57f4fb2

Please sign in to comment.