Probably missing ability to dispatch non-typed messages
Hi again,
I have a use-case of message forwarding from a different event bus. This event bus can dispatch generic-typed events as well as an ability to handle all the events in a single general handler, in which case the dispatched event will be passed as an object.
// e here is an `object` which cannot be dispatched to a generic AsyncMessageBus handler
DomainEvents.RegisterGeneralHandler(e => AsyncMessageBus.Default.PublishAsync(e));
An easy fix here would be to add an overload that accepts object as a msg argument and just msg.GetType(). Probably it is not needed in UniTaskPubSub or maybe there's a better way to do this, but right now I'm not sure how to deal with this issue without Reflection.
Hi :)
Ok, I'm going to consider adding a Non-Generic version of Publish.
However, the current Generic version of Publish does not have boxing allocation, while the Non-Generic version definitely has boxing and will degrade performance compared to the Generic version.
--
Another way to do msg.GetType() might be to cast the object.
switch (msg)
{
case msg is MessageA x:
PublishAsync(x);
break;
case msg is MessageB x:
PublishAsync(x);
break;
default:
throw new NotImplementationException();
}
Yes, we can use the This is a pain. But maybe we can do automatic code generation.
Oof
Again, most likely it's not worth it. As you said, it will have performance implications, but more than that it adds some sematics which might be not clear:
AsyncMessageBus.Default.Subscribe(Handler)
UniTask Handler(object message)
Does this code mean that Handler will handle any possible message? Looks like it should if Non-Generic publish is introduced. And after that the question of polymorphic dispatch can arise. Similar thing is done in MediatR
https://github.com/jbogard/MediatR/wiki#polymorphic-dispatch
But this feature might be an overkill for a Unity message bus, since performance is paramount in gamedev.