<commit_before>﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EasyEventBus.Util;

namespace EasyEventBus
{
    /// <summary>
    /// Represents an event bus.
    /// </summary>
    public sealed class EventBus
    {
        private readonly IEnumerable<IPublicationStrategy> publicationStrategies;

        /// <summary>
        /// Creates a new instance of the EventBus class.
        /// </summary>
        /// <param name="publicationStrategy">Publication strategy to be used for publishing events.</param>
        public EventBus(IPublicationStrategy publicationStrategy)
        {
            Precondition.NotNull(publicationStrategy);

            publicationStrategies = new[] { publicationStrategy };
        }

        /// <summary>
        /// Creates a new instance of the EventBus class.
        /// </summary>
        /// <param name="publicationStrategies">List of publication strategies to be used for publishing events.</param>
        public EventBus(IEnumerable<IPublicationStrategy> publicationStrategies)
            : this(publicationStrategies.ToArray())
        {
        }

        private EventBus(IPublicationStrategy[] publicationStrategies)
        {
            Precondition.NotNull(publicationStrategies);
            Precondition.NotEmpty(publicationStrategies);

            this.publicationStrategies = publicationStrategies;
        }

        /// <summary>
        /// Publishes an event to registered handlers of the provided type.
        /// </summary>
        /// <typeparam name="T">Type of the event. Must be a reference type.</typeparam>
        /// <param name="eventData">Data of the event.</param>
        public void Publish<T>(T eventData) where T : class
        {
            Precondition.NotNull(eventData);

            foreach (var strategy in publicationStrategies)
            {
                strategy.Publish(eventData);
            }
        }

        /// <summary>
        /// Publishes an event to registered handlers of the provided type asynchronously.
        /// </summary>
        /// <typeparam name="T">Type of the event. Must be a reference type.</typeparam>
        /// <param name="eventData">Data of the event.</param>
        /// <returns>A <see cref="System.Threading.Tasks.Task"/>.</returns>
        public async Task PublishAsync<T>(T eventData) where T : class<commit_msg>Add error handling to publication methods of the EventBus class<commit_after>﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EasyEventBus.Util;

namespace EasyEventBus
{
    /// <summary>
    /// Represents an event bus.
    /// </summary>
    public sealed class EventBus
    {
        private readonly IEnumerable<IPublicationStrategy> publicationStrategies;

        /// <summary>
        /// Creates a new instance of the EventBus class.
        /// </summary>
        /// <param name="publicationStrategy">Publication strategy to be used for publishing events.</param>
        public EventBus(IPublicationStrategy publicationStrategy)
        {
            Precondition.NotNull(publicationStrategy);

            publicationStrategies = new[] { publicationStrategy };
        }

        /// <summary>
        /// Creates a new instance of the EventBus class.
        /// </summary>
        /// <param name="publicationStrategies">List of publication strategies to be used for publishing events.</param>
        public EventBus(IEnumerable<IPublicationStrategy> publicationStrategies)
            : this(publicationStrategies.ToArray())
        {
        }

        private EventBus(IPublicationStrategy[] publicationStrategies)
        {
            Precondition.NotNull(publicationStrategies);
            Precondition.NotEmpty(publicationStrategies);

            this.publicationStrategies = publicationStrategies;
        }

        /// <summary>
        /// Publishes an event to registered handlers of the provided type.
        /// </summary>
        /// <typeparam name="T">Type of the event. Must be a reference type.</typeparam>
        /// <param name="eventData">Data of the event.</param>
        public void Publish<T>(T eventData) where T : class
        {
            Precondition.NotNull(eventData);

            foreach (var strategy in publicationStrategies)
            {
                try
                {
                    strategy.Publish(eventData);
                }
                catch
                {
                    // Publication errors are silently ignored since strategies are supposed
                    // to handle them themselves
                }
            }
        }

        /// <summary>
        /// Publishes an event to registered handlers of the provided type asynchronously.
        /// </summary>
        /// <typeparam name="T">Type of the event. Must be a reference type.</typeparam>
        /// <param name="eventData">Data of the event.</param>
        /// <returns>A <see cref="System.Threading.Tasks.Task"/>.</returns>
        public async Task PublishAsync<T>(T eventData) where T : class
{'fraction_non_alphanumeric': 0.10086342229199372, 'fraction_numerical': 0.0, 'mean_word_length': 2.0797583081571, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4690974', 'n_tokens_mistral': 1239, 'n_tokens_neox': 1215, 'n_words': 347}