mirror of
https://github.com/osukey/osukey.git
synced 2025-07-02 16:59:53 +09:00
Use a task chain and fix potential misordering of events
This commit is contained in:
30
osu.Game/Utils/TaskChain.cs
Normal file
30
osu.Game/Utils/TaskChain.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user