Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/Cysharp/ValueTaskSupplement

Append supplemental methods(WhenAny, WhenAll, Lazy) to ValueTask.
https://github.com/Cysharp/ValueTaskSupplement

Last synced: 3 months ago
JSON representation

Append supplemental methods(WhenAny, WhenAll, Lazy) to ValueTask.

Awesome Lists containing this project

README

        

[![GitHub Actions](https://github.com/Cysharp/ValueTaskSupplement/workflows/Build-Debug/badge.svg)](https://github.com/Cysharp/ValueTaskSupplement/actions) [![Releases](https://img.shields.io/github/release/Cysharp/ValueTaskSupplement.svg)](https://github.com/Cysharp/ValueTaskSupplement/releases)

ValueTaskSupplement
===

`ValueTask` is a new standard of define async methods especially after being introduced `IValueTaskSource`. But it lacks utility like WhenAny, etc. ValueTaskSupplement appends supplemental methods(`WhenAny`, `WhenAll`, `Lazy`) to ValueTask and it is implemented by `IValueTaskSource` so fast and less allocation.

> PM> Install-Package [ValueTaskSupplement](https://www.nuget.org/packages/ValueTaskSupplement)

## Table of Contents

- [How to Use](#how-to-use)
- [WhenAll](#whenall)
- [WhenAny](#whenany)
- [Lazy](#lazy)
- [Factory](#factory)
- [License](#license)

How to Use
---
```csharp
using ValueTaskSupplement; // namespace

async ValueTask Demo()
{
// `ValueTaskEx` is the only types from provided this library

// like this individual types
ValueTask task1 = LoadAsyncA();
ValueTask task2 = LoadAsyncB();
ValueTask task3 = LoadAsyncC();

// await ValueTasks(has different type each other) with tuple
var (a, b, c) = await ValueTaskEx.WhenAll(task1, task2, task3);

// WhenAny with int winIndex
var (winIndex, a, b, c) = await ValueTaskEx.WhenAny(task1, task2, task2);

// like Timeout
var (hasLeftResult, value) = await ValueTaskEx.WhenAny(task1, Task.Delay(TimeSpan.FromSeconds(1)));
if (!hasLeftResult) throw new TimeoutException();

// Lazy(called factory once and delayed)
AsyncLazy asyncLazy = ValueTaskEx.Lazy(async () => 9999);
}
```

WhenAll
---

```csharp
// Same type and return array(same as Task.WhenAll).
public static ValueTask WhenAll(IEnumerable> tasks);

// T0, T1, to...
public static ValueTask<(T0, T1)> WhenAll(ValueTask task0, ValueTask task1);
...
// T0 ~ T15
public static ValueTask<(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15)> WhenAll(ValueTask task0, ValueTask task1, ValueTask task2, ValueTask task3, ValueTask task4, ValueTask task5, ValueTask task6, ValueTask task7, ValueTask task8, ValueTask task9, ValueTask task10, ValueTask task11, ValueTask task12, ValueTask task13, ValueTask task14, ValueTask task15);
```

`IEnumerable>` and `(ValueTask, ValueTask, ...)` can await directly.

```csharp
using ValueTaskSupplement;

// same as ValueTaskEx.WhenAll(new []{ }), ValueTaskEx.WhenAll(A, B, C)
var result = await new[] { LoadAsyncA(), LoadAsyncB(), LoadAsyncC() };
var (x, y, z) = await (LoadAsyncA(), LoadAsyncB(), LoadAsyncC());
```

WhenAny
---

```csharp
// binary api is useful to await with Delay(like for check Timeout).
public static ValueTask<(bool hasResultLeft, T result)> WhenAny(ValueTask left, Task right);
public static ValueTask<(bool hasResultLeft, T result)> WhenAny(ValueTask left, ValueTask right);

// receive sequence is like Task.WhenAny but returns `int winArgumentIndex`.
public static ValueTask<(int winArgumentIndex, T result)> WhenAny(IEnumerable> tasks);

// Return result of tuple methods is guaranteed only winArgumentIndex value
public static ValueTask<(int winArgumentIndex, T0 result0, T1 result1)> WhenAny(ValueTask task0, ValueTask task1);
...
// T0 ~ T15
public static ValueTask<(int winArgumentIndex, T0 result0, T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7, T8 result8, T9 result9, T10 result10, T11 result11, T12 result12, T13 result13, T14 result14, T15 result15)> WhenAny(ValueTask task0, ValueTask task1, ValueTask task2, ValueTask task3, ValueTask task4, ValueTask task5, ValueTask task6, ValueTask task7, ValueTask task8, ValueTask task9, ValueTask task10, ValueTask task11, ValueTask task12, ValueTask task13, ValueTask task14, ValueTask task15);
```

Lazy
---

```csharp
// AsyncLazy is similar to Lazy, it can store in field
// it await directly or can convert to ValueTask easily to use WhenAll.
public static AsyncLazy Lazy(Func> factory);

public class AsyncLazy
{
public ValueTask AsValueTask();
public ValueTaskAwaiter GetAwaiter();
public static implicit operator ValueTask(AsyncLazy source);
}
```

Factory
---

```csharp
public static ValueTask FromResult(T result);
public static ValueTask FromTask(Task result);
public static ValueTask FromTask(Task result);
public static ValueTask AsValueTask(this Task result);
public static ValueTask AsValueTask(this Task result);
```

License
---
This library is under the MIT License.