Skip to content

Commit dfdd01e

Browse files
committed
chore: refactor and re-added cancel token
1 parent 5e3fab1 commit dfdd01e

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

Runtime/AvatarCreator/AvatarTemplateFetcher.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ namespace ReadyPlayerMe.AvatarCreator
1010
/// </summary>
1111
public class AvatarTemplateFetcher
1212
{
13+
private readonly CancellationToken ctx;
1314
private readonly AvatarAPIRequests avatarAPIRequests;
1415

1516
public AvatarTemplateFetcher(CancellationToken ctx = default)
1617
{
18+
this.ctx = ctx;
1719
avatarAPIRequests = new AvatarAPIRequests(ctx);
1820
}
1921

@@ -34,21 +36,26 @@ public async Task<List<AvatarTemplateData>> GetTemplates()
3436
public async Task<List<AvatarTemplateData>> GetTemplatesWithRenders()
3537
{
3638
var templates = await avatarAPIRequests.GetAvatarTemplates();
37-
await FetchTemplateRenders(templates);
38-
return templates;
39+
return await FetchTemplateRenders(templates);
3940
}
4041

4142
/// <summary>
4243
/// Fetches the renders for all the templates provided.
4344
/// </summary>
44-
public async Task FetchTemplateRenders(List<AvatarTemplateData> templates)
45+
public async Task<List<AvatarTemplateData>> FetchTemplateRenders(List<AvatarTemplateData> templates)
4546
{
4647
var tasks = templates.Select(async templateData =>
4748
{
4849
templateData.Texture = await avatarAPIRequests.GetAvatarTemplateImage(templateData.ImageUrl);
49-
});
50+
}).ToList();
51+
52+
while (!tasks.All(x => x.IsCompleted) &&
53+
!ctx.IsCancellationRequested)
54+
{
55+
await Task.Yield();
56+
}
5057

51-
await Task.WhenAll(tasks);
58+
return templates;
5259
}
5360
}
5461
}

0 commit comments

Comments
 (0)