< Summary

Information
Class: MyNet.Messaging.MessengerExtensions
Assembly: MyNet.Messaging
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Messaging/Extensions/MessengerExtensions.cs
Tag: 323_28699572109
Line coverage
100%
Covered lines: 31
Uncovered lines: 0
Coverable lines: 31
Total lines: 147
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
RegisterOnce(...)100%11100%
RegisterForDerivedMessages(...)100%11100%
RegisterOnChannel(...)100%11100%
SendOnChannel(...)100%11100%
SendTo(...)100%11100%
UnregisterAll(...)100%11100%
UnregisterFromChannel(...)100%11100%
RegisterWithFilter(...)100%11100%
SendFromFactory(...)100%11100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Messaging/Extensions/MessengerExtensions.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="MessengerExtensions.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8
 9#pragma warning disable IDE0130 // Namespace does not match folder structure
 10namespace MyNet.Messaging;
 11#pragma warning restore IDE0130 // Namespace does not match folder structure
 12
 13/// <summary>
 14/// Extension methods for IMessenger to provide convenient utilities and higher-level APIs.
 15/// </summary>
 16public static class MessengerExtensions
 17{
 18    extension(IMessenger messenger)
 19    {
 20        /// <summary>
 21        /// Registers a recipient to receive a message once, then automatically unregisters.
 22        /// Useful for one-time notifications.
 23        /// </summary>
 24        /// <typeparam name="TMessage">The type of message to listen for.</typeparam>
 25        /// <param name="recipient">The recipient object.</param>
 26        /// <param name="action">The action to execute once.</param>
 27        public void RegisterOnce<TMessage>(object recipient,
 28            Action<TMessage> action)
 29        {
 630            Action<TMessage>? wrappedAction = null;
 631            wrappedAction = msg =>
 632            {
 633                try
 634                {
 635                    action(msg);
 636                }
 637                finally
 638                {
 639                    if (wrappedAction != null)
 640                    {
 641                        messenger.Unregister(recipient, wrappedAction);
 642                    }
 643                }
 644            };
 45
 646            messenger.Register(recipient, wrappedAction, keepTargetAlive: true);
 647        }
 48
 49        /// <summary>
 50        /// Registers a recipient to receive derived messages of a type.
 51        /// Convenient shorthand for Register with receiveDerivedMessagesToo=true.
 52        /// </summary>
 53        /// <typeparam name="TMessage">The base message type to listen for.</typeparam>
 54        /// <param name="recipient">The recipient object.</param>
 55        /// <param name="action">The action to execute.</param>
 56        public void RegisterForDerivedMessages<TMessage>(object recipient,
 57            Action<TMessage> action)
 58            where TMessage : class =>
 359            messenger.Register(recipient, true, action, keepTargetAlive: false);
 60
 61        /// <summary>
 62        /// Registers a recipient to receive messages on a specific channel (token).
 63        /// </summary>
 64        /// <typeparam name="TMessage">The type of message to listen for.</typeparam>
 65        /// <param name="recipient">The recipient object.</param>
 66        /// <param name="channel">The channel identifier.</param>
 67        /// <param name="action">The action to execute.</param>
 68        public void RegisterOnChannel<TMessage>(object recipient,
 69            string channel,
 70            Action<TMessage> action) =>
 1271            messenger.Register(recipient, channel, action, keepTargetAlive: false);
 72
 73        /// <summary>
 74        /// Sends a message on a specific channel (token).
 75        /// </summary>
 76        /// <typeparam name="TMessage">The type of message to send.</typeparam>
 77        /// <param name="message">The message to send.</param>
 78        /// <param name="channel">The channel identifier.</param>
 79        public void SendOnChannel<TMessage>(TMessage message,
 80            string channel) =>
 981            messenger.Send(message, channel);
 82
 83        /// <summary>
 84        /// Sends a message only to recipients of a specific type.
 85        /// Convenient shorthand for Send{TMessage, TTarget}.
 86        /// </summary>
 87        /// <typeparam name="TMessage">The type of message to send.</typeparam>
 88        /// <typeparam name="TTarget">The type of recipients to target.</typeparam>
 89        /// <param name="message">The message to send.</param>
 90        public void SendTo<TMessage, TTarget>(TMessage message)
 91            where TTarget : class =>
 392            messenger.Send<TMessage, TTarget>(message);
 93
 94        /// <summary>
 95        /// Unregisters a recipient from all message types and channels.
 96        /// Convenient shorthand for standalone Unregister.
 97        /// </summary>
 98        /// <param name="recipient">The recipient to unregister.</param>
 399        public void UnregisterAll(object recipient) => messenger.Unregister(recipient);
 100
 101        /// <summary>
 102        /// Unregisters a recipient from a specific token/channel.
 103        /// </summary>
 104        /// <typeparam name="TMessage">The message type.</typeparam>
 105        /// <param name="recipient">The recipient to unregister.</param>
 106        /// <param name="channel">The channel identifier.</param>
 3107        public void UnregisterFromChannel<TMessage>(object recipient, string channel) => messenger.Unregister<TMessage>(
 108
 109        /// <summary>
 110        /// Creates a predicate-based observer that registers for messages and filters them.
 111        /// </summary>
 112        /// <typeparam name="TMessage">The type of message.</typeparam>
 113        /// <param name="recipient">The recipient object.</param>
 114        /// <param name="predicate">The filter predicate.</param>
 115        /// <param name="action">The action to execute when predicate matches.</param>
 116        public void RegisterWithFilter<TMessage>(object recipient,
 117            Func<TMessage, bool> predicate,
 118            Action<TMessage> action)
 119        {
 6120            messenger.Register<TMessage>(
 6121                recipient,
 6122                filteredAction,
 6123                keepTargetAlive: true);
 6124            return;
 125
 126            void filteredAction(TMessage msg)
 127            {
 128                if (predicate(msg))
 129                {
 130                    action(msg);
 131                }
 132            }
 133        }
 134
 135        /// <summary>
 136        /// Sends a broadcast message with optional filtering by target type.
 137        /// </summary>
 138        /// <typeparam name="TMessage">The message type.</typeparam>
 139        /// <param name="messageFactory">Factory function to create the message.</param>
 140        public void SendFromFactory<TMessage>(Func<TMessage> messageFactory)
 141        {
 3142            var message = messageFactory();
 3143            messenger.Send(message);
 3144        }
 145    }
 146}
 147