MessageHandlerDispatcher.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Runtime.CompilerServices;
  5. namespace CommunityToolkit.Mvvm.Messaging.Internals;
  6. /// <summary>
  7. /// A dispatcher type that invokes a given <see cref="MessageHandler{TRecipient, TMessage}"/> callback.
  8. /// </summary>
  9. /// <remarks>
  10. /// This type is used to avoid type aliasing with <see cref="Unsafe.As{T}(object)"/> when the generic
  11. /// arguments are not known. Additionally, this is an abstract class and not an interface so that when
  12. /// <see cref="Invoke(object, object)"/> is called, virtual dispatch will be used instead of interface
  13. /// stub dispatch, which is much slower and with more indirections.
  14. /// </remarks>
  15. internal abstract class MessageHandlerDispatcher
  16. {
  17. /// <summary>
  18. /// Invokes the current callback on a target recipient, with a specified message.
  19. /// </summary>
  20. /// <param name="recipient">The target recipient for the message.</param>
  21. /// <param name="message">The message being broadcast.</param>
  22. public abstract void Invoke(object recipient, object message);
  23. /// <summary>
  24. /// A generic version of <see cref="MessageHandlerDispatcher"/>.
  25. /// </summary>
  26. /// <typeparam name="TRecipient">The type of recipient for the message.</typeparam>
  27. /// <typeparam name="TMessage">The type of message to receive.</typeparam>
  28. public sealed class For<TRecipient, TMessage> : MessageHandlerDispatcher
  29. where TRecipient : class
  30. where TMessage : class
  31. {
  32. /// <summary>
  33. /// The underlying <see cref="MessageHandler{TRecipient, TMessage}"/> callback to invoke.
  34. /// </summary>
  35. private readonly MessageHandler<TRecipient, TMessage> handler;
  36. /// <summary>
  37. /// Initializes a new instance of the <see cref="For{TRecipient, TMessage}"/> class.
  38. /// </summary>
  39. /// <param name="handler">The input <see cref="MessageHandler{TRecipient, TMessage}"/> instance.</param>
  40. public For(MessageHandler<TRecipient, TMessage> handler)
  41. {
  42. this.handler = handler;
  43. }
  44. /// <inheritdoc/>
  45. public override void Invoke(object recipient, object message)
  46. {
  47. this.handler(Unsafe.As<TRecipient>(recipient), Unsafe.As<TMessage>(message));
  48. }
  49. }
  50. }