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

namespace Wp7Faye {
    public abstract class MessageHandler : IDisposable {

        #region Fields

        internal string connectionUri;
        public readonly List<string> subscriptions = new List<string>();

        #endregion

        #region Events

        public event Action Connected;
        public event Action ConnectTimeout;
        public event Action Disconnected;

        public event ErrorEventHandler Error;

        public event Action<JObject> HandshakeResponse;
        public event Action<JObject> PublishResponse;
        public event Action<JObject> SubscribeResponse;
        public event Action<JObject> UnsubscribeResponse;
        public event Action<JObject> ConnectResponse;

        public event Action<JObject> MessageRecieved;

        #endregion

        #region Abstracts

        public abstract bool IsConnected { get; }

        protected abstract void Connect(string uri);
        public abstract void Handshake();
        public abstract void ConnectRequest();
        public abstract void Disconnect();
        public abstract void Subscribe(string channel);
        public abstract void Unsubscribe(string channel);
        public abstract void Publish(string channel, JToken data);

        public abstract void Dispose();

        #endregion

        #region Methods

        public void Connect() {
            Connect(connectionUri);
        }

        public bool IsSubscribed(string channel) {
            return subscriptions.Any(subscription => SubscriptionMatches(subscription, channel));
        }

        private static bool SubscriptionMatches(string match, string sub) {
            var matchParts = match.Split(new[] { '/' });
            var subParts = sub.Split(new[] { '/' });
            if (subParts.Length < matchParts.Length) {
                return false;
            }
            for (var i = 0; i < matchParts.Length; i++) {
                if (matchParts[i] == "**") {
                    return true;
                }
                if ((matchParts[i] != "*") && (matchParts[i] != subParts[i])) {
                    return false;
                }
            }<commit_msg>Curb dem race conditions<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Linq;

namespace Wp7Faye {
    public abstract class MessageHandler : IDisposable {

        #region Fields

        internal string connectionUri;
        public readonly List<string> subscriptions = new List<string>();

        #endregion

        #region Events

        public event Action Connected;
        public event Action ConnectTimeout;
        public event Action Disconnected;

        public event ErrorEventHandler Error;

        public event Action<JObject> HandshakeResponse;
        public event Action<JObject> PublishResponse;
        public event Action<JObject> SubscribeResponse;
        public event Action<JObject> UnsubscribeResponse;
        public event Action<JObject> ConnectResponse;

        public event Action<JObject> MessageRecieved;

        #endregion

        #region Abstracts

        public abstract bool IsConnected { get; }

        protected abstract void Connect(string uri);
        public abstract void Handshake();
        public abstract void ConnectRequest();
        public abstract void Disconnect();
        public abstract void Subscribe(string channel);
        public abstract void Unsubscribe(string channel);
        public abstract void Publish(string channel, JToken data);

        public abstract void Dispose();

        #endregion

        #region Methods

        public void Connect() {
            Connect(connectionUri);
        }

        public bool IsSubscribed(string channel) {
            return subscriptions.ToList().Any(subscription => SubscriptionMatches(subscription, channel));
        }

        private static bool SubscriptionMatches(string match, string sub) {
            var matchParts = match.Split(new[] { '/' });
            var subParts = sub.Split(new[] { '/' });
            if (subParts.Length < matchParts.Length) {
                return false;
            }
            for (var i = 0; i < matchParts.Length; i++) {
                if (matchParts[i] == "**") {
                    return true;
                }
                if ((matchParts[i] != "*") && (matchParts[i] != subParts[i])) {
                    return false;
                }
            }
{'fraction_non_alphanumeric': 0.08245382585751979, 'fraction_numerical': 0.0008795074758135445, 'mean_word_length': 2.1612230715774845, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 21, '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': '5253538', 'n_tokens_mistral': 1090, 'n_tokens_neox': 1050, 'n_words': 333}