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