using System; using System.Threading.Tasks; namespace Discord { /// /// Contains an entity that may be cached. /// /// The type of entity that is cached /// The type of this entity's ID public struct Cacheable where TEntity : IEntity where TId : IEquatable { /// /// Is this entity cached? /// public bool HasValue { get; } /// /// The ID of this entity. /// public TId Id { get; } /// /// The entity, if it could be pulled from cache. /// /// /// This value is not guaranteed to be set; in cases where the entity cannot be pulled from cache, it is null. /// public TEntity Value { get; } private Func> DownloadFunc { get; } internal Cacheable(TEntity value, TId id, bool hasValue , Func> downloadFunc) { Value = value; Id = id; HasValue = hasValue; DownloadFunc = downloadFunc; } /// /// Downloads this entity to cache. /// /// An awaitable Task containing the downloaded entity. /// Thrown when used from a user account. /// Thrown when the message is deleted. public async Task DownloadAsync() { return await DownloadFunc(); } /// /// Returns the cached entity if it exists; otherwise downloads it. /// /// An awaitable Task containing a cached or downloaded entity. /// Thrown when used from a user account. /// Thrown when the message is deleted and is not in cache. public async Task GetOrDownloadAsync() => HasValue ? Value : await DownloadAsync(); } }