<commit_before>﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Windows.ApplicationModel.Core;
using Windows.Devices.Enumeration;
using Windows.Devices.Usb;
using Windows.Foundation;
using Windows.UI.Core;
using Treehopper.Uwp;

namespace Treehopper
{
    public sealed class ConnectionService : IConnectionService, INotifyPropertyChanged
    {
        private readonly SemaphoreSlim boardAddedSignal = new SemaphoreSlim(0, 1);

        private DeviceWatcher deviceWatcher;
        private TypedEventHandler<DeviceWatcher, DeviceInformation> handlerAdded;
        private TypedEventHandler<DeviceWatcher, object> handlerEnumCompleted;
        private TypedEventHandler<DeviceWatcher, DeviceInformationUpdate> handlerRemoved;
        private TypedEventHandler<DeviceWatcher, object> handlerStopped;
        private TypedEventHandler<DeviceWatcher, DeviceInformationUpdate> handlerUpdated;

        public ConnectionService()
        {
            Boards = new ObservableCollection<TreehopperUsb>();

            StartWatcher();
        }

        public static IConnectionService Instance { get; } = new ConnectionService();

        public ObservableCollection<TreehopperUsb> Boards { get; }

        public event PropertyChangedEventHandler PropertyChanged;

        public async Task<TreehopperUsb> GetFirstDeviceAsync()
        {
            await boardAddedSignal.WaitAsync();
            return Boards[0];
        }

        public void Dispose()
        {
            throw new NotImplementedException();
        }

        public void AddBoard()
        {
        }

        private void StartWatcher()
        {
            //deviceWatcher = DeviceInformation.CreateWatcher(UsbDevice.GetDeviceSelector(0x04d8, 0xf426));
            deviceWatcher = DeviceInformation.CreateWatcher(
                UsbDevice.GetDeviceSelector(TreehopperUsb.Settings.Vid, TreehopperUsb.Settings.Pid));
            // Hook up handlers for the watcher events before starting the watcher
<commit_msg>Don't tank when we dispose a ConnectionService<commit_after>﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Windows.ApplicationModel.Core;
using Windows.Devices.Enumeration;
using Windows.Devices.Usb;
using Windows.Foundation;
using Windows.UI.Core;
using Treehopper.Uwp;

namespace Treehopper
{
    public sealed class ConnectionService : IConnectionService, INotifyPropertyChanged
    {
        private readonly SemaphoreSlim boardAddedSignal = new SemaphoreSlim(0, 1);

        private DeviceWatcher deviceWatcher;
        private TypedEventHandler<DeviceWatcher, DeviceInformation> handlerAdded;
        private TypedEventHandler<DeviceWatcher, object> handlerEnumCompleted;
        private TypedEventHandler<DeviceWatcher, DeviceInformationUpdate> handlerRemoved;
        private TypedEventHandler<DeviceWatcher, object> handlerStopped;
        private TypedEventHandler<DeviceWatcher, DeviceInformationUpdate> handlerUpdated;

        public ConnectionService()
        {
            Boards = new ObservableCollection<TreehopperUsb>();

            StartWatcher();
        }

        public static IConnectionService Instance { get; } = new ConnectionService();

        public ObservableCollection<TreehopperUsb> Boards { get; }

        public event PropertyChangedEventHandler PropertyChanged;

        public async Task<TreehopperUsb> GetFirstDeviceAsync()
        {
            await boardAddedSignal.WaitAsync();
            return Boards[0];
        }

        public void Dispose()
        {
            // TODO: Clean stuff up
        }

        public void AddBoard()
        {
        }

        private void StartWatcher()
        {
            //deviceWatcher = DeviceInformation.CreateWatcher(UsbDevice.GetDeviceSelector(0x04d8, 0xf426));
            deviceWatcher = DeviceInformation.CreateWatcher(
                UsbDevice.GetDeviceSelector(TreehopperUsb.Settings.Vid, TreehopperUsb.Settings.Pid));
            // Hook up handlers for the watcher events before starting the watcher

{'fraction_non_alphanumeric': 0.06771321462043112, 'fraction_numerical': 0.005154639175257732, 'mean_word_length': 3.230921704658077, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '7238405', 'n_tokens_mistral': 1070, 'n_tokens_neox': 1061, 'n_words': 253}