2020-12-23 08:10:02 +00:00
|
|
|
// 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.
|
|
|
|
|
2020-12-31 10:29:02 +00:00
|
|
|
using System;
|
2021-01-27 10:50:16 +00:00
|
|
|
using System.Threading;
|
2020-12-23 08:10:02 +00:00
|
|
|
using System.Threading.Tasks;
|
2021-05-14 21:42:56 +00:00
|
|
|
using osu.Framework.Extensions.ObjectExtensions;
|
2020-12-23 08:10:02 +00:00
|
|
|
|
|
|
|
namespace osu.Game.Extensions
|
|
|
|
{
|
|
|
|
public static class TaskExtensions
|
|
|
|
{
|
2021-01-27 11:01:21 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Add a continuation to be performed only after the attached task has completed.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="task">The previous task to be awaited on.</param>
|
|
|
|
/// <param name="action">The action to run.</param>
|
|
|
|
/// <param name="cancellationToken">An optional cancellation token. Will only cancel the provided action, not the sequence.</param>
|
|
|
|
/// <returns>A task representing the provided action.</returns>
|
|
|
|
public static Task ContinueWithSequential(this Task task, Action action, CancellationToken cancellationToken = default) =>
|
|
|
|
task.ContinueWithSequential(() => Task.Run(action, cancellationToken), cancellationToken);
|
2021-01-27 10:50:16 +00:00
|
|
|
|
2021-01-27 11:01:21 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Add a continuation to be performed only after the attached task has completed.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="task">The previous task to be awaited on.</param>
|
|
|
|
/// <param name="continuationFunction">The continuation to run. Generally should be an async function.</param>
|
|
|
|
/// <param name="cancellationToken">An optional cancellation token. Will only cancel the provided action, not the sequence.</param>
|
|
|
|
/// <returns>A task representing the provided action.</returns>
|
2021-01-27 10:50:16 +00:00
|
|
|
public static Task ContinueWithSequential(this Task task, Func<Task> continuationFunction, CancellationToken cancellationToken = default)
|
|
|
|
{
|
|
|
|
var tcs = new TaskCompletionSource<bool>();
|
|
|
|
|
2022-06-24 12:25:23 +00:00
|
|
|
task.ContinueWith(_ =>
|
2021-01-27 10:50:16 +00:00
|
|
|
{
|
2021-01-27 11:01:21 +00:00
|
|
|
// the previous task has finished execution or been cancelled, so we can run the provided continuation.
|
|
|
|
|
2021-01-27 10:50:16 +00:00
|
|
|
if (cancellationToken.IsCancellationRequested)
|
|
|
|
{
|
2022-12-16 09:16:26 +00:00
|
|
|
tcs.SetCanceled(cancellationToken);
|
2021-01-27 10:50:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-27 11:01:21 +00:00
|
|
|
continuationFunction().ContinueWith(continuationTask =>
|
2021-01-27 10:50:16 +00:00
|
|
|
{
|
2021-01-27 11:01:21 +00:00
|
|
|
if (cancellationToken.IsCancellationRequested || continuationTask.IsCanceled)
|
2021-01-27 10:50:16 +00:00
|
|
|
{
|
|
|
|
tcs.TrySetCanceled();
|
|
|
|
}
|
2021-01-27 11:01:21 +00:00
|
|
|
else if (continuationTask.IsFaulted)
|
2021-01-27 10:50:16 +00:00
|
|
|
{
|
2021-05-14 21:42:56 +00:00
|
|
|
tcs.TrySetException(continuationTask.Exception.AsNonNull());
|
2021-01-27 10:50:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tcs.TrySetResult(true);
|
|
|
|
}
|
|
|
|
}, cancellationToken: default);
|
|
|
|
}
|
|
|
|
}, cancellationToken: default);
|
|
|
|
|
2021-01-27 11:01:21 +00:00
|
|
|
// 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.
|
2021-01-27 10:50:16 +00:00
|
|
|
return tcs.Task;
|
|
|
|
}
|
2020-12-23 08:10:02 +00:00
|
|
|
}
|
|
|
|
}
|