<commit_before>﻿using System.Collections.Generic;
using System.Linq;

namespace SemanticPipes
{
    internal sealed class RegistryMediator
    {
        private readonly HistoricalSemanticRegistryObserver _historicalSemanticRegistry =
            new HistoricalSemanticRegistryObserver();

        private readonly List<ISemanticRegistryObserver> _observers = new List<ISemanticRegistryObserver>();

        public RegistryMediator(IEnumerable<ISemanticRegistryObserver> observers)
        {
            _observers = observers.ToList();
            _observers.Add(_historicalSemanticRegistry);
        }

        public void AppendObserver(ISemanticRegistryObserver observer)
        {
            _observers.Add(observer);

            IEnumerable<PipeOutputPackage> otherPacakgesToInstall =
                _historicalSemanticRegistry.NotifyObserverOfHistoricalRegistrations(observer);
            DoPackageInstallations(otherPacakgesToInstall);
        }

        public void AppendPackage(PipeOutputPackage package)
        {
            IEnumerable<PipeOutputPackage> packagesToInsert = new[] {package};
            DoPackageInstallations(packagesToInsert);
        }

        private void DoPackageInstallations(IEnumerable<PipeOutputPackage> packagesToInstall)
        {
            IEnumerable<IEnumerable<PipeOutputPackage>> listOfAdditionalPackages =
                from installingPackages in packagesToInstall
                from observer in _observers
                let morePackages = observer.PipePackageInstalled(installingPackages)
                where morePackages != null
                select morePackages;

            foreach (var additionalPackagesToInstall in listOfAdditionalPackages)
            {
                DoPackageInstallations(additionalPackagesToInstall);
            }
        }

        private sealed class HistoricalSemanticRegistryObserver : ISemanticRegistryObserver
        {
            private readonly List<PipeOutputPackage> _pipePackages = new List<PipeOutputPackage>();

            public IEnumerable<PipeOutputPackage> PipePackageInstalled(PipeOutputPackage package)
            {
                _pipePackages.Add(package);
                return null;
            }

            public IEnumerable<PipeOutputPackage> NotifyObserverOfHistoricalRegistrations(
                ISemanticRegistryObserver observer)
            {
                return
                    (from historicalPipePackage in _pipePackages
                        let morePackages = observer.PipePackageInstalled(historicalPipePackage)
                        where morePackages != null
                        select morePackages).SelectMany(x => x.ToArray());
            }
        }
    }
}<commit_msg>kill switch implemented<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace SemanticPipes
{
    internal sealed class RegistryMediator
    {
        private readonly HistoricalSemanticRegistryObserver _historicalSemanticRegistry =
            new HistoricalSemanticRegistryObserver();

        private readonly KillSwitchObserver _killSwitchObserver = new KillSwitchObserver();

        private readonly List<ISemanticRegistryObserver> _observers = new List<ISemanticRegistryObserver>();

        public RegistryMediator(IEnumerable<ISemanticRegistryObserver> observers)
        {
            _observers.Add(_killSwitchObserver);
            _observers.AddRange(observers);
            _observers.Add(_historicalSemanticRegistry);
        }

        public void AppendObserver(ISemanticRegistryObserver observer)
        {
            _observers.Add(observer);

            try
            {
                IEnumerable<PipeOutputPackage> otherPacakgesToInstall =
                    _historicalSemanticRegistry.NotifyObserverOfHistoricalRegistrations(observer);
                DoPackageInstallations(otherPacakgesToInstall);
            }
            catch
            {
                _killSwitchObserver.KillSignal();
                throw;
            }
        }

        public void AppendPackage(PipeOutputPackage package)
        {
            try
            {
                IEnumerable<PipeOutputPackage> packagesToInsert = new[] {package};
                DoPackageInstallations(packagesToInsert);
            }
            catch (Exception e)
            {
                _killSwitchObserver.KillSignal();
                throw;
            }
        }

        private void DoPackageInstallations(IEnumerable<PipeOutputPackage> packagesToInstall)
        {
            IEnumerable<IEnumerable<PipeOutputPackage>> listOfAdditionalPackages =
                from installingPackages in packagesToInstall
                from observer in _observers
                let morePackages = observer.PipePackageInstalled(installingPackages)
                where morePackages != null
                select morePackages;

            foreach (var additionalPackagesToInstall in listOfAdditionalPackages)
            {
                DoPackageInstallations(additionalPackagesToInstall);
            }
        }

        private sealed class HistoricalSemanticRegistryObserver : ISemanticRegistryObserver
        {
            private readonly List<PipeOutputPackage> _pipePackages = new List<PipeOutputPackage>();

            public IEnumerable<PipeOutputPackage> PipePackageInstalled(PipeOutputPackage package)
            {
                _pipePackages.Add(package);
                return null;
            }

            public IEnumerable<PipeOutputPackage> NotifyObserverOfHistoricalRegistrations(
                ISemanticRegistryObserver observer)
            {
                return
                    (from historicalPipePackage in _pipePackages
                        let morePackages = observer.PipePackageInstalled(historicalPipePackage)
                        where morePackages != null
                        select morePackages).SelectMany(x => x.ToArray());
            }
        }

        private sealed class KillSwitchObserver : ISemanticRegistryObserver
        {
            private bool _killed;

            public IEnumerable<PipeOutputPackage> PipePackageInstalled(PipeOutputPackage package)
            {
                if (_killed)
                {
                    throw new InvalidProgramException();
                }

                return null;
            }

            public void KillSignal()
            {
                _killed = true;
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.05197601107181301, 'fraction_numerical': 0.0, 'mean_word_length': 2.0293432696786216, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 30, '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': '3724726', 'n_tokens_mistral': 1519, 'n_tokens_neox': 1425, 'n_words': 266}