Use a task chain and fix potential misordering of events

This commit is contained in:
smoogipoo
2021-01-25 20:41:51 +09:00
parent 76e1f6e57b
commit 964976f604
4 changed files with 70 additions and 96 deletions

View File

@ -0,0 +1,30 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
#nullable enable
using System;
using System.Threading.Tasks;
namespace osu.Game.Utils
{
/// <summary>
/// A chain of <see cref="Task"/>s that run sequentially.
/// </summary>
public class TaskChain
{
private readonly object currentTaskLock = new object();
private Task? currentTask;
public Task Add(Func<Task> taskFunc)
{
lock (currentTaskLock)
{
currentTask = currentTask == null
? taskFunc()
: currentTask.ContinueWith(_ => taskFunc()).Unwrap();
return currentTask;
}
}
}
}