// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using System.Threading; using System.Threading.Tasks; using osu.Game.Extensions; namespace osu.Game.Utils { /// /// A chain of s that run sequentially. /// public class TaskChain { private readonly object taskLock = new object(); private Task lastTaskInChain = Task.CompletedTask; /// /// Adds a new task to the end of this . /// /// The action to be executed. /// The for this task. Does not affect further tasks in the chain. /// The awaitable . public Task Add(Action action, CancellationToken cancellationToken = default) { lock (taskLock) return lastTaskInChain = lastTaskInChain.ContinueWithSequential(action, cancellationToken); } /// /// Adds a new task to the end of this . /// /// The task to be executed. /// The for this task. Does not affect further tasks in the chain. /// The awaitable . public Task Add(Func task, CancellationToken cancellationToken = default) { lock (taskLock) return lastTaskInChain = lastTaskInChain.ContinueWithSequential(task, cancellationToken); } } }