using System; using System.Threading.Tasks; namespace Discord { /// /// Represents a cached entity. /// /// The type of entity that is cached. /// The type of this entity's ID. public struct Cacheable where TEntity : IEntity where TId : IEquatable { /// /// Gets whether this entity is cached. /// public bool HasValue { get; } /// /// Gets the ID of this entity. /// public TId Id { get; } /// /// Gets 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. /// /// Thrown when used from a user account. /// Thrown when the message is deleted. /// /// A task that represents the asynchronous download operation. The task result contains the downloaded /// entity. /// public async Task DownloadAsync() { return await DownloadFunc().ConfigureAwait(false); } /// /// Returns the cached entity if it exists; otherwise downloads it. /// /// Thrown when used from a user account. /// Thrown when the message is deleted and is not in cache. /// /// A task that represents the asynchronous operation that attempts to get the message via cache or to /// download the message. The task result contains the downloaded entity. /// public async Task GetOrDownloadAsync() => HasValue ? Value : await DownloadAsync().ConfigureAwait(false); } }