// 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.Framework.Extensions.ObjectExtensions; namespace osu.Game.Extensions { public static class TaskExtensions { /// /// Add a continuation to be performed only after the attached task has completed. /// /// The previous task to be awaited on. /// The action to run. /// An optional cancellation token. Will only cancel the provided action, not the sequence. /// A task representing the provided action. public static Task ContinueWithSequential(this Task task, Action action, CancellationToken cancellationToken = default) => task.ContinueWithSequential(() => Task.Run(action, cancellationToken), cancellationToken); /// /// Add a continuation to be performed only after the attached task has completed. /// /// The previous task to be awaited on. /// The continuation to run. Generally should be an async function. /// An optional cancellation token. Will only cancel the provided action, not the sequence. /// A task representing the provided action. public static Task ContinueWithSequential(this Task task, Func continuationFunction, CancellationToken cancellationToken = default) { var tcs = new TaskCompletionSource(); task.ContinueWith(t => { // the previous task has finished execution or been cancelled, so we can run the provided continuation. if (cancellationToken.IsCancellationRequested) { tcs.SetCanceled(); } else { continuationFunction().ContinueWith(continuationTask => { if (cancellationToken.IsCancellationRequested || continuationTask.IsCanceled) { tcs.TrySetCanceled(); } else if (continuationTask.IsFaulted) { tcs.TrySetException(continuationTask.Exception.AsNonNull()); } else { tcs.TrySetResult(true); } }, cancellationToken: default); } }, cancellationToken: default); // importantly, we are not returning the continuation itself but rather a task which represents its status in sequential execution order. // this will not be cancelled or completed until the previous task has also. return tcs.Task; } } }