Skip to content

Commit

Permalink
fix: don't use lazy task
Browse files Browse the repository at this point in the history
  • Loading branch information
anna-is-cute committed Mar 24, 2024
1 parent ed13a58 commit ecd881d
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions PackageState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,34 @@ internal class InstalledPackage : IDisposable {
internal string Author { get; }
internal string CoverImagePath { get; }

internal Lazy<Task<IDalamudTextureWrap?>> CoverImage { get; }
private bool _loading;
private string? _coverImageHash;
internal IDalamudTextureWrap? CoverImage {
get {
if (this._loading) {
return null;
}

if (
this._coverImageHash is { } hash
&& Plugin.Instance.CoverImages.TryGet(hash, out var img)
) {
return img;
}

this._loading = true;
this._coverImageHash = null;
Task.Run(async () => {
try {
await this.AttemptLoad();
} finally {
this._loading = false;
}
});

return null;
}
}

internal List<HeliosphereMeta> InternalVariants { get; }
internal IReadOnlyList<HeliosphereMeta> Variants => this.InternalVariants.ToImmutableList();
Expand All @@ -364,8 +391,6 @@ internal InstalledPackage(Guid id, string name, string author, List<HeliosphereM
this.Author = author;
this.CoverImagePath = coverImagePath;
this.InternalVariants = variants;

this.CoverImage = new(this.AttemptLoad);
}

public void Dispose() {
Expand All @@ -383,9 +408,7 @@ public override bool Equals(object? obj) {
private async Task<IDalamudTextureWrap?> AttemptLoad() {
using var guard = await SemaphoreGuard.WaitAsync(Plugin.ImageLoadSemaphore);

return await Plugin.Resilience.ExecuteAsync(async _ => {
return await this.AttemptLoadSingle();
});
return await Plugin.Resilience.ExecuteAsync(async _ => await this.AttemptLoadSingle());
}

private async Task<IDalamudTextureWrap?> AttemptLoadSingle() {
Expand All @@ -407,6 +430,7 @@ public override bool Equals(object? obj) {
var wrap = await ImageHelper.LoadImageAsync(Plugin.Instance.Interface.UiBuilder, bytes)
?? throw new Exception("image was null");
Plugin.Instance.CoverImages.AddOrUpdate(hash, wrap);
this._coverImageHash = hash;

return wrap;
}
Expand Down

0 comments on commit ecd881d

Please sign in to comment.