Added remaining gateway events, added IAudioChannel, added CacheModes

This commit is contained in:
RogueException
2016-10-04 07:32:26 -03:00
parent e038475ab4
commit 4678544fed
58 changed files with 1685 additions and 855 deletions

View File

@@ -0,0 +1,39 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace Discord
{
internal static class CollectionExtensions
{
public static IReadOnlyCollection<TValue> ToReadOnlyCollection<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> source)
=> new ConcurrentDictionaryWrapper<TValue>(source.Select(x => x.Value), () => source.Count);
public static IReadOnlyCollection<TValue> ToReadOnlyCollection<TValue, TSource>(this IEnumerable<TValue> query, IReadOnlyCollection<TSource> source)
=> new ConcurrentDictionaryWrapper<TValue>(query, () => source.Count);
public static IReadOnlyCollection<TValue> ToReadOnlyCollection<TValue>(this IEnumerable<TValue> query, Func<int> countFunc)
=> new ConcurrentDictionaryWrapper<TValue>(query, countFunc);
}
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
internal struct ConcurrentDictionaryWrapper<TValue> : IReadOnlyCollection<TValue>
{
private readonly IEnumerable<TValue> _query;
private readonly Func<int> _countFunc;
//It's okay that this count is affected by race conditions - we're wrapping a concurrent collection and that's to be expected
public int Count => _countFunc();
public ConcurrentDictionaryWrapper(IEnumerable<TValue> query, Func<int> countFunc)
{
_query = query;
_countFunc = countFunc;
}
private string DebuggerDisplay => $"Count = {Count}";
public IEnumerator<TValue> GetEnumerator() => _query.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => _query.GetEnumerator();
}
}

View File

@@ -0,0 +1,23 @@
using System;
using System.Threading.Tasks;
namespace Discord
{
internal static class TaskCompletionSourceExtensions
{
public static Task SetResultAsync<T>(this TaskCompletionSource<T> source, T result)
=> Task.Run(() => source.SetResult(result));
public static Task<bool> TrySetResultAsync<T>(this TaskCompletionSource<T> source, T result)
=> Task.Run(() => source.TrySetResult(result));
public static Task SetExceptionAsync<T>(this TaskCompletionSource<T> source, Exception ex)
=> Task.Run(() => source.SetException(ex));
public static Task<bool> TrySetExceptionAsync<T>(this TaskCompletionSource<T> source, Exception ex)
=> Task.Run(() => source.TrySetException(ex));
public static Task SetCanceledAsync<T>(this TaskCompletionSource<T> source)
=> Task.Run(() => source.SetCanceled());
public static Task<bool> TrySetCanceledAsync<T>(this TaskCompletionSource<T> source)
=> Task.Run(() => source.TrySetCanceled());
}
}