Simplified PR, renamed Cached to Cacheable.
This commit is contained in:
30
src/Discord.Net.Core/Utils/Cacheable.cs
Normal file
30
src/Discord.Net.Core/Utils/Cacheable.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Discord
|
||||
{
|
||||
public struct Cacheable<TEntity, TId>
|
||||
where TEntity : IEntity<TId>
|
||||
where TId : IEquatable<TId>
|
||||
{
|
||||
public bool HasValue => !EqualityComparer<TEntity>.Default.Equals(Value, default(TEntity));
|
||||
public ulong Id { get; }
|
||||
public TEntity Value { get; }
|
||||
private Func<Task<TEntity>> DownloadFunc { get; }
|
||||
|
||||
internal Cacheable(TEntity value, ulong id, Func<Task<TEntity>> downloadFunc)
|
||||
{
|
||||
Value = value;
|
||||
Id = id;
|
||||
DownloadFunc = downloadFunc;
|
||||
}
|
||||
|
||||
public async Task<TEntity> DownloadAsync()
|
||||
{
|
||||
return await DownloadFunc();
|
||||
}
|
||||
|
||||
public async Task<TEntity> GetOrDownloadAsync() => HasValue ? Value : await DownloadAsync();
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
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