renamed Cache to Cached, and refactored many events to use Cached
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
33
src/Discord.Net.Core/Utils/Cached.cs
Normal file
33
src/Discord.Net.Core/Utils/Cached.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user