renamed Cache to Cached, and refactored many events to use Cached

This commit is contained in:
Sindre G. Langhus
2016-12-01 11:35:45 +01:00
parent 05fcd5f076
commit 959d49a26f
4 changed files with 85 additions and 106 deletions

View File

@@ -1,23 +0,0 @@
using System;
using System.Reflection;
using System.Threading.Tasks;
using TId = Discord.IEntity<ulong>;
namespace Discord
{
public abstract class Cache<T> where T : IEntity<ulong>
{
public bool IsCached => Value != null;
public TId Id { get; }
public T Value { get; }
protected Cache(TId id)
{
Id = id;
}
public async Task<T> DownloadAsync() => typeof(T).GetRuntimeMethod("DownloadAsync",{ulong id});
public async Task<T> GetOrDownloadAsync() => IsCached ? Value : await DownloadAsync();
}
}

View File

@@ -0,0 +1,33 @@
using Discord.WebSocket;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Discord
{
public struct Cached<T> where T : IMessage
{
public bool IsCached => !EqualityComparer<T>.Default.Equals(Value, default(T));
public bool IsDownloadable { get; }
public ulong Id { get; }
public T Value { get; }
public ISocketMessageChannel Channel { get; }
public Cached(ulong id, T value, ISocketMessageChannel channel, bool isDownloadable = true)
{
Id = id;
Value = value;
Channel = channel;
IsDownloadable = isDownloadable;
}
public async Task<T> DownloadAsync()
{
if (IsDownloadable)
return (T) await Channel.GetMessageAsync(Id);
throw new InvalidOperationException("This message cannot be downloaded.");
}
public async Task<T> GetOrDownloadAsync() => IsCached ? Value : await DownloadAsync();
}
}