// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System.Runtime.CompilerServices; namespace CommunityToolkit.Mvvm.Messaging.Internals; /// /// A dispatcher type that invokes a given callback. /// /// /// This type is used to avoid type aliasing with when the generic /// arguments are not known. Additionally, this is an abstract class and not an interface so that when /// is called, virtual dispatch will be used instead of interface /// stub dispatch, which is much slower and with more indirections. /// internal abstract class MessageHandlerDispatcher { /// /// Invokes the current callback on a target recipient, with a specified message. /// /// The target recipient for the message. /// The message being broadcast. public abstract void Invoke(object recipient, object message); /// /// A generic version of . /// /// The type of recipient for the message. /// The type of message to receive. public sealed class For : MessageHandlerDispatcher where TRecipient : class where TMessage : class { /// /// The underlying callback to invoke. /// private readonly MessageHandler handler; /// /// Initializes a new instance of the class. /// /// The input instance. public For(MessageHandler handler) { this.handler = handler; } /// public override void Invoke(object recipient, object message) { this.handler(Unsafe.As(recipient), Unsafe.As(message)); } } }