From 57f4fb2c085d7ad190377ca00bab0df3a08f93c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Dobrza=C5=84ski?= Date: Wed, 2 Aug 2023 12:54:28 +0200 Subject: [PATCH] wip: preliminary potential fix for dispatcher --- .../EventEngine/Core/EffectDispatcher.cs | 4 ++-- src/Api/PubnubApi/EventEngine/Core/Utils.cs | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Api/PubnubApi/EventEngine/Core/EffectDispatcher.cs b/src/Api/PubnubApi/EventEngine/Core/EffectDispatcher.cs index bad03b562..1a326159e 100644 --- a/src/Api/PubnubApi/EventEngine/Core/EffectDispatcher.cs +++ b/src/Api/PubnubApi/EventEngine/Core/EffectDispatcher.cs @@ -13,7 +13,7 @@ public class EffectDispatcher { /// /// Dispatch an invocation i.e. call a registered effect handler. /// - public async Task Dispatch(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."); } @@ -24,7 +24,7 @@ public async Task Dispatch(T invocation) where T : IEffectInvocation { await effectInvocationHandlerMap[invocation.GetType()].Cancel(); } else { - var handler = ((IEffectHandler)effectInvocationHandlerMap[invocation.GetType()]); + var handler = effectInvocationHandlerMap[invocation.GetType()]; if (handler.IsBackground(invocation)) handler.Run(invocation).Start(); else diff --git a/src/Api/PubnubApi/EventEngine/Core/Utils.cs b/src/Api/PubnubApi/EventEngine/Core/Utils.cs index 8657d6fa7..6436a262a 100644 --- a/src/Api/PubnubApi/EventEngine/Core/Utils.cs +++ b/src/Api/PubnubApi/EventEngine/Core/Utils.cs @@ -1,6 +1,7 @@ using System; using System.Threading.Tasks; using System.Collections.Generic; +using System.Reflection; namespace PubnubApi.EventEngine.Core { @@ -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