Simplified PR, renamed Cached to Cacheable.

This commit is contained in:
Sindre G. Langhus
2016-12-16 23:50:27 +01:00
parent 8435186d79
commit 705d71875c
4 changed files with 67 additions and 60 deletions

View 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();
}
}

View File

@@ -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();
}
}