mirror of
https://github.com/Cysharp/UniTask.git
synced 2026-05-15 11:30:09 +00:00
Compare commits
93 Commits
2.0.7-rc4
...
fix-il2cpp
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
37cd00d347 | ||
|
|
859c4d706f | ||
|
|
7289fe6e25 | ||
|
|
0c33977f5a | ||
|
|
4d4466e801 | ||
|
|
79330d7cdb | ||
|
|
680ce1098b | ||
|
|
2337d705ec | ||
|
|
d2880a818f | ||
|
|
86ea128bf4 | ||
|
|
a66f378622 | ||
|
|
265f88584b | ||
|
|
686394c861 | ||
|
|
8bad158ab4 | ||
|
|
5e59e7ec86 | ||
|
|
8bb0a48720 | ||
|
|
b4468b4eba | ||
|
|
0725bd1b30 | ||
|
|
ebd80243e0 | ||
|
|
f1ac469058 | ||
|
|
2e0b603d25 | ||
|
|
1d90a40f66 | ||
|
|
1bec3f507e | ||
|
|
1d5ecbb3ab | ||
|
|
e1a4aeb9da | ||
|
|
43f1bb4d85 | ||
|
|
8b7a0e9b15 | ||
|
|
da329e19d1 | ||
|
|
b8260d4e91 | ||
|
|
345e32aaf0 | ||
|
|
d7bef8c5b5 | ||
|
|
b2f82df4d3 | ||
|
|
83596b3d1f | ||
|
|
0022598a1c | ||
|
|
f4294d3752 | ||
|
|
a1444c0b39 | ||
|
|
e1d5359d73 | ||
|
|
239bf749b6 | ||
|
|
2bf3b1e172 | ||
|
|
d225de201f | ||
|
|
c3b8a3852d | ||
|
|
c31dab888e | ||
|
|
d4cf59bd2f | ||
|
|
d5edc3acd3 | ||
|
|
130286e8c2 | ||
|
|
a9baa52309 | ||
|
|
3001996298 | ||
|
|
bfcd18aabb | ||
|
|
96aa299e7e | ||
|
|
24faa34418 | ||
|
|
21bf08a6b3 | ||
|
|
d5db96b913 | ||
|
|
a8455af16d | ||
|
|
2290b14532 | ||
|
|
90c5a6311b | ||
|
|
6e0ad3623b | ||
|
|
005e02a1fa | ||
|
|
10fb8060fa | ||
|
|
35b933730b | ||
|
|
7ab9467069 | ||
|
|
598312ba61 | ||
|
|
985aa5c43a | ||
|
|
10eff95a42 | ||
|
|
d27d6d5d9d | ||
|
|
b8c109848e | ||
|
|
8b7f832c0f | ||
|
|
7cce0f48e5 | ||
|
|
8a56838111 | ||
|
|
ff15e00003 | ||
|
|
f60d2c51fb | ||
|
|
6dfb969015 | ||
|
|
da7e9fc4b3 | ||
|
|
70385c4115 | ||
|
|
51ba740413 | ||
|
|
f3e3ba8864 | ||
|
|
07cf65c1ec | ||
|
|
eca5b1c096 | ||
|
|
c74ce14ad1 | ||
|
|
f59c56506f | ||
|
|
896eef1ee4 | ||
|
|
ec0123eec7 | ||
|
|
78f56b9b33 | ||
|
|
1d88ed85bc | ||
|
|
2b7986da19 | ||
|
|
c3d22968e1 | ||
|
|
0e25122ee2 | ||
|
|
4504d84aa8 | ||
|
|
2b87cadba3 | ||
|
|
21dc83c641 | ||
|
|
3b593f349c | ||
|
|
962c215e3b | ||
|
|
42dcfdbcdc | ||
|
|
6d7e6ec871 |
31
.github/workflows/build-docs.yml
vendored
Normal file
31
.github/workflows/build-docs.yml
vendored
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
name: build-docs
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
- feature/docs
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
run-docfx:
|
||||||
|
if: "!(contains(github.event.head_commit.message, '[skip ci]') || contains(github.event.head_commit.message, '[ci skip]'))"
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
with:
|
||||||
|
repository: Cysharp/DocfxTemplate
|
||||||
|
path: docs/_DocfxTemplate
|
||||||
|
- uses: Kirbyrawr/docfx-action@master
|
||||||
|
name: Docfx metadata
|
||||||
|
with:
|
||||||
|
args: metadata docs/docfx.json
|
||||||
|
- uses: Kirbyrawr/docfx-action@master
|
||||||
|
name: Docfx build
|
||||||
|
with:
|
||||||
|
args: build docs/docfx.json
|
||||||
|
- name: Publish to GitHub Pages
|
||||||
|
uses: peaceiris/actions-gh-pages@v3
|
||||||
|
with:
|
||||||
|
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
publish_dir: docs/_site
|
||||||
27
.github/workflows/build-release.yml
vendored
27
.github/workflows/build-release.yml
vendored
@@ -20,18 +20,17 @@ jobs:
|
|||||||
# set release tag(*.*.*) to env.GIT_TAG
|
# set release tag(*.*.*) to env.GIT_TAG
|
||||||
- run: echo ::set-env name=GIT_TAG::${GITHUB_REF#refs/tags/}
|
- run: echo ::set-env name=GIT_TAG::${GITHUB_REF#refs/tags/}
|
||||||
|
|
||||||
# build CommandTools first (use dotnet run command in ZLogger.csproj)
|
# build and pack
|
||||||
- run: dotnet build -c Release ./tools/CommandTools/CommandTools.csproj
|
|
||||||
- run: dotnet build -c Release -p:Version=${{ env.GIT_TAG }}
|
- run: dotnet build -c Release -p:Version=${{ env.GIT_TAG }}
|
||||||
- run: dotnet test -c Release --no-build
|
- run: dotnet test -c Release --no-build
|
||||||
- run: dotnet pack ./src/ZLogger/ZLogger.csproj -c Release --no-build -p:Version=${{ env.GIT_TAG }}
|
- run: dotnet pack ./src/UniTask.NetCore/UniTask.NetCore.csproj -c Release --no-build -p:Version=${{ env.GIT_TAG }}
|
||||||
|
|
||||||
# Store artifacts.
|
# Store artifacts.
|
||||||
- uses: actions/upload-artifact@v1
|
- uses: actions/upload-artifact@v1
|
||||||
with:
|
with:
|
||||||
name: nuget
|
name: nuget
|
||||||
path: ./src/ZLogger/bin/Release/ZLogger.${{ env.GIT_TAG }}.nupkg
|
path: ./src/UniTask.NetCore/bin/Release/UniTask.${{ env.GIT_TAG }}.nupkg
|
||||||
|
|
||||||
build-unity:
|
build-unity:
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
@@ -57,15 +56,13 @@ jobs:
|
|||||||
# Execute scripts: Export Package
|
# Execute scripts: Export Package
|
||||||
- name: Export unitypackage
|
- name: Export unitypackage
|
||||||
run: /opt/Unity/Editor/Unity -quit -batchmode -nographics -silent-crashes -logFile -projectPath . -executeMethod PackageExporter.Export
|
run: /opt/Unity/Editor/Unity -quit -batchmode -nographics -silent-crashes -logFile -projectPath . -executeMethod PackageExporter.Export
|
||||||
working-directory: src/ZLogger.Unity
|
working-directory: src/UniTask
|
||||||
env:
|
|
||||||
UNITY_PACKAGE_VERSION: ${{ env.GIT_TAG }}
|
|
||||||
|
|
||||||
# Store artifacts.
|
# Store artifacts.
|
||||||
- uses: actions/upload-artifact@v1
|
- uses: actions/upload-artifact@v2
|
||||||
with:
|
with:
|
||||||
name: ZLogger.Unity.${{ env.GIT_TAG }}.unitypackage
|
name: UniTask.${{ env.GIT_TAG }}.unitypackage
|
||||||
path: ./src/ZLogger.Unity/ZLogger.Unity.${{ env.GIT_TAG }}.unitypackage
|
path: ./src/UniTask/UniTask.${{ env.GIT_TAG }}.unitypackage
|
||||||
|
|
||||||
create-release:
|
create-release:
|
||||||
needs: [build-dotnet, build-unity]
|
needs: [build-dotnet, build-unity]
|
||||||
@@ -91,7 +88,7 @@ jobs:
|
|||||||
tag_name: ${{ github.ref }}
|
tag_name: ${{ github.ref }}
|
||||||
release_name: Ver.${{ github.ref }}
|
release_name: Ver.${{ github.ref }}
|
||||||
|
|
||||||
# Download (All) Artifacts to current directory
|
# Download(All) Artifacts to current directory
|
||||||
- uses: actions/download-artifact@v2-preview
|
- uses: actions/download-artifact@v2-preview
|
||||||
|
|
||||||
# Upload to NuGet
|
# Upload to NuGet
|
||||||
@@ -103,6 +100,6 @@ jobs:
|
|||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
with:
|
with:
|
||||||
upload_url: ${{ steps.create_release.outputs.upload_url }}
|
upload_url: ${{ steps.create_release.outputs.upload_url }}
|
||||||
asset_path: ./ZLogger.Unity.${{ env.GIT_TAG }}.unitypackage/ZLogger.Unity.${{ env.GIT_TAG }}.unitypackage
|
asset_path: ./UniTask.${{ env.GIT_TAG }}.unitypackage/UniTask.${{ env.GIT_TAG }}.unitypackage
|
||||||
asset_name: ZLogger.Unity.${{ env.GIT_TAG }}.unitypackage
|
asset_name: UniTask.${{ env.GIT_TAG }}.unitypackage
|
||||||
asset_content_type: application/octet-stream
|
asset_content_type: application/octet-stream
|
||||||
48
.gitignore
vendored
48
.gitignore
vendored
@@ -157,3 +157,51 @@ src/UniTask/UniTask.Tests.csproj
|
|||||||
src/UniTask/UniTask.Tests.Editor.csproj
|
src/UniTask/UniTask.Tests.Editor.csproj
|
||||||
|
|
||||||
src/UniTask/UniTask.*.unitypackage
|
src/UniTask/UniTask.*.unitypackage
|
||||||
|
|
||||||
|
src/UniTask/UniTask.Linq.csproj
|
||||||
|
|
||||||
|
src/UniTask/DOTween.Modules.csproj
|
||||||
|
|
||||||
|
src/UniTask/Unity.Addressables.csproj
|
||||||
|
|
||||||
|
src/UniTask/Unity.Addressables.Editor.csproj
|
||||||
|
|
||||||
|
src/UniTask/Unity.Analytics.DataPrivacy.csproj
|
||||||
|
|
||||||
|
src/UniTask/Unity.Recorder.csproj
|
||||||
|
|
||||||
|
src/UniTask/Unity.Recorder.Editor.csproj
|
||||||
|
|
||||||
|
src/UniTask/Unity.ResourceManager.csproj
|
||||||
|
|
||||||
|
src/UniTask/Unity.Rider.Editor.csproj
|
||||||
|
|
||||||
|
src/UniTask/Unity.ScriptableBuildPipeline.csproj
|
||||||
|
|
||||||
|
src/UniTask/Unity.ScriptableBuildPipeline.Editor.csproj
|
||||||
|
|
||||||
|
src/UniTask/Unity.TextMeshPro.csproj
|
||||||
|
|
||||||
|
src/UniTask/Unity.TextMeshPro.Editor.csproj
|
||||||
|
|
||||||
|
src/UniTask/Unity.Timeline.csproj
|
||||||
|
|
||||||
|
src/UniTask/Unity.Timeline.Editor.csproj
|
||||||
|
|
||||||
|
src/UniTask/Unity.VisualStudio.Editor.csproj
|
||||||
|
|
||||||
|
src/UniTask/Unity.VSCode.Editor.csproj
|
||||||
|
|
||||||
|
src/UniTask/UnityEditor.CacheServer.csproj
|
||||||
|
|
||||||
|
src/UniTask/UnityEditor.TestRunner.csproj
|
||||||
|
|
||||||
|
src/UniTask/UnityEditor.UI.csproj
|
||||||
|
|
||||||
|
src/UniTask/UnityEngine.Advertisements.csproj
|
||||||
|
|
||||||
|
src/UniTask/UnityEngine.Monetization.csproj
|
||||||
|
|
||||||
|
src/UniTask/UnityEngine.TestRunner.csproj
|
||||||
|
|
||||||
|
src/UniTask/UnityEngine.UI.csproj
|
||||||
|
|||||||
579
README.md
579
README.md
@@ -2,68 +2,105 @@ UniTask
|
|||||||
===
|
===
|
||||||
[](https://github.com/Cysharp/UniTask/actions) [](https://github.com/Cysharp/UniTask/releases)
|
[](https://github.com/Cysharp/UniTask/actions) [](https://github.com/Cysharp/UniTask/releases)
|
||||||
|
|
||||||
Provides an efficient async/await integration to Unity.
|
Provides an efficient async/await integration to Unity.
|
||||||
|
|
||||||
|
* Struct based `UniTask<T>` and custom AsyncMethodBuilder to achive zero allocation
|
||||||
|
* All Unity AsyncOperations and Coroutine to awaitable
|
||||||
|
* PlayerLoop based task(`UniTask.Yield`, `UniTask.Delay`, `UniTask.DelayFrame`, etc..) that enable to replace all coroutine operation
|
||||||
|
* MonoBehaviour Message Events and uGUI Events as awaitable/async-enumerable
|
||||||
|
* Completely run on Unity's PlayerLoop so don't use thread and run on WebGL, wasm, etc.
|
||||||
|
* Asynchronous LINQ, with Channel and AsyncReactiveProperty
|
||||||
|
* TaskTracker window to prevent memory leak
|
||||||
|
* Highly compatible behaviour with Task/ValueTask/IValueTaskSource
|
||||||
|
|
||||||
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
|
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
|
||||||
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
|
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
|
||||||
## Table of Contents
|
## Table of Contents
|
||||||
|
|
||||||
- [Getting started](#getting-started)
|
- [Getting started](#getting-started)
|
||||||
- [`UniTask<T>`](#unitaskt)
|
- [Basics of UniTask and AsyncOperation](#basics-of-unitask-and-asyncoperation)
|
||||||
- [Cancellation and Exception handling](#cancellation-and-exception-handling)
|
- [Cancellation and Exception handling](#cancellation-and-exception-handling)
|
||||||
- [Progress](#progress)
|
- [Progress](#progress)
|
||||||
|
- [PlayerLoop](#playerloop)
|
||||||
|
- [async void vs async UniTaskVoid](#async-void-vs-async-unitaskvoid)
|
||||||
- [UniTaskTracker](#unitasktracker)
|
- [UniTaskTracker](#unitasktracker)
|
||||||
- [Reusable Promises](#reusable-promises)
|
- [External Assets](#external-assets)
|
||||||
- [awaitable Events](#awaitable-events)
|
- [AsyncEnumerable and Async LINQ](#asyncenumerable-and-async-linq)
|
||||||
- [async void vs async UniTask/UniTaskVoid](#async-void-vs-async-unitaskunitaskvoid)
|
- [Awaitable Events](#awaitable-events)
|
||||||
|
- [Channel](#channel)
|
||||||
- [For Unit Testing](#for-unit-testing)
|
- [For Unit Testing](#for-unit-testing)
|
||||||
- [Method List](#method-list)
|
- [Pooling Configuration](#pooling-configuration)
|
||||||
|
- [API References](#api-references)
|
||||||
- [UPM Package](#upm-package)
|
- [UPM Package](#upm-package)
|
||||||
- [ECS, PlayerLoop](#ecs-playerloop)
|
- [Install via git URL](#install-via-git-url)
|
||||||
|
- [Install via OpenUPM](#install-via-openupm)
|
||||||
|
- [.NET Core](#net-core)
|
||||||
- [License](#license)
|
- [License](#license)
|
||||||
|
|
||||||
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
||||||
|
|
||||||
|
|
||||||
Getting started
|
Getting started
|
||||||
---
|
---
|
||||||
Install package(`UniRx.Async.unitypackage`) is available in [UniTask/releases](https://github.com/Cysharp/UniTask/releases) page.
|
Install via [UPM package](#upm-package) or asset package(`UniTask.*.*.*.unitypackage`) available in [UniTask/releases](https://github.com/Cysharp/UniTask/releases) page.
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
// extension awaiter/methods can be used by this namespace
|
// extension awaiter/methods can be used by this namespace
|
||||||
using UniRx.Async;
|
using Cysharp.Threading.Tasks;
|
||||||
|
|
||||||
// You can return type as struct UniTask<T>(or UniTask), it is unity specialized lightweight alternative of Task<T>
|
// You can return type as struct UniTask<T>(or UniTask), it is unity specialized lightweight alternative of Task<T>
|
||||||
// no(or less) allocation and fast excution for zero overhead async/await integrate with Unity
|
// zero allocation and fast excution for zero overhead async/await integrate with Unity
|
||||||
async UniTask<string> DemoAsync()
|
async UniTask<string> DemoAsync()
|
||||||
{
|
{
|
||||||
// You can await Unity's AsyncObject
|
// You can await Unity's AsyncObject
|
||||||
var asset = await Resources.LoadAsync<TextAsset>("foo");
|
var asset = await Resources.LoadAsync<TextAsset>("foo");
|
||||||
|
var txt = (await UnityWebRequest.Get("https://...").SendWebRequest()).downloadHandler.text;
|
||||||
// .ConfigureAwait accepts progress callback
|
await SceneManager.LoadSceneAsync("scene2");
|
||||||
await SceneManager.LoadSceneAsync("scene2").ConfigureAwait(Progress.Create<float>(x => Debug.Log(x)));
|
|
||||||
|
// .WithCancellation enables Cancel, GetCancellationTokenOnDestroy synchornizes with lifetime of GameObject
|
||||||
|
var asset2 = await Resources.LoadAsync<TextAsset>("bar").WithCancellation(this.GetCancellationTokenOnDestroy());
|
||||||
|
|
||||||
|
// .ToUniTask accepts progress callback(and all options), Progress.Create is a lightweight alternative of IProgress<T>
|
||||||
|
var asset3 = await Resources.LoadAsync<TextAsset>("baz").ToUniTask(Progress.Create<float>(x => Debug.Log(x)));
|
||||||
|
|
||||||
// await frame-based operation like coroutine
|
// await frame-based operation like coroutine
|
||||||
await UniTask.DelayFrame(100);
|
await UniTask.DelayFrame(100);
|
||||||
|
|
||||||
// replacement of WaitForSeconds/WaitForSecondsRealtime
|
// replacement of yield return new WaitForSeconds/WaitForSecondsRealtime
|
||||||
await UniTask.Delay(TimeSpan.FromSeconds(10), ignoreTimeScale: false);
|
await UniTask.Delay(TimeSpan.FromSeconds(10), ignoreTimeScale: false);
|
||||||
|
|
||||||
// replacement of WaitForEndOfFrame(or other timing like yield return null, yield return WaitForFixedUpdate)
|
// yield any playerloop timing(PreUpdate, Update, LateUpdate, etc...)
|
||||||
await UniTask.Yield(PlayerLoopTiming.PostLateUpdate);
|
await UniTask.Yield(PlayerLoopTiming.PreLateUpdate);
|
||||||
|
|
||||||
|
// replacement of yield return null
|
||||||
|
await UniTask.Yield();
|
||||||
|
await UniTask.NextFrame();
|
||||||
|
|
||||||
|
// replacement of WaitForEndOfFrame(same as UniTask.Yield(PlayerLoopTiming.LastPostLateUpdate))
|
||||||
|
await UniTask.WaitForEndOfFrame();
|
||||||
|
|
||||||
|
// replacement of yield return new WaitForFixedUpdate(same as UniTask.Yield(PlayerLoopTiming.FixedUpdate))
|
||||||
|
await UniTask.WaitForFixedUpdate();
|
||||||
|
|
||||||
// replacement of yield return WaitUntil
|
// replacement of yield return WaitUntil
|
||||||
await UniTask.WaitUntil(() => isActive == false);
|
await UniTask.WaitUntil(() => isActive == false);
|
||||||
|
|
||||||
|
// special helper of WaitUntil
|
||||||
|
await UniTask.WaitUntilValueChanged(this, x => x.isActive);
|
||||||
|
|
||||||
// You can await IEnumerator coroutine
|
// You can await IEnumerator coroutine
|
||||||
await FooCoroutineEnumerator();
|
await FooCoroutineEnumerator();
|
||||||
|
|
||||||
// You can await standard task
|
// You can await standard task
|
||||||
await Task.Run(() => 100);
|
await Task.Run(() => 100);
|
||||||
|
|
||||||
// Multithreading, run on ThreadPool under this code(use SwitchToMainThread, same as `ObserveOnMainThreadDispatcher`)
|
// Multithreading, run on ThreadPool under this code
|
||||||
await UniTask.SwitchToThreadPool();
|
await UniTask.SwitchToThreadPool();
|
||||||
|
|
||||||
|
/* work on ThreadPool */
|
||||||
|
|
||||||
|
// return to MainThread(same as `ObserveOnMainThread` in UniRx)
|
||||||
|
await UniTask.SwitchToMainThread();
|
||||||
|
|
||||||
// get async webrequest
|
// get async webrequest
|
||||||
async UniTask<string> GetTextAsync(UnityWebRequest req)
|
async UniTask<string> GetTextAsync(UnityWebRequest req)
|
||||||
{
|
{
|
||||||
@@ -77,6 +114,9 @@ async UniTask<string> DemoAsync()
|
|||||||
|
|
||||||
// concurrent async-wait and get result easily by tuple syntax
|
// concurrent async-wait and get result easily by tuple syntax
|
||||||
var (google, bing, yahoo) = await UniTask.WhenAll(task1, task2, task3);
|
var (google, bing, yahoo) = await UniTask.WhenAll(task1, task2, task3);
|
||||||
|
|
||||||
|
// shorthand of WhenAll, tuple can await directly
|
||||||
|
var (google2, bing2, yahoo2) = await (task1, task2, task3);
|
||||||
|
|
||||||
// You can handle timeout easily
|
// You can handle timeout easily
|
||||||
await GetTextAsync(UnityWebRequest.Get("http://unity.com")).Timeout(TimeSpan.FromMilliseconds(300));
|
await GetTextAsync(UnityWebRequest.Get("http://unity.com")).Timeout(TimeSpan.FromMilliseconds(300));
|
||||||
@@ -86,47 +126,38 @@ async UniTask<string> DemoAsync()
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
`UniTask<T>`
|
Basics of UniTask and AsyncOperation
|
||||||
---
|
---
|
||||||
UniTask feature rely on C# 7.0([task-like custom async method builder feature](https://github.com/dotnet/roslyn/blob/master/docs/features/task-types.md)) so required Unity version is after `Unity 2018.3`.
|
UniTask feature rely on C# 7.0([task-like custom async method builder feature](https://github.com/dotnet/roslyn/blob/master/docs/features/task-types.md)) so required Unity version is after `Unity 2018.3`, officialy lower support version is `Unity 2018.4.13f1`.
|
||||||
|
|
||||||
Why UniTask(custom task-like object) is required? Because Task is too heavy, not matched to Unity threading(single-thread). UniTask does not use thread and SynchronizationContext because almost Unity's asynchronous object is automaticaly dispatched by Unity's engine layer. It acquires more fast and more less allocation, completely integrated with Unity.
|
Why UniTask(custom task-like object) is required? Because Task is too heavy, not matched to Unity threading(single-thread). UniTask does not use thread and SynchronizationContext/ExecutionContext because almost Unity's asynchronous object is automaticaly dispatched by Unity's engine layer. It acquires more fast and more less allocation, completely integrated with Unity.
|
||||||
|
|
||||||
> More details, please see this slide: [Deep Dive async/await in Unity with UniTask(EN)
|
You can await `AsyncOperation`, `ResourceRequest`, `AssetBundleRequest`, `AssetBundleCreateRequest`, `UnityWebRequestAsyncOperation`, `IEnumerator` and others when `using Cysharp.Threading.Tasks;`.
|
||||||
](https://www.slideshare.net/neuecc/deep-dive-asyncawait-in-unity-with-unitasken)
|
|
||||||
|
|
||||||
You can await `AsyncOperation`, `ResourceRequest`, `UnityWebRequestAsyncOperation`, `IEnumerator` and others when using `UniRx.Async`.
|
UniTask provides three pattern of extension methods.
|
||||||
|
|
||||||
`UniTask.Delay`, `UniTask.Yield`, `UniTask.Timeout` that is frame-based timer operators(no uses thread so works on WebGL publish) driven by custom PlayerLoop(Unity 2018 experimental feature). In default, UniTask initialize automatically when application begin, but it is override all. If you want to append PlayerLoop, please call `PlayerLoopHelper.Initialize(ref yourCustomizedPlayerLoop)` manually.
|
|
||||||
|
|
||||||
> Before Unity 2019.3, Unity does not have `PlayerLooop.GetCurrentPlayerLoop` so you can't use with Unity ECS package in default. If you want to use with ECS and before Unity 2019.3, you can use this hack below.
|
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
// Get ECS Loop.
|
* await asyncOperation;
|
||||||
var playerLoop = ScriptBehaviourUpdateOrder.CurrentPlayerLoop;
|
* .WithCancellation(CancellationToken);
|
||||||
|
* .ToUniTask(IProgress, PlayerLoopTiming, CancellationToken);
|
||||||
// Setup UniTask's PlayerLoop.
|
|
||||||
PlayerLoopHelper.Initialize(ref playerLoop);
|
|
||||||
```
|
```
|
||||||
|
|
||||||
`UniTask.WhenAll`, `UniTask.WhenAny` is like Task.WhenAll/WhenAny but return type is more useful.
|
`WithCancellation` is a simple version of `ToUniTask`, both returns `UniTask`. Details of cancellation, see: [Cancellation and Exception handling](#cancellation-and-exception-handling) section.
|
||||||
|
|
||||||
`UniTask.ctor(Func<UniTask>)` is like the embeded [`AsyncLazy<T>`](https://blogs.msdn.microsoft.com/pfxteam/2011/01/15/asynclazyt/)
|
> Note: WithCancellation is returned from native timing of PlayerLoop but ToUniTask is returned from specified PlayerLoopTiming. Details of timing, see: [PlayerLoop](#playerloop) section.
|
||||||
|
|
||||||
|
The type of `UniTask` can use utility like `UniTask.WhenAll`, `UniTask.WhenAny`. It is like Task.WhenAll/WhenAny but return type is more useful, returns value tuple so can deconsrtuct each result and pass multiple type.
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
public class SceneAssets
|
public class SceneAssets
|
||||||
{
|
{
|
||||||
public readonly UniTask<Sprite> Front;
|
|
||||||
public readonly UniTask<Sprite> Background;
|
|
||||||
public readonly UniTask<Sprite> Effect;
|
|
||||||
|
|
||||||
public SceneAssets()
|
public SceneAssets()
|
||||||
{
|
{
|
||||||
// ctor(Func) overload is AsyncLazy, initialized once when await.
|
// parallel load.
|
||||||
// and after it, await returns zero-allocation value immediately.
|
var (a, b, c) = await UniTask.WhenAll(
|
||||||
Front = new UniTask<Sprite>(() => LoadAsSprite("foo"));
|
LoadAsSprite("foo"),
|
||||||
Background = new UniTask<Sprite>(() => LoadAsSprite("bar"));
|
LoadAsSprite("bar"),
|
||||||
Effect = new UniTask<Sprite>(() => LoadAsSprite("baz"));
|
LoadAsSprite("baz"));
|
||||||
}
|
}
|
||||||
|
|
||||||
async UniTask<Sprite> LoadAsSprite(string path)
|
async UniTask<Sprite> LoadAsSprite(string path)
|
||||||
@@ -152,13 +183,32 @@ public UniTask<int> WrapByUniTaskCompletionSource()
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
You can convert Task -> UniTask: `AsUniTask`, `UniTask` -> `UniTask<AsyncUnit>`: `AsAsyncUnitUniTask`(this is useful to use WhenAll/WhenAny), `UniTask<T>` -> `UniTask`: `AsUniTask`.
|
You can convert Task -> UniTask: `AsUniTask`, `UniTask` -> `UniTask<AsyncUnit>`: `AsAsyncUnitUniTask`, `UniTask<T>` -> `UniTask`: `AsUniTask`. `UniTask<T>` -> `UniTask`'s conversion cost is free.
|
||||||
|
|
||||||
If you want to convert async to coroutine, you can use `UniTask.ToCoroutine`, this is useful to use only allow coroutine system.
|
If you want to convert async to coroutine, you can use `.ToCoroutine()`, this is useful to use only allow coroutine system.
|
||||||
|
|
||||||
|
UniTask can not await twice. This is a similar constraint to the [ValueTask/IValueTaskSource](https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.valuetask-1?view=netcore-3.1) introduced in .NET Standard 2.1.
|
||||||
|
|
||||||
|
> The following operations should never be performed on a ValueTask<TResult> instance:
|
||||||
|
>
|
||||||
|
> * Awaiting the instance multiple times.
|
||||||
|
> * Calling AsTask multiple times.
|
||||||
|
> * Using .Result or .GetAwaiter().GetResult() when the operation hasn't yet completed, or using them multiple times.
|
||||||
|
> * Using more than one of these techniques to consume the instance.
|
||||||
|
>
|
||||||
|
> If you do any of the above, the results are undefined.
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
var task = UniTask.DelayFrame(10);
|
||||||
|
await task;
|
||||||
|
await task; // NG, throws Exception
|
||||||
|
```
|
||||||
|
|
||||||
|
Store to the class field, you can use `UniTask.Lazy` that gurantee call multipletimes. `.Prevent()` allows for multiple calls (internally cached results). This is useful when multiple calls in a function scope.
|
||||||
|
|
||||||
Cancellation and Exception handling
|
Cancellation and Exception handling
|
||||||
---
|
---
|
||||||
Some UniTask factory methods have `CancellationToken cancellation = default(CancellationToken)` parameter. Andalso some async operation for unity have `ConfigureAwait(..., CancellationToken cancellation = default(CancellationToken))` extension methods.
|
Some UniTask factory methods have `CancellationToken cancellationToken = default` parameter. Andalso some async operation for unity have `WithCancellation(CancellationToken)` and `ToUniTask(..., CancellationToken cancellation = default)` extension methods.
|
||||||
|
|
||||||
You can pass `CancellationToken` to parameter by standard [`CancellationTokenSource`](https://docs.microsoft.com/en-us/dotnet/api/system.threading.cancellationtokensource).
|
You can pass `CancellationToken` to parameter by standard [`CancellationTokenSource`](https://docs.microsoft.com/en-us/dotnet/api/system.threading.cancellationtokensource).
|
||||||
|
|
||||||
@@ -170,7 +220,7 @@ cancelButton.onClick.AddListener(() =>
|
|||||||
cts.Cancel();
|
cts.Cancel();
|
||||||
});
|
});
|
||||||
|
|
||||||
await UnityWebRequest.Get("http://google.co.jp").SendWebRequest().ConfigureAwait(cancellation: cts.Token);
|
await UnityWebRequest.Get("http://google.co.jp").SendWebRequest().WithCancellation(cts.Token);
|
||||||
|
|
||||||
await UniTask.DelayFrame(1000, cancellationToken: cts.Token);
|
await UniTask.DelayFrame(1000, cancellationToken: cts.Token);
|
||||||
```
|
```
|
||||||
@@ -184,7 +234,7 @@ await UniTask.DelayFrame(1000, cancellationToken: this.GetCancellationTokenOnDes
|
|||||||
|
|
||||||
When detect cancellation, all methods throws `OperationCanceledException` and propagate to upstream. `OperationCanceledException` is special exception, if not handled this exception, finally it is propagated to `UniTaskScheduler.UnobservedTaskException`.
|
When detect cancellation, all methods throws `OperationCanceledException` and propagate to upstream. `OperationCanceledException` is special exception, if not handled this exception, finally it is propagated to `UniTaskScheduler.UnobservedTaskException`.
|
||||||
|
|
||||||
Default behaviour of received unhandled exception is write log as warning. Log level can change by `UniTaskScheduler.UnobservedExceptionWriteLogType`. If you want to change custom beavhiour, set action to `UniTaskScheduler.UnobservedTaskException.`
|
Default behaviour of received unhandled exception is write log as exception. Log level can change by `UniTaskScheduler.UnobservedExceptionWriteLogType`. If you want to change custom beavhiour, set action to `UniTaskScheduler.UnobservedTaskException.`
|
||||||
|
|
||||||
If you want to cancel behaviour in async UniTask method, throws `OperationCanceledException` manually.
|
If you want to cancel behaviour in async UniTask method, throws `OperationCanceledException` manually.
|
||||||
|
|
||||||
@@ -223,23 +273,23 @@ if (isCanceled)
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Note: Only suppress throws if you call it directly into the most source method.
|
Note: Only suppress throws if you call it directly into the most source method. Otherwise, the return value will be converted, but the entire pipeline will not be suppressed throws.
|
||||||
|
|
||||||
Progress
|
Progress
|
||||||
---
|
---
|
||||||
Some async operation for unity have `ConfigureAwait(IProgress<float> progress = null, ...)` extension methods.
|
Some async operation for unity have `ToUniTask(IProgress<float> progress = null, ...)` extension methods.
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
var progress = Progress.Create<float>(x => Debug.Log(x));
|
var progress = Progress.Create<float>(x => Debug.Log(x));
|
||||||
|
|
||||||
var request = await UnityWebRequest.Get("http://google.co.jp")
|
var request = await UnityWebRequest.Get("http://google.co.jp")
|
||||||
.SendWebRequest()
|
.SendWebRequest()
|
||||||
.ConfigureAwait(progress: progress);
|
.ToUniTask(progress: progress);
|
||||||
```
|
```
|
||||||
|
|
||||||
Should not use `new System.Progress<T>`, because it allocate every times. Use `UniRx.Async.Progress` instead. Progress factory has two methods, `Create` and `CreateOnlyValueChanged`. `CreateOnlyValueChanged` calls only when progress value changed. Should not use `new System.Progress<T>`, it allocate every times.
|
You should not use standard `new System.Progress<T>`, because it causes allocation every times. Use `Cysharp.Threading.Tasks.Progress` instead. This progress factory has two methods, `Create` and `CreateOnlyValueChanged`. `CreateOnlyValueChanged` calls only when progress value changed.
|
||||||
|
|
||||||
Implements interface is more better.
|
Implements IProgress interface to caller is more better, there is no allocation of lambda.
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
public class Foo : MonoBehaviour, IProgress<float>
|
public class Foo : MonoBehaviour, IProgress<float>
|
||||||
@@ -253,16 +303,135 @@ public class Foo : MonoBehaviour, IProgress<float>
|
|||||||
{
|
{
|
||||||
var request = await UnityWebRequest.Get("http://google.co.jp")
|
var request = await UnityWebRequest.Get("http://google.co.jp")
|
||||||
.SendWebRequest()
|
.SendWebRequest()
|
||||||
.ConfigureAwait(progress: this); // pass this
|
.ToUniTask(progress: this); // pass this
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
PlayerLoop
|
||||||
|
---
|
||||||
|
UniTask is run on custom [PlayerLoop](https://docs.unity3d.com/ScriptReference/LowLevel.PlayerLoop.html). UniTask's playerloop based method(such as `Delay`, `DelayFrame`, `asyncOperation.ToUniTask`, etc...) accepts this `PlayerLoopTiming`.
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
public enum PlayerLoopTiming
|
||||||
|
{
|
||||||
|
Initialization = 0,
|
||||||
|
LastInitialization = 1,
|
||||||
|
|
||||||
|
EarlyUpdate = 2,
|
||||||
|
LastEarlyUpdate = 3,
|
||||||
|
|
||||||
|
FixedUpdate = 4,
|
||||||
|
LastFixedUpdate = 5,
|
||||||
|
|
||||||
|
PreUpdate = 6,
|
||||||
|
LastPreUpdate = 7,
|
||||||
|
|
||||||
|
Update = 8,
|
||||||
|
LastUpdate = 9,
|
||||||
|
|
||||||
|
PreLateUpdate = 10,
|
||||||
|
LastPreLateUpdate = 11,
|
||||||
|
|
||||||
|
PostLateUpdate = 12,
|
||||||
|
LastPostLateUpdate = 13
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
It indicates when to run, you can check [PlayerLoopList.md](https://gist.github.com/neuecc/bc3a1cfd4d74501ad057e49efcd7bdae) to Unity's default playerloop and injected UniTask's custom loop.
|
||||||
|
|
||||||
|
`PlayerLoopTiming.Update` is similar as `yield return null` in coroutine, but it is called before Update(Update and uGUI events(button.onClick, etc...) are called on `ScriptRunBehaviourUpdate`, yield return null is called on `ScriptRunDelayedDynamicFrameRate`). `PlayerLoopTiming.FixedUpdate` is similar as `WaitForFixedUpdate`, `PlayerLoopTiming.LastPostLateUpdate` is similar as `WaitForEndOfFrame` in coroutine.
|
||||||
|
|
||||||
|
`yield return null` and `UniTask.Yield` is similar but different. `yield return null` always return next frame but `UniTask.Yield` return next called, that is, call `UniTask.Yield(PlayerLoopTiming.Update)` on `PreUpdate`, it returns same frame. `UniTask.NextFrame()` gurantees return next frame, this would be expected to behave exactly the same as `yield return null`.
|
||||||
|
|
||||||
|
> UniTask.Yield(without CancellationToken) is a special type, returns `YieldAwaitable` and run on YieldRunner. It is most lightweight and faster.
|
||||||
|
|
||||||
|
AsyncOperation is returned from native timing. For example, await `SceneManager.LoadSceneAsync` is returned from `EarlyUpdate.UpdatePreloading` and after called, loaded scene called from `EarlyUpdate.ScriptRunDelayedStartupFrame`. Also `await UnityWebRequest` is returned from `EarlyUpdate.ExecuteMainThreadJobs`.
|
||||||
|
|
||||||
|
In UniTask, await directly and `WithCancellation` use native timing, `ToUniTask` use specified timing. This is usually not a particular problem, but with `LoadSceneAsync`, causes different order of Start and continuation after await. so recommend not to use `LoadSceneAsync.ToUniTask`.
|
||||||
|
|
||||||
|
In stacktrace, you can check where is running in playerloop.
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
In default, UniTask's PlayerLoop is initialized at `[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]`.
|
||||||
|
|
||||||
|
The order in which methods are called in BeforeSceneLoad is indeterminate, so if you want to use UniTask in other BeforeSceneLoad methods, you should try to initialize it before this.
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
// AfterAssembliesLoaded is called before BeforeSceneLoad
|
||||||
|
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
|
||||||
|
public static void InitUniTaskLoop()
|
||||||
|
{
|
||||||
|
var loop = PlayerLoop.GetCurrentPlayerLoop();
|
||||||
|
Cysharp.Threading.Tasks.PlayerLoopHelper.Initialize(ref loop);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
If you import Unity's `Entities` package, that reset custom player loop to default at `BeforeSceneLoad` and inject ECS's loop. When Unity call ECS's inject method after UniTask's initialize method, UniTask will no longer work.
|
||||||
|
|
||||||
|
To solve this issue, you can re-initialize UniTask PlayerLoop after ECS initialized.
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
// Get ECS Loop.
|
||||||
|
var playerLoop = ScriptBehaviourUpdateOrder.CurrentPlayerLoop;
|
||||||
|
|
||||||
|
// Setup UniTask's PlayerLoop.
|
||||||
|
PlayerLoopHelper.Initialize(ref playerLoop);
|
||||||
|
```
|
||||||
|
|
||||||
|
async void vs async UniTaskVoid
|
||||||
|
---
|
||||||
|
`async void` is a standard C# taks system so does not run on UniTask systems. It is better not to use. `async UniTaskVoid` is a lightweight version of `async UniTask` because it does not have awaitable completion and report error immediately to `UniTaskScheduler.UnobservedTaskException`. If you don't require to await it(fire and forget), use `UniTaskVoid` is better. Unfortunately to dismiss warning, require to using with `Forget()`.
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
public async UniTaskVoid FireAndForgetMethod()
|
||||||
|
{
|
||||||
|
// do anything...
|
||||||
|
await UniTask.Yield();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Caller()
|
||||||
|
{
|
||||||
|
FireAndForgetMethod().Forget();
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Also UniTask have `Forget` method, it is similar with UniTaskVoid and same effects with it. However still UniTaskVoid is more efficient if completely do not use await。
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
public async UniTask DoAsync()
|
||||||
|
{
|
||||||
|
// do anything...
|
||||||
|
await UniTask.Yield();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Caller()
|
||||||
|
{
|
||||||
|
DoAsync().Forget();
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Using async lambda in register event, it is used `async void`. To avoid it, you can use `UniTask.Action` or `UniTask.UnityAction` that creates delegate via `async UniTaskVoid` lambda.
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
Action actEvent;
|
||||||
|
UnityAction unityEvent; // especially used in uGUI
|
||||||
|
|
||||||
|
// Bad: async void
|
||||||
|
actEvent += async () => { };
|
||||||
|
unityEvent += async () => { };
|
||||||
|
|
||||||
|
// Ok: create Action delegate by lambda
|
||||||
|
actEvent += UniTask.Action(async () => { await UniTask.Yield(); });
|
||||||
|
unityEvent += UniTask.UnityAction(async () => { await UniTask.Yield(); });
|
||||||
|
```
|
||||||
|
|
||||||
UniTaskTracker
|
UniTaskTracker
|
||||||
---
|
---
|
||||||
useful for check(leak) UniTasks. You can open tracker window in `Window -> UniTask Tracker`.
|
useful for check(leak) UniTasks. You can open tracker window in `Window -> UniTask Tracker`.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
* Enable AutoReload(Toggle) - Reload automatically.
|
* Enable AutoReload(Toggle) - Reload automatically.
|
||||||
* Reload - Reload view.
|
* Reload - Reload view.
|
||||||
@@ -272,38 +441,89 @@ useful for check(leak) UniTasks. You can open tracker window in `Window -> UniTa
|
|||||||
|
|
||||||
For debug use, enable tracking and capture stacktrace is useful but it it decline performance. Recommended usage is enable both to find task leak, and when done, finally disable both.
|
For debug use, enable tracking and capture stacktrace is useful but it it decline performance. Recommended usage is enable both to find task leak, and when done, finally disable both.
|
||||||
|
|
||||||
Reusable Promises
|
External Assets
|
||||||
---
|
---
|
||||||
Some UniTask factory can reuse to reduce allocation. The list is `Yield`, `Delay`, `DelayFrame`, `WaitUntil`, `WaitWhile`, `WaitUntilValueChanged`.
|
In default, UniTask supports DOTween and Addressables(`AsyncOperationHandle` and `AsyncOpereationHandle<T>` as awaitable).
|
||||||
|
|
||||||
|
For DOTween support, require to `com.demigiant.dotween` import from [OpenUPM](https://openupm.com/packages/com.demigiant.dotween/) or define `UNITASK_DOTWEEN_SUPPORT` to enable it.
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
var reusePromise = UniTask.DelayFrame(10);
|
// sequential
|
||||||
|
await transform.DOMoveX(2, 10);
|
||||||
|
await transform.DOMoveZ(5, 20);
|
||||||
|
|
||||||
for (int i = 0; i < 10; i++)
|
// parallel with cancellation
|
||||||
|
var ct = this.GetCancellationTokenOnDestroy();
|
||||||
|
|
||||||
|
await UniTask.WhenAll(
|
||||||
|
transform.DOMoveX(10, 3).WithCancellation(ct),
|
||||||
|
transform.DOScale(10, 3).WithCancellation(ct));
|
||||||
|
```
|
||||||
|
|
||||||
|
AsyncEnumerable and Async LINQ
|
||||||
|
---
|
||||||
|
Unity 2020.2.0a12 supports C# 8.0 so you can use `await foreach`. This is the new Update notation in async era.
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
// Unity 2020.2.0a12, C# 8.0
|
||||||
|
await foreach (var _ in UniTaskAsyncEnumerable.EveryUpdate(token))
|
||||||
{
|
{
|
||||||
await reusePromise;
|
Debug.Log("Update() " + Time.frameCount);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
awaitable Events
|
In a C# 7.3 environment, you can use the `ForEachAsync` method to work in almost the same way.
|
||||||
---
|
|
||||||
Unity events can await like `OnClickAsync`, `OnCollisionEnterAsync`. It can use by `UniRx.Async.Triggers`.
|
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
using UniRx.Async.Triggers;
|
// C# 7.3(Unity 2018.3~)
|
||||||
|
await UniTaskAsyncEnumerable.EveryUpdate(token).ForEachAsync(_ =>
|
||||||
async UniTask TripleClick(CancellationToken token)
|
|
||||||
{
|
{
|
||||||
await button.OnClickAsync(token);
|
Debug.Log("Update() " + Time.frameCount);
|
||||||
await button.OnClickAsync(token);
|
});
|
||||||
await button.OnClickAsync(token);
|
```
|
||||||
|
|
||||||
|
UniTaskAsyncEnumerable implements asynchronous LINQ, similar to LINQ in `IEnumerable<T>` or Rx in `IObservable<T>`. All standard LINQ query operators can be applied to asynchronous streams. For example, the following code shows how to apply a Where filter to a button-click asynchronous stream that runs once every two clicks.
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
await okButton.OnClickAsAsyncEnumerable().Where((x, i) => i % 2 == 0).ForEachAsync(_ =>
|
||||||
|
{
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
Async LINQ is enabled when `using Cysharp.Threading.Tasks.Linq;`, and `UniTaskAsyncEnumerable` is defined in `UniTask.Linq` asmdef.
|
||||||
|
|
||||||
|
It's closer to UniRx (Reactive Extensions), but UniTaskAsyncEnumerable is a pull-based asynchronous stream, whereas Rx was a push-based asynchronous stream. Note that although similar, the characteristics are different and the details behave differently along with them.
|
||||||
|
|
||||||
|
`UniTaskAsyncEnumerable` is the entry point like `Enumerbale`. In addition to the standard query operators, there are other generators for Unity such as `EveryUpdate`, `Timer`, `TimerFrame`, `Interval`, `IntervalFrame`, and `EveryValueChanged`. And also added additional UniTask original query operators like `Append`, `Prepend`, `DistinctUntilChanged`, `ToHashSet`, `Buffer`, `CombineLatest`, `Do`, `Never`, `ForEachAsync`, `Pairwise`, `Publish`, `Queue`, `Return`, `SkipUntilCanceled`, `TakeUntilCanceled`, `TakeLast`.
|
||||||
|
|
||||||
|
The method with Func as an argument has three additional overloads, `***Await`, `***AwaitWithCancellation`.
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
Select(Func<T, TR> selector)
|
||||||
|
SelectAwait(Func<T, UniTask<TR>> selector)
|
||||||
|
SelectAwaitWithCancellation(Func<T, CancellationToken, UniTask<TR>> selector)
|
||||||
|
```
|
||||||
|
|
||||||
|
If you want to use the `async` method inside the func, use the `***Await` or `***AwaitWithCancellation`.
|
||||||
|
|
||||||
|
Awaitable Events
|
||||||
|
---
|
||||||
|
All uGUI component implements `***AsAsyncEnumerable` to convert asynchronous streams of events.
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
async UniTask TripleClick()
|
||||||
|
{
|
||||||
|
// In default, used button.GetCancellationTokenOnDestroy to manage lieftime of async
|
||||||
|
await button.OnClickAsync();
|
||||||
|
await button.OnClickAsync();
|
||||||
|
await button.OnClickAsync();
|
||||||
Debug.Log("Three times clicked");
|
Debug.Log("Three times clicked");
|
||||||
}
|
}
|
||||||
|
|
||||||
// more efficient way
|
// more efficient way
|
||||||
async UniTask TripleClick(CancellationToken token)
|
async UniTask TripleClick()
|
||||||
{
|
{
|
||||||
using (var handler = button.GetAsyncClickEventHandler(token))
|
using (var handler = button.GetAsyncClickEventHandler())
|
||||||
{
|
{
|
||||||
await handler.OnClickAsync();
|
await handler.OnClickAsync();
|
||||||
await handler.OnClickAsync();
|
await handler.OnClickAsync();
|
||||||
@@ -311,11 +531,126 @@ async UniTask TripleClick(CancellationToken token)
|
|||||||
Debug.Log("Three times clicked");
|
Debug.Log("Three times clicked");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// use async LINQ
|
||||||
|
async UniTask TripleClick(CancellationToken token)
|
||||||
|
{
|
||||||
|
await button.OnClickAsAsyncEnumerable().Take(3).Last();
|
||||||
|
Debug.Log("Three times clicked");
|
||||||
|
}
|
||||||
|
|
||||||
|
// use async LINQ2
|
||||||
|
async UniTask TripleClick(CancellationToken token)
|
||||||
|
{
|
||||||
|
await button.OnClickAsAsyncEnumerable().Take(3).ForEachAsync(_ =>
|
||||||
|
{
|
||||||
|
Debug.Log("Every clicked");
|
||||||
|
});
|
||||||
|
Debug.Log("Three times clicked, complete.");
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
async void vs async UniTask/UniTaskVoid
|
All MonoBehaviour message events can convert async-streams by `AsyncTriggers` that can enable by `using Cysharp.Threading.Tasks.Triggers;`.
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
using Cysharp.Threading.Tasks.Triggers;
|
||||||
|
|
||||||
|
async UniTaskVoid MonitorCollision()
|
||||||
|
{
|
||||||
|
await gameObject.OnCollisionEnterAsync();
|
||||||
|
Debug.Log("Collision Enter");
|
||||||
|
/* do anything */
|
||||||
|
|
||||||
|
await gameObject.OnCollisionExitAsync();
|
||||||
|
Debug.Log("Collision Exit");
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Similar as uGUI event, AsyncTrigger can get by `GetAsync***Trigger` and trigger it self is UniTaskAsyncEnumerable.
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
// use await multiple times, get AsyncTriggerHandler is more efficient.
|
||||||
|
using(var trigger = this.GetOnCollisionEnterAsyncHandler())
|
||||||
|
{
|
||||||
|
await OnCollisionEnterAsync();
|
||||||
|
await OnCollisionEnterAsync();
|
||||||
|
await OnCollisionEnterAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
// every moves.
|
||||||
|
await this.GetAsyncMoveTrigger().ForEachAsync(axisEventData =>
|
||||||
|
{
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
`AsyncReactiveProperty`, `AsyncReadOnlyReactiveProperty` is UniTask version of UniTask's ReactiveProperty.
|
||||||
|
|
||||||
|
`BindTo` extension method of `IUniTaskAsyncEnumerable<T>` for binding asynchronous stream values to Unity components(Text/Selectable/TMP/Text).
|
||||||
|
|
||||||
|
A pull-type asynchronous stream does not get the next values until the asynchronous processing in the sequence is complete. This could spill data from push-type events such as buttons.
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
// can not get click event during 3 seconds complete.
|
||||||
|
await button.OnClickAsAsyncEnumerable().ForEachAwaitAsync()
|
||||||
|
{
|
||||||
|
await UniTask.Delay(TimeSpan.FromSeconds(3));
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
It is useful(prevent double-click) but not useful in sometimes.
|
||||||
|
|
||||||
|
Using `Queue()` method, which will also queue events during asynchronous processing.
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
// queued message in asynchronous processing
|
||||||
|
await button.OnClickAsAsyncEnumerable().Queue().ForEachAwaitAsync()
|
||||||
|
{
|
||||||
|
await UniTask.Delay(TimeSpan.FromSeconds(3));
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Channel
|
||||||
---
|
---
|
||||||
`async void` is standard C# system so does not run on UniTask systems. It is better not to use. `async UniTaskVoid` is lightweight version of `async UniTask` because it does not have awaitable completion. If you don't require to await it(fire and forget), use `UniTaskVoid` is better. Unfortunately to dismiss warning, require to using with `Forget()`.
|
`Channel` is same as [System.Threading.Tasks.Channels](https://docs.microsoft.com/ja-jp/dotnet/api/system.threading.channels?view=netcore-3.1) that is similar as GoLang Channel.
|
||||||
|
|
||||||
|
Currently only supports multiple-producer, single-consumer unbounded channel. It can create by `Channel.CreateSingleConsumerUnbounded<T>()`.
|
||||||
|
|
||||||
|
For producer(`.Writer`), `TryWrite` to push value and `TryComplete` to complete channel. For consumer(`.Reader`), `TryRead`, `WaitToReadAsync`, `ReadAsync`, `Completion` and `ReadAllAsync` to read queued messages.
|
||||||
|
|
||||||
|
`ReadAllAsync` returns `IUniTaskAsyncEnumerable<T>` so query LINQ operators. Reader only allows single-consumer but use `.Publish()` query operator to enable multicast message. For example, make pub/sub utility.
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
public class AsyncMessageBroker<T> : IDisposable
|
||||||
|
{
|
||||||
|
Channel<T> channel;
|
||||||
|
|
||||||
|
IConnectableUniTaskAsyncEnumerable<T> multicastSource;
|
||||||
|
IDisposable connection;
|
||||||
|
|
||||||
|
public AsyncMessageBroker()
|
||||||
|
{
|
||||||
|
channel = Channel.CreateSingleConsumerUnbounded<T>();
|
||||||
|
multicastSource = channel.Reader.ReadAllAsync().Publish();
|
||||||
|
connection = multicastSource.Connect(); // Publish returns IConnectableUniTaskAsyncEnumerable.
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Publish(T value)
|
||||||
|
{
|
||||||
|
channel.Writer.TryWrite(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IUniTaskAsyncEnumerable<T> Subscribe()
|
||||||
|
{
|
||||||
|
return multicastSource;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
channel.Writer.TryComplete();
|
||||||
|
connection.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
For Unit Testing
|
For Unit Testing
|
||||||
---
|
---
|
||||||
@@ -342,46 +677,92 @@ public IEnumerator DelayIgnore() => UniTask.ToCoroutine(async () =>
|
|||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
Method List
|
UniTask itself's unit test is written by Unity Test Runner and [Cysharp/RuntimeUnitTestToolkit](https://github.com/Cysharp/RuntimeUnitTestToolkit) to check on CI and IL2CPP working.
|
||||||
|
|
||||||
|
Pooling Configuration
|
||||||
---
|
---
|
||||||
|
UniTask is aggressively caching async promise object to achive zero allocation. In default, cache all promises but you can configure `TaskPool.SetMaxPoolSize` to your value, the value indicates cache size per type. `TaskPool.GetCacheSizeInfo` returns current cached object in pool.
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
UniTask.WaitUntil
|
foreach (var (type, size) in TaskPool.GetCacheSizeInfo())
|
||||||
UniTask.WaitWhile
|
{
|
||||||
UniTask.WaitUntilValueChanged
|
Debug.Log(type + ":" + size);
|
||||||
UniTask.SwitchToThreadPool
|
}
|
||||||
UniTask.SwitchToTaskPool
|
|
||||||
UniTask.SwitchToMainThread
|
|
||||||
UniTask.SwitchToSynchronizationContext
|
|
||||||
UniTask.Yield
|
|
||||||
UniTask.Run
|
|
||||||
UniTask.Lazy
|
|
||||||
UniTask.Void
|
|
||||||
UniTask.ConfigureAwait
|
|
||||||
UniTask.DelayFrame
|
|
||||||
UniTask.Delay(..., bool ignoreTimeScale = false, ...) parameter
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
API References
|
||||||
|
---
|
||||||
|
UniTask's API References is hosted at [cysharp.github.io/UniTask](https://cysharp.github.io/UniTask/api/Cysharp.Threading.Tasks.html) by [DocFX](https://dotnet.github.io/docfx/) and [Cysharp/DocfXTemplate](https://github.com/Cysharp/DocfxTemplate).
|
||||||
|
|
||||||
|
For example, UniTask's factory methods can see at [UniTask#methods](https://cysharp.github.io/UniTask/api/Cysharp.Threading.Tasks.UniTask.html#methods-1). UniTaskAsyncEnumerable's factory/extension methods can see at [UniTaskAsyncEnumerable#methods](https://cysharp.github.io/UniTask/api/Cysharp.Threading.Tasks.Linq.UniTaskAsyncEnumerable.html#methods-1).
|
||||||
|
|
||||||
UPM Package
|
UPM Package
|
||||||
---
|
---
|
||||||
After Unity 2019.3.4f1, Unity 2020.1a21, that support path query parameter of git package. You can add `https://github.com/Cysharp/UniTask.git?path=Assets/UniRx.Async` to Package Manager
|
### Install via git URL
|
||||||
|
|
||||||
|
After Unity 2019.3.4f1, Unity 2020.1a21, that support path query parameter of git package. You can add `https://github.com/Cysharp/UniTask.git?path=src/UniTask/Assets/Plugins/UniTask` to Package Manager
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
or add `"com.cysharp.unitask": "https://github.com/Cysharp/UniTask.git?path=Assets/UniRx.Async"` to `Packages/manifest.json`.
|
or add `"com.cysharp.unitask": "https://github.com/Cysharp/UniTask.git?path=src/UniTask/Assets/Plugins/UniTask"` to `Packages/manifest.json`.
|
||||||
|
|
||||||
If you want to set a target version, UniTask is using `*.*.*` release tag so you can specify a version like `#1.3.0`. For example `https://github.com/Cysharp/UniTask.git?path=Assets/UniRx.Async#1.3.1`.
|
If you want to set a target version, UniTask is using `*.*.*` release tag so you can specify a version like `#2.0.13`. For example `https://github.com/Cysharp/UniTask.git?path=src/UniTask/Assets/Plugins/UniTask#2.0.13`.
|
||||||
|
|
||||||
ECS, PlayerLoop
|
### Install via OpenUPM
|
||||||
|
|
||||||
|
The package is available on the [openupm registry](https://openupm.com). It's recommended to install it via [openupm-cli](https://github.com/openupm/openupm-cli).
|
||||||
|
|
||||||
|
```
|
||||||
|
openupm add com.cysharp.unitask
|
||||||
|
```
|
||||||
|
|
||||||
|
.NET Core
|
||||||
---
|
---
|
||||||
TODO:
|
For .NET Core, use NuGet.
|
||||||
|
|
||||||
|
> PM> Install-Package [UniTask](https://www.nuget.org/packages/UniTask)
|
||||||
|
|
||||||
|
UniTask of .NET Core version is a subset of Unity UniTask, removed PlayerLoop dependent methods.
|
||||||
|
|
||||||
|
It runs at higher performance than the standard Task/ValueTask, but you should be careful to ignore the ExecutionContext/SynchronizationContext when using it. `AysncLocal` also does not work because it ignores ExecutionContext.
|
||||||
|
|
||||||
|
If you use UniTask internally, but provide ValueTask as an external API, you can write like the following(Inspired by [PooledAwait](https://github.com/mgravell/PooledAwait)).
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
var loop = PlayerLoop.GetCurrentPlayerLoop();
|
public class ZeroAllocAsyncAwaitInDotNetCore
|
||||||
PlayerLoopHelper.Initialize(ref loop);
|
{
|
||||||
|
public ValueTask<int> DoAsync(int x, int y)
|
||||||
|
{
|
||||||
|
return Core(this, x, y);
|
||||||
|
|
||||||
|
static async UniTask<int> Core(ZeroAllocAsyncAwaitInDotNetCore self, int x, int y)
|
||||||
|
{
|
||||||
|
// do anything...
|
||||||
|
await Task.Delay(TimeSpan.FromSeconds(x + y));
|
||||||
|
await UniTask.Yield();
|
||||||
|
|
||||||
|
return 10;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// UniTask does not return to original SynchronizationContext but you can use helper `ReturnToCurrentSynchronizationContext`.
|
||||||
|
public ValueTask TestAsync()
|
||||||
|
{
|
||||||
|
await using (UniTask.ReturnToCurrentSynchronizationContext())
|
||||||
|
{
|
||||||
|
await UniTask.SwitchToThreadPool();
|
||||||
|
// do anything..
|
||||||
|
}
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
.NET Core version is intended to allow users to use UniTask as an interface when sharing code with Unity (such as [Cysharp/MagicOnion](https://github.com/Cysharp/MagicOnion/)). .NET Core version of UniTask enables smooth code sharing.
|
||||||
|
|
||||||
|
Utility methods such as WhenAll which is equivalent to UniTask are provided as [Cysharp/ValueTaskSupplement](https://github.com/Cysharp/ValueTaskSupplement).
|
||||||
|
|
||||||
License
|
License
|
||||||
---
|
---
|
||||||
This library is under the MIT License.
|
This library is under the MIT License.
|
||||||
|
|||||||
10
docs/.gitignore
vendored
Normal file
10
docs/.gitignore
vendored
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
###############
|
||||||
|
# folder #
|
||||||
|
###############
|
||||||
|
/**/DROP/
|
||||||
|
/**/TEMP/
|
||||||
|
/**/packages/
|
||||||
|
/**/bin/
|
||||||
|
/**/obj/
|
||||||
|
_site
|
||||||
|
_DocfxTemplate
|
||||||
5
docs/api/.gitignore
vendored
Normal file
5
docs/api/.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
###############
|
||||||
|
# temp file #
|
||||||
|
###############
|
||||||
|
*.yml
|
||||||
|
.manifest
|
||||||
70
docs/docfx.json
Normal file
70
docs/docfx.json
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
{
|
||||||
|
"metadata": [
|
||||||
|
{
|
||||||
|
"src": [
|
||||||
|
{
|
||||||
|
"files": [
|
||||||
|
"UniTask/Assets/Plugins/UniTask/Runtime/**/*.cs"
|
||||||
|
],
|
||||||
|
"src": "../src"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"dest": "api",
|
||||||
|
"disableGitFeatures": false,
|
||||||
|
"disableDefaultFilter": false
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"build": {
|
||||||
|
"globalMetadata": {
|
||||||
|
"_disableContribution": true,
|
||||||
|
"_appTitle": "UniTask"
|
||||||
|
},
|
||||||
|
"content": [
|
||||||
|
{
|
||||||
|
"files": [
|
||||||
|
"api/**.yml",
|
||||||
|
"api/index.md"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"files": [
|
||||||
|
"articles/**.md",
|
||||||
|
"articles/**/toc.yml",
|
||||||
|
"toc.yml",
|
||||||
|
"*.md"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"resource": [
|
||||||
|
{
|
||||||
|
"files": [
|
||||||
|
"images/**"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"overwrite": [
|
||||||
|
{
|
||||||
|
"files": [
|
||||||
|
"apidoc/**.md"
|
||||||
|
],
|
||||||
|
"exclude": [
|
||||||
|
"obj/**",
|
||||||
|
"_site/**"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"dest": "_site",
|
||||||
|
|
||||||
|
"globalMetadataFiles": [],
|
||||||
|
"fileMetadataFiles": [],
|
||||||
|
"template": [
|
||||||
|
"_DocfxTemplate/templates/default-v2.5.2",
|
||||||
|
"_DocfxTemplate/templates/cysharp"
|
||||||
|
],
|
||||||
|
"postProcessors": [],
|
||||||
|
"markdownEngineName": "markdig",
|
||||||
|
"noLangKeyword": false,
|
||||||
|
"keepFileLink": false,
|
||||||
|
"cleanupCacheHistory": false
|
||||||
|
}
|
||||||
|
}
|
||||||
8
docs/index.md
Normal file
8
docs/index.md
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
---
|
||||||
|
title: Home
|
||||||
|
---
|
||||||
|
# UniTask
|
||||||
|
|
||||||
|
Provides an efficient async/await integration to Unity.
|
||||||
|
|
||||||
|
https://github.com/Cysharp/UniTask
|
||||||
11
docs/toc.yml
Normal file
11
docs/toc.yml
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
- name: API Documentation
|
||||||
|
href: api/
|
||||||
|
homepage: api/Cysharp.Threading.Tasks.html
|
||||||
|
|
||||||
|
- name: Repository
|
||||||
|
href: https://github.com/Cysharp/UniTask
|
||||||
|
homepage: https://github.com/Cysharp/UniTask
|
||||||
|
|
||||||
|
- name: Releases
|
||||||
|
href: https://github.com/Cysharp/UniTask/releases
|
||||||
|
homepage: https://github.com/Cysharp/UniTask/releases
|
||||||
BIN
src/UniTask.NetCore/Icon.png
Normal file
BIN
src/UniTask.NetCore/Icon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.1 KiB |
@@ -1,7 +1,6 @@
|
|||||||
#pragma warning disable 0649
|
#pragma warning disable 0649
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Threading.Tasks.Sources;
|
using System.Threading.Tasks.Sources;
|
||||||
|
|
||||||
@@ -9,115 +8,91 @@ namespace Cysharp.Threading.Tasks
|
|||||||
{
|
{
|
||||||
public static class UniTaskValueTaskExtensions
|
public static class UniTaskValueTaskExtensions
|
||||||
{
|
{
|
||||||
public static ValueTask AsValueTask(this UniTask task)
|
public static ValueTask AsValueTask(this in UniTask task)
|
||||||
{
|
{
|
||||||
ref var core = ref Unsafe.As<UniTask, UniTaskToValueTask>(ref task);
|
#if NETSTANDARD2_0
|
||||||
if (core.source == null)
|
return new ValueTask(new UniTaskValueTaskSource(task), 0);
|
||||||
{
|
#else
|
||||||
return default;
|
return task;
|
||||||
}
|
#endif
|
||||||
|
|
||||||
return new ValueTask(new UniTaskValueTaskSource(core.source), core.token);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ValueTask<T> AsValueTask<T>(this UniTask<T> task)
|
public static ValueTask<T> AsValueTask<T>(this in UniTask<T> task)
|
||||||
{
|
{
|
||||||
ref var core = ref Unsafe.As<UniTask<T>, UniTaskToValueTask<T>>(ref task);
|
#if NETSTANDARD2_0
|
||||||
if (core.source == null)
|
return new ValueTask<T>(new UniTaskValueTaskSource<T>(task), 0);
|
||||||
{
|
#else
|
||||||
return new ValueTask<T>(core.result);
|
return task;
|
||||||
}
|
#endif
|
||||||
|
|
||||||
return new ValueTask<T>(new UniTaskValueTaskSource<T>(core.source), core.token);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct UniTaskToValueTask
|
public static UniTask<T> AsUniTask<T>(this ValueTask<T> task, bool useCurrentSynchronizationContext = true)
|
||||||
{
|
{
|
||||||
public IUniTaskSource source;
|
// NOTE: get _obj and _token directly for low overhead conversion but not yet implemented.
|
||||||
public short token;
|
return task.AsTask().AsUniTask(useCurrentSynchronizationContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static UniTask AsUniTask(this ValueTask task, bool useCurrentSynchronizationContext = true)
|
||||||
|
{
|
||||||
|
return task.AsTask().AsUniTask(useCurrentSynchronizationContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
#if NETSTANDARD2_0
|
||||||
|
|
||||||
class UniTaskValueTaskSource : IValueTaskSource
|
class UniTaskValueTaskSource : IValueTaskSource
|
||||||
{
|
{
|
||||||
readonly IUniTaskSource source;
|
readonly UniTask task;
|
||||||
|
readonly UniTask.Awaiter awaiter;
|
||||||
|
|
||||||
public UniTaskValueTaskSource(IUniTaskSource source)
|
public UniTaskValueTaskSource(UniTask task)
|
||||||
{
|
{
|
||||||
this.source = source;
|
this.task = task;
|
||||||
|
this.awaiter = task.GetAwaiter();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void GetResult(short token)
|
public void GetResult(short token)
|
||||||
{
|
{
|
||||||
source.GetResult(token);
|
awaiter.GetResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ValueTaskSourceStatus GetStatus(short token)
|
public ValueTaskSourceStatus GetStatus(short token)
|
||||||
{
|
{
|
||||||
var status = source.GetStatus(token);
|
return (ValueTaskSourceStatus)task.Status;
|
||||||
switch (status)
|
|
||||||
{
|
|
||||||
case UniTaskStatus.Pending:
|
|
||||||
return ValueTaskSourceStatus.Pending;
|
|
||||||
case UniTaskStatus.Succeeded:
|
|
||||||
return ValueTaskSourceStatus.Succeeded;
|
|
||||||
case UniTaskStatus.Faulted:
|
|
||||||
return ValueTaskSourceStatus.Faulted;
|
|
||||||
case UniTaskStatus.Canceled:
|
|
||||||
return ValueTaskSourceStatus.Canceled;
|
|
||||||
default:
|
|
||||||
return (ValueTaskSourceStatus)status;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnCompleted(Action<object> continuation, object state, short token, ValueTaskSourceOnCompletedFlags flags)
|
public void OnCompleted(Action<object> continuation, object state, short token, ValueTaskSourceOnCompletedFlags flags)
|
||||||
{
|
{
|
||||||
source.OnCompleted(continuation, state, token);
|
awaiter.SourceOnCompleted(continuation, state);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct UniTaskToValueTask<T>
|
|
||||||
{
|
|
||||||
public IUniTaskSource<T> source;
|
|
||||||
public T result;
|
|
||||||
public short token;
|
|
||||||
}
|
|
||||||
|
|
||||||
class UniTaskValueTaskSource<T> : IValueTaskSource<T>
|
class UniTaskValueTaskSource<T> : IValueTaskSource<T>
|
||||||
{
|
{
|
||||||
readonly IUniTaskSource<T> source;
|
readonly UniTask<T> task;
|
||||||
|
readonly UniTask<T>.Awaiter awaiter;
|
||||||
|
|
||||||
public UniTaskValueTaskSource(IUniTaskSource<T> source)
|
public UniTaskValueTaskSource(UniTask<T> task)
|
||||||
{
|
{
|
||||||
this.source = source;
|
this.task = task;
|
||||||
|
this.awaiter = task.GetAwaiter();
|
||||||
}
|
}
|
||||||
|
|
||||||
public T GetResult(short token)
|
public T GetResult(short token)
|
||||||
{
|
{
|
||||||
return source.GetResult(token);
|
return awaiter.GetResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ValueTaskSourceStatus GetStatus(short token)
|
public ValueTaskSourceStatus GetStatus(short token)
|
||||||
{
|
{
|
||||||
var status = source.GetStatus(token);
|
return (ValueTaskSourceStatus)task.Status;
|
||||||
switch (status)
|
|
||||||
{
|
|
||||||
case UniTaskStatus.Pending:
|
|
||||||
return ValueTaskSourceStatus.Pending;
|
|
||||||
case UniTaskStatus.Succeeded:
|
|
||||||
return ValueTaskSourceStatus.Succeeded;
|
|
||||||
case UniTaskStatus.Faulted:
|
|
||||||
return ValueTaskSourceStatus.Faulted;
|
|
||||||
case UniTaskStatus.Canceled:
|
|
||||||
return ValueTaskSourceStatus.Canceled;
|
|
||||||
default:
|
|
||||||
return (ValueTaskSourceStatus)status;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnCompleted(Action<object> continuation, object state, short token, ValueTaskSourceOnCompletedFlags flags)
|
public void OnCompleted(Action<object> continuation, object state, short token, ValueTaskSourceOnCompletedFlags flags)
|
||||||
{
|
{
|
||||||
source.OnCompleted(continuation, state, token);
|
awaiter.SourceOnCompleted(continuation, state);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
39
src/UniTask.NetCore/NetCore/UniTask.Delay.cs
Normal file
39
src/UniTask.NetCore/NetCore/UniTask.Delay.cs
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
//using Cysharp.Threading.Tasks.Internal;
|
||||||
|
//using System;
|
||||||
|
//using System.Collections.Concurrent;
|
||||||
|
//using System.Runtime.CompilerServices;
|
||||||
|
//using System.Threading;
|
||||||
|
|
||||||
|
//namespace Cysharp.Threading.Tasks
|
||||||
|
//{
|
||||||
|
// public partial struct UniTask
|
||||||
|
// {
|
||||||
|
// public static UniTask Delay()
|
||||||
|
// {
|
||||||
|
// return default;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// sealed class DelayPromise : IUniTaskSource
|
||||||
|
// {
|
||||||
|
// public void GetResult(short token)
|
||||||
|
// {
|
||||||
|
// throw new NotImplementedException();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// public UniTaskStatus GetStatus(short token)
|
||||||
|
// {
|
||||||
|
// throw new NotImplementedException();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// public void OnCompleted(Action<object> continuation, object state, short token)
|
||||||
|
// {
|
||||||
|
// throw new NotImplementedException();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// public UniTaskStatus UnsafeGetStatus()
|
||||||
|
// {
|
||||||
|
// throw new NotImplementedException();
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//}
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
using System;
|
using Cysharp.Threading.Tasks.Internal;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
|
||||||
@@ -41,7 +43,11 @@ namespace Cysharp.Threading.Tasks
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
#if NETCOREAPP3_1
|
||||||
|
ThreadPool.UnsafeQueueUserWorkItem(ThreadPoolWorkItem.Create(continuation), false);
|
||||||
|
#else
|
||||||
ThreadPool.UnsafeQueueUserWorkItem(WaitCallbackDelegate, continuation);
|
ThreadPool.UnsafeQueueUserWorkItem(WaitCallbackDelegate, continuation);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,6 +56,47 @@ namespace Cysharp.Threading.Tasks
|
|||||||
((Action)state).Invoke();
|
((Action)state).Invoke();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if NETCOREAPP3_1
|
||||||
|
|
||||||
|
sealed class ThreadPoolWorkItem : IThreadPoolWorkItem, ITaskPoolNode<ThreadPoolWorkItem>
|
||||||
|
{
|
||||||
|
static TaskPool<ThreadPoolWorkItem> pool;
|
||||||
|
public ThreadPoolWorkItem NextNode { get; set; }
|
||||||
|
|
||||||
|
static ThreadPoolWorkItem()
|
||||||
|
{
|
||||||
|
TaskPool.RegisterSizeGetter(typeof(ThreadPoolWorkItem), () => pool.Size);
|
||||||
|
}
|
||||||
|
|
||||||
|
Action continuation;
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static ThreadPoolWorkItem Create(Action continuation)
|
||||||
|
{
|
||||||
|
if (!pool.TryPop(out var item))
|
||||||
|
{
|
||||||
|
item = new ThreadPoolWorkItem();
|
||||||
|
}
|
||||||
|
|
||||||
|
item.continuation = continuation;
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void Execute()
|
||||||
|
{
|
||||||
|
var call = continuation;
|
||||||
|
continuation = null;
|
||||||
|
if (call != null)
|
||||||
|
{
|
||||||
|
pool.TryPush(this);
|
||||||
|
call.Invoke();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,11 +1,30 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
<TargetFrameworks>netcoreapp3.1;netstandard2.1;netstandard2.0</TargetFrameworks>
|
||||||
<AssemblyName>UniTask</AssemblyName>
|
<AssemblyName>UniTask</AssemblyName>
|
||||||
|
<LangVersion>8.0</LangVersion>
|
||||||
<RootNamespace>Cysharp.Threading.Tasks</RootNamespace>
|
<RootNamespace>Cysharp.Threading.Tasks</RootNamespace>
|
||||||
|
|
||||||
|
<!-- NuGet Packaging -->
|
||||||
|
<Id>UniTask</Id>
|
||||||
|
<PackageVersion>$(Version)</PackageVersion>
|
||||||
|
<Company>Cysharp</Company>
|
||||||
|
<Authors>Cysharp</Authors>
|
||||||
|
<Copyright>© Cysharp, Inc.</Copyright>
|
||||||
|
<PackageTags>task;async</PackageTags>
|
||||||
|
<Description>Provides an efficient async/await integration to Unity and .NET Core.</Description>
|
||||||
|
<PackageProjectUrl>https://github.com/Cysharp/UniTask</PackageProjectUrl>
|
||||||
|
<RepositoryUrl>$(PackageProjectUrl)</RepositoryUrl>
|
||||||
|
<RepositoryType>git</RepositoryType>
|
||||||
|
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||||
|
<PackageIcon>Icon.png</PackageIcon>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Include="Icon.png" Pack="true" PackagePath="/" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="..\UniTask\Assets\Plugins\UniTask\Runtime\**\*.cs"
|
<Compile Include="..\UniTask\Assets\Plugins\UniTask\Runtime\**\*.cs"
|
||||||
Exclude="
|
Exclude="
|
||||||
|
|||||||
258
src/UniTask.NetCoreSandbox/AllocationCheck.cs
Normal file
258
src/UniTask.NetCoreSandbox/AllocationCheck.cs
Normal file
@@ -0,0 +1,258 @@
|
|||||||
|
using BenchmarkDotNet.Attributes;
|
||||||
|
using System.Linq;
|
||||||
|
using BenchmarkDotNet.Configs;
|
||||||
|
using BenchmarkDotNet.Diagnosers;
|
||||||
|
using BenchmarkDotNet.Exporters;
|
||||||
|
using BenchmarkDotNet.Jobs;
|
||||||
|
using BenchmarkDotNet.Running;
|
||||||
|
using Cysharp.Threading.Tasks;
|
||||||
|
using PooledAwait;
|
||||||
|
using System;
|
||||||
|
using System.Runtime.ExceptionServices;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using Cysharp.Threading.Tasks.CompilerServices;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
|
||||||
|
[Config(typeof(BenchmarkConfig))]
|
||||||
|
public class AllocationCheck
|
||||||
|
{
|
||||||
|
// note: all the benchmarks use Task/Task<T> for the public API, because BenchmarkDotNet
|
||||||
|
// doesn't work reliably with more exotic task-types (even just ValueTask fails); instead,
|
||||||
|
// we'll obscure the cost of the outer awaitable by doing a relatively large number of
|
||||||
|
// iterations, so that we're only really measuring the inner loop
|
||||||
|
private const int InnerOps = 1000;
|
||||||
|
|
||||||
|
[Benchmark(OperationsPerInvoke = InnerOps)]
|
||||||
|
public async Task ViaUniTask()
|
||||||
|
{
|
||||||
|
for (int i = 0; i < InnerOps; i++)
|
||||||
|
{
|
||||||
|
var a = Core();
|
||||||
|
var b = Core();
|
||||||
|
var c = Core();
|
||||||
|
await a;
|
||||||
|
await b;
|
||||||
|
await c;
|
||||||
|
}
|
||||||
|
|
||||||
|
static async UniTask Core()
|
||||||
|
{
|
||||||
|
await new TestAwaiter(false, UniTaskStatus.Succeeded);
|
||||||
|
await new TestAwaiter(false, UniTaskStatus.Succeeded);
|
||||||
|
await new TestAwaiter(false, UniTaskStatus.Succeeded);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Benchmark(OperationsPerInvoke = InnerOps)]
|
||||||
|
public async Task<int> ViaUniTaskT()
|
||||||
|
{
|
||||||
|
var sum = 0;
|
||||||
|
for (int i = 0; i < InnerOps; i++)
|
||||||
|
{
|
||||||
|
var a = Core();
|
||||||
|
var b = Core();
|
||||||
|
var c = Core();
|
||||||
|
sum += await a;
|
||||||
|
sum += await b;
|
||||||
|
sum += await c;
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
|
||||||
|
static async UniTask<int> Core()
|
||||||
|
{
|
||||||
|
var a = await new TestAwaiter<int>(false, UniTaskStatus.Succeeded, 10);
|
||||||
|
var b = await new TestAwaiter<int>(false, UniTaskStatus.Succeeded, 10);
|
||||||
|
var c = await new TestAwaiter<int>(false, UniTaskStatus.Succeeded, 10);
|
||||||
|
return 10;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//[Benchmark(OperationsPerInvoke = InnerOps)]
|
||||||
|
//[Benchmark]
|
||||||
|
public void ViaUniTaskVoid()
|
||||||
|
{
|
||||||
|
for (int i = 0; i < InnerOps; i++)
|
||||||
|
{
|
||||||
|
Core().Forget();
|
||||||
|
Core().Forget();
|
||||||
|
Core().Forget();
|
||||||
|
}
|
||||||
|
|
||||||
|
static async UniTaskVoid Core()
|
||||||
|
{
|
||||||
|
await new TestAwaiter(false, UniTaskStatus.Succeeded);
|
||||||
|
await new TestAwaiter(false, UniTaskStatus.Succeeded);
|
||||||
|
await new TestAwaiter(false, UniTaskStatus.Succeeded);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Foo : IAsyncStateMachine
|
||||||
|
{
|
||||||
|
public AsyncUniTaskVoidMethodBuilder builder;
|
||||||
|
public TestAwaiter awaiter;
|
||||||
|
public TestAwaiter awaiterawaiter;
|
||||||
|
|
||||||
|
public int state;
|
||||||
|
|
||||||
|
public void MoveNext()
|
||||||
|
{
|
||||||
|
switch (state)
|
||||||
|
{
|
||||||
|
case -1:
|
||||||
|
awaiterawaiter = awaiter.GetAwaiter();
|
||||||
|
if (awaiterawaiter.IsCompleted)
|
||||||
|
{
|
||||||
|
goto case 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 0;
|
||||||
|
builder.AwaitUnsafeOnCompleted(ref awaiterawaiter, ref this);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
case 0:
|
||||||
|
default:
|
||||||
|
goto END;
|
||||||
|
}
|
||||||
|
|
||||||
|
END:
|
||||||
|
builder.SetResult();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetStateMachine(IAsyncStateMachine stateMachine)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class TaskTestException : Exception
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct TestAwaiter : ICriticalNotifyCompletion
|
||||||
|
{
|
||||||
|
readonly UniTaskStatus status;
|
||||||
|
readonly bool isCompleted;
|
||||||
|
|
||||||
|
public TestAwaiter(bool isCompleted, UniTaskStatus status)
|
||||||
|
{
|
||||||
|
this.isCompleted = isCompleted;
|
||||||
|
this.status = status;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TestAwaiter GetAwaiter() => this;
|
||||||
|
|
||||||
|
public bool IsCompleted => isCompleted;
|
||||||
|
|
||||||
|
public void GetResult()
|
||||||
|
{
|
||||||
|
switch (status)
|
||||||
|
{
|
||||||
|
case UniTaskStatus.Faulted:
|
||||||
|
throw new TaskTestException();
|
||||||
|
case UniTaskStatus.Canceled:
|
||||||
|
throw new OperationCanceledException();
|
||||||
|
case UniTaskStatus.Pending:
|
||||||
|
case UniTaskStatus.Succeeded:
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnCompleted(Action continuation)
|
||||||
|
{
|
||||||
|
ThreadPool.UnsafeQueueUserWorkItem(ThreadPoolWorkItem.Create(continuation), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UnsafeOnCompleted(Action continuation)
|
||||||
|
{
|
||||||
|
ThreadPool.UnsafeQueueUserWorkItem(ThreadPoolWorkItem.Create(continuation), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct TestAwaiter<T> : ICriticalNotifyCompletion
|
||||||
|
{
|
||||||
|
readonly UniTaskStatus status;
|
||||||
|
readonly bool isCompleted;
|
||||||
|
readonly T value;
|
||||||
|
|
||||||
|
public TestAwaiter(bool isCompleted, UniTaskStatus status, T value)
|
||||||
|
{
|
||||||
|
this.isCompleted = isCompleted;
|
||||||
|
this.status = status;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TestAwaiter<T> GetAwaiter() => this;
|
||||||
|
|
||||||
|
public bool IsCompleted => isCompleted;
|
||||||
|
|
||||||
|
public T GetResult()
|
||||||
|
{
|
||||||
|
switch (status)
|
||||||
|
{
|
||||||
|
case UniTaskStatus.Faulted:
|
||||||
|
throw new TaskTestException();
|
||||||
|
case UniTaskStatus.Canceled:
|
||||||
|
throw new OperationCanceledException();
|
||||||
|
case UniTaskStatus.Pending:
|
||||||
|
case UniTaskStatus.Succeeded:
|
||||||
|
default:
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnCompleted(Action continuation)
|
||||||
|
{
|
||||||
|
ThreadPool.UnsafeQueueUserWorkItem(ThreadPoolWorkItem.Create(continuation), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UnsafeOnCompleted(Action continuation)
|
||||||
|
{
|
||||||
|
ThreadPool.UnsafeQueueUserWorkItem(ThreadPoolWorkItem.Create(continuation), false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public sealed class ThreadPoolWorkItem : IThreadPoolWorkItem
|
||||||
|
{
|
||||||
|
public static readonly ConcurrentQueue<ThreadPoolWorkItem> pool = new ConcurrentQueue<ThreadPoolWorkItem>();
|
||||||
|
|
||||||
|
public static void CreatePoolItems(int count)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < count; i++)
|
||||||
|
{
|
||||||
|
pool.Enqueue(new ThreadPoolWorkItem());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Action continuation;
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static ThreadPoolWorkItem Create(Action continuation)
|
||||||
|
{
|
||||||
|
if (!pool.TryDequeue(out var item))
|
||||||
|
{
|
||||||
|
item = new ThreadPoolWorkItem();
|
||||||
|
}
|
||||||
|
|
||||||
|
item.continuation = continuation;
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void Execute()
|
||||||
|
{
|
||||||
|
var call = continuation;
|
||||||
|
continuation = null;
|
||||||
|
pool.Enqueue(this);
|
||||||
|
|
||||||
|
call.Invoke();
|
||||||
|
}
|
||||||
|
}
|
||||||
283
src/UniTask.NetCoreSandbox/Benchmark.cs
Normal file
283
src/UniTask.NetCoreSandbox/Benchmark.cs
Normal file
@@ -0,0 +1,283 @@
|
|||||||
|
using BenchmarkDotNet.Attributes;
|
||||||
|
using System.Linq;
|
||||||
|
using BenchmarkDotNet.Configs;
|
||||||
|
using BenchmarkDotNet.Diagnosers;
|
||||||
|
using BenchmarkDotNet.Exporters;
|
||||||
|
using BenchmarkDotNet.Jobs;
|
||||||
|
using BenchmarkDotNet.Running;
|
||||||
|
using Cysharp.Threading.Tasks;
|
||||||
|
using PooledAwait;
|
||||||
|
using System;
|
||||||
|
using System.Runtime.ExceptionServices;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using Cysharp.Threading.Tasks.CompilerServices;
|
||||||
|
|
||||||
|
//class Program
|
||||||
|
//{
|
||||||
|
// static void Main(string[] args)
|
||||||
|
// {
|
||||||
|
// var switcher = new BenchmarkSwitcher(new[]
|
||||||
|
// {
|
||||||
|
// typeof(StandardBenchmark)
|
||||||
|
// });
|
||||||
|
|
||||||
|
//#if DEBUG
|
||||||
|
// var b = new StandardBenchmark();
|
||||||
|
|
||||||
|
//#else
|
||||||
|
// switcher.Run(args);
|
||||||
|
//#endif
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
||||||
|
public class BenchmarkConfig : ManualConfig
|
||||||
|
{
|
||||||
|
public BenchmarkConfig()
|
||||||
|
{
|
||||||
|
AddDiagnoser(MemoryDiagnoser.Default);
|
||||||
|
AddJob(Job.ShortRun.WithLaunchCount(1).WithIterationCount(1).WithWarmupCount(1)/*.RunOncePerIteration()*/);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// borrowed from PooledAwait
|
||||||
|
|
||||||
|
[Config(typeof(BenchmarkConfig))]
|
||||||
|
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)]
|
||||||
|
[CategoriesColumn]
|
||||||
|
public class ComparisonBenchmarks
|
||||||
|
{
|
||||||
|
// note: all the benchmarks use Task/Task<T> for the public API, because BenchmarkDotNet
|
||||||
|
// doesn't work reliably with more exotic task-types (even just ValueTask fails); instead,
|
||||||
|
// we'll obscure the cost of the outer awaitable by doing a relatively large number of
|
||||||
|
// iterations, so that we're only really measuring the inner loop
|
||||||
|
private const int InnerOps = 1000;
|
||||||
|
|
||||||
|
public bool ConfigureAwait { get; set; } = false;
|
||||||
|
|
||||||
|
[Benchmark(OperationsPerInvoke = InnerOps, Description = ".NET")]
|
||||||
|
[BenchmarkCategory("Task<T>")]
|
||||||
|
public async Task<int> ViaTaskT()
|
||||||
|
{
|
||||||
|
int sum = 0;
|
||||||
|
for (int i = 0; i < InnerOps; i++)
|
||||||
|
sum += await Inner(1, 2).ConfigureAwait(ConfigureAwait);
|
||||||
|
return sum;
|
||||||
|
|
||||||
|
static async Task<int> Inner(int x, int y)
|
||||||
|
{
|
||||||
|
int i = x;
|
||||||
|
await Task.Yield();
|
||||||
|
i *= y;
|
||||||
|
await Task.Yield();
|
||||||
|
return 5 * i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Benchmark(OperationsPerInvoke = InnerOps, Description = ".NET")]
|
||||||
|
[BenchmarkCategory("Task")]
|
||||||
|
public async Task ViaTask()
|
||||||
|
{
|
||||||
|
for (int i = 0; i < InnerOps; i++)
|
||||||
|
await Inner().ConfigureAwait(ConfigureAwait);
|
||||||
|
|
||||||
|
static async Task Inner()
|
||||||
|
{
|
||||||
|
await Task.Yield();
|
||||||
|
await Task.Yield();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Benchmark(OperationsPerInvoke = InnerOps, Description = ".NET")]
|
||||||
|
[BenchmarkCategory("ValueTask<T>")]
|
||||||
|
public async Task<int> ViaValueTaskT()
|
||||||
|
{
|
||||||
|
int sum = 0;
|
||||||
|
for (int i = 0; i < InnerOps; i++)
|
||||||
|
sum += await Inner(1, 2).ConfigureAwait(ConfigureAwait);
|
||||||
|
return sum;
|
||||||
|
|
||||||
|
static async ValueTask<int> Inner(int x, int y)
|
||||||
|
{
|
||||||
|
int i = x;
|
||||||
|
await Task.Yield();
|
||||||
|
i *= y;
|
||||||
|
await Task.Yield();
|
||||||
|
return 5 * i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Benchmark(OperationsPerInvoke = InnerOps, Description = ".NET")]
|
||||||
|
[BenchmarkCategory("ValueTask")]
|
||||||
|
public async Task ViaValueTask()
|
||||||
|
{
|
||||||
|
for (int i = 0; i < InnerOps; i++)
|
||||||
|
await Inner().ConfigureAwait(ConfigureAwait);
|
||||||
|
|
||||||
|
static async ValueTask Inner()
|
||||||
|
{
|
||||||
|
await Task.Yield();
|
||||||
|
await Task.Yield();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Benchmark(OperationsPerInvoke = InnerOps, Description = "Pooled")]
|
||||||
|
[BenchmarkCategory("ValueTask<T>")]
|
||||||
|
public async Task<int> ViaPooledValueTaskT()
|
||||||
|
{
|
||||||
|
int sum = 0;
|
||||||
|
for (int i = 0; i < InnerOps; i++)
|
||||||
|
sum += await Inner(1, 2).ConfigureAwait(ConfigureAwait);
|
||||||
|
return sum;
|
||||||
|
|
||||||
|
static async PooledValueTask<int> Inner(int x, int y)
|
||||||
|
{
|
||||||
|
int i = x;
|
||||||
|
await Task.Yield();
|
||||||
|
i *= y;
|
||||||
|
await Task.Yield();
|
||||||
|
return 5 * i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Benchmark(OperationsPerInvoke = InnerOps, Description = "Pooled")]
|
||||||
|
[BenchmarkCategory("ValueTask")]
|
||||||
|
public async Task ViaPooledValueTask()
|
||||||
|
{
|
||||||
|
for (int i = 0; i < InnerOps; i++)
|
||||||
|
await Inner().ConfigureAwait(ConfigureAwait);
|
||||||
|
|
||||||
|
static async PooledValueTask Inner()
|
||||||
|
{
|
||||||
|
await Task.Yield();
|
||||||
|
await Task.Yield();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Benchmark(OperationsPerInvoke = InnerOps, Description = "Pooled")]
|
||||||
|
[BenchmarkCategory("Task<T>")]
|
||||||
|
public async Task<int> ViaPooledTaskT()
|
||||||
|
{
|
||||||
|
int sum = 0;
|
||||||
|
for (int i = 0; i < InnerOps; i++)
|
||||||
|
sum += await Inner(1, 2).ConfigureAwait(ConfigureAwait);
|
||||||
|
return sum;
|
||||||
|
|
||||||
|
static async PooledTask<int> Inner(int x, int y)
|
||||||
|
{
|
||||||
|
int i = x;
|
||||||
|
await Task.Yield();
|
||||||
|
i *= y;
|
||||||
|
await Task.Yield();
|
||||||
|
return 5 * i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Benchmark(OperationsPerInvoke = InnerOps, Description = "Pooled")]
|
||||||
|
[BenchmarkCategory("Task")]
|
||||||
|
public async Task ViaPooledTask()
|
||||||
|
{
|
||||||
|
for (int i = 0; i < InnerOps; i++)
|
||||||
|
await Inner().ConfigureAwait(ConfigureAwait);
|
||||||
|
|
||||||
|
static async PooledTask Inner()
|
||||||
|
{
|
||||||
|
await Task.Yield();
|
||||||
|
await Task.Yield();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---
|
||||||
|
|
||||||
|
//[Benchmark(OperationsPerInvoke = InnerOps, Description = "UniTaskVoid")]
|
||||||
|
//[BenchmarkCategory("UniTask")]
|
||||||
|
//public async Task ViaUniTaskVoid()
|
||||||
|
//{
|
||||||
|
// for (int i = 0; i < InnerOps; i++)
|
||||||
|
// {
|
||||||
|
// await Inner();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// static async UniTaskVoid Inner()
|
||||||
|
// {
|
||||||
|
// await UniTask.Yield();
|
||||||
|
// await UniTask.Yield();
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
||||||
|
[Benchmark(OperationsPerInvoke = InnerOps, Description = "UniTask")]
|
||||||
|
[BenchmarkCategory("UniTask")]
|
||||||
|
public async Task ViaUniTask()
|
||||||
|
{
|
||||||
|
for (int i = 0; i < InnerOps; i++)
|
||||||
|
{
|
||||||
|
await Inner();
|
||||||
|
}
|
||||||
|
|
||||||
|
static async UniTask Inner()
|
||||||
|
{
|
||||||
|
await UniTask.Yield();
|
||||||
|
await UniTask.Yield();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Benchmark(OperationsPerInvoke = InnerOps, Description = "UniTaskT")]
|
||||||
|
[BenchmarkCategory("UniTask")]
|
||||||
|
public async Task<int> ViaUniTaskT()
|
||||||
|
{
|
||||||
|
var sum = 0;
|
||||||
|
for (int i = 0; i < InnerOps; i++)
|
||||||
|
{
|
||||||
|
sum += await Inner(1, 2);
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
|
||||||
|
static async UniTask<int> Inner(int x, int y)
|
||||||
|
{
|
||||||
|
int i = x;
|
||||||
|
await UniTask.Yield();
|
||||||
|
i *= y;
|
||||||
|
await UniTask.Yield();
|
||||||
|
return 5 * i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct MyAwaiter : ICriticalNotifyCompletion
|
||||||
|
{
|
||||||
|
public MyAwaiter GetAwaiter() => this;
|
||||||
|
|
||||||
|
public bool IsCompleted => false;
|
||||||
|
|
||||||
|
public void GetResult()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnCompleted(Action continuation)
|
||||||
|
{
|
||||||
|
continuation();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UnsafeOnCompleted(Action continuation)
|
||||||
|
{
|
||||||
|
continuation();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct MyTestStateMachine : IAsyncStateMachine
|
||||||
|
{
|
||||||
|
public void MoveNext()
|
||||||
|
{
|
||||||
|
//throw new NotImplementedException();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetStateMachine(IAsyncStateMachine stateMachine)
|
||||||
|
{
|
||||||
|
//throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,12 +15,132 @@ using System.Reactive.Concurrency;
|
|||||||
|
|
||||||
namespace NetCoreSandbox
|
namespace NetCoreSandbox
|
||||||
{
|
{
|
||||||
|
public class MySyncContext : SynchronizationContext
|
||||||
|
{
|
||||||
|
public MySyncContext()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Post(SendOrPostCallback d, object state)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Called SyncContext Post!");
|
||||||
|
base.Post(d, state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public class Text
|
public class Text
|
||||||
{
|
{
|
||||||
|
|
||||||
public string text { get; set; }
|
public string text { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class ZeroAllocAsyncAwaitInDotNetCore
|
||||||
|
{
|
||||||
|
public ValueTask<int> NanikaAsync(int x, int y)
|
||||||
|
{
|
||||||
|
return Core(this, x, y);
|
||||||
|
|
||||||
|
static async UniTask<int> Core(ZeroAllocAsyncAwaitInDotNetCore self, int x, int y)
|
||||||
|
{
|
||||||
|
// nanika suru...
|
||||||
|
await Task.Delay(TimeSpan.FromSeconds(x + y));
|
||||||
|
|
||||||
|
return 10;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public class TaskTestException : Exception
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public struct TestAwaiter : ICriticalNotifyCompletion
|
||||||
|
{
|
||||||
|
readonly UniTaskStatus status;
|
||||||
|
readonly bool isCompleted;
|
||||||
|
|
||||||
|
public TestAwaiter(bool isCompleted, UniTaskStatus status)
|
||||||
|
{
|
||||||
|
this.isCompleted = isCompleted;
|
||||||
|
this.status = status;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TestAwaiter GetAwaiter() => this;
|
||||||
|
|
||||||
|
public bool IsCompleted => isCompleted;
|
||||||
|
|
||||||
|
public void GetResult()
|
||||||
|
{
|
||||||
|
switch (status)
|
||||||
|
{
|
||||||
|
case UniTaskStatus.Faulted:
|
||||||
|
throw new TaskTestException();
|
||||||
|
case UniTaskStatus.Canceled:
|
||||||
|
throw new OperationCanceledException();
|
||||||
|
case UniTaskStatus.Pending:
|
||||||
|
case UniTaskStatus.Succeeded:
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnCompleted(Action continuation)
|
||||||
|
{
|
||||||
|
ThreadPool.QueueUserWorkItem(_ => continuation(), null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UnsafeOnCompleted(Action continuation)
|
||||||
|
{
|
||||||
|
ThreadPool.UnsafeQueueUserWorkItem(_ => continuation(), null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public struct TestAwaiter<T> : ICriticalNotifyCompletion
|
||||||
|
{
|
||||||
|
readonly UniTaskStatus status;
|
||||||
|
readonly bool isCompleted;
|
||||||
|
readonly T value;
|
||||||
|
|
||||||
|
public TestAwaiter(bool isCompleted, UniTaskStatus status, T value)
|
||||||
|
{
|
||||||
|
this.isCompleted = isCompleted;
|
||||||
|
this.status = status;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TestAwaiter<T> GetAwaiter() => this;
|
||||||
|
|
||||||
|
public bool IsCompleted => isCompleted;
|
||||||
|
|
||||||
|
public T GetResult()
|
||||||
|
{
|
||||||
|
switch (status)
|
||||||
|
{
|
||||||
|
case UniTaskStatus.Faulted:
|
||||||
|
throw new TaskTestException();
|
||||||
|
case UniTaskStatus.Canceled:
|
||||||
|
throw new OperationCanceledException();
|
||||||
|
case UniTaskStatus.Pending:
|
||||||
|
case UniTaskStatus.Succeeded:
|
||||||
|
default:
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnCompleted(Action continuation)
|
||||||
|
{
|
||||||
|
ThreadPool.QueueUserWorkItem(_ => continuation(), null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UnsafeOnCompleted(Action continuation)
|
||||||
|
{
|
||||||
|
ThreadPool.UnsafeQueueUserWorkItem(_ => continuation(), null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public static partial class UnityUIComponentExtensions
|
public static partial class UnityUIComponentExtensions
|
||||||
{
|
{
|
||||||
public static void BindTo(this IUniTaskAsyncEnumerable<string> source, Text text)
|
public static void BindTo(this IUniTaskAsyncEnumerable<string> source, Text text)
|
||||||
@@ -86,21 +206,144 @@ namespace NetCoreSandbox
|
|||||||
await Task.Delay(10, cancellationToken);
|
await Task.Delay(10, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class MyDisposable : IDisposable
|
||||||
|
|
||||||
static void Main(string[] args)
|
|
||||||
{
|
{
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
|
||||||
var channel = Channel.CreateSingleConsumerUnbounded<int>();
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static void Test()
|
||||||
|
{
|
||||||
|
var disp = new MyDisposable();
|
||||||
|
|
||||||
|
using var _ = new MyDisposable();
|
||||||
|
|
||||||
|
Console.WriteLine("tako");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static async UniTask FooBarAsync()
|
||||||
|
{
|
||||||
|
await using (UniTask.ReturnToCurrentSynchronizationContext())
|
||||||
|
{
|
||||||
|
await UniTask.SwitchToThreadPool();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static async UniTask Aaa()
|
||||||
|
{
|
||||||
|
await FooBarAsync();
|
||||||
|
|
||||||
|
Console.WriteLine("FooBarAsync End");
|
||||||
|
}
|
||||||
|
|
||||||
|
static async UniTask WhereSelect()
|
||||||
|
{
|
||||||
|
await foreach (var item in UniTaskAsyncEnumerable.Range(1, 10)
|
||||||
|
.SelectAwait(async x =>
|
||||||
|
{
|
||||||
|
await UniTask.Yield();
|
||||||
|
return x;
|
||||||
|
})
|
||||||
|
.Where(x => x % 2 == 0))
|
||||||
|
{
|
||||||
|
Console.WriteLine(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static async Task Main(string[] args)
|
||||||
|
{
|
||||||
|
#if !DEBUG
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//await new AllocationCheck().ViaUniTaskVoid();
|
||||||
|
//Console.ReadLine();
|
||||||
|
BenchmarkDotNet.Running.BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
|
||||||
|
|
||||||
|
//await new ComparisonBenchmarks().ViaUniTaskT();
|
||||||
|
return;
|
||||||
|
#endif
|
||||||
|
// await new AllocationCheck().ViaUniTaskVoid();
|
||||||
|
|
||||||
|
// AsyncTest().Forge
|
||||||
|
|
||||||
|
Console.WriteLine("A?");
|
||||||
|
var a = await new ZeroAllocAsyncAwaitInDotNetCore().NanikaAsync(1, 2);
|
||||||
|
Console.WriteLine("RET:" + a);
|
||||||
|
await WhereSelect();
|
||||||
|
|
||||||
|
SynchronizationContext.SetSynchronizationContext(new MySyncContext());
|
||||||
|
|
||||||
|
await Aaa();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//AsyncTest().Forget();
|
||||||
|
|
||||||
|
// AsyncTest().Forget();
|
||||||
|
|
||||||
|
ThreadPool.SetMinThreads(100, 100);
|
||||||
|
|
||||||
|
//List<UniTask<int>> list = new List<UniTask<int>>();
|
||||||
|
for (int i = 0; i < short.MaxValue; i++)
|
||||||
|
{
|
||||||
|
//// list.Add(AsyncTest());
|
||||||
|
await YieldCore();
|
||||||
|
}
|
||||||
|
//await UniTask.WhenAll(list);
|
||||||
|
|
||||||
|
//Console.WriteLine("TOGO");
|
||||||
|
|
||||||
|
//var a = await AsyncTest();
|
||||||
|
//var b = AsyncTest();
|
||||||
|
//var c = AsyncTest();
|
||||||
|
await YieldCore();
|
||||||
|
|
||||||
|
//await b;
|
||||||
|
//await c;
|
||||||
|
|
||||||
|
|
||||||
|
//foreach (var item in Cysharp.Threading.Tasks.Internal.TaskPool.GetCacheSizeInfo())
|
||||||
|
//{
|
||||||
|
// Console.WriteLine(item);
|
||||||
|
//}
|
||||||
|
|
||||||
|
Console.ReadLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
static async UniTask YieldCore()
|
||||||
|
{
|
||||||
|
await UniTask.Yield();
|
||||||
|
}
|
||||||
|
|
||||||
|
#pragma warning disable CS1998
|
||||||
|
|
||||||
|
|
||||||
|
static async UniTask<int> AsyncTest()
|
||||||
|
{
|
||||||
|
// empty
|
||||||
|
await new TestAwaiter(false, UniTaskStatus.Succeeded);
|
||||||
|
await new TestAwaiter(true, UniTaskStatus.Succeeded);
|
||||||
|
await new TestAwaiter(false, UniTaskStatus.Succeeded);
|
||||||
|
return 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#pragma warning restore CS1998
|
||||||
|
|
||||||
void Foo()
|
void Foo()
|
||||||
{
|
{
|
||||||
|
|||||||
396
src/UniTask.NetCoreSandbox/QueueCheck.cs
Normal file
396
src/UniTask.NetCoreSandbox/QueueCheck.cs
Normal file
@@ -0,0 +1,396 @@
|
|||||||
|
using BenchmarkDotNet.Attributes;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
[Config(typeof(BenchmarkConfig))]
|
||||||
|
public class QueueCheck
|
||||||
|
{
|
||||||
|
Node node1 = new Node();
|
||||||
|
Node node2 = new Node();
|
||||||
|
Queue<Node> q1 = new Queue<Node>();
|
||||||
|
Stack<Node> s1 = new Stack<Node>();
|
||||||
|
ConcurrentQueue<Node> cq = new ConcurrentQueue<Node>();
|
||||||
|
ConcurrentStack<Node> cs = new ConcurrentStack<Node>();
|
||||||
|
static TaskPool<Node> pool;
|
||||||
|
static TaskPoolEqualNull<Node> poolEqualNull;
|
||||||
|
static TaskPoolClass<Node> poolClass = new TaskPoolClass<Node>();
|
||||||
|
static TaskPoolWithoutSize<Node> poolWithoutSize;
|
||||||
|
static TaskPoolWithoutLock<Node> poolWithoutLock;
|
||||||
|
|
||||||
|
[Benchmark]
|
||||||
|
public void Queue()
|
||||||
|
{
|
||||||
|
q1.Enqueue(node1);
|
||||||
|
q1.Enqueue(node1);
|
||||||
|
q1.TryDequeue(out _);
|
||||||
|
q1.TryDequeue(out _);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Benchmark]
|
||||||
|
public void QueueLock()
|
||||||
|
{
|
||||||
|
lock (q1) { q1.Enqueue(node1); }
|
||||||
|
lock (q1) { q1.Enqueue(node1); }
|
||||||
|
lock (q1) { q1.TryDequeue(out _); }
|
||||||
|
lock (q1) { q1.TryDequeue(out _); }
|
||||||
|
}
|
||||||
|
|
||||||
|
[Benchmark]
|
||||||
|
public void Stack()
|
||||||
|
{
|
||||||
|
s1.Push(node1);
|
||||||
|
s1.Push(node2);
|
||||||
|
s1.TryPop(out _);
|
||||||
|
s1.TryPop(out _);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Benchmark]
|
||||||
|
public void StackLock()
|
||||||
|
{
|
||||||
|
lock (s1) { s1.Push(node1); }
|
||||||
|
lock (s1) { s1.Push(node2); }
|
||||||
|
lock (s1) { s1.TryPop(out _); }
|
||||||
|
lock (s1) { s1.TryPop(out _); }
|
||||||
|
}
|
||||||
|
|
||||||
|
[Benchmark]
|
||||||
|
public void ConcurrentQueue()
|
||||||
|
{
|
||||||
|
cq.Enqueue(node1);
|
||||||
|
cq.Enqueue(node1);
|
||||||
|
cq.TryDequeue(out _);
|
||||||
|
cq.TryDequeue(out _);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Benchmark]
|
||||||
|
public void ConcurrentStack()
|
||||||
|
{
|
||||||
|
cs.Push(node1);
|
||||||
|
cs.Push(node2);
|
||||||
|
cs.TryPop(out _);
|
||||||
|
cs.TryPop(out _);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Benchmark]
|
||||||
|
public void TaskPool()
|
||||||
|
{
|
||||||
|
pool.TryPush(node1);
|
||||||
|
pool.TryPush(node2);
|
||||||
|
pool.TryPop(out _);
|
||||||
|
pool.TryPop(out _);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Benchmark]
|
||||||
|
public void TaskPoolEqualNull()
|
||||||
|
{
|
||||||
|
poolEqualNull.TryPush(node1);
|
||||||
|
poolEqualNull.TryPush(node2);
|
||||||
|
poolEqualNull.TryPop(out _);
|
||||||
|
poolEqualNull.TryPop(out _);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Benchmark]
|
||||||
|
public void TaskPoolClass()
|
||||||
|
{
|
||||||
|
poolClass.TryPush(node1);
|
||||||
|
poolClass.TryPush(node2);
|
||||||
|
poolClass.TryPop(out _);
|
||||||
|
poolClass.TryPop(out _);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Benchmark]
|
||||||
|
public void TaskPoolWithoutSize()
|
||||||
|
{
|
||||||
|
poolWithoutSize.TryPush(node1);
|
||||||
|
poolWithoutSize.TryPush(node2);
|
||||||
|
poolWithoutSize.TryPop(out _);
|
||||||
|
poolWithoutSize.TryPop(out _);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Benchmark]
|
||||||
|
public void TaskPoolWithoutLock()
|
||||||
|
{
|
||||||
|
poolWithoutLock.TryPush(node1);
|
||||||
|
poolWithoutLock.TryPush(node2);
|
||||||
|
poolWithoutLock.TryPop(out _);
|
||||||
|
poolWithoutLock.TryPop(out _);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public sealed class Node : ITaskPoolNode<Node>
|
||||||
|
{
|
||||||
|
public Node NextNode { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface ITaskPoolNode<T>
|
||||||
|
{
|
||||||
|
T NextNode { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
// mutable struct, don't mark readonly.
|
||||||
|
[StructLayout(LayoutKind.Auto)]
|
||||||
|
public struct TaskPoolWithoutLock<T>
|
||||||
|
where T : class, ITaskPoolNode<T>
|
||||||
|
{
|
||||||
|
int size;
|
||||||
|
T root;
|
||||||
|
|
||||||
|
public int Size => size;
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public bool TryPop(out T result)
|
||||||
|
{
|
||||||
|
//if (Interlocked.CompareExchange(ref gate, 1, 0) == 0)
|
||||||
|
{
|
||||||
|
var v = root;
|
||||||
|
if (!(v is null))
|
||||||
|
{
|
||||||
|
root = v.NextNode;
|
||||||
|
v.NextNode = null;
|
||||||
|
size--;
|
||||||
|
result = v;
|
||||||
|
// Volatile.Write(ref gate, 0);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Volatile.Write(ref gate, 0);
|
||||||
|
}
|
||||||
|
result = default;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public bool TryPush(T item)
|
||||||
|
{
|
||||||
|
//if (Interlocked.CompareExchange(ref gate, 1, 0) == 0)
|
||||||
|
{
|
||||||
|
//if (size < TaskPool.MaxPoolSize)
|
||||||
|
{
|
||||||
|
item.NextNode = root;
|
||||||
|
root = item;
|
||||||
|
size++;
|
||||||
|
// Volatile.Write(ref gate, 0);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
//else
|
||||||
|
{
|
||||||
|
// Volatile.Write(ref gate, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[StructLayout(LayoutKind.Auto)]
|
||||||
|
public struct TaskPool<T>
|
||||||
|
where T : class, ITaskPoolNode<T>
|
||||||
|
{
|
||||||
|
int gate;
|
||||||
|
int size;
|
||||||
|
T root;
|
||||||
|
|
||||||
|
public int Size => size;
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public bool TryPop(out T result)
|
||||||
|
{
|
||||||
|
if (Interlocked.CompareExchange(ref gate, 1, 0) == 0)
|
||||||
|
{
|
||||||
|
var v = root;
|
||||||
|
if (!(v is null))
|
||||||
|
{
|
||||||
|
root = v.NextNode;
|
||||||
|
v.NextNode = null;
|
||||||
|
size--;
|
||||||
|
result = v;
|
||||||
|
Volatile.Write(ref gate, 0);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
Volatile.Write(ref gate, 0);
|
||||||
|
}
|
||||||
|
result = default;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public bool TryPush(T item)
|
||||||
|
{
|
||||||
|
if (Interlocked.CompareExchange(ref gate, 1, 0) == 0)
|
||||||
|
{
|
||||||
|
//if (size < TaskPool.MaxPoolSize)
|
||||||
|
{
|
||||||
|
item.NextNode = root;
|
||||||
|
root = item;
|
||||||
|
size++;
|
||||||
|
Volatile.Write(ref gate, 0);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
//else
|
||||||
|
{
|
||||||
|
// Volatile.Write(ref gate, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[StructLayout(LayoutKind.Auto)]
|
||||||
|
public struct TaskPoolEqualNull<T>
|
||||||
|
where T : class, ITaskPoolNode<T>
|
||||||
|
{
|
||||||
|
int gate;
|
||||||
|
int size;
|
||||||
|
T root;
|
||||||
|
|
||||||
|
public int Size => size;
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public bool TryPop(out T result)
|
||||||
|
{
|
||||||
|
if (Interlocked.CompareExchange(ref gate, 1, 0) == 0)
|
||||||
|
{
|
||||||
|
var v = root;
|
||||||
|
if (v != null)
|
||||||
|
{
|
||||||
|
root = v.NextNode;
|
||||||
|
v.NextNode = null;
|
||||||
|
size--;
|
||||||
|
result = v;
|
||||||
|
Volatile.Write(ref gate, 0);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
Volatile.Write(ref gate, 0);
|
||||||
|
}
|
||||||
|
result = default;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public bool TryPush(T item)
|
||||||
|
{
|
||||||
|
if (Interlocked.CompareExchange(ref gate, 1, 0) == 0)
|
||||||
|
{
|
||||||
|
//if (size < TaskPool.MaxPoolSize)
|
||||||
|
{
|
||||||
|
item.NextNode = root;
|
||||||
|
root = item;
|
||||||
|
size++;
|
||||||
|
Volatile.Write(ref gate, 0);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
//else
|
||||||
|
{
|
||||||
|
// Volatile.Write(ref gate, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class TaskPoolClass<T>
|
||||||
|
where T : class, ITaskPoolNode<T>
|
||||||
|
{
|
||||||
|
int gate;
|
||||||
|
int size;
|
||||||
|
T root;
|
||||||
|
|
||||||
|
public int Size => size;
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public bool TryPop(out T result)
|
||||||
|
{
|
||||||
|
if (Interlocked.CompareExchange(ref gate, 1, 0) == 0)
|
||||||
|
{
|
||||||
|
var v = root;
|
||||||
|
if (!(v is null))
|
||||||
|
{
|
||||||
|
root = v.NextNode;
|
||||||
|
v.NextNode = null;
|
||||||
|
size--;
|
||||||
|
result = v;
|
||||||
|
Volatile.Write(ref gate, 0);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
Volatile.Write(ref gate, 0);
|
||||||
|
}
|
||||||
|
result = default;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public bool TryPush(T item)
|
||||||
|
{
|
||||||
|
if (Interlocked.CompareExchange(ref gate, 1, 0) == 0)
|
||||||
|
{
|
||||||
|
//if (size < TaskPool.MaxPoolSize)
|
||||||
|
{
|
||||||
|
item.NextNode = root;
|
||||||
|
root = item;
|
||||||
|
size++;
|
||||||
|
Volatile.Write(ref gate, 0);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
//else
|
||||||
|
{
|
||||||
|
// Volatile.Write(ref gate, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[StructLayout(LayoutKind.Auto)]
|
||||||
|
public struct TaskPoolWithoutSize<T>
|
||||||
|
where T : class, ITaskPoolNode<T>
|
||||||
|
{
|
||||||
|
int gate;
|
||||||
|
T root;
|
||||||
|
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public bool TryPop(out T result)
|
||||||
|
{
|
||||||
|
if (Interlocked.CompareExchange(ref gate, 1, 0) == 0)
|
||||||
|
{
|
||||||
|
var v = root;
|
||||||
|
if (!(v is null))
|
||||||
|
{
|
||||||
|
root = v.NextNode;
|
||||||
|
v.NextNode = null;
|
||||||
|
result = v;
|
||||||
|
Volatile.Write(ref gate, 0);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
Volatile.Write(ref gate, 0);
|
||||||
|
}
|
||||||
|
result = default;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public bool TryPush(T item)
|
||||||
|
{
|
||||||
|
if (Interlocked.CompareExchange(ref gate, 1, 0) == 0)
|
||||||
|
{
|
||||||
|
//if (size < TaskPool.MaxPoolSize)
|
||||||
|
{
|
||||||
|
item.NextNode = root;
|
||||||
|
root = item;
|
||||||
|
Volatile.Write(ref gate, 0);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
//else
|
||||||
|
{
|
||||||
|
// Volatile.Write(ref gate, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,6 +7,8 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
|
||||||
|
<PackageReference Include="PooledAwait" Version="1.0.49" />
|
||||||
<PackageReference Include="System.Interactive.Async" Version="4.1.1" />
|
<PackageReference Include="System.Interactive.Async" Version="4.1.1" />
|
||||||
<PackageReference Include="System.Reactive" Version="4.4.1" />
|
<PackageReference Include="System.Reactive" Version="4.4.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -51,6 +51,67 @@ namespace NetCoreTests
|
|||||||
|
|
||||||
ar.Should().BeEquivalentTo(new[] { 100, 100, 100, 131, 191 });
|
ar.Should().BeEquivalentTo(new[] { 100, 100, 100, 131, 191 });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//[Fact]
|
||||||
|
//public async Task StateIteration()
|
||||||
|
//{
|
||||||
|
// var rp = new ReadOnlyAsyncReactiveProperty<int>(99);
|
||||||
|
// var setter = rp.GetSetter();
|
||||||
|
|
||||||
|
// var f = await rp.FirstAsync();
|
||||||
|
// f.Should().Be(99);
|
||||||
|
|
||||||
|
// var array = rp.Take(5).ToArrayAsync();
|
||||||
|
|
||||||
|
// setter(100);
|
||||||
|
// setter(100);
|
||||||
|
// setter(100);
|
||||||
|
// setter(131);
|
||||||
|
|
||||||
|
// var ar = await array;
|
||||||
|
|
||||||
|
// ar.Should().BeEquivalentTo(new[] { 99, 100, 100, 100, 131 });
|
||||||
|
//}
|
||||||
|
|
||||||
|
//[Fact]
|
||||||
|
//public async Task StateWithoutCurrent()
|
||||||
|
//{
|
||||||
|
// var rp = new ReadOnlyAsyncReactiveProperty<int>(99);
|
||||||
|
// var setter = rp.GetSetter();
|
||||||
|
|
||||||
|
// var array = rp.WithoutCurrent().Take(5).ToArrayAsync();
|
||||||
|
// setter(100);
|
||||||
|
// setter(100);
|
||||||
|
// setter(100);
|
||||||
|
// setter(131);
|
||||||
|
// setter(191);
|
||||||
|
|
||||||
|
// var ar = await array;
|
||||||
|
|
||||||
|
// ar.Should().BeEquivalentTo(new[] { 100, 100, 100, 131, 191 });
|
||||||
|
//}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void StateFromEnumeration()
|
||||||
|
{
|
||||||
|
var rp = new AsyncReactiveProperty<int>(10);
|
||||||
|
|
||||||
|
var state = rp.ToReadOnlyAsyncReactiveProperty(CancellationToken.None);
|
||||||
|
|
||||||
|
rp.Value = 10;
|
||||||
|
state.Value.Should().Be(10);
|
||||||
|
|
||||||
|
rp.Value = 20;
|
||||||
|
state.Value.Should().Be(20);
|
||||||
|
|
||||||
|
state.Dispose();
|
||||||
|
|
||||||
|
rp.Value = 30;
|
||||||
|
state.Value.Should().Be(20);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -319,5 +319,119 @@ namespace NetCoreTests.Linq
|
|||||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await ys);
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await ys);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task CombineLatestOK()
|
||||||
|
{
|
||||||
|
var a = new AsyncReactiveProperty<int>(0);
|
||||||
|
var b = new AsyncReactiveProperty<int>(0);
|
||||||
|
|
||||||
|
var list = new List<(int, int)>();
|
||||||
|
var complete = a.WithoutCurrent().CombineLatest(b.WithoutCurrent(), (x, y) => (x, y)).ForEachAsync(x => list.Add(x));
|
||||||
|
|
||||||
|
list.Count.Should().Be(0);
|
||||||
|
|
||||||
|
a.Value = 10;
|
||||||
|
list.Count.Should().Be(0);
|
||||||
|
|
||||||
|
a.Value = 20;
|
||||||
|
list.Count.Should().Be(0);
|
||||||
|
|
||||||
|
b.Value = 1;
|
||||||
|
list.Count.Should().Be(1);
|
||||||
|
|
||||||
|
list[0].Should().Be((20, 1));
|
||||||
|
|
||||||
|
a.Value = 30;
|
||||||
|
list.Last().Should().Be((30, 1));
|
||||||
|
|
||||||
|
b.Value = 2;
|
||||||
|
list.Last().Should().Be((30, 2));
|
||||||
|
|
||||||
|
a.Dispose();
|
||||||
|
b.Value = 3;
|
||||||
|
list.Last().Should().Be((30, 3));
|
||||||
|
|
||||||
|
b.Dispose();
|
||||||
|
|
||||||
|
await complete;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task CombineLatestLong()
|
||||||
|
{
|
||||||
|
var a = UniTaskAsyncEnumerable.Range(1, 100000);
|
||||||
|
var b = new AsyncReactiveProperty<int>(0);
|
||||||
|
|
||||||
|
var list = new List<(int, int)>();
|
||||||
|
var complete = a.CombineLatest(b.WithoutCurrent(), (x, y) => (x, y)).ForEachAsync(x => list.Add(x));
|
||||||
|
|
||||||
|
b.Value = 1;
|
||||||
|
|
||||||
|
list[0].Should().Be((100000, 1));
|
||||||
|
|
||||||
|
b.Dispose();
|
||||||
|
|
||||||
|
await complete;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task CombineLatestError()
|
||||||
|
{
|
||||||
|
var a = new AsyncReactiveProperty<int>(0);
|
||||||
|
var b = new AsyncReactiveProperty<int>(0);
|
||||||
|
|
||||||
|
var list = new List<(int, int)>();
|
||||||
|
var complete = a.WithoutCurrent()
|
||||||
|
.Select(x => { if (x == 0) { throw new MyException(); } return x; })
|
||||||
|
.CombineLatest(b.WithoutCurrent(), (x, y) => (x, y)).ForEachAsync(x => list.Add(x));
|
||||||
|
|
||||||
|
|
||||||
|
a.Value = 10;
|
||||||
|
b.Value = 1;
|
||||||
|
list.Last().Should().Be((10, 1));
|
||||||
|
|
||||||
|
a.Value = 0;
|
||||||
|
|
||||||
|
await Assert.ThrowsAsync<MyException>(async () => await complete);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task PariwiseImmediate()
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(1, 5).Pairwise().ToArrayAsync();
|
||||||
|
xs.Should().BeEquivalentTo((1, 2), (2, 3), (3, 4), (4, 5));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Pariwise()
|
||||||
|
{
|
||||||
|
var a = new AsyncReactiveProperty<int>(0);
|
||||||
|
|
||||||
|
var list = new List<(int, int)>();
|
||||||
|
var complete = a.WithoutCurrent().Pairwise().ForEachAsync(x => list.Add(x));
|
||||||
|
|
||||||
|
list.Count.Should().Be(0);
|
||||||
|
a.Value = 10;
|
||||||
|
list.Count.Should().Be(0);
|
||||||
|
a.Value = 20;
|
||||||
|
list.Count.Should().Be(1);
|
||||||
|
a.Value = 30;
|
||||||
|
a.Value = 40;
|
||||||
|
a.Value = 50;
|
||||||
|
|
||||||
|
a.Dispose();
|
||||||
|
|
||||||
|
await complete;
|
||||||
|
|
||||||
|
list.Should().BeEquivalentTo((10, 20), (20, 30), (30, 40), (40, 50));
|
||||||
|
}
|
||||||
|
|
||||||
|
class MyException : Exception
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
301
src/UniTask.NetCoreTests/TaskBuilderCases.cs
Normal file
301
src/UniTask.NetCoreTests/TaskBuilderCases.cs
Normal file
@@ -0,0 +1,301 @@
|
|||||||
|
#pragma warning disable CS1998
|
||||||
|
|
||||||
|
using Cysharp.Threading.Tasks;
|
||||||
|
using FluentAssertions;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Channels;
|
||||||
|
using Cysharp.Threading.Tasks.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Xunit;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
|
namespace NetCoreTests
|
||||||
|
{
|
||||||
|
public class UniTaskBuilderTest
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task Empty()
|
||||||
|
{
|
||||||
|
await Core();
|
||||||
|
|
||||||
|
static async UniTask Core()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task EmptyThrow()
|
||||||
|
{
|
||||||
|
await Assert.ThrowsAsync<TaskTestException>(async () => await Core());
|
||||||
|
|
||||||
|
static async UniTask Core()
|
||||||
|
{
|
||||||
|
throw new TaskTestException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Task_Done()
|
||||||
|
{
|
||||||
|
await Core();
|
||||||
|
|
||||||
|
static async UniTask Core()
|
||||||
|
{
|
||||||
|
await new TestAwaiter(true, UniTaskStatus.Succeeded);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Task_Fail()
|
||||||
|
{
|
||||||
|
await Assert.ThrowsAsync<TaskTestException>(async () => await Core());
|
||||||
|
|
||||||
|
static async UniTask Core()
|
||||||
|
{
|
||||||
|
await new TestAwaiter(true, UniTaskStatus.Faulted);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Task_Cancel()
|
||||||
|
{
|
||||||
|
await Assert.ThrowsAsync<OperationCanceledException>(async () => await Core());
|
||||||
|
|
||||||
|
static async UniTask Core()
|
||||||
|
{
|
||||||
|
await new TestAwaiter(true, UniTaskStatus.Canceled);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task AwaitUnsafeOnCompletedCall_Task_SetResult()
|
||||||
|
{
|
||||||
|
await Core();
|
||||||
|
|
||||||
|
static async UniTask Core()
|
||||||
|
{
|
||||||
|
await new TestAwaiter(false, UniTaskStatus.Succeeded);
|
||||||
|
await new TestAwaiter(false, UniTaskStatus.Succeeded);
|
||||||
|
await new TestAwaiter(false, UniTaskStatus.Succeeded);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task AwaitUnsafeOnCompletedCall_Task_SetException()
|
||||||
|
{
|
||||||
|
await Assert.ThrowsAsync<TaskTestException>(async () => await Core());
|
||||||
|
|
||||||
|
static async UniTask Core()
|
||||||
|
{
|
||||||
|
await new TestAwaiter(false, UniTaskStatus.Succeeded);
|
||||||
|
await new TestAwaiter(false, UniTaskStatus.Faulted);
|
||||||
|
throw new InvalidOperationException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task AwaitUnsafeOnCompletedCall_Task_SetCancelException()
|
||||||
|
{
|
||||||
|
await Assert.ThrowsAsync<OperationCanceledException>(async () => await Core());
|
||||||
|
|
||||||
|
static async UniTask Core()
|
||||||
|
{
|
||||||
|
await new TestAwaiter(false, UniTaskStatus.Succeeded);
|
||||||
|
await new TestAwaiter(false, UniTaskStatus.Canceled);
|
||||||
|
throw new InvalidOperationException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class UniTask_T_BuilderTest
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task Empty()
|
||||||
|
{
|
||||||
|
(await Core()).Should().Be(10);
|
||||||
|
|
||||||
|
static async UniTask<int> Core()
|
||||||
|
{
|
||||||
|
return 10;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task EmptyThrow()
|
||||||
|
{
|
||||||
|
await Assert.ThrowsAsync<TaskTestException>(async () => await Core());
|
||||||
|
|
||||||
|
static async UniTask<int> Core()
|
||||||
|
{
|
||||||
|
throw new TaskTestException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Task_Done()
|
||||||
|
{
|
||||||
|
(await Core()).Should().Be(10);
|
||||||
|
|
||||||
|
static async UniTask<int> Core()
|
||||||
|
{
|
||||||
|
return await new TestAwaiter<int>(true, UniTaskStatus.Succeeded, 10);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Task_Fail()
|
||||||
|
{
|
||||||
|
await Assert.ThrowsAsync<TaskTestException>(async () => await Core());
|
||||||
|
|
||||||
|
static async UniTask<int> Core()
|
||||||
|
{
|
||||||
|
return await new TestAwaiter<int>(true, UniTaskStatus.Faulted, 10);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Task_Cancel()
|
||||||
|
{
|
||||||
|
await Assert.ThrowsAsync<OperationCanceledException>(async () => await Core());
|
||||||
|
|
||||||
|
static async UniTask<int> Core()
|
||||||
|
{
|
||||||
|
return await new TestAwaiter<int>(true, UniTaskStatus.Canceled, 10);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task AwaitUnsafeOnCompletedCall_Task_SetResult()
|
||||||
|
{
|
||||||
|
(await Core()).Should().Be(6);
|
||||||
|
|
||||||
|
static async UniTask<int> Core()
|
||||||
|
{
|
||||||
|
var sum = 0;
|
||||||
|
sum += await new TestAwaiter<int>(false, UniTaskStatus.Succeeded, 1);
|
||||||
|
sum += await new TestAwaiter<int>(false, UniTaskStatus.Succeeded, 2);
|
||||||
|
sum += await new TestAwaiter<int>(false, UniTaskStatus.Succeeded, 3);
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task AwaitUnsafeOnCompletedCall_Task_SetException()
|
||||||
|
{
|
||||||
|
await Assert.ThrowsAsync<TaskTestException>(async () => await Core());
|
||||||
|
|
||||||
|
static async UniTask<int> Core()
|
||||||
|
{
|
||||||
|
await new TestAwaiter<int>(false, UniTaskStatus.Succeeded, 10);
|
||||||
|
await new TestAwaiter<int>(false, UniTaskStatus.Faulted, 10);
|
||||||
|
throw new InvalidOperationException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task AwaitUnsafeOnCompletedCall_Task_SetCancelException()
|
||||||
|
{
|
||||||
|
await Assert.ThrowsAsync<OperationCanceledException>(async () => await Core());
|
||||||
|
|
||||||
|
static async UniTask<int> Core()
|
||||||
|
{
|
||||||
|
await new TestAwaiter<int>(false, UniTaskStatus.Succeeded, 10);
|
||||||
|
await new TestAwaiter<int>(false, UniTaskStatus.Canceled, 10);
|
||||||
|
throw new InvalidOperationException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class TaskTestException : Exception
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct TestAwaiter : ICriticalNotifyCompletion
|
||||||
|
{
|
||||||
|
readonly UniTaskStatus status;
|
||||||
|
readonly bool isCompleted;
|
||||||
|
|
||||||
|
public TestAwaiter(bool isCompleted, UniTaskStatus status)
|
||||||
|
{
|
||||||
|
this.isCompleted = isCompleted;
|
||||||
|
this.status = status;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TestAwaiter GetAwaiter() => this;
|
||||||
|
|
||||||
|
public bool IsCompleted => isCompleted;
|
||||||
|
|
||||||
|
public void GetResult()
|
||||||
|
{
|
||||||
|
switch (status)
|
||||||
|
{
|
||||||
|
case UniTaskStatus.Faulted:
|
||||||
|
throw new TaskTestException();
|
||||||
|
case UniTaskStatus.Canceled:
|
||||||
|
throw new OperationCanceledException();
|
||||||
|
case UniTaskStatus.Pending:
|
||||||
|
case UniTaskStatus.Succeeded:
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnCompleted(Action continuation)
|
||||||
|
{
|
||||||
|
ThreadPool.QueueUserWorkItem(_ => continuation(), null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UnsafeOnCompleted(Action continuation)
|
||||||
|
{
|
||||||
|
ThreadPool.UnsafeQueueUserWorkItem(_ => continuation(), null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct TestAwaiter<T> : ICriticalNotifyCompletion
|
||||||
|
{
|
||||||
|
readonly UniTaskStatus status;
|
||||||
|
readonly bool isCompleted;
|
||||||
|
readonly T value;
|
||||||
|
|
||||||
|
public TestAwaiter(bool isCompleted, UniTaskStatus status, T value)
|
||||||
|
{
|
||||||
|
this.isCompleted = isCompleted;
|
||||||
|
this.status = status;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TestAwaiter<T> GetAwaiter() => this;
|
||||||
|
|
||||||
|
public bool IsCompleted => isCompleted;
|
||||||
|
|
||||||
|
public T GetResult()
|
||||||
|
{
|
||||||
|
switch (status)
|
||||||
|
{
|
||||||
|
case UniTaskStatus.Faulted:
|
||||||
|
throw new TaskTestException();
|
||||||
|
case UniTaskStatus.Canceled:
|
||||||
|
throw new OperationCanceledException();
|
||||||
|
case UniTaskStatus.Pending:
|
||||||
|
case UniTaskStatus.Succeeded:
|
||||||
|
default:
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnCompleted(Action continuation)
|
||||||
|
{
|
||||||
|
ThreadPool.QueueUserWorkItem(_ => continuation(), null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UnsafeOnCompleted(Action continuation)
|
||||||
|
{
|
||||||
|
ThreadPool.UnsafeQueueUserWorkItem(_ => continuation(), null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
637
src/UniTask.NetCoreTests/TriggerEventTest.cs
Normal file
637
src/UniTask.NetCoreTests/TriggerEventTest.cs
Normal file
@@ -0,0 +1,637 @@
|
|||||||
|
using Cysharp.Threading.Tasks;
|
||||||
|
using FluentAssertions;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Channels;
|
||||||
|
using Cysharp.Threading.Tasks.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace NetCoreTests
|
||||||
|
{
|
||||||
|
public class TriggerEventTest
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void SimpleAdd()
|
||||||
|
{
|
||||||
|
var ev = new TriggerEvent<int>();
|
||||||
|
|
||||||
|
// do nothing
|
||||||
|
ev.SetResult(0);
|
||||||
|
ev.SetError(null);
|
||||||
|
ev.SetCompleted();
|
||||||
|
ev.SetCanceled(default);
|
||||||
|
|
||||||
|
{
|
||||||
|
var one = new TestEvent(1);
|
||||||
|
|
||||||
|
ev.Add(one);
|
||||||
|
|
||||||
|
ev.SetResult(10);
|
||||||
|
ev.SetResult(20);
|
||||||
|
ev.SetResult(30);
|
||||||
|
|
||||||
|
one.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
|
||||||
|
ev.SetCompleted();
|
||||||
|
|
||||||
|
one.CompletedCalled.Count.Should().Be(1);
|
||||||
|
|
||||||
|
// do nothing
|
||||||
|
ev.SetResult(0);
|
||||||
|
ev.SetError(null);
|
||||||
|
ev.SetCompleted();
|
||||||
|
ev.SetCanceled(default);
|
||||||
|
|
||||||
|
one.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
one.CompletedCalled.Count.Should().Be(1);
|
||||||
|
}
|
||||||
|
// after removed, onemore
|
||||||
|
{
|
||||||
|
var one = new TestEvent(1);
|
||||||
|
|
||||||
|
ev.Add(one);
|
||||||
|
|
||||||
|
ev.SetResult(10);
|
||||||
|
ev.SetResult(20);
|
||||||
|
ev.SetResult(30);
|
||||||
|
|
||||||
|
one.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
|
||||||
|
ev.SetCompleted();
|
||||||
|
|
||||||
|
one.CompletedCalled.Count.Should().Be(1);
|
||||||
|
|
||||||
|
// do nothing
|
||||||
|
ev.SetResult(0);
|
||||||
|
ev.SetError(null);
|
||||||
|
ev.SetCompleted();
|
||||||
|
ev.SetCanceled(default);
|
||||||
|
|
||||||
|
one.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
one.CompletedCalled.Count.Should().Be(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void AddFour()
|
||||||
|
{
|
||||||
|
var ev = new TriggerEvent<int>();
|
||||||
|
|
||||||
|
// do nothing
|
||||||
|
ev.SetResult(0);
|
||||||
|
ev.SetError(null);
|
||||||
|
ev.SetCompleted();
|
||||||
|
ev.SetCanceled(default);
|
||||||
|
|
||||||
|
{
|
||||||
|
var one = new TestEvent(1);
|
||||||
|
var two = new TestEvent(2);
|
||||||
|
var three = new TestEvent(3);
|
||||||
|
var four = new TestEvent(4);
|
||||||
|
|
||||||
|
ev.Add(one);
|
||||||
|
ev.Add(two);
|
||||||
|
ev.Add(three);
|
||||||
|
ev.Add(four);
|
||||||
|
|
||||||
|
ev.SetResult(10);
|
||||||
|
ev.SetResult(20);
|
||||||
|
ev.SetResult(30);
|
||||||
|
|
||||||
|
one.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
two.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
three.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
four.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
|
||||||
|
ev.SetCompleted();
|
||||||
|
|
||||||
|
one.CompletedCalled.Count.Should().Be(1);
|
||||||
|
two.CompletedCalled.Count.Should().Be(1);
|
||||||
|
three.CompletedCalled.Count.Should().Be(1);
|
||||||
|
|
||||||
|
|
||||||
|
// do nothing
|
||||||
|
ev.SetResult(0);
|
||||||
|
ev.SetError(null);
|
||||||
|
ev.SetCompleted();
|
||||||
|
ev.SetCanceled(default);
|
||||||
|
|
||||||
|
one.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
one.CompletedCalled.Count.Should().Be(1);
|
||||||
|
two.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
three.CompletedCalled.Count.Should().Be(1);
|
||||||
|
two.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
three.CompletedCalled.Count.Should().Be(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// after removed, onemore.
|
||||||
|
{
|
||||||
|
var one = new TestEvent(1);
|
||||||
|
var two = new TestEvent(2);
|
||||||
|
var three = new TestEvent(3);
|
||||||
|
var four = new TestEvent(4);
|
||||||
|
|
||||||
|
ev.Add(one);
|
||||||
|
ev.Add(two);
|
||||||
|
ev.Add(three);
|
||||||
|
ev.Add(four);
|
||||||
|
|
||||||
|
ev.SetResult(10);
|
||||||
|
ev.SetResult(20);
|
||||||
|
ev.SetResult(30);
|
||||||
|
ev.Add(four);
|
||||||
|
|
||||||
|
one.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
two.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
three.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
four.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
|
||||||
|
ev.SetCompleted();
|
||||||
|
|
||||||
|
one.CompletedCalled.Count.Should().Be(1);
|
||||||
|
two.CompletedCalled.Count.Should().Be(1);
|
||||||
|
three.CompletedCalled.Count.Should().Be(1);
|
||||||
|
|
||||||
|
|
||||||
|
// do nothing
|
||||||
|
ev.SetResult(0);
|
||||||
|
ev.SetError(null);
|
||||||
|
ev.SetCompleted();
|
||||||
|
ev.SetCanceled(default);
|
||||||
|
|
||||||
|
one.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
one.CompletedCalled.Count.Should().Be(1);
|
||||||
|
two.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
three.CompletedCalled.Count.Should().Be(1);
|
||||||
|
two.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
three.CompletedCalled.Count.Should().Be(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void OneRemove()
|
||||||
|
{
|
||||||
|
var ev = new TriggerEvent<int>();
|
||||||
|
{
|
||||||
|
var one = new TestEvent(1);
|
||||||
|
var two = new TestEvent(2);
|
||||||
|
var three = new TestEvent(3);
|
||||||
|
|
||||||
|
ev.Add(one);
|
||||||
|
ev.Add(two);
|
||||||
|
ev.Add(three);
|
||||||
|
|
||||||
|
ev.SetResult(10);
|
||||||
|
ev.SetResult(20);
|
||||||
|
ev.SetResult(30);
|
||||||
|
|
||||||
|
one.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
two.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
three.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
|
||||||
|
ev.Remove(one);
|
||||||
|
|
||||||
|
ev.SetResult(40);
|
||||||
|
ev.SetResult(50);
|
||||||
|
ev.SetResult(60);
|
||||||
|
|
||||||
|
one.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
two.NextCalled.Should().BeEquivalentTo(10, 20, 30, 40, 50, 60);
|
||||||
|
three.NextCalled.Should().BeEquivalentTo(10, 20, 30, 40, 50, 60);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void TwoRemove()
|
||||||
|
{
|
||||||
|
var ev = new TriggerEvent<int>();
|
||||||
|
{
|
||||||
|
var one = new TestEvent(1);
|
||||||
|
var two = new TestEvent(2);
|
||||||
|
var three = new TestEvent(3);
|
||||||
|
|
||||||
|
ev.Add(one);
|
||||||
|
ev.Add(two);
|
||||||
|
ev.Add(three);
|
||||||
|
|
||||||
|
ev.SetResult(10);
|
||||||
|
ev.SetResult(20);
|
||||||
|
ev.SetResult(30);
|
||||||
|
|
||||||
|
one.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
two.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
three.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
|
||||||
|
ev.Remove(two);
|
||||||
|
|
||||||
|
ev.SetResult(40);
|
||||||
|
ev.SetResult(50);
|
||||||
|
ev.SetResult(60);
|
||||||
|
|
||||||
|
one.NextCalled.Should().BeEquivalentTo(10, 20, 30, 40, 50, 60);
|
||||||
|
two.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
three.NextCalled.Should().BeEquivalentTo(10, 20, 30, 40, 50, 60);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void ThreeRemove()
|
||||||
|
{
|
||||||
|
var ev = new TriggerEvent<int>();
|
||||||
|
{
|
||||||
|
var one = new TestEvent(1);
|
||||||
|
var two = new TestEvent(2);
|
||||||
|
var three = new TestEvent(3);
|
||||||
|
|
||||||
|
ev.Add(one);
|
||||||
|
ev.Add(two);
|
||||||
|
ev.Add(three);
|
||||||
|
|
||||||
|
ev.SetResult(10);
|
||||||
|
ev.SetResult(20);
|
||||||
|
ev.SetResult(30);
|
||||||
|
|
||||||
|
one.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
two.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
three.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
|
||||||
|
ev.Remove(three);
|
||||||
|
|
||||||
|
ev.SetResult(40);
|
||||||
|
ev.SetResult(50);
|
||||||
|
ev.SetResult(60);
|
||||||
|
|
||||||
|
one.NextCalled.Should().BeEquivalentTo(10, 20, 30, 40, 50, 60);
|
||||||
|
two.NextCalled.Should().BeEquivalentTo(10, 20, 30, 40, 50, 60);
|
||||||
|
three.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void RemoveSelf()
|
||||||
|
{
|
||||||
|
new RemoveMe().Run1();
|
||||||
|
new RemoveMe().Run2();
|
||||||
|
new RemoveMe().Run3();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void RemoveNextInIterating()
|
||||||
|
{
|
||||||
|
new RemoveNext().Run1();
|
||||||
|
new RemoveNext().Run2();
|
||||||
|
new RemoveNext().Run3();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void RemoveNextNextTest()
|
||||||
|
{
|
||||||
|
new RemoveNextNext().Run1();
|
||||||
|
new RemoveNextNext().Run2();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void AddTest()
|
||||||
|
{
|
||||||
|
new AddMe().Run1();
|
||||||
|
new AddMe().Run2();
|
||||||
|
}
|
||||||
|
|
||||||
|
public class RemoveMe
|
||||||
|
{
|
||||||
|
TriggerEvent<int> ev;
|
||||||
|
|
||||||
|
public void Run1()
|
||||||
|
{
|
||||||
|
TestEvent one = default;
|
||||||
|
one = new TestEvent(1, () => ev.Remove(one));
|
||||||
|
|
||||||
|
var two = new TestEvent(2);
|
||||||
|
var three = new TestEvent(3);
|
||||||
|
|
||||||
|
ev.Add(one);
|
||||||
|
ev.Add(two);
|
||||||
|
ev.Add(three);
|
||||||
|
|
||||||
|
ev.SetResult(10);
|
||||||
|
ev.SetResult(20);
|
||||||
|
ev.SetResult(30);
|
||||||
|
|
||||||
|
one.NextCalled.Should().BeEquivalentTo(10);
|
||||||
|
two.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
three.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Run2()
|
||||||
|
{
|
||||||
|
TestEvent one = default;
|
||||||
|
one = new TestEvent(1, () => ev.Remove(one));
|
||||||
|
|
||||||
|
var two = new TestEvent(2);
|
||||||
|
var three = new TestEvent(3);
|
||||||
|
|
||||||
|
ev.Add(two);
|
||||||
|
ev.Add(one); // add second.
|
||||||
|
ev.Add(three);
|
||||||
|
|
||||||
|
ev.SetResult(10);
|
||||||
|
ev.SetResult(20);
|
||||||
|
ev.SetResult(30);
|
||||||
|
|
||||||
|
one.NextCalled.Should().BeEquivalentTo(10);
|
||||||
|
two.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
three.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Run3()
|
||||||
|
{
|
||||||
|
TestEvent one = default;
|
||||||
|
one = new TestEvent(1, () => ev.Remove(one));
|
||||||
|
|
||||||
|
var two = new TestEvent(2);
|
||||||
|
var three = new TestEvent(3);
|
||||||
|
|
||||||
|
ev.Add(two);
|
||||||
|
ev.Add(three);
|
||||||
|
ev.Add(one); // add thired.
|
||||||
|
|
||||||
|
ev.SetResult(10);
|
||||||
|
ev.SetResult(20);
|
||||||
|
ev.SetResult(30);
|
||||||
|
|
||||||
|
one.NextCalled.Should().BeEquivalentTo(10);
|
||||||
|
two.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
three.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class RemoveNext
|
||||||
|
{
|
||||||
|
TriggerEvent<int> ev;
|
||||||
|
|
||||||
|
public void Run1()
|
||||||
|
{
|
||||||
|
TestEvent one = default;
|
||||||
|
TestEvent two = default;
|
||||||
|
TestEvent three = default;
|
||||||
|
one = new TestEvent(1, () => ev.Remove(two));
|
||||||
|
two = new TestEvent(2);
|
||||||
|
three = new TestEvent(3);
|
||||||
|
|
||||||
|
ev.Add(one);
|
||||||
|
ev.Add(two);
|
||||||
|
ev.Add(three);
|
||||||
|
|
||||||
|
ev.SetResult(10);
|
||||||
|
ev.SetResult(20);
|
||||||
|
ev.SetResult(30);
|
||||||
|
|
||||||
|
one.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
two.NextCalled.Count.Should().Be(0);
|
||||||
|
three.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Run2()
|
||||||
|
{
|
||||||
|
TestEvent one = default;
|
||||||
|
TestEvent two = default;
|
||||||
|
TestEvent three = default;
|
||||||
|
one = new TestEvent(1, () => ev.Remove(two));
|
||||||
|
two = new TestEvent(2);
|
||||||
|
three = new TestEvent(3);
|
||||||
|
|
||||||
|
ev.Add(two);
|
||||||
|
ev.Add(one); // add second
|
||||||
|
ev.Add(three);
|
||||||
|
|
||||||
|
ev.SetResult(10);
|
||||||
|
ev.SetResult(20);
|
||||||
|
ev.SetResult(30);
|
||||||
|
|
||||||
|
one.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
two.NextCalled.Should().BeEquivalentTo(10);
|
||||||
|
three.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Run3()
|
||||||
|
{
|
||||||
|
TestEvent one = default;
|
||||||
|
TestEvent two = default;
|
||||||
|
TestEvent three = default;
|
||||||
|
one = new TestEvent(1, () => ev.Remove(two));
|
||||||
|
two = new TestEvent(2);
|
||||||
|
three = new TestEvent(3);
|
||||||
|
|
||||||
|
ev.Add(two);
|
||||||
|
ev.Add(three);
|
||||||
|
ev.Add(one); // add thired.
|
||||||
|
|
||||||
|
ev.SetResult(10);
|
||||||
|
ev.SetResult(20);
|
||||||
|
ev.SetResult(30);
|
||||||
|
|
||||||
|
one.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
two.NextCalled.Should().BeEquivalentTo(10);
|
||||||
|
three.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class RemoveNextNext
|
||||||
|
{
|
||||||
|
TriggerEvent<int> ev;
|
||||||
|
|
||||||
|
public void Run1()
|
||||||
|
{
|
||||||
|
TestEvent one = default;
|
||||||
|
TestEvent two = default;
|
||||||
|
TestEvent three = default;
|
||||||
|
TestEvent four = default;
|
||||||
|
one = new TestEvent(1, () => { ev.Remove(two); ev.Remove(three); });
|
||||||
|
two = new TestEvent(2);
|
||||||
|
three = new TestEvent(3);
|
||||||
|
four = new TestEvent(4);
|
||||||
|
|
||||||
|
ev.Add(one);
|
||||||
|
ev.Add(two);
|
||||||
|
ev.Add(three);
|
||||||
|
ev.Add(four);
|
||||||
|
|
||||||
|
ev.SetResult(10);
|
||||||
|
ev.SetResult(20);
|
||||||
|
ev.SetResult(30);
|
||||||
|
|
||||||
|
one.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
two.NextCalled.Count.Should().Be(0);
|
||||||
|
three.NextCalled.Count.Should().Be(0);
|
||||||
|
four.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Run2()
|
||||||
|
{
|
||||||
|
TestEvent one = default;
|
||||||
|
TestEvent two = default;
|
||||||
|
TestEvent three = default;
|
||||||
|
TestEvent four = default;
|
||||||
|
one = new TestEvent(1, () => { ev.Remove(one); ev.Remove(two); ev.Remove(three); });
|
||||||
|
two = new TestEvent(2);
|
||||||
|
three = new TestEvent(3);
|
||||||
|
four = new TestEvent(4);
|
||||||
|
|
||||||
|
ev.Add(one);
|
||||||
|
ev.Add(two);
|
||||||
|
ev.Add(three);
|
||||||
|
ev.Add(four);
|
||||||
|
|
||||||
|
ev.SetResult(10);
|
||||||
|
ev.SetResult(20);
|
||||||
|
ev.SetResult(30);
|
||||||
|
|
||||||
|
one.NextCalled.Should().BeEquivalentTo(10);
|
||||||
|
two.NextCalled.Count.Should().Be(0);
|
||||||
|
three.NextCalled.Count.Should().Be(0);
|
||||||
|
four.NextCalled.Should().BeEquivalentTo(10, 20, 30);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public class AddMe
|
||||||
|
{
|
||||||
|
TriggerEvent<int> ev;
|
||||||
|
|
||||||
|
public void Run1()
|
||||||
|
{
|
||||||
|
TestEvent one = default;
|
||||||
|
TestEvent two = default;
|
||||||
|
TestEvent three = default;
|
||||||
|
TestEvent four = default;
|
||||||
|
|
||||||
|
one = new TestEvent(1, () =>
|
||||||
|
{
|
||||||
|
if (two == null)
|
||||||
|
{
|
||||||
|
ev.Add(two = new TestEvent(2));
|
||||||
|
}
|
||||||
|
else if (three == null)
|
||||||
|
{
|
||||||
|
ev.Add(three = new TestEvent(3));
|
||||||
|
}
|
||||||
|
else if (four == null)
|
||||||
|
{
|
||||||
|
ev.Add(four = new TestEvent(4));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
ev.Add(one);
|
||||||
|
|
||||||
|
ev.SetResult(10);
|
||||||
|
ev.SetResult(20);
|
||||||
|
ev.SetResult(30);
|
||||||
|
ev.SetResult(40);
|
||||||
|
|
||||||
|
one.NextCalled.Should().BeEquivalentTo(10, 20, 30, 40);
|
||||||
|
two.NextCalled.Should().BeEquivalentTo(20, 30, 40);
|
||||||
|
three.NextCalled.Should().BeEquivalentTo(30, 40);
|
||||||
|
four.NextCalled.Should().BeEquivalentTo(40);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Run2()
|
||||||
|
{
|
||||||
|
TestEvent one = default;
|
||||||
|
TestEvent two = default;
|
||||||
|
TestEvent three = default;
|
||||||
|
TestEvent four = default;
|
||||||
|
|
||||||
|
one = new TestEvent(1, () =>
|
||||||
|
{
|
||||||
|
if (two == null)
|
||||||
|
{
|
||||||
|
ev.Add(two = new TestEvent(2, () =>
|
||||||
|
{
|
||||||
|
if (three == null)
|
||||||
|
{
|
||||||
|
ev.Add(three = new TestEvent(3, () =>
|
||||||
|
{
|
||||||
|
if (four == null)
|
||||||
|
{
|
||||||
|
ev.Add(four = new TestEvent(4));
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
ev.Add(one);
|
||||||
|
|
||||||
|
ev.SetResult(10);
|
||||||
|
ev.SetResult(20);
|
||||||
|
ev.SetResult(30);
|
||||||
|
ev.SetResult(40);
|
||||||
|
|
||||||
|
one.NextCalled.Should().BeEquivalentTo(10, 20, 30, 40);
|
||||||
|
two.NextCalled.Should().BeEquivalentTo(20, 30, 40);
|
||||||
|
three.NextCalled.Should().BeEquivalentTo(30, 40);
|
||||||
|
four.NextCalled.Should().BeEquivalentTo(40);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class TestEvent : ITriggerHandler<int>
|
||||||
|
{
|
||||||
|
public readonly int Id;
|
||||||
|
readonly Action iteratingEvent;
|
||||||
|
|
||||||
|
public TestEvent(int id)
|
||||||
|
{
|
||||||
|
this.Id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TestEvent(int id, Action iteratingEvent)
|
||||||
|
{
|
||||||
|
this.Id = id;
|
||||||
|
this.iteratingEvent = iteratingEvent;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<int> NextCalled = new List<int>();
|
||||||
|
public List<Exception> ErrorCalled = new List<Exception>();
|
||||||
|
public List<object> CompletedCalled = new List<object>();
|
||||||
|
public List<CancellationToken> CancelCalled = new List<CancellationToken>();
|
||||||
|
|
||||||
|
public ITriggerHandler<int> Prev { get; set; }
|
||||||
|
public ITriggerHandler<int> Next { get; set; }
|
||||||
|
|
||||||
|
public void OnCanceled(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
CancelCalled.Add(cancellationToken);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnCompleted()
|
||||||
|
{
|
||||||
|
CompletedCalled.Add(new object());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnError(Exception ex)
|
||||||
|
{
|
||||||
|
ErrorCalled.Add(ex);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnNext(int value)
|
||||||
|
{
|
||||||
|
NextCalled.Add(value);
|
||||||
|
iteratingEvent?.Invoke();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return Id.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
30
src/UniTask/Assets/Editor/EditorRunnerChecker.cs
Normal file
30
src/UniTask/Assets/Editor/EditorRunnerChecker.cs
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
#if UNITY_EDITOR
|
||||||
|
|
||||||
|
using Cysharp.Threading.Tasks;
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using UnityEditor;
|
||||||
|
using UnityEngine;
|
||||||
|
using UnityEngine.Networking;
|
||||||
|
|
||||||
|
public static class EditorRunnerChecker
|
||||||
|
{
|
||||||
|
[MenuItem("Tools/UniTaskEditorRunnerChecker")]
|
||||||
|
public static void RunUniTaskAsync()
|
||||||
|
{
|
||||||
|
RunCore().Forget();
|
||||||
|
}
|
||||||
|
|
||||||
|
static async UniTaskVoid RunCore()
|
||||||
|
{
|
||||||
|
Debug.Log("Start");
|
||||||
|
|
||||||
|
var r = await UnityWebRequest.Get("https://bing.com/").SendWebRequest().ToUniTask();
|
||||||
|
Debug.Log(r.downloadHandler.text.Substring(0, 100));
|
||||||
|
|
||||||
|
Debug.Log("End");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
fileFormatVersion: 2
|
fileFormatVersion: 2
|
||||||
guid: fcb1f7467a3e2b64c8a016c8aee2f9b4
|
guid: e51b78c06cb410f42b36e0af9de3b065
|
||||||
MonoImporter:
|
MonoImporter:
|
||||||
externalObjects: {}
|
externalObjects: {}
|
||||||
serializedVersion: 2
|
serializedVersion: 2
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
using Cysharp.Threading.Tasks.Linq;
|
using System;
|
||||||
using System;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
|
||||||
namespace Cysharp.Threading.Tasks
|
namespace Cysharp.Threading.Tasks
|
||||||
@@ -59,6 +58,24 @@ namespace Cysharp.Threading.Tasks
|
|||||||
triggerEvent.SetCompleted();
|
triggerEvent.SetCompleted();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static implicit operator T(AsyncReactiveProperty<T> value)
|
||||||
|
{
|
||||||
|
return value.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
if (isValueType) return latestValue.ToString();
|
||||||
|
return latestValue?.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool isValueType;
|
||||||
|
|
||||||
|
static AsyncReactiveProperty()
|
||||||
|
{
|
||||||
|
isValueType = typeof(T).IsValueType;
|
||||||
|
}
|
||||||
|
|
||||||
class WithoutCurrentEnumerable : IUniTaskAsyncEnumerable<T>
|
class WithoutCurrentEnumerable : IUniTaskAsyncEnumerable<T>
|
||||||
{
|
{
|
||||||
readonly AsyncReactiveProperty<T> parent;
|
readonly AsyncReactiveProperty<T> parent;
|
||||||
@@ -102,6 +119,9 @@ namespace Cysharp.Threading.Tasks
|
|||||||
|
|
||||||
public T Current => value;
|
public T Current => value;
|
||||||
|
|
||||||
|
ITriggerHandler<T> ITriggerHandler<T>.Prev { get; set; }
|
||||||
|
ITriggerHandler<T> ITriggerHandler<T>.Next { get; set; }
|
||||||
|
|
||||||
public UniTask<bool> MoveNextAsync()
|
public UniTask<bool> MoveNextAsync()
|
||||||
{
|
{
|
||||||
// raise latest value on first call.
|
// raise latest value on first call.
|
||||||
@@ -156,4 +176,200 @@ namespace Cysharp.Threading.Tasks
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
public class ReadOnlyAsyncReactiveProperty<T> : IReadOnlyAsyncReactiveProperty<T>, IDisposable
|
||||||
|
{
|
||||||
|
TriggerEvent<T> triggerEvent;
|
||||||
|
|
||||||
|
T latestValue;
|
||||||
|
IUniTaskAsyncEnumerator<T> enumerator;
|
||||||
|
|
||||||
|
public T Value
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return latestValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ReadOnlyAsyncReactiveProperty(T initialValue, IUniTaskAsyncEnumerable<T> source, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
latestValue = initialValue;
|
||||||
|
ConsumeEnumerator(source, cancellationToken).Forget();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ReadOnlyAsyncReactiveProperty(IUniTaskAsyncEnumerable<T> source, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
ConsumeEnumerator(source, cancellationToken).Forget();
|
||||||
|
}
|
||||||
|
|
||||||
|
async UniTaskVoid ConsumeEnumerator(IUniTaskAsyncEnumerable<T> source, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
enumerator = source.GetAsyncEnumerator(cancellationToken);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
while (await enumerator.MoveNextAsync())
|
||||||
|
{
|
||||||
|
var value = enumerator.Current;
|
||||||
|
this.latestValue = value;
|
||||||
|
triggerEvent.SetResult(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
await enumerator.DisposeAsync();
|
||||||
|
enumerator = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public IUniTaskAsyncEnumerable<T> WithoutCurrent()
|
||||||
|
{
|
||||||
|
return new WithoutCurrentEnumerable(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IUniTaskAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return new Enumerator(this, cancellationToken, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
if (enumerator != null)
|
||||||
|
{
|
||||||
|
enumerator.DisposeAsync().Forget();
|
||||||
|
}
|
||||||
|
|
||||||
|
triggerEvent.SetCompleted();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static implicit operator T(ReadOnlyAsyncReactiveProperty<T> value)
|
||||||
|
{
|
||||||
|
return value.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
if (isValueType) return latestValue.ToString();
|
||||||
|
return latestValue?.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool isValueType;
|
||||||
|
|
||||||
|
static ReadOnlyAsyncReactiveProperty()
|
||||||
|
{
|
||||||
|
isValueType = typeof(T).IsValueType;
|
||||||
|
}
|
||||||
|
|
||||||
|
class WithoutCurrentEnumerable : IUniTaskAsyncEnumerable<T>
|
||||||
|
{
|
||||||
|
readonly ReadOnlyAsyncReactiveProperty<T> parent;
|
||||||
|
|
||||||
|
public WithoutCurrentEnumerable(ReadOnlyAsyncReactiveProperty<T> parent)
|
||||||
|
{
|
||||||
|
this.parent = parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IUniTaskAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
return new Enumerator(parent, cancellationToken, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sealed class Enumerator : MoveNextSource, IUniTaskAsyncEnumerator<T>, ITriggerHandler<T>
|
||||||
|
{
|
||||||
|
static Action<object> cancellationCallback = CancellationCallback;
|
||||||
|
|
||||||
|
readonly ReadOnlyAsyncReactiveProperty<T> parent;
|
||||||
|
readonly CancellationToken cancellationToken;
|
||||||
|
readonly CancellationTokenRegistration cancellationTokenRegistration;
|
||||||
|
T value;
|
||||||
|
bool isDisposed;
|
||||||
|
bool firstCall;
|
||||||
|
|
||||||
|
public Enumerator(ReadOnlyAsyncReactiveProperty<T> parent, CancellationToken cancellationToken, bool publishCurrentValue)
|
||||||
|
{
|
||||||
|
this.parent = parent;
|
||||||
|
this.cancellationToken = cancellationToken;
|
||||||
|
this.firstCall = publishCurrentValue;
|
||||||
|
|
||||||
|
parent.triggerEvent.Add(this);
|
||||||
|
TaskTracker.TrackActiveTask(this, 3);
|
||||||
|
|
||||||
|
if (cancellationToken.CanBeCanceled)
|
||||||
|
{
|
||||||
|
cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(cancellationCallback, this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public T Current => value;
|
||||||
|
ITriggerHandler<T> ITriggerHandler<T>.Prev { get; set; }
|
||||||
|
ITriggerHandler<T> ITriggerHandler<T>.Next { get; set; }
|
||||||
|
|
||||||
|
public UniTask<bool> MoveNextAsync()
|
||||||
|
{
|
||||||
|
// raise latest value on first call.
|
||||||
|
if (firstCall)
|
||||||
|
{
|
||||||
|
firstCall = false;
|
||||||
|
value = parent.Value;
|
||||||
|
return CompletedTasks.True;
|
||||||
|
}
|
||||||
|
|
||||||
|
completionSource.Reset();
|
||||||
|
return new UniTask<bool>(this, completionSource.Version);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTask DisposeAsync()
|
||||||
|
{
|
||||||
|
if (!isDisposed)
|
||||||
|
{
|
||||||
|
isDisposed = true;
|
||||||
|
TaskTracker.RemoveTracking(this);
|
||||||
|
completionSource.TrySetCanceled(cancellationToken);
|
||||||
|
parent.triggerEvent.Remove(this);
|
||||||
|
}
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnNext(T value)
|
||||||
|
{
|
||||||
|
this.value = value;
|
||||||
|
completionSource.TrySetResult(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnCanceled(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
DisposeAsync().Forget();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnCompleted()
|
||||||
|
{
|
||||||
|
completionSource.TrySetResult(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnError(Exception ex)
|
||||||
|
{
|
||||||
|
completionSource.TrySetException(ex);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void CancellationCallback(object state)
|
||||||
|
{
|
||||||
|
var self = (Enumerator)state;
|
||||||
|
self.DisposeAsync().Forget();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class StateExtensions
|
||||||
|
{
|
||||||
|
public static ReadOnlyAsyncReactiveProperty<T> ToReadOnlyAsyncReactiveProperty<T>(this IUniTaskAsyncEnumerable<T> source, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return new ReadOnlyAsyncReactiveProperty<T>(source, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ReadOnlyAsyncReactiveProperty<T> ToReadOnlyAsyncReactiveProperty<T>(this IUniTaskAsyncEnumerable<T> source, T initialValue, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return new ReadOnlyAsyncReactiveProperty<T>(initialValue, source, cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,7 +4,7 @@ using System;
|
|||||||
|
|
||||||
namespace Cysharp.Threading.Tasks
|
namespace Cysharp.Threading.Tasks
|
||||||
{
|
{
|
||||||
public struct AsyncUnit : IEquatable<AsyncUnit>
|
public readonly struct AsyncUnit : IEquatable<AsyncUnit>
|
||||||
{
|
{
|
||||||
public static readonly AsyncUnit Default = new AsyncUnit();
|
public static readonly AsyncUnit Default = new AsyncUnit();
|
||||||
|
|
||||||
|
|||||||
@@ -368,8 +368,8 @@ namespace Cysharp.Threading.Tasks
|
|||||||
readonly SingleConsumerUnboundedChannelReader parent;
|
readonly SingleConsumerUnboundedChannelReader parent;
|
||||||
CancellationToken cancellationToken1;
|
CancellationToken cancellationToken1;
|
||||||
CancellationToken cancellationToken2;
|
CancellationToken cancellationToken2;
|
||||||
CancellationTokenRegistration CancellationTokenRegistration1;
|
CancellationTokenRegistration cancellationTokenRegistration1;
|
||||||
CancellationTokenRegistration CancellationTokenRegistration2;
|
CancellationTokenRegistration cancellationTokenRegistration2;
|
||||||
|
|
||||||
T current;
|
T current;
|
||||||
bool cacheValue;
|
bool cacheValue;
|
||||||
@@ -395,12 +395,12 @@ namespace Cysharp.Threading.Tasks
|
|||||||
|
|
||||||
if (this.cancellationToken1.CanBeCanceled)
|
if (this.cancellationToken1.CanBeCanceled)
|
||||||
{
|
{
|
||||||
this.cancellationToken1.RegisterWithoutCaptureExecutionContext(CancellationCallback1Delegate, this);
|
this.cancellationTokenRegistration1 = this.cancellationToken1.RegisterWithoutCaptureExecutionContext(CancellationCallback1Delegate, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.cancellationToken2.CanBeCanceled)
|
if (this.cancellationToken2.CanBeCanceled)
|
||||||
{
|
{
|
||||||
this.cancellationToken2.RegisterWithoutCaptureExecutionContext(CancellationCallback2Delegate, this);
|
this.cancellationTokenRegistration2 = this.cancellationToken2.RegisterWithoutCaptureExecutionContext(CancellationCallback2Delegate, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
running = true;
|
running = true;
|
||||||
@@ -428,8 +428,8 @@ namespace Cysharp.Threading.Tasks
|
|||||||
|
|
||||||
public UniTask DisposeAsync()
|
public UniTask DisposeAsync()
|
||||||
{
|
{
|
||||||
CancellationTokenRegistration1.Dispose();
|
cancellationTokenRegistration1.Dispose();
|
||||||
CancellationTokenRegistration2.Dispose();
|
cancellationTokenRegistration2.Dispose();
|
||||||
return default;
|
return default;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,9 +12,8 @@ namespace Cysharp.Threading.Tasks.CompilerServices
|
|||||||
[StructLayout(LayoutKind.Auto)]
|
[StructLayout(LayoutKind.Auto)]
|
||||||
public struct AsyncUniTaskMethodBuilder
|
public struct AsyncUniTaskMethodBuilder
|
||||||
{
|
{
|
||||||
// cache items.
|
IStateMachineRunnerPromise runnerPromise;
|
||||||
AutoResetUniTaskCompletionSource promise;
|
Exception ex;
|
||||||
IMoveNextRunner runner;
|
|
||||||
|
|
||||||
// 1. Static Create method.
|
// 1. Static Create method.
|
||||||
[DebuggerHidden]
|
[DebuggerHidden]
|
||||||
@@ -31,98 +30,81 @@ namespace Cysharp.Threading.Tasks.CompilerServices
|
|||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
if (promise != null)
|
if (runnerPromise != null)
|
||||||
{
|
{
|
||||||
return promise.Task;
|
return runnerPromise.Task;
|
||||||
}
|
}
|
||||||
|
else if (ex != null)
|
||||||
if (runner == null)
|
{
|
||||||
|
return UniTask.FromException(ex);
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
return UniTask.CompletedTask;
|
return UniTask.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
promise = AutoResetUniTaskCompletionSource.Create();
|
|
||||||
return promise.Task;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. SetException
|
// 3. SetException
|
||||||
[DebuggerHidden]
|
[DebuggerHidden]
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void SetException(Exception exception)
|
public void SetException(Exception exception)
|
||||||
{
|
{
|
||||||
// runner is finished, return first.
|
if (runnerPromise == null)
|
||||||
if (runner != null)
|
|
||||||
{
|
{
|
||||||
runner.Return();
|
ex = exception;
|
||||||
runner = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (promise != null)
|
|
||||||
{
|
|
||||||
promise.TrySetException(exception);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
promise = AutoResetUniTaskCompletionSource.CreateFromException(exception, out _);
|
runnerPromise.SetException(exception);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. SetResult
|
// 4. SetResult
|
||||||
[DebuggerHidden]
|
[DebuggerHidden]
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void SetResult()
|
public void SetResult()
|
||||||
{
|
{
|
||||||
// runner is finished, return first.
|
if (runnerPromise != null)
|
||||||
if (runner != null)
|
|
||||||
{
|
{
|
||||||
runner.Return();
|
runnerPromise.SetResult();
|
||||||
runner = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (promise != null)
|
|
||||||
{
|
|
||||||
promise.TrySetResult();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 5. AwaitOnCompleted
|
// 5. AwaitOnCompleted
|
||||||
[DebuggerHidden]
|
[DebuggerHidden]
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine)
|
public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine)
|
||||||
where TAwaiter : INotifyCompletion
|
where TAwaiter : INotifyCompletion
|
||||||
where TStateMachine : IAsyncStateMachine
|
where TStateMachine : IAsyncStateMachine
|
||||||
{
|
{
|
||||||
if (promise == null)
|
if (runnerPromise == null)
|
||||||
{
|
{
|
||||||
promise = AutoResetUniTaskCompletionSource.Create();
|
AsyncUniTask<TStateMachine>.SetStateMachine(ref stateMachine, ref runnerPromise);
|
||||||
}
|
|
||||||
if (runner == null)
|
|
||||||
{
|
|
||||||
runner = MoveNextRunner<TStateMachine>.Create(ref stateMachine);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
awaiter.OnCompleted(runner.CallMoveNext);
|
awaiter.OnCompleted(runnerPromise.MoveNext);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 6. AwaitUnsafeOnCompleted
|
// 6. AwaitUnsafeOnCompleted
|
||||||
[DebuggerHidden]
|
[DebuggerHidden]
|
||||||
[SecuritySafeCritical]
|
[SecuritySafeCritical]
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine)
|
public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine)
|
||||||
where TAwaiter : ICriticalNotifyCompletion
|
where TAwaiter : ICriticalNotifyCompletion
|
||||||
where TStateMachine : IAsyncStateMachine
|
where TStateMachine : IAsyncStateMachine
|
||||||
{
|
{
|
||||||
if (promise == null)
|
if (runnerPromise == null)
|
||||||
{
|
{
|
||||||
promise = AutoResetUniTaskCompletionSource.Create();
|
AsyncUniTask<TStateMachine>.SetStateMachine(ref stateMachine, ref runnerPromise);
|
||||||
}
|
|
||||||
if (runner == null)
|
|
||||||
{
|
|
||||||
runner = MoveNextRunner<TStateMachine>.Create(ref stateMachine);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
awaiter.OnCompleted(runner.CallMoveNext);
|
awaiter.UnsafeOnCompleted(runnerPromise.MoveNext);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 7. Start
|
// 7. Start
|
||||||
[DebuggerHidden]
|
[DebuggerHidden]
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void Start<TStateMachine>(ref TStateMachine stateMachine)
|
public void Start<TStateMachine>(ref TStateMachine stateMachine)
|
||||||
where TStateMachine : IAsyncStateMachine
|
where TStateMachine : IAsyncStateMachine
|
||||||
{
|
{
|
||||||
@@ -156,9 +138,8 @@ namespace Cysharp.Threading.Tasks.CompilerServices
|
|||||||
[StructLayout(LayoutKind.Auto)]
|
[StructLayout(LayoutKind.Auto)]
|
||||||
public struct AsyncUniTaskMethodBuilder<T>
|
public struct AsyncUniTaskMethodBuilder<T>
|
||||||
{
|
{
|
||||||
// cache items.
|
IStateMachineRunnerPromise<T> runnerPromise;
|
||||||
AutoResetUniTaskCompletionSource<T> promise;
|
Exception ex;
|
||||||
IMoveNextRunner runner;
|
|
||||||
T result;
|
T result;
|
||||||
|
|
||||||
// 1. Static Create method.
|
// 1. Static Create method.
|
||||||
@@ -170,106 +151,91 @@ namespace Cysharp.Threading.Tasks.CompilerServices
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 2. TaskLike Task property.
|
// 2. TaskLike Task property.
|
||||||
[DebuggerHidden]
|
|
||||||
public UniTask<T> Task
|
public UniTask<T> Task
|
||||||
{
|
{
|
||||||
|
[DebuggerHidden]
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
if (promise != null)
|
if (runnerPromise != null)
|
||||||
{
|
{
|
||||||
return promise.Task;
|
return runnerPromise.Task;
|
||||||
}
|
}
|
||||||
|
else if (ex != null)
|
||||||
if (runner == null)
|
{
|
||||||
|
return UniTask.FromException<T>(ex);
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
return UniTask.FromResult(result);
|
return UniTask.FromResult(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
promise = AutoResetUniTaskCompletionSource<T>.Create();
|
|
||||||
return promise.Task;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. SetException
|
// 3. SetException
|
||||||
[DebuggerHidden]
|
[DebuggerHidden]
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void SetException(Exception exception)
|
public void SetException(Exception exception)
|
||||||
{
|
{
|
||||||
// runner is finished, return first.
|
if (runnerPromise == null)
|
||||||
if (runner != null)
|
|
||||||
{
|
{
|
||||||
runner.Return();
|
ex = exception;
|
||||||
runner = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (promise == null)
|
|
||||||
{
|
|
||||||
promise = AutoResetUniTaskCompletionSource<T>.CreateFromException(exception, out _);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
promise.TrySetException(exception);
|
runnerPromise.SetException(exception);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. SetResult
|
// 4. SetResult
|
||||||
[DebuggerHidden]
|
[DebuggerHidden]
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void SetResult(T result)
|
public void SetResult(T result)
|
||||||
{
|
{
|
||||||
// runner is finished, return first.
|
if (runnerPromise == null)
|
||||||
if (runner != null)
|
|
||||||
{
|
|
||||||
runner.Return();
|
|
||||||
runner = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (promise == null)
|
|
||||||
{
|
{
|
||||||
this.result = result;
|
this.result = result;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
promise.TrySetResult(result);
|
{
|
||||||
|
runnerPromise.SetResult(result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 5. AwaitOnCompleted
|
// 5. AwaitOnCompleted
|
||||||
[DebuggerHidden]
|
[DebuggerHidden]
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine)
|
public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine)
|
||||||
where TAwaiter : INotifyCompletion
|
where TAwaiter : INotifyCompletion
|
||||||
where TStateMachine : IAsyncStateMachine
|
where TStateMachine : IAsyncStateMachine
|
||||||
{
|
{
|
||||||
if (promise == null)
|
if (runnerPromise == null)
|
||||||
{
|
{
|
||||||
promise = AutoResetUniTaskCompletionSource<T>.Create();
|
AsyncUniTask<TStateMachine, T>.SetStateMachine(ref stateMachine, ref runnerPromise);
|
||||||
}
|
|
||||||
if (runner == null)
|
|
||||||
{
|
|
||||||
runner = MoveNextRunner<TStateMachine>.Create(ref stateMachine);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
awaiter.OnCompleted(runner.CallMoveNext);
|
awaiter.OnCompleted(runnerPromise.MoveNext);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 6. AwaitUnsafeOnCompleted
|
// 6. AwaitUnsafeOnCompleted
|
||||||
[DebuggerHidden]
|
[DebuggerHidden]
|
||||||
[SecuritySafeCritical]
|
[SecuritySafeCritical]
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine)
|
public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine)
|
||||||
where TAwaiter : ICriticalNotifyCompletion
|
where TAwaiter : ICriticalNotifyCompletion
|
||||||
where TStateMachine : IAsyncStateMachine
|
where TStateMachine : IAsyncStateMachine
|
||||||
{
|
{
|
||||||
if (promise == null)
|
if (runnerPromise == null)
|
||||||
{
|
{
|
||||||
promise = AutoResetUniTaskCompletionSource<T>.Create();
|
AsyncUniTask<TStateMachine, T>.SetStateMachine(ref stateMachine, ref runnerPromise);
|
||||||
}
|
|
||||||
if (runner == null)
|
|
||||||
{
|
|
||||||
runner = MoveNextRunner<TStateMachine>.Create(ref stateMachine);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
awaiter.OnCompleted(runner.CallMoveNext);
|
awaiter.UnsafeOnCompleted(runnerPromise.MoveNext);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 7. Start
|
// 7. Start
|
||||||
[DebuggerHidden]
|
[DebuggerHidden]
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void Start<TStateMachine>(ref TStateMachine stateMachine)
|
public void Start<TStateMachine>(ref TStateMachine stateMachine)
|
||||||
where TStateMachine : IAsyncStateMachine
|
where TStateMachine : IAsyncStateMachine
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -4,13 +4,15 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
using System.Security;
|
using System.Security;
|
||||||
|
|
||||||
namespace Cysharp.Threading.Tasks.CompilerServices
|
namespace Cysharp.Threading.Tasks.CompilerServices
|
||||||
{
|
{
|
||||||
|
[StructLayout(LayoutKind.Auto)]
|
||||||
public struct AsyncUniTaskVoidMethodBuilder
|
public struct AsyncUniTaskVoidMethodBuilder
|
||||||
{
|
{
|
||||||
IMoveNextRunner runner;
|
IStateMachineRunner runner;
|
||||||
|
|
||||||
// 1. Static Create method.
|
// 1. Static Create method.
|
||||||
[DebuggerHidden]
|
[DebuggerHidden]
|
||||||
@@ -33,6 +35,7 @@ namespace Cysharp.Threading.Tasks.CompilerServices
|
|||||||
|
|
||||||
// 3. SetException
|
// 3. SetException
|
||||||
[DebuggerHidden]
|
[DebuggerHidden]
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void SetException(Exception exception)
|
public void SetException(Exception exception)
|
||||||
{
|
{
|
||||||
// runner is finished, return first.
|
// runner is finished, return first.
|
||||||
@@ -47,6 +50,7 @@ namespace Cysharp.Threading.Tasks.CompilerServices
|
|||||||
|
|
||||||
// 4. SetResult
|
// 4. SetResult
|
||||||
[DebuggerHidden]
|
[DebuggerHidden]
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void SetResult()
|
public void SetResult()
|
||||||
{
|
{
|
||||||
// runner is finished, return.
|
// runner is finished, return.
|
||||||
@@ -59,31 +63,33 @@ namespace Cysharp.Threading.Tasks.CompilerServices
|
|||||||
|
|
||||||
// 5. AwaitOnCompleted
|
// 5. AwaitOnCompleted
|
||||||
[DebuggerHidden]
|
[DebuggerHidden]
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine)
|
public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine)
|
||||||
where TAwaiter : INotifyCompletion
|
where TAwaiter : INotifyCompletion
|
||||||
where TStateMachine : IAsyncStateMachine
|
where TStateMachine : IAsyncStateMachine
|
||||||
{
|
{
|
||||||
if (runner == null)
|
if (runner == null)
|
||||||
{
|
{
|
||||||
runner = MoveNextRunner<TStateMachine>.Create(ref stateMachine);
|
AsyncUniTaskVoid<TStateMachine>.SetStateMachine(ref stateMachine, ref runner);
|
||||||
}
|
}
|
||||||
|
|
||||||
awaiter.OnCompleted(runner.CallMoveNext);
|
awaiter.OnCompleted(runner.MoveNext);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 6. AwaitUnsafeOnCompleted
|
// 6. AwaitUnsafeOnCompleted
|
||||||
[DebuggerHidden]
|
[DebuggerHidden]
|
||||||
[SecuritySafeCritical]
|
[SecuritySafeCritical]
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine)
|
public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine)
|
||||||
where TAwaiter : ICriticalNotifyCompletion
|
where TAwaiter : ICriticalNotifyCompletion
|
||||||
where TStateMachine : IAsyncStateMachine
|
where TStateMachine : IAsyncStateMachine
|
||||||
{
|
{
|
||||||
if (runner == null)
|
if (runner == null)
|
||||||
{
|
{
|
||||||
runner = MoveNextRunner<TStateMachine>.Create(ref stateMachine);
|
AsyncUniTaskVoid<TStateMachine>.SetStateMachine(ref stateMachine, ref runner);
|
||||||
}
|
}
|
||||||
|
|
||||||
awaiter.OnCompleted(runner.CallMoveNext);
|
awaiter.UnsafeOnCompleted(runner.MoveNext);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 7. Start
|
// 7. Start
|
||||||
|
|||||||
@@ -1,57 +0,0 @@
|
|||||||
|
|
||||||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
|
||||||
|
|
||||||
using System;
|
|
||||||
using System.Diagnostics;
|
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
using Cysharp.Threading.Tasks.Internal;
|
|
||||||
|
|
||||||
namespace Cysharp.Threading.Tasks.CompilerServices
|
|
||||||
{
|
|
||||||
internal interface IMoveNextRunner
|
|
||||||
{
|
|
||||||
Action CallMoveNext { get; }
|
|
||||||
void Return();
|
|
||||||
}
|
|
||||||
|
|
||||||
internal sealed class MoveNextRunner<TStateMachine> : IMoveNextRunner, IPromisePoolItem
|
|
||||||
where TStateMachine : IAsyncStateMachine
|
|
||||||
{
|
|
||||||
static PromisePool<MoveNextRunner<TStateMachine>> pool = new PromisePool<MoveNextRunner<TStateMachine>>();
|
|
||||||
|
|
||||||
TStateMachine stateMachine;
|
|
||||||
internal readonly Action callMoveNext;
|
|
||||||
|
|
||||||
public Action CallMoveNext => callMoveNext;
|
|
||||||
|
|
||||||
MoveNextRunner()
|
|
||||||
{
|
|
||||||
callMoveNext = MoveNext;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static MoveNextRunner<TStateMachine> Create(ref TStateMachine stateMachine)
|
|
||||||
{
|
|
||||||
var result = pool.TryRent() ?? new MoveNextRunner<TStateMachine>();
|
|
||||||
result.stateMachine = stateMachine;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
[DebuggerHidden]
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
void MoveNext()
|
|
||||||
{
|
|
||||||
stateMachine.MoveNext();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Return()
|
|
||||||
{
|
|
||||||
pool.TryReturn(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
void IPromisePoolItem.Reset()
|
|
||||||
{
|
|
||||||
stateMachine = default;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -0,0 +1,343 @@
|
|||||||
|
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
||||||
|
|
||||||
|
using Cysharp.Threading.Tasks.Internal;
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
|
namespace Cysharp.Threading.Tasks.CompilerServices
|
||||||
|
{
|
||||||
|
internal interface IStateMachineRunner
|
||||||
|
{
|
||||||
|
Action MoveNext { get; }
|
||||||
|
void Return();
|
||||||
|
}
|
||||||
|
|
||||||
|
internal interface IStateMachineRunnerPromise : IUniTaskSource
|
||||||
|
{
|
||||||
|
Action MoveNext { get; }
|
||||||
|
UniTask Task { get; }
|
||||||
|
void SetResult();
|
||||||
|
void SetException(Exception exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal interface IStateMachineRunnerPromise<T> : IUniTaskSource<T>
|
||||||
|
{
|
||||||
|
Action MoveNext { get; }
|
||||||
|
UniTask<T> Task { get; }
|
||||||
|
void SetResult(T result);
|
||||||
|
void SetException(Exception exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static class StateMachineUtility
|
||||||
|
{
|
||||||
|
public static int GetState(IAsyncStateMachine stateMachine)
|
||||||
|
{
|
||||||
|
var info = stateMachine.GetType().GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
|
||||||
|
.First(x => x.Name.EndsWith("__state"));
|
||||||
|
return (int)info.GetValue(stateMachine);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal sealed class AsyncUniTaskVoid<TStateMachine> : IStateMachineRunner, ITaskPoolNode<AsyncUniTaskVoid<TStateMachine>>, IUniTaskSource
|
||||||
|
where TStateMachine : IAsyncStateMachine
|
||||||
|
{
|
||||||
|
static TaskPool<AsyncUniTaskVoid<TStateMachine>> pool;
|
||||||
|
|
||||||
|
IAsyncStateMachine stateMachine; // unfortunatelly boxed to fix IL2CPP issue.
|
||||||
|
|
||||||
|
public Action MoveNext { get; }
|
||||||
|
|
||||||
|
public AsyncUniTaskVoid()
|
||||||
|
{
|
||||||
|
MoveNext = Run;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void SetStateMachine(ref TStateMachine stateMachine, ref IStateMachineRunner runnerFieldRef)
|
||||||
|
{
|
||||||
|
if (!pool.TryPop(out var result))
|
||||||
|
{
|
||||||
|
result = new AsyncUniTaskVoid<TStateMachine>();
|
||||||
|
}
|
||||||
|
TaskTracker.TrackActiveTask(result, 3);
|
||||||
|
|
||||||
|
runnerFieldRef = result; // set runner before copied.
|
||||||
|
result.stateMachine = stateMachine; // copy struct StateMachine(in release build).
|
||||||
|
}
|
||||||
|
|
||||||
|
static AsyncUniTaskVoid()
|
||||||
|
{
|
||||||
|
TaskPool.RegisterSizeGetter(typeof(AsyncUniTaskVoid<TStateMachine>), () => pool.Size);
|
||||||
|
}
|
||||||
|
|
||||||
|
public AsyncUniTaskVoid<TStateMachine> NextNode { get; set; }
|
||||||
|
|
||||||
|
public void Return()
|
||||||
|
{
|
||||||
|
TaskTracker.RemoveTracking(this);
|
||||||
|
stateMachine = default;
|
||||||
|
pool.TryPush(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
[DebuggerHidden]
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
void Run()
|
||||||
|
{
|
||||||
|
stateMachine.MoveNext();
|
||||||
|
}
|
||||||
|
|
||||||
|
// dummy interface implementation for TaskTracker.
|
||||||
|
|
||||||
|
UniTaskStatus IUniTaskSource.GetStatus(short token)
|
||||||
|
{
|
||||||
|
return UniTaskStatus.Pending;
|
||||||
|
}
|
||||||
|
|
||||||
|
UniTaskStatus IUniTaskSource.UnsafeGetStatus()
|
||||||
|
{
|
||||||
|
return UniTaskStatus.Pending;
|
||||||
|
}
|
||||||
|
|
||||||
|
void IUniTaskSource.OnCompleted(Action<object> continuation, object state, short token)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void IUniTaskSource.GetResult(short token)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal sealed class AsyncUniTask<TStateMachine> : IStateMachineRunnerPromise, IUniTaskSource, ITaskPoolNode<AsyncUniTask<TStateMachine>>
|
||||||
|
where TStateMachine : IAsyncStateMachine
|
||||||
|
{
|
||||||
|
static TaskPool<AsyncUniTask<TStateMachine>> pool;
|
||||||
|
|
||||||
|
IAsyncStateMachine stateMachine; // unfortunatelly boxed to fix IL2CPP issue.
|
||||||
|
|
||||||
|
public Action MoveNext { get; }
|
||||||
|
|
||||||
|
UniTaskCompletionSourceCore<AsyncUnit> core;
|
||||||
|
|
||||||
|
AsyncUniTask()
|
||||||
|
{
|
||||||
|
MoveNext = Run;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void SetStateMachine(ref TStateMachine stateMachine, ref IStateMachineRunnerPromise runnerPromiseFieldRef)
|
||||||
|
{
|
||||||
|
if (!pool.TryPop(out var result))
|
||||||
|
{
|
||||||
|
result = new AsyncUniTask<TStateMachine>();
|
||||||
|
}
|
||||||
|
TaskTracker.TrackActiveTask(result, 3);
|
||||||
|
|
||||||
|
runnerPromiseFieldRef = result; // set runner before copied.
|
||||||
|
result.stateMachine = stateMachine; // copy struct StateMachine(in release build).
|
||||||
|
}
|
||||||
|
|
||||||
|
public AsyncUniTask<TStateMachine> NextNode { get; set; }
|
||||||
|
|
||||||
|
static AsyncUniTask()
|
||||||
|
{
|
||||||
|
TaskPool.RegisterSizeGetter(typeof(AsyncUniTask<TStateMachine>), () => pool.Size);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool TryReturn()
|
||||||
|
{
|
||||||
|
TaskTracker.RemoveTracking(this);
|
||||||
|
core.Reset();
|
||||||
|
stateMachine = default;
|
||||||
|
return pool.TryPush(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
[DebuggerHidden]
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
void Run()
|
||||||
|
{
|
||||||
|
stateMachine.MoveNext();
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTask Task
|
||||||
|
{
|
||||||
|
[DebuggerHidden]
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return new UniTask(this, core.Version);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[DebuggerHidden]
|
||||||
|
public void SetResult()
|
||||||
|
{
|
||||||
|
core.TrySetResult(AsyncUnit.Default);
|
||||||
|
}
|
||||||
|
|
||||||
|
[DebuggerHidden]
|
||||||
|
public void SetException(Exception exception)
|
||||||
|
{
|
||||||
|
core.TrySetException(exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
[DebuggerHidden]
|
||||||
|
public void GetResult(short token)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
core.GetResult(token);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
TryReturn();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[DebuggerHidden]
|
||||||
|
public UniTaskStatus GetStatus(short token)
|
||||||
|
{
|
||||||
|
return core.GetStatus(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
[DebuggerHidden]
|
||||||
|
public UniTaskStatus UnsafeGetStatus()
|
||||||
|
{
|
||||||
|
return core.UnsafeGetStatus();
|
||||||
|
}
|
||||||
|
|
||||||
|
[DebuggerHidden]
|
||||||
|
public void OnCompleted(Action<object> continuation, object state, short token)
|
||||||
|
{
|
||||||
|
core.OnCompleted(continuation, state, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
~AsyncUniTask()
|
||||||
|
{
|
||||||
|
if (TryReturn())
|
||||||
|
{
|
||||||
|
GC.ReRegisterForFinalize(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal sealed class AsyncUniTask<TStateMachine, T> : IStateMachineRunnerPromise<T>, IUniTaskSource<T>, ITaskPoolNode<AsyncUniTask<TStateMachine, T>>
|
||||||
|
where TStateMachine : IAsyncStateMachine
|
||||||
|
{
|
||||||
|
static TaskPool<AsyncUniTask<TStateMachine, T>> pool;
|
||||||
|
|
||||||
|
IAsyncStateMachine stateMachine; // unfortunatelly boxed to fix IL2CPP issue.
|
||||||
|
|
||||||
|
public Action MoveNext { get; }
|
||||||
|
|
||||||
|
UniTaskCompletionSourceCore<T> core;
|
||||||
|
|
||||||
|
AsyncUniTask()
|
||||||
|
{
|
||||||
|
MoveNext = Run;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void SetStateMachine(ref TStateMachine stateMachine, ref IStateMachineRunnerPromise<T> runnerPromiseFieldRef)
|
||||||
|
{
|
||||||
|
if (!pool.TryPop(out var result))
|
||||||
|
{
|
||||||
|
result = new AsyncUniTask<TStateMachine, T>();
|
||||||
|
}
|
||||||
|
TaskTracker.TrackActiveTask(result, 3);
|
||||||
|
|
||||||
|
runnerPromiseFieldRef = result; // set runner before copied.
|
||||||
|
result.stateMachine = stateMachine; // copy struct StateMachine(in release build).
|
||||||
|
|
||||||
|
// UnityEngine.Debug.Log($"SetStateMachine State:" + StateMachineUtility.GetState(stateMachine));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public AsyncUniTask<TStateMachine, T> NextNode { get; set; }
|
||||||
|
|
||||||
|
static AsyncUniTask()
|
||||||
|
{
|
||||||
|
TaskPool.RegisterSizeGetter(typeof(AsyncUniTask<TStateMachine, T>), () => pool.Size);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool TryReturn()
|
||||||
|
{
|
||||||
|
TaskTracker.RemoveTracking(this);
|
||||||
|
core.Reset();
|
||||||
|
stateMachine = default;
|
||||||
|
return pool.TryPush(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
[DebuggerHidden]
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
void Run()
|
||||||
|
{
|
||||||
|
// UnityEngine.Debug.Log($"MoveNext State:" + StateMachineUtility.GetState(stateMachine));
|
||||||
|
stateMachine.MoveNext();
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTask<T> Task
|
||||||
|
{
|
||||||
|
[DebuggerHidden]
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return new UniTask<T>(this, core.Version);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[DebuggerHidden]
|
||||||
|
public void SetResult(T result)
|
||||||
|
{
|
||||||
|
core.TrySetResult(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[DebuggerHidden]
|
||||||
|
public void SetException(Exception exception)
|
||||||
|
{
|
||||||
|
core.TrySetException(exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
[DebuggerHidden]
|
||||||
|
public T GetResult(short token)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return core.GetResult(token);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
TryReturn();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[DebuggerHidden]
|
||||||
|
void IUniTaskSource.GetResult(short token)
|
||||||
|
{
|
||||||
|
GetResult(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
[DebuggerHidden]
|
||||||
|
public UniTaskStatus GetStatus(short token)
|
||||||
|
{
|
||||||
|
return core.GetStatus(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
[DebuggerHidden]
|
||||||
|
public UniTaskStatus UnsafeGetStatus()
|
||||||
|
{
|
||||||
|
return core.UnsafeGetStatus();
|
||||||
|
}
|
||||||
|
|
||||||
|
[DebuggerHidden]
|
||||||
|
public void OnCompleted(Action<object> continuation, object state, short token)
|
||||||
|
{
|
||||||
|
core.OnCompleted(continuation, state, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
~AsyncUniTask()
|
||||||
|
{
|
||||||
|
if (TryReturn())
|
||||||
|
{
|
||||||
|
GC.ReRegisterForFinalize(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -12,24 +12,35 @@ namespace Cysharp.Threading.Tasks
|
|||||||
{
|
{
|
||||||
public static class EnumeratorAsyncExtensions
|
public static class EnumeratorAsyncExtensions
|
||||||
{
|
{
|
||||||
public static UniTask.Awaiter GetAwaiter(this IEnumerator enumerator)
|
public static UniTask.Awaiter GetAwaiter<T>(this T enumerator)
|
||||||
|
where T : IEnumerator
|
||||||
{
|
{
|
||||||
return new UniTask(EnumeratorPromise.Create(enumerator, PlayerLoopTiming.Update, CancellationToken.None, out var token), token).GetAwaiter();
|
var e = (IEnumerator)enumerator;
|
||||||
|
Error.ThrowArgumentNullException(e, nameof(enumerator));
|
||||||
|
return new UniTask(EnumeratorPromise.Create(e, PlayerLoopTiming.Update, CancellationToken.None, out var token), token).GetAwaiter();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UniTask ToUniTask(this IEnumerator enumerator)
|
public static UniTask WithCancellation(this IEnumerator enumerator, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
return new UniTask(EnumeratorPromise.Create(enumerator, PlayerLoopTiming.Update, CancellationToken.None, out var token), token);
|
Error.ThrowArgumentNullException(enumerator, nameof(enumerator));
|
||||||
|
return new UniTask(EnumeratorPromise.Create(enumerator, PlayerLoopTiming.Update, cancellationToken, out var token), token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UniTask ConfigureAwait(this IEnumerator enumerator, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken))
|
public static UniTask ToUniTask(this IEnumerator enumerator, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
{
|
{
|
||||||
|
Error.ThrowArgumentNullException(enumerator, nameof(enumerator));
|
||||||
return new UniTask(EnumeratorPromise.Create(enumerator, timing, cancellationToken, out var token), token);
|
return new UniTask(EnumeratorPromise.Create(enumerator, timing, cancellationToken, out var token), token);
|
||||||
}
|
}
|
||||||
|
|
||||||
class EnumeratorPromise : IUniTaskSource, IPlayerLoopItem, IPromisePoolItem
|
sealed class EnumeratorPromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode<EnumeratorPromise>
|
||||||
{
|
{
|
||||||
static readonly PromisePool<EnumeratorPromise> pool = new PromisePool<EnumeratorPromise>();
|
static TaskPool<EnumeratorPromise> pool;
|
||||||
|
public EnumeratorPromise NextNode { get; set; }
|
||||||
|
|
||||||
|
static EnumeratorPromise()
|
||||||
|
{
|
||||||
|
TaskPool.RegisterSizeGetter(typeof(EnumeratorPromise), () => pool.Size);
|
||||||
|
}
|
||||||
|
|
||||||
IEnumerator innerEnumerator;
|
IEnumerator innerEnumerator;
|
||||||
CancellationToken cancellationToken;
|
CancellationToken cancellationToken;
|
||||||
@@ -47,13 +58,15 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
|
return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
|
||||||
}
|
}
|
||||||
|
|
||||||
var result = pool.TryRent() ?? new EnumeratorPromise();
|
if (!pool.TryPop(out var result))
|
||||||
|
{
|
||||||
|
result = new EnumeratorPromise();
|
||||||
|
}
|
||||||
|
TaskTracker.TrackActiveTask(result, 3);
|
||||||
|
|
||||||
result.innerEnumerator = ConsumeEnumerator(innerEnumerator);
|
result.innerEnumerator = ConsumeEnumerator(innerEnumerator);
|
||||||
result.cancellationToken = cancellationToken;
|
result.cancellationToken = cancellationToken;
|
||||||
|
|
||||||
TaskTracker.TrackActiveTask(result, 3);
|
|
||||||
|
|
||||||
PlayerLoopHelper.AddAction(timing, result);
|
PlayerLoopHelper.AddAction(timing, result);
|
||||||
|
|
||||||
token = result.core.Version;
|
token = result.core.Version;
|
||||||
@@ -64,12 +77,11 @@ namespace Cysharp.Threading.Tasks
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
TaskTracker.RemoveTracking(this);
|
|
||||||
core.GetResult(token);
|
core.GetResult(token);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
pool.TryReturn(this);
|
TryReturn();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,16 +125,18 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Reset()
|
bool TryReturn()
|
||||||
{
|
{
|
||||||
|
TaskTracker.RemoveTracking(this);
|
||||||
core.Reset();
|
core.Reset();
|
||||||
innerEnumerator = default;
|
innerEnumerator = default;
|
||||||
cancellationToken = default;
|
cancellationToken = default;
|
||||||
|
return pool.TryPush(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
~EnumeratorPromise()
|
~EnumeratorPromise()
|
||||||
{
|
{
|
||||||
if (pool.TryReturn(this))
|
if (TryReturn())
|
||||||
{
|
{
|
||||||
GC.ReRegisterForFinalize(this);
|
GC.ReRegisterForFinalize(this);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,21 +13,23 @@ namespace Cysharp.Threading.Tasks
|
|||||||
{
|
{
|
||||||
public static class AddressableAsyncExtensions
|
public static class AddressableAsyncExtensions
|
||||||
{
|
{
|
||||||
#region AsyncOperationHandle
|
#region AsyncOperationHandle
|
||||||
|
|
||||||
public static AsyncOperationHandleAwaiter GetAwaiter(this AsyncOperationHandle handle)
|
public static AsyncOperationHandleAwaiter GetAwaiter(this AsyncOperationHandle handle)
|
||||||
{
|
{
|
||||||
return new AsyncOperationHandleAwaiter(handle);
|
return new AsyncOperationHandleAwaiter(handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UniTask ToUniTask(this AsyncOperationHandle handle)
|
public static UniTask WithCancellation(this AsyncOperationHandle handle, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
return new UniTask(AsyncOperationHandleConfiguredSource.Create(handle, PlayerLoopTiming.Update, null, CancellationToken.None, out var token), token);
|
if (handle.IsDone) return UniTask.CompletedTask;
|
||||||
|
return new UniTask(AsyncOperationHandleWithCancellationSource.Create(handle, cancellationToken, out var token), token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UniTask ConfigureAwait(this AsyncOperationHandle handle, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellation = default(CancellationToken))
|
public static UniTask ToUniTask(this AsyncOperationHandle handle, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
{
|
{
|
||||||
return new UniTask(AsyncOperationHandleConfiguredSource.Create(handle, timing, progress, cancellation, out var token), token);
|
if (handle.IsDone) return UniTask.CompletedTask;
|
||||||
|
return new UniTask(AsyncOperationHandleConfiguredSource.Create(handle, timing, progress, cancellationToken, out var token), token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct AsyncOperationHandleAwaiter : ICriticalNotifyCompletion
|
public struct AsyncOperationHandleAwaiter : ICriticalNotifyCompletion
|
||||||
@@ -70,14 +72,146 @@ namespace Cysharp.Threading.Tasks
|
|||||||
public void UnsafeOnCompleted(Action continuation)
|
public void UnsafeOnCompleted(Action continuation)
|
||||||
{
|
{
|
||||||
Error.ThrowWhenContinuationIsAlreadyRegistered(continuationAction);
|
Error.ThrowWhenContinuationIsAlreadyRegistered(continuationAction);
|
||||||
continuationAction = continuation.AsFuncOfT<AsyncOperationHandle>(); // allocate delegate.
|
continuationAction = PooledDelegate<AsyncOperationHandle>.Create(continuation);
|
||||||
handle.Completed += continuationAction;
|
handle.Completed += continuationAction;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class AsyncOperationHandleConfiguredSource : IUniTaskSource, IPlayerLoopItem, IPromisePoolItem
|
sealed class AsyncOperationHandleWithCancellationSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode<AsyncOperationHandleWithCancellationSource>
|
||||||
{
|
{
|
||||||
static readonly PromisePool<AsyncOperationHandleConfiguredSource> pool = new PromisePool<AsyncOperationHandleConfiguredSource>();
|
static TaskPool<AsyncOperationHandleWithCancellationSource> pool;
|
||||||
|
public AsyncOperationHandleWithCancellationSource NextNode { get; set; }
|
||||||
|
|
||||||
|
static AsyncOperationHandleWithCancellationSource()
|
||||||
|
{
|
||||||
|
TaskPool.RegisterSizeGetter(typeof(AsyncOperationHandleWithCancellationSource), () => pool.Size);
|
||||||
|
}
|
||||||
|
|
||||||
|
readonly Action<AsyncOperationHandle> continuationAction;
|
||||||
|
AsyncOperationHandle handle;
|
||||||
|
CancellationToken cancellationToken;
|
||||||
|
bool completed;
|
||||||
|
|
||||||
|
UniTaskCompletionSourceCore<AsyncUnit> core;
|
||||||
|
|
||||||
|
AsyncOperationHandleWithCancellationSource()
|
||||||
|
{
|
||||||
|
continuationAction = Continuation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IUniTaskSource Create(AsyncOperationHandle handle, CancellationToken cancellationToken, out short token)
|
||||||
|
{
|
||||||
|
if (cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!pool.TryPop(out var result))
|
||||||
|
{
|
||||||
|
result = new AsyncOperationHandleWithCancellationSource();
|
||||||
|
}
|
||||||
|
|
||||||
|
result.handle = handle;
|
||||||
|
result.cancellationToken = cancellationToken;
|
||||||
|
result.completed = false;
|
||||||
|
|
||||||
|
TaskTracker.TrackActiveTask(result, 3);
|
||||||
|
|
||||||
|
PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, result);
|
||||||
|
|
||||||
|
handle.Completed += result.continuationAction;
|
||||||
|
|
||||||
|
token = result.core.Version;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Continuation(AsyncOperationHandle _)
|
||||||
|
{
|
||||||
|
handle.Completed -= continuationAction;
|
||||||
|
|
||||||
|
if (completed)
|
||||||
|
{
|
||||||
|
TryReturn();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
completed = true;
|
||||||
|
if (handle.Status == AsyncOperationStatus.Failed)
|
||||||
|
{
|
||||||
|
core.TrySetException(handle.OperationException);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
core.TrySetResult(AsyncUnit.Default);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void GetResult(short token)
|
||||||
|
{
|
||||||
|
core.GetResult(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTaskStatus GetStatus(short token)
|
||||||
|
{
|
||||||
|
return core.GetStatus(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTaskStatus UnsafeGetStatus()
|
||||||
|
{
|
||||||
|
return core.UnsafeGetStatus();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnCompleted(Action<object> continuation, object state, short token)
|
||||||
|
{
|
||||||
|
core.OnCompleted(continuation, state, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool MoveNext()
|
||||||
|
{
|
||||||
|
if (completed)
|
||||||
|
{
|
||||||
|
TryReturn();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
completed = true;
|
||||||
|
core.TrySetCanceled(cancellationToken);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool TryReturn()
|
||||||
|
{
|
||||||
|
TaskTracker.RemoveTracking(this);
|
||||||
|
core.Reset();
|
||||||
|
handle = default;
|
||||||
|
cancellationToken = default;
|
||||||
|
return pool.TryPush(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
~AsyncOperationHandleWithCancellationSource()
|
||||||
|
{
|
||||||
|
if (TryReturn())
|
||||||
|
{
|
||||||
|
GC.ReRegisterForFinalize(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sealed class AsyncOperationHandleConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode<AsyncOperationHandleConfiguredSource>
|
||||||
|
{
|
||||||
|
static TaskPool<AsyncOperationHandleConfiguredSource> pool;
|
||||||
|
public AsyncOperationHandleConfiguredSource NextNode { get; set; }
|
||||||
|
|
||||||
|
static AsyncOperationHandleConfiguredSource()
|
||||||
|
{
|
||||||
|
TaskPool.RegisterSizeGetter(typeof(AsyncOperationHandleConfiguredSource), () => pool.Size);
|
||||||
|
}
|
||||||
|
|
||||||
AsyncOperationHandle handle;
|
AsyncOperationHandle handle;
|
||||||
IProgress<float> progress;
|
IProgress<float> progress;
|
||||||
@@ -97,7 +231,10 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
|
return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
|
||||||
}
|
}
|
||||||
|
|
||||||
var result = pool.TryRent() ?? new AsyncOperationHandleConfiguredSource();
|
if (!pool.TryPop(out var result))
|
||||||
|
{
|
||||||
|
result = new AsyncOperationHandleConfiguredSource();
|
||||||
|
}
|
||||||
|
|
||||||
result.handle = handle;
|
result.handle = handle;
|
||||||
result.progress = progress;
|
result.progress = progress;
|
||||||
@@ -115,12 +252,12 @@ namespace Cysharp.Threading.Tasks
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
TaskTracker.RemoveTracking(this);
|
|
||||||
core.GetResult(token);
|
core.GetResult(token);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
pool.TryReturn(this);
|
TryReturn();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,40 +305,44 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Reset()
|
bool TryReturn()
|
||||||
{
|
{
|
||||||
core.Reset();
|
core.Reset();
|
||||||
|
TaskTracker.RemoveTracking(this);
|
||||||
handle = default;
|
handle = default;
|
||||||
progress = default;
|
progress = default;
|
||||||
cancellationToken = default;
|
cancellationToken = default;
|
||||||
|
return pool.TryPush(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
~AsyncOperationHandleConfiguredSource()
|
~AsyncOperationHandleConfiguredSource()
|
||||||
{
|
{
|
||||||
if (pool.TryReturn(this))
|
if (TryReturn())
|
||||||
{
|
{
|
||||||
GC.ReRegisterForFinalize(this);
|
GC.ReRegisterForFinalize(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region AsyncOperationHandle_T
|
#region AsyncOperationHandle_T
|
||||||
|
|
||||||
public static AsyncOperationHandleAwaiter<T> GetAwaiter<T>(this AsyncOperationHandle<T> handle)
|
public static AsyncOperationHandleAwaiter<T> GetAwaiter<T>(this AsyncOperationHandle<T> handle)
|
||||||
{
|
{
|
||||||
return new AsyncOperationHandleAwaiter<T>(handle);
|
return new AsyncOperationHandleAwaiter<T>(handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UniTask<T> ToUniTask<T>(this AsyncOperationHandle<T> handle)
|
public static UniTask<T> WithCancellation<T>(this AsyncOperationHandle<T> handle, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
return new UniTask<T>(AsyncOperationHandleConfiguredSource<T>.Create(handle, PlayerLoopTiming.Update, null, CancellationToken.None, out var token), token);
|
if (handle.IsDone) return UniTask.FromResult(handle.Result);
|
||||||
|
return new UniTask<T>(AsyncOperationHandleWithCancellationSource<T>.Create(handle, cancellationToken, out var token), token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UniTask<T> ConfigureAwait<T>(this AsyncOperationHandle<T> handle, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellation = default(CancellationToken))
|
public static UniTask<T> ToUniTask<T>(this AsyncOperationHandle<T> handle, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
{
|
{
|
||||||
return new UniTask<T>(AsyncOperationHandleConfiguredSource<T>.Create(handle, timing, progress, cancellation, out var token), token);
|
if (handle.IsDone) return UniTask.FromResult(handle.Result);
|
||||||
|
return new UniTask<T>(AsyncOperationHandleConfiguredSource<T>.Create(handle, timing, progress, cancellationToken, out var token), token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct AsyncOperationHandleAwaiter<T> : ICriticalNotifyCompletion
|
public struct AsyncOperationHandleAwaiter<T> : ICriticalNotifyCompletion
|
||||||
@@ -245,14 +386,151 @@ namespace Cysharp.Threading.Tasks
|
|||||||
public void UnsafeOnCompleted(Action continuation)
|
public void UnsafeOnCompleted(Action continuation)
|
||||||
{
|
{
|
||||||
Error.ThrowWhenContinuationIsAlreadyRegistered(continuationAction);
|
Error.ThrowWhenContinuationIsAlreadyRegistered(continuationAction);
|
||||||
continuationAction = continuation.AsFuncOfT<AsyncOperationHandle>(); // allocate delegate.
|
continuationAction = PooledDelegate<AsyncOperationHandle>.Create(continuation);
|
||||||
handle.CompletedTypeless += continuationAction;
|
handle.CompletedTypeless += continuationAction;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class AsyncOperationHandleConfiguredSource<T> : IUniTaskSource<T>, IPlayerLoopItem, IPromisePoolItem
|
sealed class AsyncOperationHandleWithCancellationSource<T> : IUniTaskSource<T>, IPlayerLoopItem, ITaskPoolNode<AsyncOperationHandleWithCancellationSource<T>>
|
||||||
{
|
{
|
||||||
static readonly PromisePool<AsyncOperationHandleConfiguredSource<T>> pool = new PromisePool<AsyncOperationHandleConfiguredSource<T>>();
|
static TaskPool<AsyncOperationHandleWithCancellationSource<T>> pool;
|
||||||
|
public AsyncOperationHandleWithCancellationSource<T> NextNode { get; set; }
|
||||||
|
|
||||||
|
static AsyncOperationHandleWithCancellationSource()
|
||||||
|
{
|
||||||
|
TaskPool.RegisterSizeGetter(typeof(AsyncOperationHandleWithCancellationSource<T>), () => pool.Size);
|
||||||
|
}
|
||||||
|
|
||||||
|
readonly Action<AsyncOperationHandle<T>> continuationAction;
|
||||||
|
AsyncOperationHandle<T> handle;
|
||||||
|
CancellationToken cancellationToken;
|
||||||
|
bool completed;
|
||||||
|
|
||||||
|
UniTaskCompletionSourceCore<T> core;
|
||||||
|
|
||||||
|
AsyncOperationHandleWithCancellationSource()
|
||||||
|
{
|
||||||
|
continuationAction = Continuation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IUniTaskSource<T> Create(AsyncOperationHandle<T> handle, CancellationToken cancellationToken, out short token)
|
||||||
|
{
|
||||||
|
if (cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
return AutoResetUniTaskCompletionSource<T>.CreateFromCanceled(cancellationToken, out token);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!pool.TryPop(out var result))
|
||||||
|
{
|
||||||
|
result = new AsyncOperationHandleWithCancellationSource<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
result.handle = handle;
|
||||||
|
result.cancellationToken = cancellationToken;
|
||||||
|
result.completed = false;
|
||||||
|
|
||||||
|
TaskTracker.TrackActiveTask(result, 3);
|
||||||
|
|
||||||
|
PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, result);
|
||||||
|
|
||||||
|
handle.Completed += result.continuationAction;
|
||||||
|
|
||||||
|
token = result.core.Version;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Continuation(AsyncOperationHandle<T> _)
|
||||||
|
{
|
||||||
|
handle.Completed -= continuationAction;
|
||||||
|
|
||||||
|
if (completed)
|
||||||
|
{
|
||||||
|
TryReturn();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
completed = true;
|
||||||
|
if (handle.Status == AsyncOperationStatus.Failed)
|
||||||
|
{
|
||||||
|
core.TrySetException(handle.OperationException);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
core.TrySetResult(handle.Result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public T GetResult(short token)
|
||||||
|
{
|
||||||
|
return core.GetResult(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
void IUniTaskSource.GetResult(short token)
|
||||||
|
{
|
||||||
|
GetResult(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTaskStatus GetStatus(short token)
|
||||||
|
{
|
||||||
|
return core.GetStatus(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTaskStatus UnsafeGetStatus()
|
||||||
|
{
|
||||||
|
return core.UnsafeGetStatus();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnCompleted(Action<object> continuation, object state, short token)
|
||||||
|
{
|
||||||
|
core.OnCompleted(continuation, state, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool MoveNext()
|
||||||
|
{
|
||||||
|
if (completed)
|
||||||
|
{
|
||||||
|
TryReturn();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
completed = true;
|
||||||
|
core.TrySetCanceled(cancellationToken);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool TryReturn()
|
||||||
|
{
|
||||||
|
TaskTracker.RemoveTracking(this);
|
||||||
|
core.Reset();
|
||||||
|
handle = default;
|
||||||
|
cancellationToken = default;
|
||||||
|
return pool.TryPush(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
~AsyncOperationHandleWithCancellationSource()
|
||||||
|
{
|
||||||
|
if (TryReturn())
|
||||||
|
{
|
||||||
|
GC.ReRegisterForFinalize(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sealed class AsyncOperationHandleConfiguredSource<T> : IUniTaskSource<T>, IPlayerLoopItem, ITaskPoolNode<AsyncOperationHandleConfiguredSource<T>>
|
||||||
|
{
|
||||||
|
static TaskPool<AsyncOperationHandleConfiguredSource<T>> pool;
|
||||||
|
public AsyncOperationHandleConfiguredSource<T> NextNode { get; set; }
|
||||||
|
|
||||||
|
static AsyncOperationHandleConfiguredSource()
|
||||||
|
{
|
||||||
|
TaskPool.RegisterSizeGetter(typeof(AsyncOperationHandleConfiguredSource<T>), () => pool.Size);
|
||||||
|
}
|
||||||
|
|
||||||
AsyncOperationHandle<T> handle;
|
AsyncOperationHandle<T> handle;
|
||||||
IProgress<float> progress;
|
IProgress<float> progress;
|
||||||
@@ -272,7 +550,10 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return AutoResetUniTaskCompletionSource<T>.CreateFromCanceled(cancellationToken, out token);
|
return AutoResetUniTaskCompletionSource<T>.CreateFromCanceled(cancellationToken, out token);
|
||||||
}
|
}
|
||||||
|
|
||||||
var result = pool.TryRent() ?? new AsyncOperationHandleConfiguredSource<T>();
|
if (!pool.TryPop(out var result))
|
||||||
|
{
|
||||||
|
result = new AsyncOperationHandleConfiguredSource<T>();
|
||||||
|
}
|
||||||
|
|
||||||
result.handle = handle;
|
result.handle = handle;
|
||||||
result.progress = progress;
|
result.progress = progress;
|
||||||
@@ -290,13 +571,11 @@ namespace Cysharp.Threading.Tasks
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
TaskTracker.RemoveTracking(this);
|
|
||||||
|
|
||||||
return core.GetResult(token);
|
return core.GetResult(token);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
pool.TryReturn(this);
|
TryReturn();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -349,24 +628,26 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Reset()
|
bool TryReturn()
|
||||||
{
|
{
|
||||||
|
TaskTracker.RemoveTracking(this);
|
||||||
core.Reset();
|
core.Reset();
|
||||||
handle = default;
|
handle = default;
|
||||||
progress = default;
|
progress = default;
|
||||||
cancellationToken = default;
|
cancellationToken = default;
|
||||||
|
return pool.TryPush(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
~AsyncOperationHandleConfiguredSource()
|
~AsyncOperationHandleConfiguredSource()
|
||||||
{
|
{
|
||||||
if (pool.TryReturn(this))
|
if (TryReturn())
|
||||||
{
|
{
|
||||||
GC.ReRegisterForFinalize(this);
|
GC.ReRegisterForFinalize(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
332
src/UniTask/Assets/Plugins/UniTask/Runtime/External/DoTweenAsyncExtensions.cs
vendored
Normal file
332
src/UniTask/Assets/Plugins/UniTask/Runtime/External/DoTweenAsyncExtensions.cs
vendored
Normal file
@@ -0,0 +1,332 @@
|
|||||||
|
// asmdef Version Defines, enabled when com.demigiant.dotween is imported.
|
||||||
|
|
||||||
|
#if UNITASK_DOTWEEN_SUPPORT
|
||||||
|
|
||||||
|
using Cysharp.Threading.Tasks.Internal;
|
||||||
|
using DG.Tweening;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
namespace Cysharp.Threading.Tasks
|
||||||
|
{
|
||||||
|
// The idea of TweenCancelBehaviour is borrowed from https://www.shibuya24.info/entry/dotween_async_await
|
||||||
|
public enum TweenCancelBehaviour
|
||||||
|
{
|
||||||
|
Kill,
|
||||||
|
KillWithCompleteCallback,
|
||||||
|
Complete,
|
||||||
|
CompleteWithSeqeunceCallback,
|
||||||
|
CancelAwait,
|
||||||
|
|
||||||
|
// AndCancelAwait
|
||||||
|
KillAndCancelAwait,
|
||||||
|
KillWithCompleteCallbackAndCancelAwait,
|
||||||
|
CompleteAndCancelAwait,
|
||||||
|
CompleteWithSeqeunceCallbackAndCancelAwait
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class DOTweenAsyncExtensions
|
||||||
|
{
|
||||||
|
public static TweenAwaiter GetAwaiter(this Tween tween)
|
||||||
|
{
|
||||||
|
return new TweenAwaiter(tween);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UniTask WithCancellation(this Tween tween, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
Error.ThrowArgumentNullException(tween, nameof(tween));
|
||||||
|
|
||||||
|
if (!tween.IsActive()) return UniTask.CompletedTask;
|
||||||
|
return new UniTask(TweenConfiguredSource.Create(tween, TweenCancelBehaviour.Kill, cancellationToken, out var token), token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UniTask ToUniTask(this Tween tween, TweenCancelBehaviour tweenCancelBehaviour = TweenCancelBehaviour.Kill, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
Error.ThrowArgumentNullException(tween, nameof(tween));
|
||||||
|
|
||||||
|
if (!tween.IsActive()) return UniTask.CompletedTask;
|
||||||
|
return new UniTask(TweenConfiguredSource.Create(tween, tweenCancelBehaviour, cancellationToken, out var token), token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct TweenAwaiter : ICriticalNotifyCompletion
|
||||||
|
{
|
||||||
|
readonly Tween tween;
|
||||||
|
|
||||||
|
// killed(non active) as completed.
|
||||||
|
public bool IsCompleted => !tween.IsActive();
|
||||||
|
|
||||||
|
public TweenAwaiter(Tween tween)
|
||||||
|
{
|
||||||
|
this.tween = tween;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TweenAwaiter GetAwaiter()
|
||||||
|
{
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void GetResult()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnCompleted(System.Action continuation)
|
||||||
|
{
|
||||||
|
UnsafeOnCompleted(continuation);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UnsafeOnCompleted(System.Action continuation)
|
||||||
|
{
|
||||||
|
// onKill is called after OnCompleted, both Complete(false/true) and Kill(false/true).
|
||||||
|
tween.onKill = PooledTweenCallback.Create(continuation);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sealed class TweenConfiguredSource : IUniTaskSource, ITaskPoolNode<TweenConfiguredSource>
|
||||||
|
{
|
||||||
|
static TaskPool<TweenConfiguredSource> pool;
|
||||||
|
public TweenConfiguredSource NextNode { get; set; }
|
||||||
|
|
||||||
|
static TweenConfiguredSource()
|
||||||
|
{
|
||||||
|
TaskPool.RegisterSizeGetter(typeof(TweenConfiguredSource), () => pool.Size);
|
||||||
|
}
|
||||||
|
|
||||||
|
static readonly TweenCallback EmptyTweenCallback = () => { };
|
||||||
|
|
||||||
|
readonly TweenCallback onKillDelegate;
|
||||||
|
readonly TweenCallback onUpdateDelegate;
|
||||||
|
|
||||||
|
Tween tween;
|
||||||
|
TweenCancelBehaviour cancelBehaviour;
|
||||||
|
CancellationToken cancellationToken;
|
||||||
|
bool canceled;
|
||||||
|
|
||||||
|
TweenCallback originalUpdateAction;
|
||||||
|
UniTaskCompletionSourceCore<AsyncUnit> core;
|
||||||
|
|
||||||
|
TweenConfiguredSource()
|
||||||
|
{
|
||||||
|
onKillDelegate = OnKill;
|
||||||
|
onUpdateDelegate = OnUpdate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IUniTaskSource Create(Tween tween, TweenCancelBehaviour cancelBehaviour, CancellationToken cancellationToken, out short token)
|
||||||
|
{
|
||||||
|
if (cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
DoCancelBeforeCreate(tween, cancelBehaviour);
|
||||||
|
return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!pool.TryPop(out var result))
|
||||||
|
{
|
||||||
|
result = new TweenConfiguredSource();
|
||||||
|
}
|
||||||
|
|
||||||
|
result.tween = tween;
|
||||||
|
result.cancelBehaviour = cancelBehaviour;
|
||||||
|
result.cancellationToken = cancellationToken;
|
||||||
|
|
||||||
|
result.originalUpdateAction = tween.onUpdate;
|
||||||
|
result.canceled = false;
|
||||||
|
|
||||||
|
if (result.originalUpdateAction == result.onUpdateDelegate)
|
||||||
|
{
|
||||||
|
result.originalUpdateAction = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
tween.onUpdate = result.onUpdateDelegate;
|
||||||
|
tween.onKill = result.onKillDelegate;
|
||||||
|
|
||||||
|
TaskTracker.TrackActiveTask(result, 3);
|
||||||
|
|
||||||
|
token = result.core.Version;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void OnKill()
|
||||||
|
{
|
||||||
|
if (canceled)
|
||||||
|
{
|
||||||
|
core.TrySetCanceled(cancellationToken);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
core.TrySetResult(AsyncUnit.Default);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void OnUpdate()
|
||||||
|
{
|
||||||
|
originalUpdateAction?.Invoke();
|
||||||
|
|
||||||
|
if (!cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (this.cancelBehaviour)
|
||||||
|
{
|
||||||
|
case TweenCancelBehaviour.Kill:
|
||||||
|
default:
|
||||||
|
this.tween.Kill(false);
|
||||||
|
break;
|
||||||
|
case TweenCancelBehaviour.KillAndCancelAwait:
|
||||||
|
this.canceled = true;
|
||||||
|
this.tween.Kill(false);
|
||||||
|
break;
|
||||||
|
case TweenCancelBehaviour.KillWithCompleteCallback:
|
||||||
|
this.tween.Kill(true);
|
||||||
|
break;
|
||||||
|
case TweenCancelBehaviour.KillWithCompleteCallbackAndCancelAwait:
|
||||||
|
this.canceled = true;
|
||||||
|
this.tween.Kill(true);
|
||||||
|
break;
|
||||||
|
case TweenCancelBehaviour.Complete:
|
||||||
|
this.tween.Complete(false);
|
||||||
|
break;
|
||||||
|
case TweenCancelBehaviour.CompleteAndCancelAwait:
|
||||||
|
this.canceled = true;
|
||||||
|
this.tween.Complete(false);
|
||||||
|
break;
|
||||||
|
case TweenCancelBehaviour.CompleteWithSeqeunceCallback:
|
||||||
|
this.tween.Complete(true);
|
||||||
|
break;
|
||||||
|
case TweenCancelBehaviour.CompleteWithSeqeunceCallbackAndCancelAwait:
|
||||||
|
this.canceled = true;
|
||||||
|
this.tween.Complete(true);
|
||||||
|
break;
|
||||||
|
case TweenCancelBehaviour.CancelAwait:
|
||||||
|
this.tween.onKill = EmptyTweenCallback; // replace to empty(avoid callback after Canceled(instance is returned to pool.)
|
||||||
|
this.core.TrySetCanceled(this.cancellationToken);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void DoCancelBeforeCreate(Tween tween, TweenCancelBehaviour tweenCancelBehaviour)
|
||||||
|
{
|
||||||
|
|
||||||
|
switch (tweenCancelBehaviour)
|
||||||
|
{
|
||||||
|
case TweenCancelBehaviour.Kill:
|
||||||
|
default:
|
||||||
|
tween.Kill(false);
|
||||||
|
break;
|
||||||
|
case TweenCancelBehaviour.KillAndCancelAwait:
|
||||||
|
tween.Kill(false);
|
||||||
|
break;
|
||||||
|
case TweenCancelBehaviour.KillWithCompleteCallback:
|
||||||
|
tween.Kill(true);
|
||||||
|
break;
|
||||||
|
case TweenCancelBehaviour.KillWithCompleteCallbackAndCancelAwait:
|
||||||
|
tween.Kill(true);
|
||||||
|
break;
|
||||||
|
case TweenCancelBehaviour.Complete:
|
||||||
|
tween.Complete(false);
|
||||||
|
break;
|
||||||
|
case TweenCancelBehaviour.CompleteAndCancelAwait:
|
||||||
|
tween.Complete(false);
|
||||||
|
break;
|
||||||
|
case TweenCancelBehaviour.CompleteWithSeqeunceCallback:
|
||||||
|
tween.Complete(true);
|
||||||
|
break;
|
||||||
|
case TweenCancelBehaviour.CompleteWithSeqeunceCallbackAndCancelAwait:
|
||||||
|
tween.Complete(true);
|
||||||
|
break;
|
||||||
|
case TweenCancelBehaviour.CancelAwait:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void GetResult(short token)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
core.GetResult(token);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
TryReturn();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTaskStatus GetStatus(short token)
|
||||||
|
{
|
||||||
|
return core.GetStatus(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTaskStatus UnsafeGetStatus()
|
||||||
|
{
|
||||||
|
return core.UnsafeGetStatus();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnCompleted(Action<object> continuation, object state, short token)
|
||||||
|
{
|
||||||
|
core.OnCompleted(continuation, state, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool TryReturn()
|
||||||
|
{
|
||||||
|
TaskTracker.RemoveTracking(this);
|
||||||
|
core.Reset();
|
||||||
|
tween.onUpdate = originalUpdateAction;
|
||||||
|
tween.onKill = null;
|
||||||
|
tween = default;
|
||||||
|
cancellationToken = default;
|
||||||
|
originalUpdateAction = default;
|
||||||
|
return pool.TryPush(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
~TweenConfiguredSource()
|
||||||
|
{
|
||||||
|
if (TryReturn())
|
||||||
|
{
|
||||||
|
GC.ReRegisterForFinalize(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sealed class PooledTweenCallback
|
||||||
|
{
|
||||||
|
static readonly ConcurrentQueue<PooledTweenCallback> pool = new ConcurrentQueue<PooledTweenCallback>();
|
||||||
|
|
||||||
|
readonly TweenCallback runDelegate;
|
||||||
|
|
||||||
|
Action continuation;
|
||||||
|
|
||||||
|
|
||||||
|
PooledTweenCallback()
|
||||||
|
{
|
||||||
|
runDelegate = Run;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static TweenCallback Create(Action continuation)
|
||||||
|
{
|
||||||
|
if (!pool.TryDequeue(out var item))
|
||||||
|
{
|
||||||
|
item = new PooledTweenCallback();
|
||||||
|
}
|
||||||
|
|
||||||
|
item.continuation = continuation;
|
||||||
|
return item.runDelegate;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
void Run()
|
||||||
|
{
|
||||||
|
var call = continuation;
|
||||||
|
continuation = null;
|
||||||
|
if (call != null)
|
||||||
|
{
|
||||||
|
pool.Enqueue(this);
|
||||||
|
call.Invoke();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
fileFormatVersion: 2
|
fileFormatVersion: 2
|
||||||
guid: 4d5a9a3e1f0f069478969f752fde29a9
|
guid: 1f448d5bc5b232e4f98d89d5d1832e8e
|
||||||
MonoImporter:
|
MonoImporter:
|
||||||
externalObjects: {}
|
externalObjects: {}
|
||||||
serializedVersion: 2
|
serializedVersion: 2
|
||||||
@@ -19,17 +19,75 @@ namespace Cysharp.Threading.Tasks
|
|||||||
|
|
||||||
// similar as IValueTaskSource
|
// similar as IValueTaskSource
|
||||||
public interface IUniTaskSource
|
public interface IUniTaskSource
|
||||||
|
#if !UNITY_2018_3_OR_NEWER && !NETSTANDARD2_0
|
||||||
|
: System.Threading.Tasks.Sources.IValueTaskSource
|
||||||
|
#pragma warning disable CS0108
|
||||||
|
#endif
|
||||||
{
|
{
|
||||||
UniTaskStatus GetStatus(short token);
|
UniTaskStatus GetStatus(short token);
|
||||||
void OnCompleted(Action<object> continuation, object state, short token);
|
void OnCompleted(Action<object> continuation, object state, short token);
|
||||||
void GetResult(short token);
|
void GetResult(short token);
|
||||||
|
|
||||||
UniTaskStatus UnsafeGetStatus(); // only for debug use.
|
UniTaskStatus UnsafeGetStatus(); // only for debug use.
|
||||||
|
|
||||||
|
#if !UNITY_2018_3_OR_NEWER && !NETSTANDARD2_0
|
||||||
|
#pragma warning restore CS0108
|
||||||
|
|
||||||
|
System.Threading.Tasks.Sources.ValueTaskSourceStatus System.Threading.Tasks.Sources.IValueTaskSource.GetStatus(short token)
|
||||||
|
{
|
||||||
|
return (System.Threading.Tasks.Sources.ValueTaskSourceStatus)(int)((IUniTaskSource)this).GetStatus(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
void System.Threading.Tasks.Sources.IValueTaskSource.GetResult(short token)
|
||||||
|
{
|
||||||
|
((IUniTaskSource)this).GetResult(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
void System.Threading.Tasks.Sources.IValueTaskSource.OnCompleted(Action<object> continuation, object state, short token, System.Threading.Tasks.Sources.ValueTaskSourceOnCompletedFlags flags)
|
||||||
|
{
|
||||||
|
// ignore flags, always none.
|
||||||
|
((IUniTaskSource)this).OnCompleted(continuation, state, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface IUniTaskSource<out T> : IUniTaskSource
|
public interface IUniTaskSource<out T> : IUniTaskSource
|
||||||
|
#if !UNITY_2018_3_OR_NEWER && !NETSTANDARD2_0
|
||||||
|
, System.Threading.Tasks.Sources.IValueTaskSource<T>
|
||||||
|
#endif
|
||||||
{
|
{
|
||||||
new T GetResult(short token);
|
new T GetResult(short token);
|
||||||
|
|
||||||
|
#if !UNITY_2018_3_OR_NEWER && !NETSTANDARD2_0
|
||||||
|
|
||||||
|
new public UniTaskStatus GetStatus(short token)
|
||||||
|
{
|
||||||
|
return ((IUniTaskSource)this).GetStatus(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
new public void OnCompleted(Action<object> continuation, object state, short token)
|
||||||
|
{
|
||||||
|
((IUniTaskSource)this).OnCompleted(continuation, state, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.Threading.Tasks.Sources.ValueTaskSourceStatus System.Threading.Tasks.Sources.IValueTaskSource<T>.GetStatus(short token)
|
||||||
|
{
|
||||||
|
return (System.Threading.Tasks.Sources.ValueTaskSourceStatus)(int)((IUniTaskSource)this).GetStatus(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
T System.Threading.Tasks.Sources.IValueTaskSource<T>.GetResult(short token)
|
||||||
|
{
|
||||||
|
return ((IUniTaskSource<T>)this).GetResult(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
void System.Threading.Tasks.Sources.IValueTaskSource<T>.OnCompleted(Action<object> continuation, object state, short token, System.Threading.Tasks.Sources.ValueTaskSourceOnCompletedFlags flags)
|
||||||
|
{
|
||||||
|
// ignore flags, always none.
|
||||||
|
((IUniTaskSource)this).OnCompleted(continuation, state, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class UniTaskStatusExtensions
|
public static class UniTaskStatusExtensions
|
||||||
|
|||||||
@@ -239,7 +239,7 @@ namespace Cysharp.Threading.Tasks.Internal
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var fname = fi.FullName.Replace(Path.DirectorySeparatorChar, '/').Replace(Application.dataPath, "");
|
var fname = fi.FullName.Replace(Path.DirectorySeparatorChar, '/').Replace(PlayerLoopHelper.ApplicationDataPath, "");
|
||||||
var withAssetsPath = "Assets/" + fname;
|
var withAssetsPath = "Assets/" + fname;
|
||||||
return "<a href=\"" + withAssetsPath + "\" line=\"" + line + "\">" + withAssetsPath + ":" + line + "</a>";
|
return "<a href=\"" + withAssetsPath + "\" line=\"" + line + "\">" + withAssetsPath + ":" + line + "</a>";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,23 +0,0 @@
|
|||||||
#if NET_4_6 || NET_STANDARD_2_0 || CSHARP_7_OR_LATER
|
|
||||||
|
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace Cysharp.Threading.Tasks.Internal
|
|
||||||
{
|
|
||||||
internal static class FuncExtensions
|
|
||||||
{
|
|
||||||
// avoid lambda capture
|
|
||||||
|
|
||||||
internal static Action<T> AsFuncOfT<T>(this Action action)
|
|
||||||
{
|
|
||||||
return new Action<T>(action.Invoke);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void Invoke<T>(this Action action, T unused)
|
|
||||||
{
|
|
||||||
action();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
using System;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
|
namespace Cysharp.Threading.Tasks.Internal
|
||||||
|
{
|
||||||
|
internal sealed class PooledDelegate<T> : ITaskPoolNode<PooledDelegate<T>>
|
||||||
|
{
|
||||||
|
static TaskPool<PooledDelegate<T>> pool;
|
||||||
|
|
||||||
|
public PooledDelegate<T> NextNode { get; set; }
|
||||||
|
|
||||||
|
static PooledDelegate()
|
||||||
|
{
|
||||||
|
TaskPool.RegisterSizeGetter(typeof(PooledDelegate<T>), () => pool.Size);
|
||||||
|
}
|
||||||
|
|
||||||
|
readonly Action<T> runDelegate;
|
||||||
|
Action continuation;
|
||||||
|
|
||||||
|
PooledDelegate()
|
||||||
|
{
|
||||||
|
runDelegate = Run;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static Action<T> Create(Action continuation)
|
||||||
|
{
|
||||||
|
if (!pool.TryPop(out var item))
|
||||||
|
{
|
||||||
|
item = new PooledDelegate<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
item.continuation = continuation;
|
||||||
|
return item.runDelegate;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
void Run(T _)
|
||||||
|
{
|
||||||
|
var call = continuation;
|
||||||
|
continuation = null;
|
||||||
|
if (call != null)
|
||||||
|
{
|
||||||
|
pool.TryPush(this);
|
||||||
|
call.Invoke();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 8932579438742fa40b010edd412dbfba
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@@ -1,55 +0,0 @@
|
|||||||
using System.Collections.Concurrent;
|
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
using System.Threading;
|
|
||||||
|
|
||||||
namespace Cysharp.Threading.Tasks.Internal
|
|
||||||
{
|
|
||||||
// public, allow to user create custom operator with pool.
|
|
||||||
|
|
||||||
public interface IPromisePoolItem
|
|
||||||
{
|
|
||||||
void Reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
public class PromisePool<T>
|
|
||||||
where T : class, IPromisePoolItem
|
|
||||||
{
|
|
||||||
int count = 0;
|
|
||||||
readonly ConcurrentQueue<T> queue = new ConcurrentQueue<T>();
|
|
||||||
readonly int maxSize;
|
|
||||||
|
|
||||||
public PromisePool(int maxSize = 256)
|
|
||||||
{
|
|
||||||
this.maxSize = maxSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public T TryRent()
|
|
||||||
{
|
|
||||||
if (queue.TryDequeue(out var value))
|
|
||||||
{
|
|
||||||
Interlocked.Decrement(ref count);
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public bool TryReturn(T value)
|
|
||||||
{
|
|
||||||
value.Reset(); // reset when return.
|
|
||||||
|
|
||||||
if (count < maxSize)
|
|
||||||
{
|
|
||||||
queue.Enqueue(value);
|
|
||||||
Interlocked.Increment(ref count);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using Cysharp.Threading.Tasks.Internal;
|
using Cysharp.Threading.Tasks.Internal;
|
||||||
|
|
||||||
@@ -59,9 +60,9 @@ namespace Cysharp.Threading.Tasks
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
static List<KeyValuePair<IUniTaskSource, (int trackingId, DateTime addTime, string stackTrace)>> listPool = new List<KeyValuePair<IUniTaskSource, (int trackingId, DateTime addTime, string stackTrace)>>();
|
static List<KeyValuePair<IUniTaskSource, (string formattedType, int trackingId, DateTime addTime, string stackTrace)>> listPool = new List<KeyValuePair<IUniTaskSource, (string formattedType, int trackingId, DateTime addTime, string stackTrace)>>();
|
||||||
|
|
||||||
static readonly WeakDictionary<IUniTaskSource, (int trackingId, DateTime addTime, string stackTrace)> tracking = new WeakDictionary<IUniTaskSource, (int trackingId, DateTime addTime, string stackTrace)>();
|
static readonly WeakDictionary<IUniTaskSource, (string formattedType, int trackingId, DateTime addTime, string stackTrace)> tracking = new WeakDictionary<IUniTaskSource, (string formattedType, int trackingId, DateTime addTime, string stackTrace)>();
|
||||||
|
|
||||||
[Conditional("UNITY_EDITOR")]
|
[Conditional("UNITY_EDITOR")]
|
||||||
public static void TrackActiveTask(IUniTaskSource task, int skipFrame)
|
public static void TrackActiveTask(IUniTaskSource task, int skipFrame)
|
||||||
@@ -70,7 +71,19 @@ namespace Cysharp.Threading.Tasks
|
|||||||
dirty = true;
|
dirty = true;
|
||||||
if (!EditorEnableState.EnableTracking) return;
|
if (!EditorEnableState.EnableTracking) return;
|
||||||
var stackTrace = EditorEnableState.EnableStackTrace ? new StackTrace(skipFrame, true).CleanupAsyncStackTrace() : "";
|
var stackTrace = EditorEnableState.EnableStackTrace ? new StackTrace(skipFrame, true).CleanupAsyncStackTrace() : "";
|
||||||
tracking.TryAdd(task, (Interlocked.Increment(ref trackingId), DateTime.UtcNow, stackTrace));
|
|
||||||
|
string typeName;
|
||||||
|
if (EditorEnableState.EnableStackTrace)
|
||||||
|
{
|
||||||
|
var sb = new StringBuilder();
|
||||||
|
TypeBeautify(task.GetType(), sb);
|
||||||
|
typeName = sb.ToString();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
typeName = task.GetType().Name;
|
||||||
|
}
|
||||||
|
tracking.TryAdd(task, (typeName, Interlocked.Increment(ref trackingId), DateTime.UtcNow, stackTrace));
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,19 +116,8 @@ namespace Cysharp.Threading.Tasks
|
|||||||
{
|
{
|
||||||
for (int i = 0; i < count; i++)
|
for (int i = 0; i < count; i++)
|
||||||
{
|
{
|
||||||
string typeName = null;
|
action(listPool[i].Value.trackingId, listPool[i].Value.formattedType, listPool[i].Key.UnsafeGetStatus(), listPool[i].Value.addTime, listPool[i].Value.stackTrace);
|
||||||
var keyType = listPool[i].Key.GetType();
|
listPool[i] = default;
|
||||||
if (keyType.IsNested)
|
|
||||||
{
|
|
||||||
typeName = keyType.DeclaringType.Name + "." + keyType.Name;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
typeName = keyType.Name;
|
|
||||||
}
|
|
||||||
|
|
||||||
action(listPool[i].Value.trackingId, typeName, listPool[i].Key.UnsafeGetStatus(), listPool[i].Value.addTime, listPool[i].Value.stackTrace);
|
|
||||||
listPool[i] = new KeyValuePair<IUniTaskSource, (int trackingId, DateTime addTime, string stackTrace)>(null, (0, default(DateTime), null)); // clear
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
@@ -125,6 +127,52 @@ namespace Cysharp.Threading.Tasks
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void TypeBeautify(Type type, StringBuilder sb)
|
||||||
|
{
|
||||||
|
if (type.IsNested)
|
||||||
|
{
|
||||||
|
// TypeBeautify(type.DeclaringType, sb);
|
||||||
|
sb.Append(type.DeclaringType.Name.ToString());
|
||||||
|
sb.Append(".");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type.IsGenericType)
|
||||||
|
{
|
||||||
|
var genericsStart = type.Name.IndexOf("`");
|
||||||
|
if (genericsStart != -1)
|
||||||
|
{
|
||||||
|
sb.Append(type.Name.Substring(0, genericsStart));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sb.Append(type.Name);
|
||||||
|
}
|
||||||
|
sb.Append("<");
|
||||||
|
var first = true;
|
||||||
|
foreach (var item in type.GetGenericArguments())
|
||||||
|
{
|
||||||
|
if (!first)
|
||||||
|
{
|
||||||
|
sb.Append(", ");
|
||||||
|
}
|
||||||
|
first = false;
|
||||||
|
TypeBeautify(item, sb);
|
||||||
|
}
|
||||||
|
sb.Append(">");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sb.Append(type.Name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//static string RemoveUniTaskNamespace(string str)
|
||||||
|
//{
|
||||||
|
// return str.Replace("Cysharp.Threading.Tasks.CompilerServices", "")
|
||||||
|
// .Replace("Cysharp.Threading.Tasks.Linq", "")
|
||||||
|
// .Replace("Cysharp.Threading.Tasks", "");
|
||||||
|
//}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,66 +3,9 @@ using System.Threading;
|
|||||||
|
|
||||||
namespace Cysharp.Threading.Tasks.Linq
|
namespace Cysharp.Threading.Tasks.Linq
|
||||||
{
|
{
|
||||||
public abstract class MoveNextSource : IUniTaskSource<bool>
|
// note: refactor all inherit class and should remove this.
|
||||||
{
|
// see Select and Where.
|
||||||
protected UniTaskCompletionSourceCore<bool> completionSource;
|
internal abstract class AsyncEnumeratorBase<TSource, TResult> : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
|
||||||
|
|
||||||
public bool GetResult(short token)
|
|
||||||
{
|
|
||||||
return completionSource.GetResult(token);
|
|
||||||
}
|
|
||||||
|
|
||||||
public UniTaskStatus GetStatus(short token)
|
|
||||||
{
|
|
||||||
return completionSource.GetStatus(token);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnCompleted(Action<object> continuation, object state, short token)
|
|
||||||
{
|
|
||||||
completionSource.OnCompleted(continuation, state, token);
|
|
||||||
}
|
|
||||||
|
|
||||||
public UniTaskStatus UnsafeGetStatus()
|
|
||||||
{
|
|
||||||
return completionSource.UnsafeGetStatus();
|
|
||||||
}
|
|
||||||
|
|
||||||
void IUniTaskSource.GetResult(short token)
|
|
||||||
{
|
|
||||||
completionSource.GetResult(token);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected bool TryGetResult<T>(UniTask<T>.Awaiter awaiter, out T result)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
result = awaiter.GetResult();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
completionSource.TrySetException(ex);
|
|
||||||
result = default;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected bool TryGetResult(UniTask.Awaiter awaiter)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
awaiter.GetResult();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
completionSource.TrySetException(ex);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract class AsyncEnumeratorBase<TSource, TResult> : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
|
|
||||||
{
|
{
|
||||||
static readonly Action<object> moveNextCallbackDelegate = MoveNextCallBack;
|
static readonly Action<object> moveNextCallbackDelegate = MoveNextCallBack;
|
||||||
|
|
||||||
@@ -188,7 +131,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract class AsyncEnumeratorAwaitSelectorBase<TSource, TResult, TAwait> : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
|
internal abstract class AsyncEnumeratorAwaitSelectorBase<TSource, TResult, TAwait> : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
|
||||||
{
|
{
|
||||||
static readonly Action<object> moveNextCallbackDelegate = MoveNextCallBack;
|
static readonly Action<object> moveNextCallbackDelegate = MoveNextCallBack;
|
||||||
static readonly Action<object> setCurrentCallbackDelegate = SetCurrentCallBack;
|
static readonly Action<object> setCurrentCallbackDelegate = SetCurrentCallBack;
|
||||||
@@ -410,5 +353,4 @@ namespace Cysharp.Threading.Tasks.Linq
|
|||||||
return default;
|
return default;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
11372
src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/CombineLatest.cs
Normal file
11372
src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/CombineLatest.cs
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 6cb07f6e88287e34d9b9301a572284a5
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
221
src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/CombineLatest.tt
Normal file
221
src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/CombineLatest.tt
Normal file
@@ -0,0 +1,221 @@
|
|||||||
|
<#@ template debug="false" hostspecific="false" language="C#" #>
|
||||||
|
<#@ assembly name="System.Core" #>
|
||||||
|
<#@ import namespace="System.Linq" #>
|
||||||
|
<#@ import namespace="System.Text" #>
|
||||||
|
<#@ import namespace="System.Collections.Generic" #>
|
||||||
|
<#@ output extension=".cs" #>
|
||||||
|
<#
|
||||||
|
var tMax = 15;
|
||||||
|
Func<int, string> typeArgs = x => string.Join(", ", Enumerable.Range(1, x).Select(x => $"T{x}")) + ", TResult";
|
||||||
|
Func<int, string> paramArgs = x => string.Join(", ", Enumerable.Range(1, x).Select(x => $"IUniTaskAsyncEnumerable<T{x}> source{x}"));
|
||||||
|
Func<int, string> parameters = x => string.Join(", ", Enumerable.Range(1, x).Select(x => $"source{x}"));
|
||||||
|
|
||||||
|
|
||||||
|
#>
|
||||||
|
using Cysharp.Threading.Tasks.Internal;
|
||||||
|
using System;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
namespace Cysharp.Threading.Tasks.Linq
|
||||||
|
{
|
||||||
|
public static partial class UniTaskAsyncEnumerable
|
||||||
|
{
|
||||||
|
<# for(var i = 2; i <= tMax; i++) { #>
|
||||||
|
public static IUniTaskAsyncEnumerable<TResult> CombineLatest<<#= typeArgs(i) #>>(this <#= paramArgs(i) #>, Func<<#= typeArgs(i) #>> resultSelector)
|
||||||
|
{
|
||||||
|
<# for(var j = 1; j <= i; j++) { #>
|
||||||
|
Error.ThrowArgumentNullException(source<#= j #>, nameof(source<#= j #>));
|
||||||
|
<# } #>
|
||||||
|
Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
|
||||||
|
|
||||||
|
return new CombineLatest<<#= typeArgs(i) #>>(<#= parameters(i) #>, resultSelector);
|
||||||
|
}
|
||||||
|
|
||||||
|
<# } #>
|
||||||
|
}
|
||||||
|
|
||||||
|
<# for(var i = 2; i <= tMax; i++) { #>
|
||||||
|
internal class CombineLatest<<#= typeArgs(i) #>> : IUniTaskAsyncEnumerable<TResult>
|
||||||
|
{
|
||||||
|
<# for(var j = 1; j <= i; j++) { #>
|
||||||
|
readonly IUniTaskAsyncEnumerable<T<#= j #>> source<#= j #>;
|
||||||
|
<# } #>
|
||||||
|
readonly Func<<#= typeArgs(i) #>> resultSelector;
|
||||||
|
|
||||||
|
public CombineLatest(<#= paramArgs(i) #>, Func<<#= typeArgs(i) #>> resultSelector)
|
||||||
|
{
|
||||||
|
<# for(var j = 1; j <= i; j++) { #>
|
||||||
|
this.source<#= j #> = source<#= j #>;
|
||||||
|
<# } #>
|
||||||
|
this.resultSelector = resultSelector;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
return new _CombineLatest(<#= parameters(i) #>, resultSelector, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
class _CombineLatest : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
|
||||||
|
{
|
||||||
|
<# for(var j = 1; j <= i; j++) { #>
|
||||||
|
static readonly Action<object> Completed<#= j #>Delegate = Completed<#= j #>;
|
||||||
|
<# } #>
|
||||||
|
const int CompleteCount = <#= i #>;
|
||||||
|
|
||||||
|
<# for(var j = 1; j <= i; j++) { #>
|
||||||
|
readonly IUniTaskAsyncEnumerable<T<#= j #>> source<#= j #>;
|
||||||
|
<# } #>
|
||||||
|
readonly Func<<#= typeArgs(i) #>> resultSelector;
|
||||||
|
CancellationToken cancellationToken;
|
||||||
|
|
||||||
|
<# for(var j = 1; j <= i; j++) { #>
|
||||||
|
IUniTaskAsyncEnumerator<T<#= j #>> enumerator<#= j #>;
|
||||||
|
UniTask<bool>.Awaiter awaiter<#= j #>;
|
||||||
|
bool hasCurrent<#= j #>;
|
||||||
|
bool running<#= j #>;
|
||||||
|
T<#= j #> current<#= j #>;
|
||||||
|
|
||||||
|
<# } #>
|
||||||
|
int completedCount;
|
||||||
|
bool syncRunning;
|
||||||
|
TResult result;
|
||||||
|
|
||||||
|
public _CombineLatest(<#= paramArgs(i) #>, Func<<#= typeArgs(i) #>> resultSelector, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
<# for(var j = 1; j <= i; j++) { #>
|
||||||
|
this.source<#= j #> = source<#= j #>;
|
||||||
|
<# } #>
|
||||||
|
this.resultSelector = resultSelector;
|
||||||
|
this.cancellationToken = cancellationToken;
|
||||||
|
TaskTracker.TrackActiveTask(this, 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
public TResult Current => result;
|
||||||
|
|
||||||
|
public UniTask<bool> MoveNextAsync()
|
||||||
|
{
|
||||||
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
|
if (completedCount == CompleteCount) return CompletedTasks.False;
|
||||||
|
|
||||||
|
if (enumerator1 == null)
|
||||||
|
{
|
||||||
|
<# for(var j = 1; j <= i; j++) { #>
|
||||||
|
enumerator<#= j #> = source<#= j #>.GetAsyncEnumerator(cancellationToken);
|
||||||
|
<# } #>
|
||||||
|
}
|
||||||
|
|
||||||
|
completionSource.Reset();
|
||||||
|
|
||||||
|
AGAIN:
|
||||||
|
syncRunning = true;
|
||||||
|
<# for(var j = 1; j <= i; j++) { #>
|
||||||
|
if (!running<#= j #>)
|
||||||
|
{
|
||||||
|
running<#= j #> = true;
|
||||||
|
awaiter<#= j #> = enumerator<#= j #>.MoveNextAsync().GetAwaiter();
|
||||||
|
if (awaiter<#= j #>.IsCompleted)
|
||||||
|
{
|
||||||
|
Completed<#= j #>(this);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
awaiter<#= j #>.SourceOnCompleted(Completed<#= j #>Delegate, this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
<# } #>
|
||||||
|
|
||||||
|
if (<#= string.Join(" || ", Enumerable.Range(1, i).Select(x => $"!running{x}")) #>)
|
||||||
|
{
|
||||||
|
goto AGAIN;
|
||||||
|
}
|
||||||
|
syncRunning = false;
|
||||||
|
|
||||||
|
return new UniTask<bool>(this, completionSource.Version);
|
||||||
|
}
|
||||||
|
|
||||||
|
<# for(var j = 1; j <= i; j++) { #>
|
||||||
|
static void Completed<#= j #>(object state)
|
||||||
|
{
|
||||||
|
var self = (_CombineLatest)state;
|
||||||
|
self.running<#= j #> = false;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (self.awaiter<#= j #>.GetResult())
|
||||||
|
{
|
||||||
|
self.hasCurrent<#= j #> = true;
|
||||||
|
self.current<#= j #> = self.enumerator<#= j #>.Current;
|
||||||
|
goto SUCCESS;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
self.running<#= j #> = true; // as complete, no more call MoveNextAsync.
|
||||||
|
if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
|
||||||
|
{
|
||||||
|
goto COMPLETE;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
self.running<#= j #> = true; // as complete, no more call MoveNextAsync.
|
||||||
|
self.completedCount = CompleteCount;
|
||||||
|
self.completionSource.TrySetException(ex);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
SUCCESS:
|
||||||
|
if (!self.TrySetResult())
|
||||||
|
{
|
||||||
|
if (self.syncRunning) return;
|
||||||
|
self.running<#= j #> = true; // as complete, no more call MoveNextAsync.
|
||||||
|
try
|
||||||
|
{
|
||||||
|
self.awaiter<#= j #> = self.enumerator<#= j #>.MoveNextAsync().GetAwaiter();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
self.completedCount = CompleteCount;
|
||||||
|
self.completionSource.TrySetException(ex);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.awaiter<#= j #>.SourceOnCompleted(Completed<#= j #>Delegate, self);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
COMPLETE:
|
||||||
|
self.completionSource.TrySetResult(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
<# } #>
|
||||||
|
bool TrySetResult()
|
||||||
|
{
|
||||||
|
if (<#= string.Join(" && ", Enumerable.Range(1, i).Select(x => $"hasCurrent{x}")) #>)
|
||||||
|
{
|
||||||
|
result = resultSelector(<#= string.Join(", ", Enumerable.Range(1, i).Select(x => $"current{x}")) #>);
|
||||||
|
completionSource.TrySetResult(true);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async UniTask DisposeAsync()
|
||||||
|
{
|
||||||
|
TaskTracker.RemoveTracking(this);
|
||||||
|
<# for(var j = 1; j <= i; j++) { #>
|
||||||
|
if (enumerator<#= j #> != null)
|
||||||
|
{
|
||||||
|
await enumerator<#= j #>.DisposeAsync();
|
||||||
|
}
|
||||||
|
<# } #>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
<# } #>
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: b1b8cfa9d17af814a971ee2224aaaaa2
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@@ -79,41 +79,123 @@ namespace Cysharp.Threading.Tasks.Linq
|
|||||||
return new _DistinctUntilChanged(source, comparer, cancellationToken);
|
return new _DistinctUntilChanged(source, comparer, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
class _DistinctUntilChanged : AsyncEnumeratorBase<TSource, TSource>
|
sealed class _DistinctUntilChanged : MoveNextSource, IUniTaskAsyncEnumerator<TSource>
|
||||||
{
|
{
|
||||||
|
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||||
readonly IEqualityComparer<TSource> comparer;
|
readonly IEqualityComparer<TSource> comparer;
|
||||||
TSource prev;
|
readonly CancellationToken cancellationToken;
|
||||||
bool first;
|
|
||||||
|
int state = -1;
|
||||||
|
IUniTaskAsyncEnumerator<TSource> enumerator;
|
||||||
|
UniTask<bool>.Awaiter awaiter;
|
||||||
|
Action moveNextAction;
|
||||||
|
|
||||||
public _DistinctUntilChanged(IUniTaskAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer, CancellationToken cancellationToken)
|
public _DistinctUntilChanged(IUniTaskAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer, CancellationToken cancellationToken)
|
||||||
|
|
||||||
: base(source, cancellationToken)
|
|
||||||
{
|
{
|
||||||
|
this.source = source;
|
||||||
this.comparer = comparer;
|
this.comparer = comparer;
|
||||||
this.first = true;
|
this.cancellationToken = cancellationToken;
|
||||||
|
this.moveNextAction = MoveNext;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override bool TryMoveNextCore(bool sourceHasCurrent, out bool result)
|
public TSource Current { get; private set; }
|
||||||
|
|
||||||
|
public UniTask<bool> MoveNextAsync()
|
||||||
{
|
{
|
||||||
if (sourceHasCurrent)
|
if (state == -2) return default;
|
||||||
|
|
||||||
|
completionSource.Reset();
|
||||||
|
MoveNext();
|
||||||
|
return new UniTask<bool>(this, completionSource.Version);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MoveNext()
|
||||||
|
{
|
||||||
|
REPEAT:
|
||||||
|
try
|
||||||
{
|
{
|
||||||
var v = SourceCurrent;
|
switch (state)
|
||||||
if (first || !comparer.Equals(prev, v))
|
|
||||||
{
|
{
|
||||||
first = false;
|
case -1: // init
|
||||||
Current = prev = v;
|
enumerator = source.GetAsyncEnumerator(cancellationToken);
|
||||||
result = true;
|
awaiter = enumerator.MoveNextAsync().GetAwaiter();
|
||||||
return true;
|
if (awaiter.IsCompleted)
|
||||||
}
|
{
|
||||||
else
|
goto case -3;
|
||||||
{
|
}
|
||||||
result = default;
|
else
|
||||||
return false;
|
{
|
||||||
|
state = -3;
|
||||||
|
awaiter.UnsafeOnCompleted(moveNextAction);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case -3: // first
|
||||||
|
if (awaiter.GetResult())
|
||||||
|
{
|
||||||
|
Current = enumerator.Current;
|
||||||
|
goto CONTINUE;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
goto DONE;
|
||||||
|
}
|
||||||
|
case 0: // normal
|
||||||
|
awaiter = enumerator.MoveNextAsync().GetAwaiter();
|
||||||
|
if (awaiter.IsCompleted)
|
||||||
|
{
|
||||||
|
goto case 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 1;
|
||||||
|
awaiter.UnsafeOnCompleted(moveNextAction);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case 1:
|
||||||
|
if (awaiter.GetResult())
|
||||||
|
{
|
||||||
|
var v = enumerator.Current;
|
||||||
|
if (!comparer.Equals(Current, v))
|
||||||
|
{
|
||||||
|
Current = v;
|
||||||
|
goto CONTINUE;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 0;
|
||||||
|
goto REPEAT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
goto DONE;
|
||||||
|
}
|
||||||
|
case -2:
|
||||||
|
default:
|
||||||
|
goto DONE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
state = -2;
|
||||||
|
completionSource.TrySetException(ex);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
result = false;
|
DONE:
|
||||||
return true;
|
state = -2;
|
||||||
|
completionSource.TrySetResult(false);
|
||||||
|
return;
|
||||||
|
|
||||||
|
CONTINUE:
|
||||||
|
state = 0;
|
||||||
|
completionSource.TrySetResult(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTask DisposeAsync()
|
||||||
|
{
|
||||||
|
return enumerator.DisposeAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -136,45 +218,128 @@ namespace Cysharp.Threading.Tasks.Linq
|
|||||||
return new _DistinctUntilChanged(source, keySelector, comparer, cancellationToken);
|
return new _DistinctUntilChanged(source, keySelector, comparer, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
class _DistinctUntilChanged : AsyncEnumeratorBase<TSource, TSource>
|
sealed class _DistinctUntilChanged : MoveNextSource, IUniTaskAsyncEnumerator<TSource>
|
||||||
{
|
{
|
||||||
readonly IEqualityComparer<TKey> comparer;
|
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||||
readonly Func<TSource, TKey> keySelector;
|
readonly Func<TSource, TKey> keySelector;
|
||||||
|
readonly IEqualityComparer<TKey> comparer;
|
||||||
|
readonly CancellationToken cancellationToken;
|
||||||
|
|
||||||
|
int state = -1;
|
||||||
|
IUniTaskAsyncEnumerator<TSource> enumerator;
|
||||||
|
UniTask<bool>.Awaiter awaiter;
|
||||||
|
Action moveNextAction;
|
||||||
TKey prev;
|
TKey prev;
|
||||||
bool first;
|
|
||||||
|
|
||||||
public _DistinctUntilChanged(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
|
public _DistinctUntilChanged(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
|
||||||
|
|
||||||
: base(source, cancellationToken)
|
|
||||||
{
|
{
|
||||||
this.comparer = comparer;
|
this.source = source;
|
||||||
this.keySelector = keySelector;
|
this.keySelector = keySelector;
|
||||||
this.first = true;
|
this.comparer = comparer;
|
||||||
|
this.cancellationToken = cancellationToken;
|
||||||
|
this.moveNextAction = MoveNext;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override bool TryMoveNextCore(bool sourceHasCurrent, out bool result)
|
public TSource Current { get; private set; }
|
||||||
|
|
||||||
|
public UniTask<bool> MoveNextAsync()
|
||||||
{
|
{
|
||||||
if (sourceHasCurrent)
|
if (state == -2) return default;
|
||||||
|
|
||||||
|
completionSource.Reset();
|
||||||
|
MoveNext();
|
||||||
|
return new UniTask<bool>(this, completionSource.Version);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MoveNext()
|
||||||
|
{
|
||||||
|
REPEAT:
|
||||||
|
try
|
||||||
{
|
{
|
||||||
var v = SourceCurrent;
|
switch (state)
|
||||||
var key = keySelector(v);
|
|
||||||
if (first || !comparer.Equals(prev, key))
|
|
||||||
{
|
{
|
||||||
first = false;
|
case -1: // init
|
||||||
prev = key;
|
enumerator = source.GetAsyncEnumerator(cancellationToken);
|
||||||
Current = v;
|
awaiter = enumerator.MoveNextAsync().GetAwaiter();
|
||||||
result = true;
|
if (awaiter.IsCompleted)
|
||||||
return true;
|
{
|
||||||
}
|
goto case -3;
|
||||||
else
|
}
|
||||||
{
|
else
|
||||||
result = default;
|
{
|
||||||
return false;
|
state = -3;
|
||||||
|
awaiter.UnsafeOnCompleted(moveNextAction);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case -3: // first
|
||||||
|
if (awaiter.GetResult())
|
||||||
|
{
|
||||||
|
Current = enumerator.Current;
|
||||||
|
goto CONTINUE;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
goto DONE;
|
||||||
|
}
|
||||||
|
case 0: // normal
|
||||||
|
awaiter = enumerator.MoveNextAsync().GetAwaiter();
|
||||||
|
if (awaiter.IsCompleted)
|
||||||
|
{
|
||||||
|
goto case 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 1;
|
||||||
|
awaiter.UnsafeOnCompleted(moveNextAction);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case 1:
|
||||||
|
if (awaiter.GetResult())
|
||||||
|
{
|
||||||
|
var v = enumerator.Current;
|
||||||
|
var key = keySelector(v);
|
||||||
|
if (!comparer.Equals(prev, key))
|
||||||
|
{
|
||||||
|
prev = key;
|
||||||
|
Current = v;
|
||||||
|
goto CONTINUE;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 0;
|
||||||
|
goto REPEAT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
goto DONE;
|
||||||
|
}
|
||||||
|
case -2:
|
||||||
|
default:
|
||||||
|
goto DONE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
state = -2;
|
||||||
|
completionSource.TrySetException(ex);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
result = false;
|
DONE:
|
||||||
return true;
|
state = -2;
|
||||||
|
completionSource.TrySetResult(false);
|
||||||
|
return;
|
||||||
|
|
||||||
|
CONTINUE:
|
||||||
|
state = 0;
|
||||||
|
completionSource.TrySetResult(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTask DisposeAsync()
|
||||||
|
{
|
||||||
|
return enumerator.DisposeAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -197,42 +362,142 @@ namespace Cysharp.Threading.Tasks.Linq
|
|||||||
return new _DistinctUntilChangedAwait(source, keySelector, comparer, cancellationToken);
|
return new _DistinctUntilChangedAwait(source, keySelector, comparer, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
class _DistinctUntilChangedAwait : AsyncEnumeratorAwaitSelectorBase<TSource, TSource, TKey>
|
sealed class _DistinctUntilChangedAwait : MoveNextSource, IUniTaskAsyncEnumerator<TSource>
|
||||||
{
|
{
|
||||||
readonly IEqualityComparer<TKey> comparer;
|
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||||
readonly Func<TSource, UniTask<TKey>> keySelector;
|
readonly Func<TSource, UniTask<TKey>> keySelector;
|
||||||
|
readonly IEqualityComparer<TKey> comparer;
|
||||||
|
readonly CancellationToken cancellationToken;
|
||||||
|
|
||||||
|
int state = -1;
|
||||||
|
IUniTaskAsyncEnumerator<TSource> enumerator;
|
||||||
|
UniTask<bool>.Awaiter awaiter;
|
||||||
|
UniTask<TKey>.Awaiter awaiter2;
|
||||||
|
Action moveNextAction;
|
||||||
|
TSource enumeratorCurrent;
|
||||||
TKey prev;
|
TKey prev;
|
||||||
bool first;
|
|
||||||
|
|
||||||
public _DistinctUntilChangedAwait(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
|
public _DistinctUntilChangedAwait(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
|
||||||
|
|
||||||
: base(source, cancellationToken)
|
|
||||||
{
|
{
|
||||||
this.comparer = comparer;
|
this.source = source;
|
||||||
this.keySelector = keySelector;
|
this.keySelector = keySelector;
|
||||||
this.first = true;
|
this.comparer = comparer;
|
||||||
|
this.cancellationToken = cancellationToken;
|
||||||
|
this.moveNextAction = MoveNext;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override UniTask<TKey> TransformAsync(TSource sourceCurrent)
|
public TSource Current { get; private set; }
|
||||||
|
|
||||||
|
public UniTask<bool> MoveNextAsync()
|
||||||
{
|
{
|
||||||
return keySelector(sourceCurrent);
|
if (state == -2) return default;
|
||||||
|
|
||||||
|
completionSource.Reset();
|
||||||
|
MoveNext();
|
||||||
|
return new UniTask<bool>(this, completionSource.Version);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override bool TrySetCurrentCore(TKey key, out bool terminateIteration)
|
void MoveNext()
|
||||||
{
|
{
|
||||||
if (first || !comparer.Equals(prev, key))
|
REPEAT:
|
||||||
|
try
|
||||||
{
|
{
|
||||||
first = false;
|
switch (state)
|
||||||
prev = key;
|
{
|
||||||
Current = SourceCurrent;
|
case -1: // init
|
||||||
terminateIteration = false;
|
enumerator = source.GetAsyncEnumerator(cancellationToken);
|
||||||
return true;
|
awaiter = enumerator.MoveNextAsync().GetAwaiter();
|
||||||
|
if (awaiter.IsCompleted)
|
||||||
|
{
|
||||||
|
goto case -3;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = -3;
|
||||||
|
awaiter.UnsafeOnCompleted(moveNextAction);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case -3: // first
|
||||||
|
if (awaiter.GetResult())
|
||||||
|
{
|
||||||
|
Current = enumerator.Current;
|
||||||
|
goto CONTINUE;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
goto DONE;
|
||||||
|
}
|
||||||
|
case 0: // normal
|
||||||
|
awaiter = enumerator.MoveNextAsync().GetAwaiter();
|
||||||
|
if (awaiter.IsCompleted)
|
||||||
|
{
|
||||||
|
goto case 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 1;
|
||||||
|
awaiter.UnsafeOnCompleted(moveNextAction);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case 1:
|
||||||
|
if (awaiter.GetResult())
|
||||||
|
{
|
||||||
|
enumeratorCurrent = enumerator.Current;
|
||||||
|
awaiter2 = keySelector(enumeratorCurrent).GetAwaiter();
|
||||||
|
if (awaiter2.IsCompleted)
|
||||||
|
{
|
||||||
|
goto case 2;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 2;
|
||||||
|
awaiter2.UnsafeOnCompleted(moveNextAction);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
goto DONE;
|
||||||
|
}
|
||||||
|
case 2:
|
||||||
|
var key = awaiter2.GetResult();
|
||||||
|
if (!comparer.Equals(prev, key))
|
||||||
|
{
|
||||||
|
prev = key;
|
||||||
|
Current = enumeratorCurrent;
|
||||||
|
goto CONTINUE;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 0;
|
||||||
|
goto REPEAT;
|
||||||
|
}
|
||||||
|
case -2:
|
||||||
|
default:
|
||||||
|
goto DONE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
terminateIteration = false;
|
state = -2;
|
||||||
return false;
|
completionSource.TrySetException(ex);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DONE:
|
||||||
|
state = -2;
|
||||||
|
completionSource.TrySetResult(false);
|
||||||
|
return;
|
||||||
|
|
||||||
|
CONTINUE:
|
||||||
|
state = 0;
|
||||||
|
completionSource.TrySetResult(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTask DisposeAsync()
|
||||||
|
{
|
||||||
|
return enumerator.DisposeAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -255,42 +520,142 @@ namespace Cysharp.Threading.Tasks.Linq
|
|||||||
return new _DistinctUntilChangedAwaitWithCancellation(source, keySelector, comparer, cancellationToken);
|
return new _DistinctUntilChangedAwaitWithCancellation(source, keySelector, comparer, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
class _DistinctUntilChangedAwaitWithCancellation : AsyncEnumeratorAwaitSelectorBase<TSource, TSource, TKey>
|
sealed class _DistinctUntilChangedAwaitWithCancellation : MoveNextSource, IUniTaskAsyncEnumerator<TSource>
|
||||||
{
|
{
|
||||||
readonly IEqualityComparer<TKey> comparer;
|
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||||
readonly Func<TSource, CancellationToken, UniTask<TKey>> keySelector;
|
readonly Func<TSource, CancellationToken, UniTask<TKey>> keySelector;
|
||||||
|
readonly IEqualityComparer<TKey> comparer;
|
||||||
|
readonly CancellationToken cancellationToken;
|
||||||
|
|
||||||
|
int state = -1;
|
||||||
|
IUniTaskAsyncEnumerator<TSource> enumerator;
|
||||||
|
UniTask<bool>.Awaiter awaiter;
|
||||||
|
UniTask<TKey>.Awaiter awaiter2;
|
||||||
|
Action moveNextAction;
|
||||||
|
TSource enumeratorCurrent;
|
||||||
TKey prev;
|
TKey prev;
|
||||||
bool first;
|
|
||||||
|
|
||||||
public _DistinctUntilChangedAwaitWithCancellation(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
|
public _DistinctUntilChangedAwaitWithCancellation(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
|
||||||
|
|
||||||
: base(source, cancellationToken)
|
|
||||||
{
|
{
|
||||||
this.comparer = comparer;
|
this.source = source;
|
||||||
this.keySelector = keySelector;
|
this.keySelector = keySelector;
|
||||||
this.first = true;
|
this.comparer = comparer;
|
||||||
|
this.cancellationToken = cancellationToken;
|
||||||
|
this.moveNextAction = MoveNext;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override UniTask<TKey> TransformAsync(TSource sourceCurrent)
|
public TSource Current { get; private set; }
|
||||||
|
|
||||||
|
public UniTask<bool> MoveNextAsync()
|
||||||
{
|
{
|
||||||
return keySelector(sourceCurrent, cancellationToken);
|
if (state == -2) return default;
|
||||||
|
|
||||||
|
completionSource.Reset();
|
||||||
|
MoveNext();
|
||||||
|
return new UniTask<bool>(this, completionSource.Version);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override bool TrySetCurrentCore(TKey key, out bool terminateIteration)
|
void MoveNext()
|
||||||
{
|
{
|
||||||
if (first || !comparer.Equals(prev, key))
|
REPEAT:
|
||||||
|
try
|
||||||
{
|
{
|
||||||
first = false;
|
switch (state)
|
||||||
prev = key;
|
{
|
||||||
Current = SourceCurrent;
|
case -1: // init
|
||||||
terminateIteration = false;
|
enumerator = source.GetAsyncEnumerator(cancellationToken);
|
||||||
return true;
|
awaiter = enumerator.MoveNextAsync().GetAwaiter();
|
||||||
|
if (awaiter.IsCompleted)
|
||||||
|
{
|
||||||
|
goto case -3;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = -3;
|
||||||
|
awaiter.UnsafeOnCompleted(moveNextAction);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case -3: // first
|
||||||
|
if (awaiter.GetResult())
|
||||||
|
{
|
||||||
|
Current = enumerator.Current;
|
||||||
|
goto CONTINUE;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
goto DONE;
|
||||||
|
}
|
||||||
|
case 0: // normal
|
||||||
|
awaiter = enumerator.MoveNextAsync().GetAwaiter();
|
||||||
|
if (awaiter.IsCompleted)
|
||||||
|
{
|
||||||
|
goto case 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 1;
|
||||||
|
awaiter.UnsafeOnCompleted(moveNextAction);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case 1:
|
||||||
|
if (awaiter.GetResult())
|
||||||
|
{
|
||||||
|
enumeratorCurrent = enumerator.Current;
|
||||||
|
awaiter2 = keySelector(enumeratorCurrent, cancellationToken).GetAwaiter();
|
||||||
|
if (awaiter2.IsCompleted)
|
||||||
|
{
|
||||||
|
goto case 2;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 2;
|
||||||
|
awaiter2.UnsafeOnCompleted(moveNextAction);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
goto DONE;
|
||||||
|
}
|
||||||
|
case 2:
|
||||||
|
var key = awaiter2.GetResult();
|
||||||
|
if (!comparer.Equals(prev, key))
|
||||||
|
{
|
||||||
|
prev = key;
|
||||||
|
Current = enumeratorCurrent;
|
||||||
|
goto CONTINUE;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 0;
|
||||||
|
goto REPEAT;
|
||||||
|
}
|
||||||
|
case -2:
|
||||||
|
default:
|
||||||
|
goto DONE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
terminateIteration = false;
|
state = -2;
|
||||||
return false;
|
completionSource.TrySetException(ex);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DONE:
|
||||||
|
state = -2;
|
||||||
|
completionSource.TrySetResult(false);
|
||||||
|
return;
|
||||||
|
|
||||||
|
CONTINUE:
|
||||||
|
state = 0;
|
||||||
|
completionSource.TrySetResult(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTask DisposeAsync()
|
||||||
|
{
|
||||||
|
return enumerator.DisposeAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
128
src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/Pairwise.cs
Normal file
128
src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/Pairwise.cs
Normal file
@@ -0,0 +1,128 @@
|
|||||||
|
using Cysharp.Threading.Tasks.Internal;
|
||||||
|
using System;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
namespace Cysharp.Threading.Tasks.Linq
|
||||||
|
{
|
||||||
|
public static partial class UniTaskAsyncEnumerable
|
||||||
|
{
|
||||||
|
public static IUniTaskAsyncEnumerable<(TSource, TSource)> Pairwise<TSource>(this IUniTaskAsyncEnumerable<TSource> source)
|
||||||
|
{
|
||||||
|
Error.ThrowArgumentNullException(source, nameof(source));
|
||||||
|
|
||||||
|
return new Pairwise<TSource>(source);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal sealed class Pairwise<TSource> : IUniTaskAsyncEnumerable<(TSource, TSource)>
|
||||||
|
{
|
||||||
|
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||||
|
|
||||||
|
public Pairwise(IUniTaskAsyncEnumerable<TSource> source)
|
||||||
|
{
|
||||||
|
this.source = source;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IUniTaskAsyncEnumerator<(TSource, TSource)> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
return new _Pairwise(source, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
sealed class _Pairwise : MoveNextSource, IUniTaskAsyncEnumerator<(TSource, TSource)>
|
||||||
|
{
|
||||||
|
static readonly Action<object> MoveNextCoreDelegate = MoveNextCore;
|
||||||
|
|
||||||
|
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||||
|
CancellationToken cancellationToken;
|
||||||
|
|
||||||
|
IUniTaskAsyncEnumerator<TSource> enumerator;
|
||||||
|
UniTask<bool>.Awaiter awaiter;
|
||||||
|
|
||||||
|
TSource prev;
|
||||||
|
bool isFirst;
|
||||||
|
|
||||||
|
public _Pairwise(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
this.source = source;
|
||||||
|
this.cancellationToken = cancellationToken;
|
||||||
|
TaskTracker.TrackActiveTask(this, 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
public (TSource, TSource) Current { get; private set; }
|
||||||
|
|
||||||
|
public UniTask<bool> MoveNextAsync()
|
||||||
|
{
|
||||||
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
|
|
||||||
|
if (enumerator == null)
|
||||||
|
{
|
||||||
|
isFirst = true;
|
||||||
|
enumerator = source.GetAsyncEnumerator(cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
completionSource.Reset();
|
||||||
|
SourceMoveNext();
|
||||||
|
return new UniTask<bool>(this, completionSource.Version);
|
||||||
|
}
|
||||||
|
|
||||||
|
void SourceMoveNext()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
awaiter = enumerator.MoveNextAsync().GetAwaiter();
|
||||||
|
if (awaiter.IsCompleted)
|
||||||
|
{
|
||||||
|
MoveNextCore(this);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
awaiter.SourceOnCompleted(MoveNextCoreDelegate, this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
completionSource.TrySetException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void MoveNextCore(object state)
|
||||||
|
{
|
||||||
|
var self = (_Pairwise)state;
|
||||||
|
|
||||||
|
if (self.TryGetResult(self.awaiter, out var result))
|
||||||
|
{
|
||||||
|
if (result)
|
||||||
|
{
|
||||||
|
if (self.isFirst)
|
||||||
|
{
|
||||||
|
self.isFirst = false;
|
||||||
|
self.prev = self.enumerator.Current;
|
||||||
|
self.SourceMoveNext(); // run again. okay to use recursive(only one more).
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var p = self.prev;
|
||||||
|
self.prev = self.enumerator.Current;
|
||||||
|
self.Current = (p, self.prev);
|
||||||
|
self.completionSource.TrySetResult(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
self.completionSource.TrySetResult(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTask DisposeAsync()
|
||||||
|
{
|
||||||
|
TaskTracker.RemoveTracking(this);
|
||||||
|
if (enumerator != null)
|
||||||
|
{
|
||||||
|
return enumerator.DisposeAsync();
|
||||||
|
}
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: cddbf051d2a88f549986c468b23214af
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@@ -115,6 +115,8 @@ namespace Cysharp.Threading.Tasks.Linq
|
|||||||
}
|
}
|
||||||
|
|
||||||
public TSource Current { get; private set; }
|
public TSource Current { get; private set; }
|
||||||
|
ITriggerHandler<TSource> ITriggerHandler<TSource>.Prev { get; set; }
|
||||||
|
ITriggerHandler<TSource> ITriggerHandler<TSource>.Next { get; set; }
|
||||||
|
|
||||||
public UniTask<bool> MoveNextAsync()
|
public UniTask<bool> MoveNextAsync()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -71,29 +71,92 @@ namespace Cysharp.Threading.Tasks.Linq
|
|||||||
return new _Select(source, selector, cancellationToken);
|
return new _Select(source, selector, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
sealed class _Select : AsyncEnumeratorBase<TSource, TResult>
|
sealed class _Select : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
|
||||||
{
|
{
|
||||||
|
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||||
readonly Func<TSource, TResult> selector;
|
readonly Func<TSource, TResult> selector;
|
||||||
|
readonly CancellationToken cancellationToken;
|
||||||
|
|
||||||
|
int state = -1;
|
||||||
|
IUniTaskAsyncEnumerator<TSource> enumerator;
|
||||||
|
UniTask<bool>.Awaiter awaiter;
|
||||||
|
Action moveNextAction;
|
||||||
|
|
||||||
public _Select(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TResult> selector, CancellationToken cancellationToken)
|
public _Select(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TResult> selector, CancellationToken cancellationToken)
|
||||||
: base(source, cancellationToken)
|
|
||||||
{
|
{
|
||||||
|
this.source = source;
|
||||||
this.selector = selector;
|
this.selector = selector;
|
||||||
|
this.cancellationToken = cancellationToken;
|
||||||
|
this.moveNextAction = MoveNext;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override bool TryMoveNextCore(bool sourceHasCurrent, out bool result)
|
public TResult Current { get; private set; }
|
||||||
|
|
||||||
|
public UniTask<bool> MoveNextAsync()
|
||||||
{
|
{
|
||||||
if (sourceHasCurrent)
|
if (state == -2) return default;
|
||||||
|
|
||||||
|
completionSource.Reset();
|
||||||
|
MoveNext();
|
||||||
|
return new UniTask<bool>(this, completionSource.Version);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MoveNext()
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
Current = selector(SourceCurrent);
|
switch (state)
|
||||||
result = true;
|
{
|
||||||
return true;
|
case -1: // init
|
||||||
|
enumerator = source.GetAsyncEnumerator(cancellationToken);
|
||||||
|
goto case 0;
|
||||||
|
case 0:
|
||||||
|
awaiter = enumerator.MoveNextAsync().GetAwaiter();
|
||||||
|
if (awaiter.IsCompleted)
|
||||||
|
{
|
||||||
|
goto case 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 1;
|
||||||
|
awaiter.UnsafeOnCompleted(moveNextAction);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case 1:
|
||||||
|
if (awaiter.GetResult())
|
||||||
|
{
|
||||||
|
Current = selector(enumerator.Current);
|
||||||
|
goto CONTINUE;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
goto DONE;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
goto DONE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
result = false;
|
state = -2;
|
||||||
return true;
|
completionSource.TrySetException(ex);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DONE:
|
||||||
|
state = -2;
|
||||||
|
completionSource.TrySetResult(false);
|
||||||
|
return;
|
||||||
|
|
||||||
|
CONTINUE:
|
||||||
|
state = 0;
|
||||||
|
completionSource.TrySetResult(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTask DisposeAsync()
|
||||||
|
{
|
||||||
|
return enumerator.DisposeAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -111,33 +174,96 @@ namespace Cysharp.Threading.Tasks.Linq
|
|||||||
|
|
||||||
public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
return new _SelectInt(source, selector, cancellationToken);
|
return new _Select(source, selector, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
sealed class _SelectInt : AsyncEnumeratorBase<TSource, TResult>
|
sealed class _Select : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
|
||||||
{
|
{
|
||||||
|
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||||
readonly Func<TSource, int, TResult> selector;
|
readonly Func<TSource, int, TResult> selector;
|
||||||
|
readonly CancellationToken cancellationToken;
|
||||||
|
|
||||||
|
int state = -1;
|
||||||
|
IUniTaskAsyncEnumerator<TSource> enumerator;
|
||||||
|
UniTask<bool>.Awaiter awaiter;
|
||||||
|
Action moveNextAction;
|
||||||
int index;
|
int index;
|
||||||
|
|
||||||
public _SelectInt(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, TResult> selector, CancellationToken cancellationToken)
|
public _Select(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, TResult> selector, CancellationToken cancellationToken)
|
||||||
: base(source, cancellationToken)
|
|
||||||
{
|
{
|
||||||
|
this.source = source;
|
||||||
this.selector = selector;
|
this.selector = selector;
|
||||||
|
this.cancellationToken = cancellationToken;
|
||||||
|
this.moveNextAction = MoveNext;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override bool TryMoveNextCore(bool sourceHasCurrent, out bool result)
|
public TResult Current { get; private set; }
|
||||||
|
|
||||||
|
public UniTask<bool> MoveNextAsync()
|
||||||
{
|
{
|
||||||
if (sourceHasCurrent)
|
if (state == -2) return default;
|
||||||
|
|
||||||
|
completionSource.Reset();
|
||||||
|
MoveNext();
|
||||||
|
return new UniTask<bool>(this, completionSource.Version);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MoveNext()
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
Current = selector(SourceCurrent, checked(index++));
|
switch (state)
|
||||||
result = true;
|
{
|
||||||
return true;
|
case -1: // init
|
||||||
|
enumerator = source.GetAsyncEnumerator(cancellationToken);
|
||||||
|
goto case 0;
|
||||||
|
case 0:
|
||||||
|
awaiter = enumerator.MoveNextAsync().GetAwaiter();
|
||||||
|
if (awaiter.IsCompleted)
|
||||||
|
{
|
||||||
|
goto case 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 1;
|
||||||
|
awaiter.UnsafeOnCompleted(moveNextAction);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case 1:
|
||||||
|
if (awaiter.GetResult())
|
||||||
|
{
|
||||||
|
Current = selector(enumerator.Current, checked(index++));
|
||||||
|
goto CONTINUE;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
goto DONE;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
goto DONE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
result = false;
|
state = -2;
|
||||||
return true;
|
completionSource.TrySetException(ex);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DONE:
|
||||||
|
state = -2;
|
||||||
|
completionSource.TrySetResult(false);
|
||||||
|
return;
|
||||||
|
|
||||||
|
CONTINUE:
|
||||||
|
state = 0;
|
||||||
|
completionSource.TrySetResult(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTask DisposeAsync()
|
||||||
|
{
|
||||||
|
return enumerator.DisposeAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -158,26 +284,105 @@ namespace Cysharp.Threading.Tasks.Linq
|
|||||||
return new _SelectAwait(source, selector, cancellationToken);
|
return new _SelectAwait(source, selector, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
sealed class _SelectAwait : AsyncEnumeratorAwaitSelectorBase<TSource, TResult, TResult>
|
sealed class _SelectAwait : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
|
||||||
{
|
{
|
||||||
|
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||||
readonly Func<TSource, UniTask<TResult>> selector;
|
readonly Func<TSource, UniTask<TResult>> selector;
|
||||||
|
readonly CancellationToken cancellationToken;
|
||||||
|
|
||||||
|
int state = -1;
|
||||||
|
IUniTaskAsyncEnumerator<TSource> enumerator;
|
||||||
|
UniTask<bool>.Awaiter awaiter;
|
||||||
|
UniTask<TResult>.Awaiter awaiter2;
|
||||||
|
Action moveNextAction;
|
||||||
|
|
||||||
public _SelectAwait(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TResult>> selector, CancellationToken cancellationToken)
|
public _SelectAwait(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TResult>> selector, CancellationToken cancellationToken)
|
||||||
: base(source, cancellationToken)
|
|
||||||
{
|
{
|
||||||
|
this.source = source;
|
||||||
this.selector = selector;
|
this.selector = selector;
|
||||||
|
this.cancellationToken = cancellationToken;
|
||||||
|
this.moveNextAction = MoveNext;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override UniTask<TResult> TransformAsync(TSource sourceCurrent)
|
public TResult Current { get; private set; }
|
||||||
|
|
||||||
|
public UniTask<bool> MoveNextAsync()
|
||||||
{
|
{
|
||||||
return selector(sourceCurrent);
|
if (state == -2) return default;
|
||||||
|
|
||||||
|
completionSource.Reset();
|
||||||
|
MoveNext();
|
||||||
|
return new UniTask<bool>(this, completionSource.Version);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override bool TrySetCurrentCore(TResult awaitResult, out bool terminateIteration)
|
void MoveNext()
|
||||||
{
|
{
|
||||||
Current = awaitResult;
|
try
|
||||||
terminateIteration = false;
|
{
|
||||||
return true;
|
switch (state)
|
||||||
|
{
|
||||||
|
case -1: // init
|
||||||
|
enumerator = source.GetAsyncEnumerator(cancellationToken);
|
||||||
|
goto case 0;
|
||||||
|
case 0:
|
||||||
|
awaiter = enumerator.MoveNextAsync().GetAwaiter();
|
||||||
|
if (awaiter.IsCompleted)
|
||||||
|
{
|
||||||
|
goto case 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 1;
|
||||||
|
awaiter.UnsafeOnCompleted(moveNextAction);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case 1:
|
||||||
|
if (awaiter.GetResult())
|
||||||
|
{
|
||||||
|
awaiter2 = selector(enumerator.Current).GetAwaiter();
|
||||||
|
if (awaiter2.IsCompleted)
|
||||||
|
{
|
||||||
|
goto case 2;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 2;
|
||||||
|
awaiter2.UnsafeOnCompleted(moveNextAction);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
goto DONE;
|
||||||
|
}
|
||||||
|
case 2:
|
||||||
|
Current = awaiter2.GetResult();
|
||||||
|
goto CONTINUE;
|
||||||
|
default:
|
||||||
|
goto DONE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
state = -2;
|
||||||
|
completionSource.TrySetException(ex);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
DONE:
|
||||||
|
state = -2;
|
||||||
|
completionSource.TrySetResult(false);
|
||||||
|
return;
|
||||||
|
|
||||||
|
CONTINUE:
|
||||||
|
state = 0;
|
||||||
|
completionSource.TrySetResult(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTask DisposeAsync()
|
||||||
|
{
|
||||||
|
return enumerator.DisposeAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -195,30 +400,109 @@ namespace Cysharp.Threading.Tasks.Linq
|
|||||||
|
|
||||||
public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
return new _SelectIntAwait(source, selector, cancellationToken);
|
return new _SelectAwait(source, selector, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
sealed class _SelectIntAwait : AsyncEnumeratorAwaitSelectorBase<TSource, TResult, TResult>
|
sealed class _SelectAwait : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
|
||||||
{
|
{
|
||||||
|
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||||
readonly Func<TSource, int, UniTask<TResult>> selector;
|
readonly Func<TSource, int, UniTask<TResult>> selector;
|
||||||
|
readonly CancellationToken cancellationToken;
|
||||||
|
|
||||||
|
int state = -1;
|
||||||
|
IUniTaskAsyncEnumerator<TSource> enumerator;
|
||||||
|
UniTask<bool>.Awaiter awaiter;
|
||||||
|
UniTask<TResult>.Awaiter awaiter2;
|
||||||
|
Action moveNextAction;
|
||||||
int index;
|
int index;
|
||||||
|
|
||||||
public _SelectIntAwait(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, UniTask<TResult>> selector, CancellationToken cancellationToken)
|
public _SelectAwait(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, UniTask<TResult>> selector, CancellationToken cancellationToken)
|
||||||
: base(source, cancellationToken)
|
|
||||||
{
|
{
|
||||||
|
this.source = source;
|
||||||
this.selector = selector;
|
this.selector = selector;
|
||||||
|
this.cancellationToken = cancellationToken;
|
||||||
|
this.moveNextAction = MoveNext;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override UniTask<TResult> TransformAsync(TSource sourceCurrent)
|
public TResult Current { get; private set; }
|
||||||
|
|
||||||
|
public UniTask<bool> MoveNextAsync()
|
||||||
{
|
{
|
||||||
return selector(sourceCurrent, checked(index++));
|
if (state == -2) return default;
|
||||||
|
|
||||||
|
completionSource.Reset();
|
||||||
|
MoveNext();
|
||||||
|
return new UniTask<bool>(this, completionSource.Version);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override bool TrySetCurrentCore(TResult awaitResult, out bool terminateIteration)
|
void MoveNext()
|
||||||
{
|
{
|
||||||
Current = awaitResult;
|
try
|
||||||
terminateIteration = false;
|
{
|
||||||
return true;
|
switch (state)
|
||||||
|
{
|
||||||
|
case -1: // init
|
||||||
|
enumerator = source.GetAsyncEnumerator(cancellationToken);
|
||||||
|
goto case 0;
|
||||||
|
case 0:
|
||||||
|
awaiter = enumerator.MoveNextAsync().GetAwaiter();
|
||||||
|
if (awaiter.IsCompleted)
|
||||||
|
{
|
||||||
|
goto case 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 1;
|
||||||
|
awaiter.UnsafeOnCompleted(moveNextAction);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case 1:
|
||||||
|
if (awaiter.GetResult())
|
||||||
|
{
|
||||||
|
awaiter2 = selector(enumerator.Current, checked(index++)).GetAwaiter();
|
||||||
|
if (awaiter2.IsCompleted)
|
||||||
|
{
|
||||||
|
goto case 2;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 2;
|
||||||
|
awaiter2.UnsafeOnCompleted(moveNextAction);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
goto DONE;
|
||||||
|
}
|
||||||
|
case 2:
|
||||||
|
Current = awaiter2.GetResult();
|
||||||
|
goto CONTINUE;
|
||||||
|
default:
|
||||||
|
goto DONE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
state = -2;
|
||||||
|
completionSource.TrySetException(ex);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
DONE:
|
||||||
|
state = -2;
|
||||||
|
completionSource.TrySetResult(false);
|
||||||
|
return;
|
||||||
|
|
||||||
|
CONTINUE:
|
||||||
|
state = 0;
|
||||||
|
completionSource.TrySetResult(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTask DisposeAsync()
|
||||||
|
{
|
||||||
|
return enumerator.DisposeAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -239,26 +523,105 @@ namespace Cysharp.Threading.Tasks.Linq
|
|||||||
return new _SelectAwaitWithCancellation(source, selector, cancellationToken);
|
return new _SelectAwaitWithCancellation(source, selector, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
sealed class _SelectAwaitWithCancellation : AsyncEnumeratorAwaitSelectorBase<TSource, TResult, TResult>
|
sealed class _SelectAwaitWithCancellation : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
|
||||||
{
|
{
|
||||||
|
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||||
readonly Func<TSource, CancellationToken, UniTask<TResult>> selector;
|
readonly Func<TSource, CancellationToken, UniTask<TResult>> selector;
|
||||||
|
readonly CancellationToken cancellationToken;
|
||||||
|
|
||||||
|
int state = -1;
|
||||||
|
IUniTaskAsyncEnumerator<TSource> enumerator;
|
||||||
|
UniTask<bool>.Awaiter awaiter;
|
||||||
|
UniTask<TResult>.Awaiter awaiter2;
|
||||||
|
Action moveNextAction;
|
||||||
|
|
||||||
public _SelectAwaitWithCancellation(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TResult>> selector, CancellationToken cancellationToken)
|
public _SelectAwaitWithCancellation(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TResult>> selector, CancellationToken cancellationToken)
|
||||||
: base(source, cancellationToken)
|
|
||||||
{
|
{
|
||||||
|
this.source = source;
|
||||||
this.selector = selector;
|
this.selector = selector;
|
||||||
|
this.cancellationToken = cancellationToken;
|
||||||
|
this.moveNextAction = MoveNext;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override UniTask<TResult> TransformAsync(TSource sourceCurrent)
|
public TResult Current { get; private set; }
|
||||||
|
|
||||||
|
public UniTask<bool> MoveNextAsync()
|
||||||
{
|
{
|
||||||
return selector(sourceCurrent, cancellationToken);
|
if (state == -2) return default;
|
||||||
|
|
||||||
|
completionSource.Reset();
|
||||||
|
MoveNext();
|
||||||
|
return new UniTask<bool>(this, completionSource.Version);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override bool TrySetCurrentCore(TResult awaitResult, out bool terminateIteration)
|
void MoveNext()
|
||||||
{
|
{
|
||||||
Current = awaitResult;
|
try
|
||||||
terminateIteration = false;
|
{
|
||||||
return true;
|
switch (state)
|
||||||
|
{
|
||||||
|
case -1: // init
|
||||||
|
enumerator = source.GetAsyncEnumerator(cancellationToken);
|
||||||
|
goto case 0;
|
||||||
|
case 0:
|
||||||
|
awaiter = enumerator.MoveNextAsync().GetAwaiter();
|
||||||
|
if (awaiter.IsCompleted)
|
||||||
|
{
|
||||||
|
goto case 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 1;
|
||||||
|
awaiter.UnsafeOnCompleted(moveNextAction);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case 1:
|
||||||
|
if (awaiter.GetResult())
|
||||||
|
{
|
||||||
|
awaiter2 = selector(enumerator.Current, cancellationToken).GetAwaiter();
|
||||||
|
if (awaiter2.IsCompleted)
|
||||||
|
{
|
||||||
|
goto case 2;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 2;
|
||||||
|
awaiter2.UnsafeOnCompleted(moveNextAction);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
goto DONE;
|
||||||
|
}
|
||||||
|
case 2:
|
||||||
|
Current = awaiter2.GetResult();
|
||||||
|
goto CONTINUE;
|
||||||
|
default:
|
||||||
|
goto DONE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
state = -2;
|
||||||
|
completionSource.TrySetException(ex);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
DONE:
|
||||||
|
state = -2;
|
||||||
|
completionSource.TrySetResult(false);
|
||||||
|
return;
|
||||||
|
|
||||||
|
CONTINUE:
|
||||||
|
state = 0;
|
||||||
|
completionSource.TrySetResult(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTask DisposeAsync()
|
||||||
|
{
|
||||||
|
return enumerator.DisposeAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -276,32 +639,110 @@ namespace Cysharp.Threading.Tasks.Linq
|
|||||||
|
|
||||||
public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
return new _SelectIntAwaitWithCancellation(source, selector, cancellationToken);
|
return new _SelectAwaitWithCancellation(source, selector, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
sealed class _SelectIntAwaitWithCancellation : AsyncEnumeratorAwaitSelectorBase<TSource, TResult, TResult>
|
sealed class _SelectAwaitWithCancellation : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
|
||||||
{
|
{
|
||||||
|
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||||
readonly Func<TSource, int, CancellationToken, UniTask<TResult>> selector;
|
readonly Func<TSource, int, CancellationToken, UniTask<TResult>> selector;
|
||||||
|
readonly CancellationToken cancellationToken;
|
||||||
|
|
||||||
|
int state = -1;
|
||||||
|
IUniTaskAsyncEnumerator<TSource> enumerator;
|
||||||
|
UniTask<bool>.Awaiter awaiter;
|
||||||
|
UniTask<TResult>.Awaiter awaiter2;
|
||||||
|
Action moveNextAction;
|
||||||
int index;
|
int index;
|
||||||
|
|
||||||
public _SelectIntAwaitWithCancellation(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, UniTask<TResult>> selector, CancellationToken cancellationToken)
|
public _SelectAwaitWithCancellation(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, UniTask<TResult>> selector, CancellationToken cancellationToken)
|
||||||
: base(source, cancellationToken)
|
|
||||||
{
|
{
|
||||||
|
this.source = source;
|
||||||
this.selector = selector;
|
this.selector = selector;
|
||||||
|
this.cancellationToken = cancellationToken;
|
||||||
|
this.moveNextAction = MoveNext;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override UniTask<TResult> TransformAsync(TSource sourceCurrent)
|
public TResult Current { get; private set; }
|
||||||
|
|
||||||
|
public UniTask<bool> MoveNextAsync()
|
||||||
{
|
{
|
||||||
return selector(sourceCurrent, checked(index++), cancellationToken);
|
if (state == -2) return default;
|
||||||
|
|
||||||
|
completionSource.Reset();
|
||||||
|
MoveNext();
|
||||||
|
return new UniTask<bool>(this, completionSource.Version);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override bool TrySetCurrentCore(TResult awaitResult, out bool terminateIteration)
|
void MoveNext()
|
||||||
{
|
{
|
||||||
Current = awaitResult;
|
try
|
||||||
terminateIteration = false;
|
{
|
||||||
return true;
|
switch (state)
|
||||||
|
{
|
||||||
|
case -1: // init
|
||||||
|
enumerator = source.GetAsyncEnumerator(cancellationToken);
|
||||||
|
goto case 0;
|
||||||
|
case 0:
|
||||||
|
awaiter = enumerator.MoveNextAsync().GetAwaiter();
|
||||||
|
if (awaiter.IsCompleted)
|
||||||
|
{
|
||||||
|
goto case 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 1;
|
||||||
|
awaiter.UnsafeOnCompleted(moveNextAction);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case 1:
|
||||||
|
if (awaiter.GetResult())
|
||||||
|
{
|
||||||
|
awaiter2 = selector(enumerator.Current, checked(index++), cancellationToken).GetAwaiter();
|
||||||
|
if (awaiter2.IsCompleted)
|
||||||
|
{
|
||||||
|
goto case 2;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 2;
|
||||||
|
awaiter2.UnsafeOnCompleted(moveNextAction);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
goto DONE;
|
||||||
|
}
|
||||||
|
case 2:
|
||||||
|
Current = awaiter2.GetResult();
|
||||||
|
goto CONTINUE;
|
||||||
|
default:
|
||||||
|
goto DONE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
state = -2;
|
||||||
|
completionSource.TrySetException(ex);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
DONE:
|
||||||
|
state = -2;
|
||||||
|
completionSource.TrySetResult(false);
|
||||||
|
return;
|
||||||
|
|
||||||
|
CONTINUE:
|
||||||
|
state = 0;
|
||||||
|
completionSource.TrySetResult(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTask DisposeAsync()
|
||||||
|
{
|
||||||
|
return enumerator.DisposeAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -19,6 +19,8 @@ namespace Cysharp.Threading.Tasks.Linq
|
|||||||
{
|
{
|
||||||
internal static async UniTask<TSource[]> ToArrayAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
|
internal static async UniTask<TSource[]> ToArrayAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
|
// UnityEngine.Debug.Log("Called ToArray");
|
||||||
|
|
||||||
var pool = ArrayPool<TSource>.Shared;
|
var pool = ArrayPool<TSource>.Shared;
|
||||||
var array = pool.Rent(16);
|
var array = pool.Rent(16);
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"name": "UniTask.Linq",
|
||||||
|
"references": [
|
||||||
|
"UniTask"
|
||||||
|
],
|
||||||
|
"includePlatforms": [],
|
||||||
|
"excludePlatforms": [],
|
||||||
|
"allowUnsafeCode": false,
|
||||||
|
"overrideReferences": false,
|
||||||
|
"precompiledReferences": [],
|
||||||
|
"autoReferenced": true,
|
||||||
|
"defineConstraints": [],
|
||||||
|
"versionDefines": [],
|
||||||
|
"noEngineReferences": false
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 5c01796d064528144a599661eaab93a6
|
||||||
|
AssemblyDefinitionImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@@ -1,7 +1,4 @@
|
|||||||
using Cysharp.Threading.Tasks.Internal;
|
using System.Threading;
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Threading;
|
|
||||||
|
|
||||||
namespace Cysharp.Threading.Tasks.Linq
|
namespace Cysharp.Threading.Tasks.Linq
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,241 @@
|
|||||||
|
using Cysharp.Threading.Tasks.Internal;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
namespace Cysharp.Threading.Tasks.Linq
|
||||||
|
{
|
||||||
|
public static partial class UniTaskAsyncEnumerable
|
||||||
|
{
|
||||||
|
public static IUniTaskAsyncEnumerable<TProperty> EveryValueChanged<TTarget, TProperty>(TTarget target, Func<TTarget, TProperty> propertySelector, PlayerLoopTiming monitorTiming = PlayerLoopTiming.Update, IEqualityComparer<TProperty> equalityComparer = null)
|
||||||
|
where TTarget : class
|
||||||
|
{
|
||||||
|
var unityObject = target as UnityEngine.Object;
|
||||||
|
var isUnityObject = target is UnityEngine.Object; // don't use (unityObject == null)
|
||||||
|
|
||||||
|
if (isUnityObject)
|
||||||
|
{
|
||||||
|
return new EveryValueChangedUnityObject<TTarget, TProperty>(target, propertySelector, equalityComparer ?? UnityEqualityComparer.GetDefault<TProperty>(), monitorTiming);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return new EveryValueChangedStandardObject<TTarget, TProperty>(target, propertySelector, equalityComparer ?? UnityEqualityComparer.GetDefault<TProperty>(), monitorTiming);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal sealed class EveryValueChangedUnityObject<TTarget, TProperty> : IUniTaskAsyncEnumerable<TProperty>
|
||||||
|
{
|
||||||
|
readonly TTarget target;
|
||||||
|
readonly Func<TTarget, TProperty> propertySelector;
|
||||||
|
readonly IEqualityComparer<TProperty> equalityComparer;
|
||||||
|
readonly PlayerLoopTiming monitorTiming;
|
||||||
|
|
||||||
|
public EveryValueChangedUnityObject(TTarget target, Func<TTarget, TProperty> propertySelector, IEqualityComparer<TProperty> equalityComparer, PlayerLoopTiming monitorTiming)
|
||||||
|
{
|
||||||
|
this.target = target;
|
||||||
|
this.propertySelector = propertySelector;
|
||||||
|
this.equalityComparer = equalityComparer;
|
||||||
|
this.monitorTiming = monitorTiming;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IUniTaskAsyncEnumerator<TProperty> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
return new _EveryValueChanged(target, propertySelector, equalityComparer, monitorTiming, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
sealed class _EveryValueChanged : MoveNextSource, IUniTaskAsyncEnumerator<TProperty>, IPlayerLoopItem
|
||||||
|
{
|
||||||
|
readonly TTarget target;
|
||||||
|
readonly UnityEngine.Object targetAsUnityObject;
|
||||||
|
readonly IEqualityComparer<TProperty> equalityComparer;
|
||||||
|
readonly Func<TTarget, TProperty> propertySelector;
|
||||||
|
CancellationToken cancellationToken;
|
||||||
|
|
||||||
|
bool first;
|
||||||
|
TProperty currentValue;
|
||||||
|
bool disposed;
|
||||||
|
|
||||||
|
public _EveryValueChanged(TTarget target, Func<TTarget, TProperty> propertySelector, IEqualityComparer<TProperty> equalityComparer, PlayerLoopTiming monitorTiming, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
this.target = target;
|
||||||
|
this.targetAsUnityObject = target as UnityEngine.Object;
|
||||||
|
this.propertySelector = propertySelector;
|
||||||
|
this.equalityComparer = equalityComparer;
|
||||||
|
this.cancellationToken = cancellationToken;
|
||||||
|
this.first = true;
|
||||||
|
TaskTracker.TrackActiveTask(this, 2);
|
||||||
|
PlayerLoopHelper.AddAction(monitorTiming, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public TProperty Current => currentValue;
|
||||||
|
|
||||||
|
public UniTask<bool> MoveNextAsync()
|
||||||
|
{
|
||||||
|
// return false instead of throw
|
||||||
|
if (disposed || cancellationToken.IsCancellationRequested) return CompletedTasks.False;
|
||||||
|
|
||||||
|
if (first)
|
||||||
|
{
|
||||||
|
first = false;
|
||||||
|
if (targetAsUnityObject == null)
|
||||||
|
{
|
||||||
|
return CompletedTasks.False;
|
||||||
|
}
|
||||||
|
this.currentValue = propertySelector(target);
|
||||||
|
return CompletedTasks.True;
|
||||||
|
}
|
||||||
|
|
||||||
|
completionSource.Reset();
|
||||||
|
return new UniTask<bool>(this, completionSource.Version);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTask DisposeAsync()
|
||||||
|
{
|
||||||
|
if (!disposed)
|
||||||
|
{
|
||||||
|
disposed = true;
|
||||||
|
TaskTracker.RemoveTracking(this);
|
||||||
|
}
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool MoveNext()
|
||||||
|
{
|
||||||
|
if (disposed || cancellationToken.IsCancellationRequested || targetAsUnityObject == null) // destroyed = cancel.
|
||||||
|
{
|
||||||
|
completionSource.TrySetResult(false);
|
||||||
|
DisposeAsync().Forget();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
TProperty nextValue = default(TProperty);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
nextValue = propertySelector(target);
|
||||||
|
if (equalityComparer.Equals(currentValue, nextValue))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
completionSource.TrySetException(ex);
|
||||||
|
DisposeAsync().Forget();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
currentValue = nextValue;
|
||||||
|
completionSource.TrySetResult(true);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal sealed class EveryValueChangedStandardObject<TTarget, TProperty> : IUniTaskAsyncEnumerable<TProperty>
|
||||||
|
where TTarget : class
|
||||||
|
{
|
||||||
|
readonly WeakReference<TTarget> target;
|
||||||
|
readonly Func<TTarget, TProperty> propertySelector;
|
||||||
|
readonly IEqualityComparer<TProperty> equalityComparer;
|
||||||
|
readonly PlayerLoopTiming monitorTiming;
|
||||||
|
|
||||||
|
public EveryValueChangedStandardObject(TTarget target, Func<TTarget, TProperty> propertySelector, IEqualityComparer<TProperty> equalityComparer, PlayerLoopTiming monitorTiming)
|
||||||
|
{
|
||||||
|
this.target = new WeakReference<TTarget>(target, false);
|
||||||
|
this.propertySelector = propertySelector;
|
||||||
|
this.equalityComparer = equalityComparer;
|
||||||
|
this.monitorTiming = monitorTiming;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IUniTaskAsyncEnumerator<TProperty> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
return new _EveryValueChanged(target, propertySelector, equalityComparer, monitorTiming, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
sealed class _EveryValueChanged : MoveNextSource, IUniTaskAsyncEnumerator<TProperty>, IPlayerLoopItem
|
||||||
|
{
|
||||||
|
readonly WeakReference<TTarget> target;
|
||||||
|
readonly IEqualityComparer<TProperty> equalityComparer;
|
||||||
|
readonly Func<TTarget, TProperty> propertySelector;
|
||||||
|
CancellationToken cancellationToken;
|
||||||
|
|
||||||
|
bool first;
|
||||||
|
TProperty currentValue;
|
||||||
|
bool disposed;
|
||||||
|
|
||||||
|
public _EveryValueChanged(WeakReference<TTarget> target, Func<TTarget, TProperty> propertySelector, IEqualityComparer<TProperty> equalityComparer, PlayerLoopTiming monitorTiming, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
this.target = target;
|
||||||
|
this.propertySelector = propertySelector;
|
||||||
|
this.equalityComparer = equalityComparer;
|
||||||
|
this.cancellationToken = cancellationToken;
|
||||||
|
this.first = true;
|
||||||
|
TaskTracker.TrackActiveTask(this, 2);
|
||||||
|
PlayerLoopHelper.AddAction(monitorTiming, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public TProperty Current => currentValue;
|
||||||
|
|
||||||
|
public UniTask<bool> MoveNextAsync()
|
||||||
|
{
|
||||||
|
if (disposed || cancellationToken.IsCancellationRequested) return CompletedTasks.False;
|
||||||
|
|
||||||
|
if (first)
|
||||||
|
{
|
||||||
|
first = false;
|
||||||
|
if (!target.TryGetTarget(out var t))
|
||||||
|
{
|
||||||
|
return CompletedTasks.False;
|
||||||
|
}
|
||||||
|
this.currentValue = propertySelector(t);
|
||||||
|
return CompletedTasks.True;
|
||||||
|
}
|
||||||
|
|
||||||
|
completionSource.Reset();
|
||||||
|
return new UniTask<bool>(this, completionSource.Version);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTask DisposeAsync()
|
||||||
|
{
|
||||||
|
if (!disposed)
|
||||||
|
{
|
||||||
|
disposed = true;
|
||||||
|
TaskTracker.RemoveTracking(this);
|
||||||
|
}
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool MoveNext()
|
||||||
|
{
|
||||||
|
UnityEngine.Debug.Log("TRY_RESULT:" + target.TryGetTarget(out var _));
|
||||||
|
if (disposed || cancellationToken.IsCancellationRequested || !target.TryGetTarget(out var t))
|
||||||
|
{
|
||||||
|
completionSource.TrySetResult(false);
|
||||||
|
DisposeAsync().Forget();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
TProperty nextValue = default(TProperty);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
nextValue = propertySelector(t);
|
||||||
|
if (equalityComparer.Equals(currentValue, nextValue))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
completionSource.TrySetException(ex);
|
||||||
|
DisposeAsync().Forget();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
currentValue = nextValue;
|
||||||
|
completionSource.TrySetResult(true);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 1ec39f1c41c305344854782c935ad354
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
namespace Cysharp.Threading.Tasks.Linq
|
namespace Cysharp.Threading.Tasks.Linq
|
||||||
{
|
{
|
||||||
@@ -22,16 +23,34 @@ namespace Cysharp.Threading.Tasks.Linq
|
|||||||
|
|
||||||
public static IUniTaskAsyncEnumerable<AsyncUnit> TimerFrame(int dueTimeFrameCount, PlayerLoopTiming updateTiming = PlayerLoopTiming.Update)
|
public static IUniTaskAsyncEnumerable<AsyncUnit> TimerFrame(int dueTimeFrameCount, PlayerLoopTiming updateTiming = PlayerLoopTiming.Update)
|
||||||
{
|
{
|
||||||
|
if (dueTimeFrameCount < 0)
|
||||||
|
{
|
||||||
|
throw new ArgumentOutOfRangeException("Delay does not allow minus delayFrameCount. dueTimeFrameCount:" + dueTimeFrameCount);
|
||||||
|
}
|
||||||
|
|
||||||
return new TimerFrame(dueTimeFrameCount, null, updateTiming);
|
return new TimerFrame(dueTimeFrameCount, null, updateTiming);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IUniTaskAsyncEnumerable<AsyncUnit> TimerFrame(int dueTimeFrameCount, int periodFrameCount, PlayerLoopTiming updateTiming = PlayerLoopTiming.Update)
|
public static IUniTaskAsyncEnumerable<AsyncUnit> TimerFrame(int dueTimeFrameCount, int periodFrameCount, PlayerLoopTiming updateTiming = PlayerLoopTiming.Update)
|
||||||
{
|
{
|
||||||
|
if (dueTimeFrameCount < 0)
|
||||||
|
{
|
||||||
|
throw new ArgumentOutOfRangeException("Delay does not allow minus delayFrameCount. dueTimeFrameCount:" + dueTimeFrameCount);
|
||||||
|
}
|
||||||
|
if (periodFrameCount < 0)
|
||||||
|
{
|
||||||
|
throw new ArgumentOutOfRangeException("Delay does not allow minus periodFrameCount. periodFrameCount:" + dueTimeFrameCount);
|
||||||
|
}
|
||||||
|
|
||||||
return new TimerFrame(dueTimeFrameCount, periodFrameCount, updateTiming);
|
return new TimerFrame(dueTimeFrameCount, periodFrameCount, updateTiming);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IUniTaskAsyncEnumerable<AsyncUnit> IntervalFrame(int intervalFrameCount, PlayerLoopTiming updateTiming = PlayerLoopTiming.Update)
|
public static IUniTaskAsyncEnumerable<AsyncUnit> IntervalFrame(int intervalFrameCount, PlayerLoopTiming updateTiming = PlayerLoopTiming.Update)
|
||||||
{
|
{
|
||||||
|
if (intervalFrameCount < 0)
|
||||||
|
{
|
||||||
|
throw new ArgumentOutOfRangeException("Delay does not allow minus intervalFrameCount. intervalFrameCount:" + intervalFrameCount);
|
||||||
|
}
|
||||||
return new TimerFrame(intervalFrameCount, intervalFrameCount, updateTiming);
|
return new TimerFrame(intervalFrameCount, intervalFrameCount, updateTiming);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -64,6 +83,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
|||||||
readonly bool ignoreTimeScale;
|
readonly bool ignoreTimeScale;
|
||||||
CancellationToken cancellationToken;
|
CancellationToken cancellationToken;
|
||||||
|
|
||||||
|
int initialFrame;
|
||||||
float elapsed;
|
float elapsed;
|
||||||
bool dueTimePhase;
|
bool dueTimePhase;
|
||||||
bool completed;
|
bool completed;
|
||||||
@@ -80,6 +100,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
|||||||
if (this.period <= 0) this.period = 1;
|
if (this.period <= 0) this.period = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.initialFrame = Time.frameCount;
|
||||||
this.dueTimePhase = true;
|
this.dueTimePhase = true;
|
||||||
this.updateTiming = updateTiming;
|
this.updateTiming = updateTiming;
|
||||||
this.ignoreTimeScale = ignoreTimeScale;
|
this.ignoreTimeScale = ignoreTimeScale;
|
||||||
@@ -119,9 +140,19 @@ namespace Cysharp.Threading.Tasks.Linq
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
elapsed += (ignoreTimeScale) ? UnityEngine.Time.unscaledDeltaTime : UnityEngine.Time.deltaTime;
|
|
||||||
if (dueTimePhase)
|
if (dueTimePhase)
|
||||||
{
|
{
|
||||||
|
if (elapsed == 0)
|
||||||
|
{
|
||||||
|
// skip in initial frame.
|
||||||
|
if (initialFrame == Time.frameCount)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
elapsed += (ignoreTimeScale) ? UnityEngine.Time.unscaledDeltaTime : UnityEngine.Time.deltaTime;
|
||||||
|
|
||||||
if (elapsed >= dueTime)
|
if (elapsed >= dueTime)
|
||||||
{
|
{
|
||||||
dueTimePhase = false;
|
dueTimePhase = false;
|
||||||
@@ -137,6 +168,8 @@ namespace Cysharp.Threading.Tasks.Linq
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
elapsed += (ignoreTimeScale) ? UnityEngine.Time.unscaledDeltaTime : UnityEngine.Time.deltaTime;
|
||||||
|
|
||||||
if (elapsed >= period)
|
if (elapsed >= period)
|
||||||
{
|
{
|
||||||
completionSource.TrySetResult(true);
|
completionSource.TrySetResult(true);
|
||||||
@@ -172,6 +205,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
|||||||
readonly int? periodFrameCount;
|
readonly int? periodFrameCount;
|
||||||
CancellationToken cancellationToken;
|
CancellationToken cancellationToken;
|
||||||
|
|
||||||
|
int initialFrame;
|
||||||
int currentFrame;
|
int currentFrame;
|
||||||
bool dueTimePhase;
|
bool dueTimePhase;
|
||||||
bool completed;
|
bool completed;
|
||||||
@@ -185,6 +219,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
|||||||
if (periodFrameCount <= 0) periodFrameCount = 1;
|
if (periodFrameCount <= 0) periodFrameCount = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.initialFrame = Time.frameCount;
|
||||||
this.dueTimePhase = true;
|
this.dueTimePhase = true;
|
||||||
this.dueTimeFrameCount = dueTimeFrameCount;
|
this.dueTimeFrameCount = dueTimeFrameCount;
|
||||||
this.periodFrameCount = periodFrameCount;
|
this.periodFrameCount = periodFrameCount;
|
||||||
@@ -228,11 +263,30 @@ namespace Cysharp.Threading.Tasks.Linq
|
|||||||
|
|
||||||
if (dueTimePhase)
|
if (dueTimePhase)
|
||||||
{
|
{
|
||||||
if (currentFrame++ >= dueTimeFrameCount)
|
if (currentFrame == 0)
|
||||||
|
{
|
||||||
|
if (dueTimeFrameCount == 0)
|
||||||
|
{
|
||||||
|
dueTimePhase = false;
|
||||||
|
completionSource.TrySetResult(true);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// skip in initial frame.
|
||||||
|
if (initialFrame == Time.frameCount)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (++currentFrame >= dueTimeFrameCount)
|
||||||
{
|
{
|
||||||
dueTimePhase = false;
|
dueTimePhase = false;
|
||||||
completionSource.TrySetResult(true);
|
completionSource.TrySetResult(true);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -71,36 +71,101 @@ namespace Cysharp.Threading.Tasks.Linq
|
|||||||
return new _Where(source, predicate, cancellationToken);
|
return new _Where(source, predicate, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
class _Where : AsyncEnumeratorBase<TSource, TSource>
|
sealed class _Where : MoveNextSource, IUniTaskAsyncEnumerator<TSource>
|
||||||
{
|
{
|
||||||
|
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||||
readonly Func<TSource, bool> predicate;
|
readonly Func<TSource, bool> predicate;
|
||||||
|
readonly CancellationToken cancellationToken;
|
||||||
|
|
||||||
|
int state = -1;
|
||||||
|
IUniTaskAsyncEnumerator<TSource> enumerator;
|
||||||
|
UniTask<bool>.Awaiter awaiter;
|
||||||
|
Action moveNextAction;
|
||||||
|
|
||||||
public _Where(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
|
public _Where(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
|
||||||
|
|
||||||
: base(source, cancellationToken)
|
|
||||||
{
|
{
|
||||||
|
this.source = source;
|
||||||
this.predicate = predicate;
|
this.predicate = predicate;
|
||||||
|
this.cancellationToken = cancellationToken;
|
||||||
|
this.moveNextAction = MoveNext;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override bool TryMoveNextCore(bool sourceHasCurrent, out bool result)
|
public TSource Current { get; private set; }
|
||||||
|
|
||||||
|
public UniTask<bool> MoveNextAsync()
|
||||||
{
|
{
|
||||||
if (sourceHasCurrent)
|
if (state == -2) return default;
|
||||||
|
|
||||||
|
completionSource.Reset();
|
||||||
|
MoveNext();
|
||||||
|
return new UniTask<bool>(this, completionSource.Version);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MoveNext()
|
||||||
|
{
|
||||||
|
REPEAT:
|
||||||
|
try
|
||||||
{
|
{
|
||||||
if (predicate(SourceCurrent))
|
switch (state)
|
||||||
{
|
{
|
||||||
Current = SourceCurrent;
|
case -1: // init
|
||||||
result = true;
|
enumerator = source.GetAsyncEnumerator(cancellationToken);
|
||||||
return true;
|
goto case 0;
|
||||||
}
|
case 0:
|
||||||
else
|
awaiter = enumerator.MoveNextAsync().GetAwaiter();
|
||||||
{
|
if (awaiter.IsCompleted)
|
||||||
result = default;
|
{
|
||||||
return false;
|
goto case 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 1;
|
||||||
|
awaiter.UnsafeOnCompleted(moveNextAction);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case 1:
|
||||||
|
if (awaiter.GetResult())
|
||||||
|
{
|
||||||
|
Current = enumerator.Current;
|
||||||
|
if (predicate(Current))
|
||||||
|
{
|
||||||
|
goto CONTINUE;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 0;
|
||||||
|
goto REPEAT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
goto DONE;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
goto DONE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
state = -2;
|
||||||
|
completionSource.TrySetException(ex);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
result = false;
|
DONE:
|
||||||
return true;
|
state = -2;
|
||||||
|
completionSource.TrySetResult(false);
|
||||||
|
return;
|
||||||
|
|
||||||
|
CONTINUE:
|
||||||
|
state = 0;
|
||||||
|
completionSource.TrySetResult(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTask DisposeAsync()
|
||||||
|
{
|
||||||
|
return enumerator.DisposeAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -118,40 +183,105 @@ namespace Cysharp.Threading.Tasks.Linq
|
|||||||
|
|
||||||
public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
return new _WhereInt(source, predicate, cancellationToken);
|
return new _Where(source, predicate, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
class _WhereInt : AsyncEnumeratorBase<TSource, TSource>
|
sealed class _Where : MoveNextSource, IUniTaskAsyncEnumerator<TSource>
|
||||||
{
|
{
|
||||||
|
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||||
readonly Func<TSource, int, bool> predicate;
|
readonly Func<TSource, int, bool> predicate;
|
||||||
|
readonly CancellationToken cancellationToken;
|
||||||
|
|
||||||
|
int state = -1;
|
||||||
|
IUniTaskAsyncEnumerator<TSource> enumerator;
|
||||||
|
UniTask<bool>.Awaiter awaiter;
|
||||||
|
Action moveNextAction;
|
||||||
int index;
|
int index;
|
||||||
|
|
||||||
public _WhereInt(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, bool> predicate, CancellationToken cancellationToken)
|
public _Where(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, bool> predicate, CancellationToken cancellationToken)
|
||||||
|
|
||||||
: base(source, cancellationToken)
|
|
||||||
{
|
{
|
||||||
|
this.source = source;
|
||||||
this.predicate = predicate;
|
this.predicate = predicate;
|
||||||
|
this.cancellationToken = cancellationToken;
|
||||||
|
this.moveNextAction = MoveNext;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override bool TryMoveNextCore(bool sourceHasCurrent, out bool result)
|
public TSource Current { get; private set; }
|
||||||
|
|
||||||
|
public UniTask<bool> MoveNextAsync()
|
||||||
{
|
{
|
||||||
if (sourceHasCurrent)
|
if (state == -2) return default;
|
||||||
|
|
||||||
|
completionSource.Reset();
|
||||||
|
MoveNext();
|
||||||
|
return new UniTask<bool>(this, completionSource.Version);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MoveNext()
|
||||||
|
{
|
||||||
|
REPEAT:
|
||||||
|
try
|
||||||
{
|
{
|
||||||
if (predicate(SourceCurrent, checked(index++)))
|
switch (state)
|
||||||
{
|
{
|
||||||
Current = SourceCurrent;
|
case -1: // init
|
||||||
result = true;
|
enumerator = source.GetAsyncEnumerator(cancellationToken);
|
||||||
return true;
|
goto case 0;
|
||||||
}
|
case 0:
|
||||||
else
|
awaiter = enumerator.MoveNextAsync().GetAwaiter();
|
||||||
{
|
if (awaiter.IsCompleted)
|
||||||
result = default;
|
{
|
||||||
return false;
|
goto case 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 1;
|
||||||
|
awaiter.UnsafeOnCompleted(moveNextAction);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case 1:
|
||||||
|
if (awaiter.GetResult())
|
||||||
|
{
|
||||||
|
Current = enumerator.Current;
|
||||||
|
if (predicate(Current, checked(index++)))
|
||||||
|
{
|
||||||
|
goto CONTINUE;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 0;
|
||||||
|
goto REPEAT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
goto DONE;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
goto DONE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
state = -2;
|
||||||
|
completionSource.TrySetException(ex);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
result = false;
|
DONE:
|
||||||
return true;
|
state = -2;
|
||||||
|
completionSource.TrySetResult(false);
|
||||||
|
return;
|
||||||
|
|
||||||
|
CONTINUE:
|
||||||
|
state = 0;
|
||||||
|
completionSource.TrySetResult(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTask DisposeAsync()
|
||||||
|
{
|
||||||
|
return enumerator.DisposeAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -172,34 +302,115 @@ namespace Cysharp.Threading.Tasks.Linq
|
|||||||
return new _WhereAwait(source, predicate, cancellationToken);
|
return new _WhereAwait(source, predicate, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
class _WhereAwait : AsyncEnumeratorAwaitSelectorBase<TSource, TSource, bool>
|
sealed class _WhereAwait : MoveNextSource, IUniTaskAsyncEnumerator<TSource>
|
||||||
{
|
{
|
||||||
|
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||||
readonly Func<TSource, UniTask<bool>> predicate;
|
readonly Func<TSource, UniTask<bool>> predicate;
|
||||||
|
readonly CancellationToken cancellationToken;
|
||||||
|
|
||||||
|
int state = -1;
|
||||||
|
IUniTaskAsyncEnumerator<TSource> enumerator;
|
||||||
|
UniTask<bool>.Awaiter awaiter;
|
||||||
|
UniTask<bool>.Awaiter awaiter2;
|
||||||
|
Action moveNextAction;
|
||||||
|
|
||||||
public _WhereAwait(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<bool>> predicate, CancellationToken cancellationToken)
|
public _WhereAwait(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<bool>> predicate, CancellationToken cancellationToken)
|
||||||
|
|
||||||
: base(source, cancellationToken)
|
|
||||||
{
|
{
|
||||||
|
this.source = source;
|
||||||
this.predicate = predicate;
|
this.predicate = predicate;
|
||||||
|
this.cancellationToken = cancellationToken;
|
||||||
|
this.moveNextAction = MoveNext;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override UniTask<bool> TransformAsync(TSource sourceCurrent)
|
public TSource Current { get; private set; }
|
||||||
|
|
||||||
|
public UniTask<bool> MoveNextAsync()
|
||||||
{
|
{
|
||||||
return predicate(sourceCurrent);
|
if (state == -2) return default;
|
||||||
|
|
||||||
|
completionSource.Reset();
|
||||||
|
MoveNext();
|
||||||
|
return new UniTask<bool>(this, completionSource.Version);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override bool TrySetCurrentCore(bool awaitResult, out bool terminateIteration)
|
void MoveNext()
|
||||||
{
|
{
|
||||||
terminateIteration = false;
|
REPEAT:
|
||||||
if (awaitResult)
|
try
|
||||||
{
|
{
|
||||||
Current = SourceCurrent;
|
switch (state)
|
||||||
return true;
|
{
|
||||||
|
case -1: // init
|
||||||
|
enumerator = source.GetAsyncEnumerator(cancellationToken);
|
||||||
|
goto case 0;
|
||||||
|
case 0:
|
||||||
|
awaiter = enumerator.MoveNextAsync().GetAwaiter();
|
||||||
|
if (awaiter.IsCompleted)
|
||||||
|
{
|
||||||
|
goto case 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 1;
|
||||||
|
awaiter.UnsafeOnCompleted(moveNextAction);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case 1:
|
||||||
|
if (awaiter.GetResult())
|
||||||
|
{
|
||||||
|
Current = enumerator.Current;
|
||||||
|
|
||||||
|
awaiter2 = predicate(Current).GetAwaiter();
|
||||||
|
if (awaiter2.IsCompleted)
|
||||||
|
{
|
||||||
|
goto case 2;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 2;
|
||||||
|
awaiter2.UnsafeOnCompleted(moveNextAction);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
goto DONE;
|
||||||
|
}
|
||||||
|
case 2:
|
||||||
|
if (awaiter2.GetResult())
|
||||||
|
{
|
||||||
|
goto CONTINUE;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 0;
|
||||||
|
goto REPEAT;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
goto DONE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
return false;
|
state = -2;
|
||||||
|
completionSource.TrySetException(ex);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DONE:
|
||||||
|
state = -2;
|
||||||
|
completionSource.TrySetResult(false);
|
||||||
|
return;
|
||||||
|
|
||||||
|
CONTINUE:
|
||||||
|
state = 0;
|
||||||
|
completionSource.TrySetResult(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTask DisposeAsync()
|
||||||
|
{
|
||||||
|
return enumerator.DisposeAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -217,44 +428,123 @@ namespace Cysharp.Threading.Tasks.Linq
|
|||||||
|
|
||||||
public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
return new _WhereIntAwait(source, predicate, cancellationToken);
|
return new _WhereAwait(source, predicate, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
class _WhereIntAwait : AsyncEnumeratorAwaitSelectorBase<TSource, TSource, bool>
|
sealed class _WhereAwait : MoveNextSource, IUniTaskAsyncEnumerator<TSource>
|
||||||
{
|
{
|
||||||
|
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||||
readonly Func<TSource, int, UniTask<bool>> predicate;
|
readonly Func<TSource, int, UniTask<bool>> predicate;
|
||||||
|
readonly CancellationToken cancellationToken;
|
||||||
|
|
||||||
|
int state = -1;
|
||||||
|
IUniTaskAsyncEnumerator<TSource> enumerator;
|
||||||
|
UniTask<bool>.Awaiter awaiter;
|
||||||
|
UniTask<bool>.Awaiter awaiter2;
|
||||||
|
Action moveNextAction;
|
||||||
int index;
|
int index;
|
||||||
|
|
||||||
public _WhereIntAwait(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, UniTask<bool>> predicate, CancellationToken cancellationToken)
|
public _WhereAwait(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, UniTask<bool>> predicate, CancellationToken cancellationToken)
|
||||||
|
|
||||||
: base(source, cancellationToken)
|
|
||||||
{
|
{
|
||||||
|
this.source = source;
|
||||||
this.predicate = predicate;
|
this.predicate = predicate;
|
||||||
|
this.cancellationToken = cancellationToken;
|
||||||
|
this.moveNextAction = MoveNext;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override UniTask<bool> TransformAsync(TSource sourceCurrent)
|
public TSource Current { get; private set; }
|
||||||
|
|
||||||
|
public UniTask<bool> MoveNextAsync()
|
||||||
{
|
{
|
||||||
return predicate(sourceCurrent, checked(index++));
|
if (state == -2) return default;
|
||||||
|
|
||||||
|
completionSource.Reset();
|
||||||
|
MoveNext();
|
||||||
|
return new UniTask<bool>(this, completionSource.Version);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override bool TrySetCurrentCore(bool awaitResult, out bool terminateIteration)
|
void MoveNext()
|
||||||
{
|
{
|
||||||
terminateIteration = false;
|
REPEAT:
|
||||||
if (awaitResult)
|
try
|
||||||
{
|
{
|
||||||
Current = SourceCurrent;
|
switch (state)
|
||||||
return true;
|
{
|
||||||
|
case -1: // init
|
||||||
|
enumerator = source.GetAsyncEnumerator(cancellationToken);
|
||||||
|
goto case 0;
|
||||||
|
case 0:
|
||||||
|
awaiter = enumerator.MoveNextAsync().GetAwaiter();
|
||||||
|
if (awaiter.IsCompleted)
|
||||||
|
{
|
||||||
|
goto case 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 1;
|
||||||
|
awaiter.UnsafeOnCompleted(moveNextAction);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case 1:
|
||||||
|
if (awaiter.GetResult())
|
||||||
|
{
|
||||||
|
Current = enumerator.Current;
|
||||||
|
|
||||||
|
awaiter2 = predicate(Current, checked(index++)).GetAwaiter();
|
||||||
|
if (awaiter2.IsCompleted)
|
||||||
|
{
|
||||||
|
goto case 2;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 2;
|
||||||
|
awaiter2.UnsafeOnCompleted(moveNextAction);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
goto DONE;
|
||||||
|
}
|
||||||
|
case 2:
|
||||||
|
if (awaiter2.GetResult())
|
||||||
|
{
|
||||||
|
goto CONTINUE;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 0;
|
||||||
|
goto REPEAT;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
goto DONE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
return false;
|
state = -2;
|
||||||
|
completionSource.TrySetException(ex);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DONE:
|
||||||
|
state = -2;
|
||||||
|
completionSource.TrySetResult(false);
|
||||||
|
return;
|
||||||
|
|
||||||
|
CONTINUE:
|
||||||
|
state = 0;
|
||||||
|
completionSource.TrySetResult(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTask DisposeAsync()
|
||||||
|
{
|
||||||
|
return enumerator.DisposeAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
internal sealed class WhereAwaitWithCancellation<TSource> : IUniTaskAsyncEnumerable<TSource>
|
internal sealed class WhereAwaitWithCancellation<TSource> : IUniTaskAsyncEnumerable<TSource>
|
||||||
{
|
{
|
||||||
readonly IUniTaskAsyncEnumerable<TSource> source;
|
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||||
@@ -271,34 +561,115 @@ namespace Cysharp.Threading.Tasks.Linq
|
|||||||
return new _WhereAwaitWithCancellation(source, predicate, cancellationToken);
|
return new _WhereAwaitWithCancellation(source, predicate, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
class _WhereAwaitWithCancellation : AsyncEnumeratorAwaitSelectorBase<TSource, TSource, bool>
|
sealed class _WhereAwaitWithCancellation : MoveNextSource, IUniTaskAsyncEnumerator<TSource>
|
||||||
{
|
{
|
||||||
|
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||||
readonly Func<TSource, CancellationToken, UniTask<bool>> predicate;
|
readonly Func<TSource, CancellationToken, UniTask<bool>> predicate;
|
||||||
|
readonly CancellationToken cancellationToken;
|
||||||
|
|
||||||
|
int state = -1;
|
||||||
|
IUniTaskAsyncEnumerator<TSource> enumerator;
|
||||||
|
UniTask<bool>.Awaiter awaiter;
|
||||||
|
UniTask<bool>.Awaiter awaiter2;
|
||||||
|
Action moveNextAction;
|
||||||
|
|
||||||
public _WhereAwaitWithCancellation(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<bool>> predicate, CancellationToken cancellationToken)
|
public _WhereAwaitWithCancellation(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<bool>> predicate, CancellationToken cancellationToken)
|
||||||
|
|
||||||
: base(source, cancellationToken)
|
|
||||||
{
|
{
|
||||||
|
this.source = source;
|
||||||
this.predicate = predicate;
|
this.predicate = predicate;
|
||||||
|
this.cancellationToken = cancellationToken;
|
||||||
|
this.moveNextAction = MoveNext;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override UniTask<bool> TransformAsync(TSource sourceCurrent)
|
public TSource Current { get; private set; }
|
||||||
|
|
||||||
|
public UniTask<bool> MoveNextAsync()
|
||||||
{
|
{
|
||||||
return predicate(sourceCurrent, cancellationToken);
|
if (state == -2) return default;
|
||||||
|
|
||||||
|
completionSource.Reset();
|
||||||
|
MoveNext();
|
||||||
|
return new UniTask<bool>(this, completionSource.Version);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override bool TrySetCurrentCore(bool awaitResult, out bool terminateIteration)
|
void MoveNext()
|
||||||
{
|
{
|
||||||
terminateIteration = false;
|
REPEAT:
|
||||||
if (awaitResult)
|
try
|
||||||
{
|
{
|
||||||
Current = SourceCurrent;
|
switch (state)
|
||||||
return true;
|
{
|
||||||
|
case -1: // init
|
||||||
|
enumerator = source.GetAsyncEnumerator(cancellationToken);
|
||||||
|
goto case 0;
|
||||||
|
case 0:
|
||||||
|
awaiter = enumerator.MoveNextAsync().GetAwaiter();
|
||||||
|
if (awaiter.IsCompleted)
|
||||||
|
{
|
||||||
|
goto case 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 1;
|
||||||
|
awaiter.UnsafeOnCompleted(moveNextAction);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case 1:
|
||||||
|
if (awaiter.GetResult())
|
||||||
|
{
|
||||||
|
Current = enumerator.Current;
|
||||||
|
|
||||||
|
awaiter2 = predicate(Current, cancellationToken).GetAwaiter();
|
||||||
|
if (awaiter2.IsCompleted)
|
||||||
|
{
|
||||||
|
goto case 2;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 2;
|
||||||
|
awaiter2.UnsafeOnCompleted(moveNextAction);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
goto DONE;
|
||||||
|
}
|
||||||
|
case 2:
|
||||||
|
if (awaiter2.GetResult())
|
||||||
|
{
|
||||||
|
goto CONTINUE;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 0;
|
||||||
|
goto REPEAT;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
goto DONE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
return false;
|
state = -2;
|
||||||
|
completionSource.TrySetException(ex);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DONE:
|
||||||
|
state = -2;
|
||||||
|
completionSource.TrySetResult(false);
|
||||||
|
return;
|
||||||
|
|
||||||
|
CONTINUE:
|
||||||
|
state = 0;
|
||||||
|
completionSource.TrySetResult(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTask DisposeAsync()
|
||||||
|
{
|
||||||
|
return enumerator.DisposeAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -316,40 +687,120 @@ namespace Cysharp.Threading.Tasks.Linq
|
|||||||
|
|
||||||
public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
return new _WhereIntAwaitWithCancellation(source, predicate, cancellationToken);
|
return new _WhereAwaitWithCancellation(source, predicate, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
class _WhereIntAwaitWithCancellation : AsyncEnumeratorAwaitSelectorBase<TSource, TSource, bool>
|
sealed class _WhereAwaitWithCancellation : MoveNextSource, IUniTaskAsyncEnumerator<TSource>
|
||||||
{
|
{
|
||||||
|
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||||
readonly Func<TSource, int, CancellationToken, UniTask<bool>> predicate;
|
readonly Func<TSource, int, CancellationToken, UniTask<bool>> predicate;
|
||||||
|
readonly CancellationToken cancellationToken;
|
||||||
|
|
||||||
|
int state = -1;
|
||||||
|
IUniTaskAsyncEnumerator<TSource> enumerator;
|
||||||
|
UniTask<bool>.Awaiter awaiter;
|
||||||
|
UniTask<bool>.Awaiter awaiter2;
|
||||||
|
Action moveNextAction;
|
||||||
int index;
|
int index;
|
||||||
|
|
||||||
public _WhereIntAwaitWithCancellation(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, UniTask<bool>> predicate, CancellationToken cancellationToken)
|
public _WhereAwaitWithCancellation(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, UniTask<bool>> predicate, CancellationToken cancellationToken)
|
||||||
|
|
||||||
: base(source, cancellationToken)
|
|
||||||
{
|
{
|
||||||
|
this.source = source;
|
||||||
this.predicate = predicate;
|
this.predicate = predicate;
|
||||||
|
this.cancellationToken = cancellationToken;
|
||||||
|
this.moveNextAction = MoveNext;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override UniTask<bool> TransformAsync(TSource sourceCurrent)
|
public TSource Current { get; private set; }
|
||||||
|
|
||||||
|
public UniTask<bool> MoveNextAsync()
|
||||||
{
|
{
|
||||||
return predicate(sourceCurrent, checked(index++), cancellationToken);
|
if (state == -2) return default;
|
||||||
|
|
||||||
|
completionSource.Reset();
|
||||||
|
MoveNext();
|
||||||
|
return new UniTask<bool>(this, completionSource.Version);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override bool TrySetCurrentCore(bool awaitResult, out bool terminateIteration)
|
void MoveNext()
|
||||||
{
|
{
|
||||||
terminateIteration = false;
|
REPEAT:
|
||||||
if (awaitResult)
|
try
|
||||||
{
|
{
|
||||||
Current = SourceCurrent;
|
switch (state)
|
||||||
return true;
|
{
|
||||||
|
case -1: // init
|
||||||
|
enumerator = source.GetAsyncEnumerator(cancellationToken);
|
||||||
|
goto case 0;
|
||||||
|
case 0:
|
||||||
|
awaiter = enumerator.MoveNextAsync().GetAwaiter();
|
||||||
|
if (awaiter.IsCompleted)
|
||||||
|
{
|
||||||
|
goto case 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 1;
|
||||||
|
awaiter.UnsafeOnCompleted(moveNextAction);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case 1:
|
||||||
|
if (awaiter.GetResult())
|
||||||
|
{
|
||||||
|
Current = enumerator.Current;
|
||||||
|
|
||||||
|
awaiter2 = predicate(Current, checked(index++), cancellationToken).GetAwaiter();
|
||||||
|
if (awaiter2.IsCompleted)
|
||||||
|
{
|
||||||
|
goto case 2;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 2;
|
||||||
|
awaiter2.UnsafeOnCompleted(moveNextAction);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
goto DONE;
|
||||||
|
}
|
||||||
|
case 2:
|
||||||
|
if (awaiter2.GetResult())
|
||||||
|
{
|
||||||
|
goto CONTINUE;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = 0;
|
||||||
|
goto REPEAT;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
goto DONE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
return false;
|
state = -2;
|
||||||
|
completionSource.TrySetException(ex);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DONE:
|
||||||
|
state = -2;
|
||||||
|
completionSource.TrySetResult(false);
|
||||||
|
return;
|
||||||
|
|
||||||
|
CONTINUE:
|
||||||
|
state = 0;
|
||||||
|
completionSource.TrySetResult(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTask DisposeAsync()
|
||||||
|
{
|
||||||
|
return enumerator.DisposeAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
63
src/UniTask/Assets/Plugins/UniTask/Runtime/MoveNextSource.cs
Normal file
63
src/UniTask/Assets/Plugins/UniTask/Runtime/MoveNextSource.cs
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Cysharp.Threading.Tasks
|
||||||
|
{
|
||||||
|
public abstract class MoveNextSource : IUniTaskSource<bool>
|
||||||
|
{
|
||||||
|
protected UniTaskCompletionSourceCore<bool> completionSource;
|
||||||
|
|
||||||
|
public bool GetResult(short token)
|
||||||
|
{
|
||||||
|
return completionSource.GetResult(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTaskStatus GetStatus(short token)
|
||||||
|
{
|
||||||
|
return completionSource.GetStatus(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnCompleted(Action<object> continuation, object state, short token)
|
||||||
|
{
|
||||||
|
completionSource.OnCompleted(continuation, state, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTaskStatus UnsafeGetStatus()
|
||||||
|
{
|
||||||
|
return completionSource.UnsafeGetStatus();
|
||||||
|
}
|
||||||
|
|
||||||
|
void IUniTaskSource.GetResult(short token)
|
||||||
|
{
|
||||||
|
completionSource.GetResult(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected bool TryGetResult<T>(UniTask<T>.Awaiter awaiter, out T result)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
result = awaiter.GetResult();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
completionSource.TrySetException(ex);
|
||||||
|
result = default;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected bool TryGetResult(UniTask.Awaiter awaiter)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
awaiter.GetResult();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
completionSource.TrySetException(ex);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: dc4c5dc2a5f246e4f8df44cab735826c
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@@ -92,8 +92,10 @@ namespace Cysharp.Threading.Tasks
|
|||||||
{
|
{
|
||||||
public static SynchronizationContext UnitySynchronizationContext => unitySynchronizationContetext;
|
public static SynchronizationContext UnitySynchronizationContext => unitySynchronizationContetext;
|
||||||
public static int MainThreadId => mainThreadId;
|
public static int MainThreadId => mainThreadId;
|
||||||
|
internal static string ApplicationDataPath => applicationDataPath;
|
||||||
|
|
||||||
static int mainThreadId;
|
static int mainThreadId;
|
||||||
|
static string applicationDataPath;
|
||||||
static SynchronizationContext unitySynchronizationContetext;
|
static SynchronizationContext unitySynchronizationContetext;
|
||||||
static ContinuationQueue[] yielders;
|
static ContinuationQueue[] yielders;
|
||||||
static PlayerLoopRunner[] runners;
|
static PlayerLoopRunner[] runners;
|
||||||
@@ -177,6 +179,11 @@ namespace Cysharp.Threading.Tasks
|
|||||||
// capture default(unity) sync-context.
|
// capture default(unity) sync-context.
|
||||||
unitySynchronizationContetext = SynchronizationContext.Current;
|
unitySynchronizationContetext = SynchronizationContext.Current;
|
||||||
mainThreadId = Thread.CurrentThread.ManagedThreadId;
|
mainThreadId = Thread.CurrentThread.ManagedThreadId;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
applicationDataPath = Application.dataPath;
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
|
||||||
#if UNITY_EDITOR && UNITY_2019_3_OR_NEWER
|
#if UNITY_EDITOR && UNITY_2019_3_OR_NEWER
|
||||||
// When domain reload is disabled, re-initialization is required when entering play mode;
|
// When domain reload is disabled, re-initialization is required when entering play mode;
|
||||||
@@ -200,27 +207,43 @@ namespace Cysharp.Threading.Tasks
|
|||||||
|
|
||||||
|
|
||||||
#if UNITY_EDITOR
|
#if UNITY_EDITOR
|
||||||
|
|
||||||
[InitializeOnLoadMethod]
|
[InitializeOnLoadMethod]
|
||||||
static void InitOnEditor()
|
static void InitOnEditor()
|
||||||
{
|
{
|
||||||
//Execute the play mode init method
|
// Execute the play mode init method
|
||||||
Init();
|
Init();
|
||||||
|
|
||||||
//register an Editor update delegate, used to forcing playerLoop update
|
// register an Editor update delegate, used to forcing playerLoop update
|
||||||
EditorApplication.update += ForceEditorPlayerLoopUpdate;
|
EditorApplication.update += ForceEditorPlayerLoopUpdate;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void ForceEditorPlayerLoopUpdate()
|
private static void ForceEditorPlayerLoopUpdate()
|
||||||
{
|
{
|
||||||
if (EditorApplication.isPlayingOrWillChangePlaymode || EditorApplication.isCompiling ||
|
if (EditorApplication.isPlayingOrWillChangePlaymode || EditorApplication.isCompiling || EditorApplication.isUpdating)
|
||||||
EditorApplication.isUpdating)
|
|
||||||
{
|
{
|
||||||
// Not in Edit mode, don't interfere
|
// Not in Edit mode, don't interfere
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//force unity to update PlayerLoop callbacks
|
// EditorApplication.QueuePlayerLoopUpdate causes performance issue, don't call directly.
|
||||||
EditorApplication.QueuePlayerLoopUpdate();
|
// EditorApplication.QueuePlayerLoopUpdate();
|
||||||
|
|
||||||
|
if (yielders != null)
|
||||||
|
{
|
||||||
|
foreach (var item in yielders)
|
||||||
|
{
|
||||||
|
if (item != null) item.Run();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (runners != null)
|
||||||
|
{
|
||||||
|
foreach (var item in runners)
|
||||||
|
{
|
||||||
|
if (item != null) item.Run();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
115
src/UniTask/Assets/Plugins/UniTask/Runtime/TaskPool.cs
Normal file
115
src/UniTask/Assets/Plugins/UniTask/Runtime/TaskPool.cs
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
namespace Cysharp.Threading.Tasks
|
||||||
|
{
|
||||||
|
// internaly used but public, allow to user create custom operator with pooling.
|
||||||
|
|
||||||
|
public static class TaskPool
|
||||||
|
{
|
||||||
|
internal static int MaxPoolSize;
|
||||||
|
static ConcurrentDictionary<Type, Func<int>> sizes = new ConcurrentDictionary<Type, Func<int>>();
|
||||||
|
|
||||||
|
static TaskPool()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var value = Environment.GetEnvironmentVariable("UNITASK_MAX_POOLSIZE");
|
||||||
|
if (value != null)
|
||||||
|
{
|
||||||
|
if (int.TryParse(value, out var size))
|
||||||
|
{
|
||||||
|
MaxPoolSize = size;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
|
||||||
|
MaxPoolSize = int.MaxValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void SetMaxPoolSize(int maxPoolSize)
|
||||||
|
{
|
||||||
|
MaxPoolSize = maxPoolSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IEnumerable<(Type, int)> GetCacheSizeInfo()
|
||||||
|
{
|
||||||
|
foreach (var item in sizes)
|
||||||
|
{
|
||||||
|
yield return (item.Key, item.Value());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void RegisterSizeGetter(Type type, Func<int> getSize)
|
||||||
|
{
|
||||||
|
sizes[type] = getSize;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public interface ITaskPoolNode<T>
|
||||||
|
{
|
||||||
|
T NextNode { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
// mutable struct, don't mark readonly.
|
||||||
|
[StructLayout(LayoutKind.Auto)]
|
||||||
|
public struct TaskPool<T>
|
||||||
|
where T : class, ITaskPoolNode<T>
|
||||||
|
{
|
||||||
|
int gate;
|
||||||
|
int size;
|
||||||
|
T root;
|
||||||
|
|
||||||
|
public int Size => size;
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public bool TryPop(out T result)
|
||||||
|
{
|
||||||
|
if (Interlocked.CompareExchange(ref gate, 1, 0) == 0)
|
||||||
|
{
|
||||||
|
var v = root;
|
||||||
|
if (!(v is null))
|
||||||
|
{
|
||||||
|
root = v.NextNode;
|
||||||
|
v.NextNode = null;
|
||||||
|
size--;
|
||||||
|
result = v;
|
||||||
|
Volatile.Write(ref gate, 0);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
Volatile.Write(ref gate, 0);
|
||||||
|
}
|
||||||
|
result = default;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public bool TryPush(T item)
|
||||||
|
{
|
||||||
|
if (Interlocked.CompareExchange(ref gate, 1, 0) == 0)
|
||||||
|
{
|
||||||
|
if (size < TaskPool.MaxPoolSize)
|
||||||
|
{
|
||||||
|
item.NextNode = root;
|
||||||
|
root = item;
|
||||||
|
size++;
|
||||||
|
Volatile.Write(ref gate, 0);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Volatile.Write(ref gate, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
11
src/UniTask/Assets/Plugins/UniTask/Runtime/TaskPool.cs.meta
Normal file
11
src/UniTask/Assets/Plugins/UniTask/Runtime/TaskPool.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 19f4e6575150765449cc99f25f06f25f
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
using Cysharp.Threading.Tasks.Internal;
|
using System;
|
||||||
using System;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
|
||||||
namespace Cysharp.Threading.Tasks
|
namespace Cysharp.Threading.Tasks
|
||||||
@@ -10,341 +9,302 @@ namespace Cysharp.Threading.Tasks
|
|||||||
void OnError(Exception ex);
|
void OnError(Exception ex);
|
||||||
void OnCompleted();
|
void OnCompleted();
|
||||||
void OnCanceled(CancellationToken cancellationToken);
|
void OnCanceled(CancellationToken cancellationToken);
|
||||||
|
|
||||||
|
// set/get from TriggerEvent<T>
|
||||||
|
ITriggerHandler<T> Prev { get; set; }
|
||||||
|
ITriggerHandler<T> Next { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
// be careful to use, itself is struct.
|
// be careful to use, itself is struct.
|
||||||
public struct TriggerEvent<T>
|
public struct TriggerEvent<T>
|
||||||
{
|
{
|
||||||
// optimize: many cases, handler is single.
|
ITriggerHandler<T> head; // head.prev is last
|
||||||
ITriggerHandler<T> singleHandler;
|
ITriggerHandler<T> iteratingHead;
|
||||||
|
|
||||||
ITriggerHandler<T>[] handlers;
|
bool preserveRemoveSelf;
|
||||||
|
ITriggerHandler<T> iteratingNode;
|
||||||
|
|
||||||
// when running(in TrySetResult), does not add immediately(trampoline).
|
void LogError(Exception ex)
|
||||||
bool isRunning;
|
{
|
||||||
ITriggerHandler<T> waitHandler;
|
#if UNITY_2018_3_OR_NEWER
|
||||||
MinimumQueue<ITriggerHandler<T>> waitQueue;
|
UnityEngine.Debug.LogException(ex);
|
||||||
|
#else
|
||||||
|
Console.WriteLine(ex);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
public void SetResult(T value)
|
public void SetResult(T value)
|
||||||
{
|
{
|
||||||
isRunning = true;
|
if (iteratingNode != null)
|
||||||
|
|
||||||
if (singleHandler != null)
|
|
||||||
{
|
{
|
||||||
|
throw new InvalidOperationException("Can not trigger itself in iterating.");
|
||||||
|
}
|
||||||
|
|
||||||
|
var h = head;
|
||||||
|
while (h != null)
|
||||||
|
{
|
||||||
|
iteratingNode = h;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
singleHandler.OnNext(value);
|
h.OnNext(value);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
#if UNITY_2018_3_OR_NEWER
|
LogError(ex);
|
||||||
UnityEngine.Debug.LogException(ex);
|
Remove(h);
|
||||||
#else
|
|
||||||
Console.WriteLine(ex);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (handlers != null)
|
if (preserveRemoveSelf)
|
||||||
{
|
|
||||||
for (int i = 0; i < handlers.Length; i++)
|
|
||||||
{
|
{
|
||||||
if (handlers[i] != null)
|
preserveRemoveSelf = false;
|
||||||
{
|
iteratingNode = null;
|
||||||
try
|
var next = h.Next;
|
||||||
{
|
Remove(h);
|
||||||
handlers[i].OnNext(value);
|
h = next;
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
handlers[i] = null;
|
|
||||||
#if UNITY_2018_3_OR_NEWER
|
|
||||||
UnityEngine.Debug.LogException(ex);
|
|
||||||
#else
|
|
||||||
Console.WriteLine(ex);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
else
|
||||||
|
|
||||||
isRunning = false;
|
|
||||||
|
|
||||||
if (waitHandler != null)
|
|
||||||
{
|
|
||||||
var h = waitHandler;
|
|
||||||
waitHandler = null;
|
|
||||||
Add(h);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (waitQueue != null)
|
|
||||||
{
|
|
||||||
while (waitQueue.Count != 0)
|
|
||||||
{
|
{
|
||||||
Add(waitQueue.Dequeue());
|
h = h.Next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
iteratingNode = null;
|
||||||
|
if (iteratingHead != null)
|
||||||
|
{
|
||||||
|
Add(iteratingHead);
|
||||||
|
iteratingHead = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetCanceled(CancellationToken cancellationToken)
|
public void SetCanceled(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
isRunning = true;
|
if (iteratingNode != null)
|
||||||
|
|
||||||
if (singleHandler != null)
|
|
||||||
{
|
{
|
||||||
|
throw new InvalidOperationException("Can not trigger itself in iterating.");
|
||||||
|
}
|
||||||
|
|
||||||
|
var h = head;
|
||||||
|
while (h != null)
|
||||||
|
{
|
||||||
|
iteratingNode = h;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
(singleHandler).OnCanceled(cancellationToken);
|
h.OnCanceled(cancellationToken);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
#if UNITY_2018_3_OR_NEWER
|
LogError(ex);
|
||||||
UnityEngine.Debug.LogException(ex);
|
|
||||||
#else
|
|
||||||
Console.WriteLine(ex);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
preserveRemoveSelf = false;
|
||||||
|
iteratingNode = null;
|
||||||
|
var next = h.Next;
|
||||||
|
Remove(h);
|
||||||
|
h = next;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (handlers != null)
|
iteratingNode = null;
|
||||||
|
if (iteratingHead != null)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < handlers.Length; i++)
|
Add(iteratingHead);
|
||||||
{
|
iteratingHead = null;
|
||||||
if (handlers[i] != null)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
(handlers[i]).OnCanceled(cancellationToken);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
#if UNITY_2018_3_OR_NEWER
|
|
||||||
UnityEngine.Debug.LogException(ex);
|
|
||||||
#else
|
|
||||||
Console.WriteLine(ex);
|
|
||||||
#endif
|
|
||||||
handlers[i] = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
isRunning = false;
|
|
||||||
|
|
||||||
if (waitHandler != null)
|
|
||||||
{
|
|
||||||
var h = waitHandler;
|
|
||||||
waitHandler = null;
|
|
||||||
Add(h);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (waitQueue != null)
|
|
||||||
{
|
|
||||||
while (waitQueue.Count != 0)
|
|
||||||
{
|
|
||||||
Add(waitQueue.Dequeue());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetCompleted()
|
public void SetCompleted()
|
||||||
{
|
{
|
||||||
isRunning = true;
|
if (iteratingNode != null)
|
||||||
|
|
||||||
if (singleHandler != null)
|
|
||||||
{
|
{
|
||||||
|
throw new InvalidOperationException("Can not trigger itself in iterating.");
|
||||||
|
}
|
||||||
|
|
||||||
|
var h = head;
|
||||||
|
while (h != null)
|
||||||
|
{
|
||||||
|
iteratingNode = h;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
(singleHandler).OnCompleted();
|
h.OnCompleted();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
#if UNITY_2018_3_OR_NEWER
|
LogError(ex);
|
||||||
UnityEngine.Debug.LogException(ex);
|
|
||||||
#else
|
|
||||||
Console.WriteLine(ex);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
preserveRemoveSelf = false;
|
||||||
|
iteratingNode = null;
|
||||||
|
var next = h.Next;
|
||||||
|
Remove(h);
|
||||||
|
h = next;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (handlers != null)
|
iteratingNode = null;
|
||||||
|
if (iteratingHead != null)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < handlers.Length; i++)
|
Add(iteratingHead);
|
||||||
{
|
iteratingHead = null;
|
||||||
if (handlers[i] != null)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
(handlers[i]).OnCompleted();
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
#if UNITY_2018_3_OR_NEWER
|
|
||||||
UnityEngine.Debug.LogException(ex);
|
|
||||||
#else
|
|
||||||
Console.WriteLine(ex);
|
|
||||||
#endif
|
|
||||||
handlers[i] = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
isRunning = false;
|
|
||||||
|
|
||||||
if (waitHandler != null)
|
|
||||||
{
|
|
||||||
var h = waitHandler;
|
|
||||||
waitHandler = null;
|
|
||||||
Add(h);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (waitQueue != null)
|
|
||||||
{
|
|
||||||
while (waitQueue.Count != 0)
|
|
||||||
{
|
|
||||||
Add(waitQueue.Dequeue());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetError(Exception exception)
|
public void SetError(Exception exception)
|
||||||
{
|
{
|
||||||
isRunning = true;
|
if (iteratingNode != null)
|
||||||
|
|
||||||
if (singleHandler != null)
|
|
||||||
{
|
{
|
||||||
|
throw new InvalidOperationException("Can not trigger itself in iterating.");
|
||||||
|
}
|
||||||
|
|
||||||
|
var h = head;
|
||||||
|
while (h != null)
|
||||||
|
{
|
||||||
|
iteratingNode = h;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
singleHandler.OnError(exception);
|
h.OnError(exception);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
#if UNITY_2018_3_OR_NEWER
|
LogError(ex);
|
||||||
UnityEngine.Debug.LogException(ex);
|
|
||||||
#else
|
|
||||||
Console.WriteLine(ex);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
preserveRemoveSelf = false;
|
||||||
|
iteratingNode = null;
|
||||||
|
var next = h.Next;
|
||||||
|
Remove(h);
|
||||||
|
h = next;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (handlers != null)
|
iteratingNode = null;
|
||||||
|
if (iteratingHead != null)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < handlers.Length; i++)
|
Add(iteratingHead);
|
||||||
{
|
iteratingHead = null;
|
||||||
if (handlers[i] != null)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
handlers[i].OnError(exception);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
handlers[i] = null;
|
|
||||||
#if UNITY_2018_3_OR_NEWER
|
|
||||||
UnityEngine.Debug.LogException(ex);
|
|
||||||
#else
|
|
||||||
Console.WriteLine(ex);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
isRunning = false;
|
|
||||||
|
|
||||||
if (waitHandler != null)
|
|
||||||
{
|
|
||||||
var h = waitHandler;
|
|
||||||
waitHandler = null;
|
|
||||||
Add(h);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (waitQueue != null)
|
|
||||||
{
|
|
||||||
while (waitQueue.Count != 0)
|
|
||||||
{
|
|
||||||
Add(waitQueue.Dequeue());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Add(ITriggerHandler<T> handler)
|
public void Add(ITriggerHandler<T> handler)
|
||||||
{
|
{
|
||||||
if (isRunning)
|
if (handler == null) throw new ArgumentNullException(nameof(handler));
|
||||||
{
|
|
||||||
if (waitHandler == null)
|
|
||||||
{
|
|
||||||
waitHandler = handler;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (waitQueue == null)
|
// zero node.
|
||||||
{
|
if (head == null)
|
||||||
waitQueue = new MinimumQueue<ITriggerHandler<T>>(4);
|
{
|
||||||
}
|
head = handler;
|
||||||
waitQueue.Enqueue(handler);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (singleHandler == null)
|
if (iteratingNode != null)
|
||||||
{
|
{
|
||||||
singleHandler = handler;
|
if (iteratingHead == null)
|
||||||
|
{
|
||||||
|
iteratingHead = handler;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var last = iteratingHead.Prev;
|
||||||
|
if (last == null)
|
||||||
|
{
|
||||||
|
// single node.
|
||||||
|
iteratingHead.Prev = handler;
|
||||||
|
iteratingHead.Next = handler;
|
||||||
|
handler.Prev = iteratingHead;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// multi node
|
||||||
|
iteratingHead.Prev = handler;
|
||||||
|
last.Next = handler;
|
||||||
|
handler.Prev = last;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (handlers == null)
|
var last = head.Prev;
|
||||||
|
if (last == null)
|
||||||
{
|
{
|
||||||
handlers = new ITriggerHandler<T>[4];
|
// single node.
|
||||||
|
head.Prev = handler;
|
||||||
|
head.Next = handler;
|
||||||
|
handler.Prev = head;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
// check empty
|
|
||||||
for (int i = 0; i < handlers.Length; i++)
|
|
||||||
{
|
{
|
||||||
if (handlers[i] == null)
|
// multi node
|
||||||
{
|
head.Prev = handler;
|
||||||
handlers[i] = handler;
|
last.Next = handler;
|
||||||
return;
|
handler.Prev = last;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// full, ensure capacity
|
|
||||||
var last = handlers.Length;
|
|
||||||
{
|
|
||||||
EnsureCapacity(ref handlers);
|
|
||||||
handlers[last] = handler;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void EnsureCapacity(ref ITriggerHandler<T>[] array)
|
|
||||||
{
|
|
||||||
var newSize = array.Length * 2;
|
|
||||||
var newArray = new ITriggerHandler<T>[newSize];
|
|
||||||
Array.Copy(array, 0, newArray, 0, array.Length);
|
|
||||||
array = newArray;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Remove(ITriggerHandler<T> handler)
|
public void Remove(ITriggerHandler<T> handler)
|
||||||
{
|
{
|
||||||
if (singleHandler == handler)
|
if (handler == null) throw new ArgumentNullException(nameof(handler));
|
||||||
|
|
||||||
|
if (iteratingNode != null && iteratingNode == handler)
|
||||||
{
|
{
|
||||||
singleHandler = null;
|
// if remove self, reserve remove self after invoke completed.
|
||||||
|
preserveRemoveSelf = true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (handlers != null)
|
var prev = handler.Prev;
|
||||||
|
var next = handler.Next;
|
||||||
|
|
||||||
|
if (next != null)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < handlers.Length; i++)
|
next.Prev = prev;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (handler == head)
|
||||||
|
{
|
||||||
|
head = next;
|
||||||
|
}
|
||||||
|
else if (handler == iteratingHead)
|
||||||
|
{
|
||||||
|
iteratingHead = next;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// when handler is head, prev indicate last so don't use it.
|
||||||
|
if (prev != null)
|
||||||
{
|
{
|
||||||
if (handlers[i] == handler)
|
prev.Next = next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (head != null)
|
||||||
|
{
|
||||||
|
if (head.Prev == handler)
|
||||||
|
{
|
||||||
|
if (prev != head)
|
||||||
{
|
{
|
||||||
// fill null.
|
head.Prev = prev;
|
||||||
handlers[i] = null;
|
}
|
||||||
return;
|
else
|
||||||
|
{
|
||||||
|
head.Prev = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (iteratingHead != null)
|
||||||
|
{
|
||||||
|
if (iteratingHead.Prev == handler)
|
||||||
|
{
|
||||||
|
if (prev != iteratingHead.Prev)
|
||||||
|
{
|
||||||
|
iteratingHead.Prev = prev;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
iteratingHead.Prev = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
handler.Prev = null;
|
||||||
|
handler.Next = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
||||||
|
|
||||||
using Cysharp.Threading.Tasks.Linq;
|
|
||||||
using System;
|
using System;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
@@ -103,6 +102,8 @@ namespace Cysharp.Threading.Tasks.Triggers
|
|||||||
}
|
}
|
||||||
|
|
||||||
public T Current { get; private set; }
|
public T Current { get; private set; }
|
||||||
|
ITriggerHandler<T> ITriggerHandler<T>.Prev { get; set; }
|
||||||
|
ITriggerHandler<T> ITriggerHandler<T>.Next { get; set; }
|
||||||
|
|
||||||
public UniTask<bool> MoveNextAsync()
|
public UniTask<bool> MoveNextAsync()
|
||||||
{
|
{
|
||||||
@@ -189,6 +190,9 @@ namespace Cysharp.Threading.Tasks.Triggers
|
|||||||
|
|
||||||
internal CancellationToken CancellationToken => cancellationToken;
|
internal CancellationToken CancellationToken => cancellationToken;
|
||||||
|
|
||||||
|
ITriggerHandler<T> ITriggerHandler<T>.Prev { get; set; }
|
||||||
|
ITriggerHandler<T> ITriggerHandler<T>.Next { get; set; }
|
||||||
|
|
||||||
internal AsyncTriggerHandler(AsyncTriggerBase<T> trigger, bool callOnce)
|
internal AsyncTriggerHandler(AsyncTriggerBase<T> trigger, bool callOnce)
|
||||||
{
|
{
|
||||||
if (cancellationToken.IsCancellationRequested)
|
if (cancellationToken.IsCancellationRequested)
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using Cysharp.Threading.Tasks.Internal;
|
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace Cysharp.Threading.Tasks
|
namespace Cysharp.Threading.Tasks
|
||||||
@@ -21,6 +20,46 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return new UniTask(YieldPromise.Create(timing, cancellationToken, out var token), token);
|
return new UniTask(YieldPromise.Create(timing, cancellationToken, out var token), token);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Similar as UniTask.Yield but guaranteed run on next frame.
|
||||||
|
/// </summary>
|
||||||
|
public static UniTask NextFrame(PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
return new UniTask(NextFramePromise.Create(timing, cancellationToken, out var token), token);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Same as UniTask.Yield(PlayerLoopTiming.LastPostLateUpdate).
|
||||||
|
/// </summary>
|
||||||
|
public static YieldAwaitable WaitForEndOfFrame()
|
||||||
|
{
|
||||||
|
return UniTask.Yield(PlayerLoopTiming.LastPostLateUpdate);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Same as UniTask.Yield(PlayerLoopTiming.LastPostLateUpdate, cancellationToken).
|
||||||
|
/// </summary>
|
||||||
|
public static UniTask WaitForEndOfFrame(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return UniTask.Yield(PlayerLoopTiming.LastPostLateUpdate, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Same as UniTask.Yield(PlayerLoopTiming.FixedUpdate).
|
||||||
|
/// </summary>
|
||||||
|
public static YieldAwaitable WaitForFixedUpdate()
|
||||||
|
{
|
||||||
|
return UniTask.Yield(PlayerLoopTiming.FixedUpdate);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Same as UniTask.Yield(PlayerLoopTiming.FixedUpdate, cancellationToken).
|
||||||
|
/// </summary>
|
||||||
|
public static UniTask WaitForFixedUpdate(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return UniTask.Yield(PlayerLoopTiming.FixedUpdate, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
public static UniTask DelayFrame(int delayFrameCount, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken))
|
public static UniTask DelayFrame(int delayFrameCount, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
{
|
{
|
||||||
if (delayFrameCount < 0)
|
if (delayFrameCount < 0)
|
||||||
@@ -56,9 +95,15 @@ namespace Cysharp.Threading.Tasks
|
|||||||
: new UniTask(DelayPromise.Create(delayTimeSpan, delayTiming, cancellationToken, out token), token);
|
: new UniTask(DelayPromise.Create(delayTimeSpan, delayTiming, cancellationToken, out token), token);
|
||||||
}
|
}
|
||||||
|
|
||||||
sealed class YieldPromise : IUniTaskSource, IPlayerLoopItem, IPromisePoolItem
|
sealed class YieldPromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode<YieldPromise>
|
||||||
{
|
{
|
||||||
static readonly PromisePool<YieldPromise> pool = new PromisePool<YieldPromise>();
|
static TaskPool<YieldPromise> pool;
|
||||||
|
public YieldPromise NextNode { get; set; }
|
||||||
|
|
||||||
|
static YieldPromise()
|
||||||
|
{
|
||||||
|
TaskPool.RegisterSizeGetter(typeof(YieldPromise), () => pool.Size);
|
||||||
|
}
|
||||||
|
|
||||||
CancellationToken cancellationToken;
|
CancellationToken cancellationToken;
|
||||||
UniTaskCompletionSourceCore<object> core;
|
UniTaskCompletionSourceCore<object> core;
|
||||||
@@ -74,7 +119,11 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
|
return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
|
||||||
}
|
}
|
||||||
|
|
||||||
var result = pool.TryRent() ?? new YieldPromise();
|
if (!pool.TryPop(out var result))
|
||||||
|
{
|
||||||
|
result = new YieldPromise();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
result.cancellationToken = cancellationToken;
|
result.cancellationToken = cancellationToken;
|
||||||
|
|
||||||
@@ -90,12 +139,11 @@ namespace Cysharp.Threading.Tasks
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
TaskTracker.RemoveTracking(this);
|
|
||||||
core.GetResult(token);
|
core.GetResult(token);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
pool.TryReturn(this);
|
TryReturn();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,45 +174,54 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Reset()
|
bool TryReturn()
|
||||||
{
|
{
|
||||||
|
TaskTracker.RemoveTracking(this);
|
||||||
core.Reset();
|
core.Reset();
|
||||||
cancellationToken = default;
|
cancellationToken = default;
|
||||||
|
return pool.TryPush(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
~YieldPromise()
|
~YieldPromise()
|
||||||
{
|
{
|
||||||
if (pool.TryReturn(this))
|
if (TryReturn())
|
||||||
{
|
{
|
||||||
GC.ReRegisterForFinalize(this);
|
GC.ReRegisterForFinalize(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sealed class DelayFramePromise : IUniTaskSource, IPlayerLoopItem, IPromisePoolItem
|
sealed class NextFramePromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode<NextFramePromise>
|
||||||
{
|
{
|
||||||
static readonly PromisePool<DelayFramePromise> pool = new PromisePool<DelayFramePromise>();
|
static TaskPool<NextFramePromise> pool;
|
||||||
|
public NextFramePromise NextNode { get; set; }
|
||||||
|
|
||||||
int delayFrameCount;
|
static NextFramePromise()
|
||||||
|
{
|
||||||
|
TaskPool.RegisterSizeGetter(typeof(NextFramePromise), () => pool.Size);
|
||||||
|
}
|
||||||
|
|
||||||
|
int frameCount;
|
||||||
CancellationToken cancellationToken;
|
CancellationToken cancellationToken;
|
||||||
|
UniTaskCompletionSourceCore<AsyncUnit> core;
|
||||||
|
|
||||||
int currentFrameCount;
|
NextFramePromise()
|
||||||
UniTaskCompletionSourceCore<object> core;
|
|
||||||
|
|
||||||
DelayFramePromise()
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IUniTaskSource Create(int delayFrameCount, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token)
|
public static IUniTaskSource Create(PlayerLoopTiming timing, CancellationToken cancellationToken, out short token)
|
||||||
{
|
{
|
||||||
if (cancellationToken.IsCancellationRequested)
|
if (cancellationToken.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
|
return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
|
||||||
}
|
}
|
||||||
|
|
||||||
var result = pool.TryRent() ?? new DelayFramePromise();
|
if (!pool.TryPop(out var result))
|
||||||
|
{
|
||||||
|
result = new NextFramePromise();
|
||||||
|
}
|
||||||
|
|
||||||
result.delayFrameCount = delayFrameCount;
|
result.frameCount = Time.frameCount;
|
||||||
result.cancellationToken = cancellationToken;
|
result.cancellationToken = cancellationToken;
|
||||||
|
|
||||||
TaskTracker.TrackActiveTask(result, 3);
|
TaskTracker.TrackActiveTask(result, 3);
|
||||||
@@ -179,12 +236,11 @@ namespace Cysharp.Threading.Tasks
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
TaskTracker.RemoveTracking(this);
|
|
||||||
core.GetResult(token);
|
core.GetResult(token);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
pool.TryReturn(this);
|
TryReturn();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -211,37 +267,166 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (currentFrameCount == delayFrameCount)
|
if (frameCount == Time.frameCount)
|
||||||
{
|
{
|
||||||
core.TrySetResult(null);
|
return true;
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
currentFrameCount++;
|
core.TrySetResult(AsyncUnit.Default);
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Reset()
|
bool TryReturn()
|
||||||
{
|
{
|
||||||
|
TaskTracker.RemoveTracking(this);
|
||||||
core.Reset();
|
core.Reset();
|
||||||
currentFrameCount = default;
|
|
||||||
delayFrameCount = default;
|
|
||||||
cancellationToken = default;
|
cancellationToken = default;
|
||||||
|
return pool.TryPush(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
~DelayFramePromise()
|
~NextFramePromise()
|
||||||
{
|
{
|
||||||
if (pool.TryReturn(this))
|
if (TryReturn())
|
||||||
{
|
{
|
||||||
GC.ReRegisterForFinalize(this);
|
GC.ReRegisterForFinalize(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sealed class DelayPromise : IUniTaskSource, IPlayerLoopItem, IPromisePoolItem
|
sealed class DelayFramePromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode<DelayFramePromise>
|
||||||
{
|
{
|
||||||
static readonly PromisePool<DelayPromise> pool = new PromisePool<DelayPromise>();
|
static TaskPool<DelayFramePromise> pool;
|
||||||
|
public DelayFramePromise NextNode { get; set; }
|
||||||
|
|
||||||
|
static DelayFramePromise()
|
||||||
|
{
|
||||||
|
TaskPool.RegisterSizeGetter(typeof(DelayFramePromise), () => pool.Size);
|
||||||
|
}
|
||||||
|
|
||||||
|
int initialFrame;
|
||||||
|
int delayFrameCount;
|
||||||
|
CancellationToken cancellationToken;
|
||||||
|
|
||||||
|
int currentFrameCount;
|
||||||
|
UniTaskCompletionSourceCore<AsyncUnit> core;
|
||||||
|
|
||||||
|
DelayFramePromise()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IUniTaskSource Create(int delayFrameCount, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token)
|
||||||
|
{
|
||||||
|
if (cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!pool.TryPop(out var result))
|
||||||
|
{
|
||||||
|
result = new DelayFramePromise();
|
||||||
|
}
|
||||||
|
|
||||||
|
result.delayFrameCount = delayFrameCount;
|
||||||
|
result.cancellationToken = cancellationToken;
|
||||||
|
result.initialFrame = Time.frameCount;
|
||||||
|
|
||||||
|
TaskTracker.TrackActiveTask(result, 3);
|
||||||
|
|
||||||
|
PlayerLoopHelper.AddAction(timing, result);
|
||||||
|
|
||||||
|
token = result.core.Version;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void GetResult(short token)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
core.GetResult(token);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
TryReturn();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTaskStatus GetStatus(short token)
|
||||||
|
{
|
||||||
|
return core.GetStatus(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTaskStatus UnsafeGetStatus()
|
||||||
|
{
|
||||||
|
return core.UnsafeGetStatus();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnCompleted(Action<object> continuation, object state, short token)
|
||||||
|
{
|
||||||
|
core.OnCompleted(continuation, state, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool MoveNext()
|
||||||
|
{
|
||||||
|
if (cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
core.TrySetCanceled(cancellationToken);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentFrameCount == 0)
|
||||||
|
{
|
||||||
|
if (delayFrameCount == 0) // same as Yield
|
||||||
|
{
|
||||||
|
core.TrySetResult(AsyncUnit.Default);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// skip in initial frame.
|
||||||
|
if (initialFrame == Time.frameCount)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (++currentFrameCount >= delayFrameCount)
|
||||||
|
{
|
||||||
|
core.TrySetResult(AsyncUnit.Default);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool TryReturn()
|
||||||
|
{
|
||||||
|
TaskTracker.RemoveTracking(this);
|
||||||
|
core.Reset();
|
||||||
|
currentFrameCount = default;
|
||||||
|
delayFrameCount = default;
|
||||||
|
cancellationToken = default;
|
||||||
|
return pool.TryPush(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
~DelayFramePromise()
|
||||||
|
{
|
||||||
|
if (TryReturn())
|
||||||
|
{
|
||||||
|
GC.ReRegisterForFinalize(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sealed class DelayPromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode<DelayPromise>
|
||||||
|
{
|
||||||
|
static TaskPool<DelayPromise> pool;
|
||||||
|
public DelayPromise NextNode { get; set; }
|
||||||
|
|
||||||
|
static DelayPromise()
|
||||||
|
{
|
||||||
|
TaskPool.RegisterSizeGetter(typeof(DelayPromise), () => pool.Size);
|
||||||
|
}
|
||||||
|
|
||||||
|
int initialFrame;
|
||||||
float delayFrameTimeSpan;
|
float delayFrameTimeSpan;
|
||||||
float elapsed;
|
float elapsed;
|
||||||
CancellationToken cancellationToken;
|
CancellationToken cancellationToken;
|
||||||
@@ -259,11 +444,15 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
|
return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
|
||||||
}
|
}
|
||||||
|
|
||||||
var result = pool.TryRent() ?? new DelayPromise();
|
if (!pool.TryPop(out var result))
|
||||||
|
{
|
||||||
|
result = new DelayPromise();
|
||||||
|
}
|
||||||
|
|
||||||
result.elapsed = 0.0f;
|
result.elapsed = 0.0f;
|
||||||
result.delayFrameTimeSpan = (float)delayFrameTimeSpan.TotalSeconds;
|
result.delayFrameTimeSpan = (float)delayFrameTimeSpan.TotalSeconds;
|
||||||
result.cancellationToken = cancellationToken;
|
result.cancellationToken = cancellationToken;
|
||||||
|
result.initialFrame = Time.frameCount;
|
||||||
|
|
||||||
TaskTracker.TrackActiveTask(result, 3);
|
TaskTracker.TrackActiveTask(result, 3);
|
||||||
|
|
||||||
@@ -277,12 +466,11 @@ namespace Cysharp.Threading.Tasks
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
TaskTracker.RemoveTracking(this);
|
|
||||||
core.GetResult(token);
|
core.GetResult(token);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
pool.TryReturn(this);
|
TryReturn();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -309,6 +497,14 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (elapsed == 0.0f)
|
||||||
|
{
|
||||||
|
if (initialFrame == Time.frameCount)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
elapsed += Time.deltaTime;
|
elapsed += Time.deltaTime;
|
||||||
if (elapsed >= delayFrameTimeSpan)
|
if (elapsed >= delayFrameTimeSpan)
|
||||||
{
|
{
|
||||||
@@ -319,29 +515,38 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Reset()
|
bool TryReturn()
|
||||||
{
|
{
|
||||||
|
TaskTracker.RemoveTracking(this);
|
||||||
core.Reset();
|
core.Reset();
|
||||||
delayFrameTimeSpan = default;
|
delayFrameTimeSpan = default;
|
||||||
elapsed = default;
|
elapsed = default;
|
||||||
cancellationToken = default;
|
cancellationToken = default;
|
||||||
|
return pool.TryPush(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
~DelayPromise()
|
~DelayPromise()
|
||||||
{
|
{
|
||||||
if (pool.TryReturn(this))
|
if (TryReturn())
|
||||||
{
|
{
|
||||||
GC.ReRegisterForFinalize(this);
|
GC.ReRegisterForFinalize(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sealed class DelayIgnoreTimeScalePromise : IUniTaskSource, IPlayerLoopItem, IPromisePoolItem
|
sealed class DelayIgnoreTimeScalePromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode<DelayIgnoreTimeScalePromise>
|
||||||
{
|
{
|
||||||
static readonly PromisePool<DelayIgnoreTimeScalePromise> pool = new PromisePool<DelayIgnoreTimeScalePromise>();
|
static TaskPool<DelayIgnoreTimeScalePromise> pool;
|
||||||
|
public DelayIgnoreTimeScalePromise NextNode { get; set; }
|
||||||
|
|
||||||
|
static DelayIgnoreTimeScalePromise()
|
||||||
|
{
|
||||||
|
TaskPool.RegisterSizeGetter(typeof(DelayIgnoreTimeScalePromise), () => pool.Size);
|
||||||
|
}
|
||||||
|
|
||||||
float delayFrameTimeSpan;
|
float delayFrameTimeSpan;
|
||||||
float elapsed;
|
float elapsed;
|
||||||
|
int initialFrame;
|
||||||
CancellationToken cancellationToken;
|
CancellationToken cancellationToken;
|
||||||
|
|
||||||
UniTaskCompletionSourceCore<object> core;
|
UniTaskCompletionSourceCore<object> core;
|
||||||
@@ -357,10 +562,14 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
|
return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
|
||||||
}
|
}
|
||||||
|
|
||||||
var result = pool.TryRent() ?? new DelayIgnoreTimeScalePromise();
|
if (!pool.TryPop(out var result))
|
||||||
|
{
|
||||||
|
result = new DelayIgnoreTimeScalePromise();
|
||||||
|
}
|
||||||
|
|
||||||
result.elapsed = 0.0f;
|
result.elapsed = 0.0f;
|
||||||
result.delayFrameTimeSpan = (float)delayFrameTimeSpan.TotalSeconds;
|
result.delayFrameTimeSpan = (float)delayFrameTimeSpan.TotalSeconds;
|
||||||
|
result.initialFrame = Time.frameCount;
|
||||||
result.cancellationToken = cancellationToken;
|
result.cancellationToken = cancellationToken;
|
||||||
|
|
||||||
TaskTracker.TrackActiveTask(result, 3);
|
TaskTracker.TrackActiveTask(result, 3);
|
||||||
@@ -375,12 +584,11 @@ namespace Cysharp.Threading.Tasks
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
TaskTracker.RemoveTracking(this);
|
|
||||||
core.GetResult(token);
|
core.GetResult(token);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
pool.TryReturn(this);
|
TryReturn();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -407,6 +615,14 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (elapsed == 0.0f)
|
||||||
|
{
|
||||||
|
if (initialFrame == Time.frameCount)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
elapsed += Time.unscaledDeltaTime;
|
elapsed += Time.unscaledDeltaTime;
|
||||||
if (elapsed >= delayFrameTimeSpan)
|
if (elapsed >= delayFrameTimeSpan)
|
||||||
{
|
{
|
||||||
@@ -417,17 +633,19 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Reset()
|
bool TryReturn()
|
||||||
{
|
{
|
||||||
|
TaskTracker.RemoveTracking(this);
|
||||||
core.Reset();
|
core.Reset();
|
||||||
delayFrameTimeSpan = default;
|
delayFrameTimeSpan = default;
|
||||||
elapsed = default;
|
elapsed = default;
|
||||||
cancellationToken = default;
|
cancellationToken = default;
|
||||||
|
return pool.TryPush(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
~DelayIgnoreTimeScalePromise()
|
~DelayIgnoreTimeScalePromise()
|
||||||
{
|
{
|
||||||
if (pool.TryReturn(this))
|
if (TryReturn())
|
||||||
{
|
{
|
||||||
GC.ReRegisterForFinalize(this);
|
GC.ReRegisterForFinalize(this);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
||||||
|
|
||||||
|
using Cysharp.Threading.Tasks.Internal;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
|
||||||
namespace Cysharp.Threading.Tasks
|
namespace Cysharp.Threading.Tasks
|
||||||
@@ -102,37 +104,68 @@ namespace Cysharp.Threading.Tasks
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// helper of create add UniTaskVoid to delegate.
|
/// helper of fire and forget void action.
|
||||||
/// For example: FooEvent += () => UniTask.Void(async () => { /* */ })
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static void Void(Func<UniTask> asyncAction)
|
public static void Void(Func<UniTaskVoid> asyncAction)
|
||||||
{
|
{
|
||||||
asyncAction().Forget();
|
asyncAction().Forget();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Action VoidAction(Func<UniTask> asyncAction)
|
/// <summary>
|
||||||
|
/// helper of fire and forget void action.
|
||||||
|
/// </summary>
|
||||||
|
public static void Void(Func<CancellationToken, UniTaskVoid> asyncAction, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
return () => Void(asyncAction);
|
asyncAction(cancellationToken).Forget();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// helper of fire and forget void action.
|
||||||
|
/// </summary>
|
||||||
|
public static void Void<T>(Func<T, UniTaskVoid> asyncAction, T state)
|
||||||
|
{
|
||||||
|
asyncAction(state).Forget();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// helper of create add UniTaskVoid to delegate.
|
||||||
|
/// For example: FooAction = UniTask.Action(async () => { /* */ })
|
||||||
|
/// </summary>
|
||||||
|
public static Action Action(Func<UniTaskVoid> asyncAction)
|
||||||
|
{
|
||||||
|
return () => asyncAction().Forget();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// helper of create add UniTaskVoid to delegate.
|
||||||
|
/// </summary>
|
||||||
|
public static Action Action(Func<CancellationToken, UniTaskVoid> asyncAction, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return () => asyncAction(cancellationToken).Forget();
|
||||||
}
|
}
|
||||||
|
|
||||||
#if UNITY_2018_3_OR_NEWER
|
#if UNITY_2018_3_OR_NEWER
|
||||||
|
|
||||||
public static UnityEngine.Events.UnityAction VoidUnityAction(Func<UniTask> asyncAction)
|
/// <summary>
|
||||||
|
/// Create async void(UniTaskVoid) UnityAction.
|
||||||
|
/// For exampe: onClick.AddListener(UniTask.UnityAction(async () => { /* */ } ))
|
||||||
|
/// </summary>
|
||||||
|
public static UnityEngine.Events.UnityAction UnityAction(Func<UniTaskVoid> asyncAction)
|
||||||
{
|
{
|
||||||
return () => Void(asyncAction);
|
return () => asyncAction().Forget();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create async void(UniTaskVoid) UnityAction.
|
||||||
|
/// For exampe: onClick.AddListener(UniTask.UnityAction(FooAsync, this.GetCancellationTokenOnDestroy()))
|
||||||
|
/// </summary>
|
||||||
|
public static UnityEngine.Events.UnityAction UnityAction(Func<CancellationToken, UniTaskVoid> asyncAction, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return () => asyncAction(cancellationToken).Forget();
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// helper of create add UniTaskVoid to delegate.
|
|
||||||
/// For example: FooEvent += (sender, e) => UniTask.Void(async arg => { /* */ }, (sender, e))
|
|
||||||
/// </summary>
|
|
||||||
public static void Void<T>(Func<T, UniTask> asyncAction, T state)
|
|
||||||
{
|
|
||||||
asyncAction(state).Forget();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Defer the task creation just before call await.
|
/// Defer the task creation just before call await.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -171,7 +204,7 @@ namespace Cysharp.Threading.Tasks
|
|||||||
if (f == null) throw new InvalidOperationException("Can't call twice.");
|
if (f == null) throw new InvalidOperationException("Can't call twice.");
|
||||||
|
|
||||||
task = f();
|
task = f();
|
||||||
awaiter = f().GetAwaiter();
|
awaiter = task.GetAwaiter();
|
||||||
return task.Status;
|
return task.Status;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -213,7 +246,7 @@ namespace Cysharp.Threading.Tasks
|
|||||||
if (f == null) throw new InvalidOperationException("Can't call twice.");
|
if (f == null) throw new InvalidOperationException("Can't call twice.");
|
||||||
|
|
||||||
task = f();
|
task = f();
|
||||||
awaiter = f().GetAwaiter();
|
awaiter = task.GetAwaiter();
|
||||||
return task.Status;
|
return task.Status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,31 @@ namespace Cysharp.Threading.Tasks
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static SwitchToMainThreadAwaitable SwitchToMainThread()
|
public static SwitchToMainThreadAwaitable SwitchToMainThread()
|
||||||
{
|
{
|
||||||
return new SwitchToMainThreadAwaitable();
|
return new SwitchToMainThreadAwaitable(PlayerLoopTiming.Update);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// If running on mainthread, do nothing. Otherwise, same as UniTask.Yield(timing).
|
||||||
|
/// </summary>
|
||||||
|
public static SwitchToMainThreadAwaitable SwitchToMainThread(PlayerLoopTiming timing)
|
||||||
|
{
|
||||||
|
return new SwitchToMainThreadAwaitable(timing);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Return to mainthread(same as await SwitchToMainThread) after using scope is closed.
|
||||||
|
/// </summary>
|
||||||
|
public static ReturnToMainThread ReturnToMainThread()
|
||||||
|
{
|
||||||
|
return new ReturnToMainThread(PlayerLoopTiming.Update);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Return to mainthread(same as await SwitchToMainThread) after using scope is closed.
|
||||||
|
/// </summary>
|
||||||
|
public static ReturnToMainThread ReturnToMainThread(PlayerLoopTiming timing)
|
||||||
|
{
|
||||||
|
return new ReturnToMainThread(timing);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -27,15 +51,28 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return new SwitchToThreadPoolAwaitable();
|
return new SwitchToThreadPoolAwaitable();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Note: use SwitchToThreadPool is recommended.
|
||||||
|
/// </summary>
|
||||||
public static SwitchToTaskPoolAwaitable SwitchToTaskPool()
|
public static SwitchToTaskPoolAwaitable SwitchToTaskPool()
|
||||||
{
|
{
|
||||||
return new SwitchToTaskPoolAwaitable();
|
return new SwitchToTaskPoolAwaitable();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SwitchToSynchronizationContextAwaitable SwitchToSynchronizationContext(SynchronizationContext syncContext)
|
public static SwitchToSynchronizationContextAwaitable SwitchToSynchronizationContext(SynchronizationContext synchronizationContext)
|
||||||
{
|
{
|
||||||
Error.ThrowArgumentNullException(syncContext, nameof(syncContext));
|
Error.ThrowArgumentNullException(synchronizationContext, nameof(synchronizationContext));
|
||||||
return new SwitchToSynchronizationContextAwaitable(syncContext);
|
return new SwitchToSynchronizationContextAwaitable(synchronizationContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ReturnToSynchronizationContext ReturnToSynchronizationContext(SynchronizationContext synchronizationContext)
|
||||||
|
{
|
||||||
|
return new ReturnToSynchronizationContext(synchronizationContext, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ReturnToSynchronizationContext ReturnToCurrentSynchronizationContext(bool dontPostWhenSameContext = true)
|
||||||
|
{
|
||||||
|
return new ReturnToSynchronizationContext(SynchronizationContext.Current, dontPostWhenSameContext);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,10 +80,24 @@ namespace Cysharp.Threading.Tasks
|
|||||||
|
|
||||||
public struct SwitchToMainThreadAwaitable
|
public struct SwitchToMainThreadAwaitable
|
||||||
{
|
{
|
||||||
public Awaiter GetAwaiter() => new Awaiter();
|
readonly PlayerLoopTiming playerLoopTiming;
|
||||||
|
|
||||||
|
public SwitchToMainThreadAwaitable(PlayerLoopTiming playerLoopTiming)
|
||||||
|
{
|
||||||
|
this.playerLoopTiming = playerLoopTiming;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Awaiter GetAwaiter() => new Awaiter(playerLoopTiming);
|
||||||
|
|
||||||
public struct Awaiter : ICriticalNotifyCompletion
|
public struct Awaiter : ICriticalNotifyCompletion
|
||||||
{
|
{
|
||||||
|
readonly PlayerLoopTiming playerLoopTiming;
|
||||||
|
|
||||||
|
public Awaiter(PlayerLoopTiming playerLoopTiming)
|
||||||
|
{
|
||||||
|
this.playerLoopTiming = playerLoopTiming;
|
||||||
|
}
|
||||||
|
|
||||||
public bool IsCompleted
|
public bool IsCompleted
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
@@ -67,12 +118,53 @@ namespace Cysharp.Threading.Tasks
|
|||||||
|
|
||||||
public void OnCompleted(Action continuation)
|
public void OnCompleted(Action continuation)
|
||||||
{
|
{
|
||||||
PlayerLoopHelper.AddContinuation(PlayerLoopTiming.Update, continuation);
|
PlayerLoopHelper.AddContinuation(playerLoopTiming, continuation);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UnsafeOnCompleted(Action continuation)
|
public void UnsafeOnCompleted(Action continuation)
|
||||||
{
|
{
|
||||||
PlayerLoopHelper.AddContinuation(PlayerLoopTiming.Update, continuation);
|
PlayerLoopHelper.AddContinuation(playerLoopTiming, continuation);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct ReturnToMainThread
|
||||||
|
{
|
||||||
|
readonly PlayerLoopTiming playerLoopTiming;
|
||||||
|
|
||||||
|
public ReturnToMainThread(PlayerLoopTiming playerLoopTiming)
|
||||||
|
{
|
||||||
|
this.playerLoopTiming = playerLoopTiming;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Awaiter DisposeAsync()
|
||||||
|
{
|
||||||
|
return new Awaiter(playerLoopTiming); // run immediate.
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly struct Awaiter : ICriticalNotifyCompletion
|
||||||
|
{
|
||||||
|
readonly PlayerLoopTiming timing;
|
||||||
|
|
||||||
|
public Awaiter(PlayerLoopTiming timing)
|
||||||
|
{
|
||||||
|
this.timing = timing;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Awaiter GetAwaiter() => this;
|
||||||
|
|
||||||
|
public bool IsCompleted => PlayerLoopHelper.MainThreadId == System.Threading.Thread.CurrentThread.ManagedThreadId;
|
||||||
|
|
||||||
|
public void GetResult() { }
|
||||||
|
|
||||||
|
public void OnCompleted(Action continuation)
|
||||||
|
{
|
||||||
|
PlayerLoopHelper.AddContinuation(timing, continuation);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UnsafeOnCompleted(Action continuation)
|
||||||
|
{
|
||||||
|
PlayerLoopHelper.AddContinuation(timing, continuation);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -92,12 +184,16 @@ namespace Cysharp.Threading.Tasks
|
|||||||
|
|
||||||
public void OnCompleted(Action continuation)
|
public void OnCompleted(Action continuation)
|
||||||
{
|
{
|
||||||
ThreadPool.UnsafeQueueUserWorkItem(switchToCallback, continuation);
|
ThreadPool.QueueUserWorkItem(switchToCallback, continuation);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UnsafeOnCompleted(Action continuation)
|
public void UnsafeOnCompleted(Action continuation)
|
||||||
{
|
{
|
||||||
|
#if NETCOREAPP3_1
|
||||||
|
ThreadPool.UnsafeQueueUserWorkItem(ThreadPoolWorkItem.Create(continuation), false);
|
||||||
|
#else
|
||||||
ThreadPool.UnsafeQueueUserWorkItem(switchToCallback, continuation);
|
ThreadPool.UnsafeQueueUserWorkItem(switchToCallback, continuation);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static void Callback(object state)
|
static void Callback(object state)
|
||||||
@@ -106,6 +202,47 @@ namespace Cysharp.Threading.Tasks
|
|||||||
continuation();
|
continuation();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if NETCOREAPP3_1
|
||||||
|
|
||||||
|
sealed class ThreadPoolWorkItem : IThreadPoolWorkItem, ITaskPoolNode<ThreadPoolWorkItem>
|
||||||
|
{
|
||||||
|
static TaskPool<ThreadPoolWorkItem> pool;
|
||||||
|
public ThreadPoolWorkItem NextNode { get; set; }
|
||||||
|
|
||||||
|
static ThreadPoolWorkItem()
|
||||||
|
{
|
||||||
|
TaskPool.RegisterSizeGetter(typeof(ThreadPoolWorkItem), () => pool.Size);
|
||||||
|
}
|
||||||
|
|
||||||
|
Action continuation;
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static ThreadPoolWorkItem Create(Action continuation)
|
||||||
|
{
|
||||||
|
if (!pool.TryPop(out var item))
|
||||||
|
{
|
||||||
|
item = new ThreadPoolWorkItem();
|
||||||
|
}
|
||||||
|
|
||||||
|
item.continuation = continuation;
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void Execute()
|
||||||
|
{
|
||||||
|
var call = continuation;
|
||||||
|
continuation = null;
|
||||||
|
if (call != null)
|
||||||
|
{
|
||||||
|
pool.TryPush(this);
|
||||||
|
call.Invoke();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct SwitchToTaskPoolAwaitable
|
public struct SwitchToTaskPoolAwaitable
|
||||||
@@ -178,5 +315,73 @@ namespace Cysharp.Threading.Tasks
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
public struct ReturnToSynchronizationContext
|
||||||
|
{
|
||||||
|
readonly SynchronizationContext syncContext;
|
||||||
|
readonly bool dontPostWhenSameContext;
|
||||||
|
|
||||||
|
public ReturnToSynchronizationContext(SynchronizationContext syncContext, bool dontPostWhenSameContext)
|
||||||
|
{
|
||||||
|
this.syncContext = syncContext;
|
||||||
|
this.dontPostWhenSameContext = dontPostWhenSameContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Awaiter DisposeAsync()
|
||||||
|
{
|
||||||
|
return new Awaiter(syncContext, dontPostWhenSameContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct Awaiter : ICriticalNotifyCompletion
|
||||||
|
{
|
||||||
|
static readonly SendOrPostCallback switchToCallback = Callback;
|
||||||
|
|
||||||
|
readonly SynchronizationContext synchronizationContext;
|
||||||
|
readonly bool dontPostWhenSameContext;
|
||||||
|
|
||||||
|
public Awaiter(SynchronizationContext synchronizationContext, bool dontPostWhenSameContext)
|
||||||
|
{
|
||||||
|
this.synchronizationContext = synchronizationContext;
|
||||||
|
this.dontPostWhenSameContext = dontPostWhenSameContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Awaiter GetAwaiter() => this;
|
||||||
|
|
||||||
|
public bool IsCompleted
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (!dontPostWhenSameContext) return false;
|
||||||
|
|
||||||
|
var current = SynchronizationContext.Current;
|
||||||
|
if (current == synchronizationContext)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void GetResult() { }
|
||||||
|
|
||||||
|
public void OnCompleted(Action continuation)
|
||||||
|
{
|
||||||
|
synchronizationContext.Post(switchToCallback, continuation);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UnsafeOnCompleted(Action continuation)
|
||||||
|
{
|
||||||
|
synchronizationContext.Post(switchToCallback, continuation);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void Callback(object state)
|
||||||
|
{
|
||||||
|
var continuation = (Action)state;
|
||||||
|
continuation();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -28,16 +28,22 @@ namespace Cysharp.Threading.Tasks
|
|||||||
where T : class
|
where T : class
|
||||||
{
|
{
|
||||||
var unityObject = target as UnityEngine.Object;
|
var unityObject = target as UnityEngine.Object;
|
||||||
var isUnityObject = !object.ReferenceEquals(target, null); // don't use (unityObject == null)
|
var isUnityObject = target is UnityEngine.Object; // don't use (unityObject == null)
|
||||||
|
|
||||||
return new UniTask<U>(isUnityObject
|
return new UniTask<U>(isUnityObject
|
||||||
? WaitUntilValueChangedUnityObjectPromise<T, U>.Create(target, monitorFunction, equalityComparer, monitorTiming, cancellationToken, out var token)
|
? WaitUntilValueChangedUnityObjectPromise<T, U>.Create(target, monitorFunction, equalityComparer, monitorTiming, cancellationToken, out var token)
|
||||||
: WaitUntilValueChangedStandardObjectPromise<T, U>.Create(target, monitorFunction, equalityComparer, monitorTiming, cancellationToken, out token), token);
|
: WaitUntilValueChangedStandardObjectPromise<T, U>.Create(target, monitorFunction, equalityComparer, monitorTiming, cancellationToken, out token), token);
|
||||||
}
|
}
|
||||||
|
|
||||||
sealed class WaitUntilPromise : IUniTaskSource, IPlayerLoopItem, IPromisePoolItem
|
sealed class WaitUntilPromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode<WaitUntilPromise>
|
||||||
{
|
{
|
||||||
static readonly PromisePool<WaitUntilPromise> pool = new PromisePool<WaitUntilPromise>();
|
static TaskPool<WaitUntilPromise> pool;
|
||||||
|
public WaitUntilPromise NextNode { get; set; }
|
||||||
|
|
||||||
|
static WaitUntilPromise()
|
||||||
|
{
|
||||||
|
TaskPool.RegisterSizeGetter(typeof(WaitUntilPromise), () => pool.Size);
|
||||||
|
}
|
||||||
|
|
||||||
Func<bool> predicate;
|
Func<bool> predicate;
|
||||||
CancellationToken cancellationToken;
|
CancellationToken cancellationToken;
|
||||||
@@ -55,7 +61,10 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
|
return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
|
||||||
}
|
}
|
||||||
|
|
||||||
var result = pool.TryRent() ?? new WaitUntilPromise();
|
if (!pool.TryPop(out var result))
|
||||||
|
{
|
||||||
|
result = new WaitUntilPromise();
|
||||||
|
}
|
||||||
|
|
||||||
result.predicate = predicate;
|
result.predicate = predicate;
|
||||||
result.cancellationToken = cancellationToken;
|
result.cancellationToken = cancellationToken;
|
||||||
@@ -72,12 +81,11 @@ namespace Cysharp.Threading.Tasks
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
TaskTracker.RemoveTracking(this);
|
|
||||||
core.GetResult(token);
|
core.GetResult(token);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
pool.TryReturn(this);
|
TryReturn();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,25 +129,33 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Reset()
|
bool TryReturn()
|
||||||
{
|
{
|
||||||
|
TaskTracker.RemoveTracking(this);
|
||||||
core.Reset();
|
core.Reset();
|
||||||
predicate = default;
|
predicate = default;
|
||||||
cancellationToken = default;
|
cancellationToken = default;
|
||||||
|
return pool.TryPush(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
~WaitUntilPromise()
|
~WaitUntilPromise()
|
||||||
{
|
{
|
||||||
if (pool.TryReturn(this))
|
if (TryReturn())
|
||||||
{
|
{
|
||||||
GC.ReRegisterForFinalize(this);
|
GC.ReRegisterForFinalize(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sealed class WaitWhilePromise : IUniTaskSource, IPlayerLoopItem, IPromisePoolItem
|
sealed class WaitWhilePromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode<WaitWhilePromise>
|
||||||
{
|
{
|
||||||
static readonly PromisePool<WaitWhilePromise> pool = new PromisePool<WaitWhilePromise>();
|
static TaskPool<WaitWhilePromise> pool;
|
||||||
|
public WaitWhilePromise NextNode { get; set; }
|
||||||
|
|
||||||
|
static WaitWhilePromise()
|
||||||
|
{
|
||||||
|
TaskPool.RegisterSizeGetter(typeof(WaitWhilePromise), () => pool.Size);
|
||||||
|
}
|
||||||
|
|
||||||
Func<bool> predicate;
|
Func<bool> predicate;
|
||||||
CancellationToken cancellationToken;
|
CancellationToken cancellationToken;
|
||||||
@@ -157,7 +173,10 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
|
return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
|
||||||
}
|
}
|
||||||
|
|
||||||
var result = pool.TryRent() ?? new WaitWhilePromise();
|
if (!pool.TryPop(out var result))
|
||||||
|
{
|
||||||
|
result = new WaitWhilePromise();
|
||||||
|
}
|
||||||
|
|
||||||
result.predicate = predicate;
|
result.predicate = predicate;
|
||||||
result.cancellationToken = cancellationToken;
|
result.cancellationToken = cancellationToken;
|
||||||
@@ -174,12 +193,11 @@ namespace Cysharp.Threading.Tasks
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
TaskTracker.RemoveTracking(this);
|
|
||||||
core.GetResult(token);
|
core.GetResult(token);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
pool.TryReturn(this);
|
TryReturn();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -223,25 +241,33 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Reset()
|
bool TryReturn()
|
||||||
{
|
{
|
||||||
|
TaskTracker.RemoveTracking(this);
|
||||||
core.Reset();
|
core.Reset();
|
||||||
predicate = default;
|
predicate = default;
|
||||||
cancellationToken = default;
|
cancellationToken = default;
|
||||||
|
return pool.TryPush(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
~WaitWhilePromise()
|
~WaitWhilePromise()
|
||||||
{
|
{
|
||||||
if (pool.TryReturn(this))
|
if (TryReturn())
|
||||||
{
|
{
|
||||||
GC.ReRegisterForFinalize(this);
|
GC.ReRegisterForFinalize(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sealed class WaitUntilCanceledPromise : IUniTaskSource, IPlayerLoopItem, IPromisePoolItem
|
sealed class WaitUntilCanceledPromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode<WaitUntilCanceledPromise>
|
||||||
{
|
{
|
||||||
static readonly PromisePool<WaitUntilCanceledPromise> pool = new PromisePool<WaitUntilCanceledPromise>();
|
static TaskPool<WaitUntilCanceledPromise> pool;
|
||||||
|
public WaitUntilCanceledPromise NextNode { get; set; }
|
||||||
|
|
||||||
|
static WaitUntilCanceledPromise()
|
||||||
|
{
|
||||||
|
TaskPool.RegisterSizeGetter(typeof(WaitUntilCanceledPromise), () => pool.Size);
|
||||||
|
}
|
||||||
|
|
||||||
CancellationToken cancellationToken;
|
CancellationToken cancellationToken;
|
||||||
|
|
||||||
@@ -258,7 +284,10 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
|
return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
|
||||||
}
|
}
|
||||||
|
|
||||||
var result = pool.TryRent() ?? new WaitUntilCanceledPromise();
|
if (!pool.TryPop(out var result))
|
||||||
|
{
|
||||||
|
result = new WaitUntilCanceledPromise();
|
||||||
|
}
|
||||||
|
|
||||||
result.cancellationToken = cancellationToken;
|
result.cancellationToken = cancellationToken;
|
||||||
|
|
||||||
@@ -274,12 +303,11 @@ namespace Cysharp.Threading.Tasks
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
TaskTracker.RemoveTracking(this);
|
|
||||||
core.GetResult(token);
|
core.GetResult(token);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
pool.TryReturn(this);
|
TryReturn();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -309,15 +337,17 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Reset()
|
bool TryReturn()
|
||||||
{
|
{
|
||||||
|
TaskTracker.RemoveTracking(this);
|
||||||
core.Reset();
|
core.Reset();
|
||||||
cancellationToken = default;
|
cancellationToken = default;
|
||||||
|
return pool.TryPush(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
~WaitUntilCanceledPromise()
|
~WaitUntilCanceledPromise()
|
||||||
{
|
{
|
||||||
if (pool.TryReturn(this))
|
if (TryReturn())
|
||||||
{
|
{
|
||||||
GC.ReRegisterForFinalize(this);
|
GC.ReRegisterForFinalize(this);
|
||||||
}
|
}
|
||||||
@@ -325,11 +355,18 @@ namespace Cysharp.Threading.Tasks
|
|||||||
}
|
}
|
||||||
|
|
||||||
// where T : UnityEngine.Object, can not add constraint
|
// where T : UnityEngine.Object, can not add constraint
|
||||||
sealed class WaitUntilValueChangedUnityObjectPromise<T, U> : IUniTaskSource<U>, IPlayerLoopItem, IPromisePoolItem
|
sealed class WaitUntilValueChangedUnityObjectPromise<T, U> : IUniTaskSource<U>, IPlayerLoopItem, ITaskPoolNode<WaitUntilValueChangedUnityObjectPromise<T, U>>
|
||||||
{
|
{
|
||||||
static readonly PromisePool<WaitUntilValueChangedUnityObjectPromise<T, U>> pool = new PromisePool<WaitUntilValueChangedUnityObjectPromise<T, U>>();
|
static TaskPool<WaitUntilValueChangedUnityObjectPromise<T, U>> pool;
|
||||||
|
public WaitUntilValueChangedUnityObjectPromise<T, U> NextNode { get; set; }
|
||||||
|
|
||||||
|
static WaitUntilValueChangedUnityObjectPromise()
|
||||||
|
{
|
||||||
|
TaskPool.RegisterSizeGetter(typeof(WaitUntilValueChangedUnityObjectPromise<T, U>), () => pool.Size);
|
||||||
|
}
|
||||||
|
|
||||||
T target;
|
T target;
|
||||||
|
UnityEngine.Object targetAsUnityObject;
|
||||||
U currentValue;
|
U currentValue;
|
||||||
Func<T, U> monitorFunction;
|
Func<T, U> monitorFunction;
|
||||||
IEqualityComparer<U> equalityComparer;
|
IEqualityComparer<U> equalityComparer;
|
||||||
@@ -348,9 +385,13 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return AutoResetUniTaskCompletionSource<U>.CreateFromCanceled(cancellationToken, out token);
|
return AutoResetUniTaskCompletionSource<U>.CreateFromCanceled(cancellationToken, out token);
|
||||||
}
|
}
|
||||||
|
|
||||||
var result = pool.TryRent() ?? new WaitUntilValueChangedUnityObjectPromise<T, U>();
|
if (!pool.TryPop(out var result))
|
||||||
|
{
|
||||||
|
result = new WaitUntilValueChangedUnityObjectPromise<T, U>();
|
||||||
|
}
|
||||||
|
|
||||||
result.target = target;
|
result.target = target;
|
||||||
|
result.targetAsUnityObject = target as UnityEngine.Object;
|
||||||
result.monitorFunction = monitorFunction;
|
result.monitorFunction = monitorFunction;
|
||||||
result.currentValue = monitorFunction(target);
|
result.currentValue = monitorFunction(target);
|
||||||
result.equalityComparer = equalityComparer ?? UnityEqualityComparer.GetDefault<U>();
|
result.equalityComparer = equalityComparer ?? UnityEqualityComparer.GetDefault<U>();
|
||||||
@@ -368,12 +409,11 @@ namespace Cysharp.Threading.Tasks
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
TaskTracker.RemoveTracking(this);
|
|
||||||
return core.GetResult(token);
|
return core.GetResult(token);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
pool.TryReturn(this);
|
TryReturn();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -399,7 +439,7 @@ namespace Cysharp.Threading.Tasks
|
|||||||
|
|
||||||
public bool MoveNext()
|
public bool MoveNext()
|
||||||
{
|
{
|
||||||
if (cancellationToken.IsCancellationRequested || target == null) // destroyed = cancel.
|
if (cancellationToken.IsCancellationRequested || targetAsUnityObject == null) // destroyed = cancel.
|
||||||
{
|
{
|
||||||
core.TrySetCanceled(cancellationToken);
|
core.TrySetCanceled(cancellationToken);
|
||||||
return false;
|
return false;
|
||||||
@@ -424,29 +464,37 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Reset()
|
bool TryReturn()
|
||||||
{
|
{
|
||||||
|
TaskTracker.RemoveTracking(this);
|
||||||
core.Reset();
|
core.Reset();
|
||||||
target = default;
|
target = default;
|
||||||
currentValue = default;
|
currentValue = default;
|
||||||
monitorFunction = default;
|
monitorFunction = default;
|
||||||
equalityComparer = default;
|
equalityComparer = default;
|
||||||
cancellationToken = default;
|
cancellationToken = default;
|
||||||
|
return pool.TryPush(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
~WaitUntilValueChangedUnityObjectPromise()
|
~WaitUntilValueChangedUnityObjectPromise()
|
||||||
{
|
{
|
||||||
if (pool.TryReturn(this))
|
if (TryReturn())
|
||||||
{
|
{
|
||||||
GC.ReRegisterForFinalize(this);
|
GC.ReRegisterForFinalize(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sealed class WaitUntilValueChangedStandardObjectPromise<T, U> : IUniTaskSource<U>, IPlayerLoopItem, IPromisePoolItem
|
sealed class WaitUntilValueChangedStandardObjectPromise<T, U> : IUniTaskSource<U>, IPlayerLoopItem, ITaskPoolNode<WaitUntilValueChangedStandardObjectPromise<T, U>>
|
||||||
where T : class
|
where T : class
|
||||||
{
|
{
|
||||||
static readonly PromisePool<WaitUntilValueChangedStandardObjectPromise<T, U>> pool = new PromisePool<WaitUntilValueChangedStandardObjectPromise<T, U>>();
|
static TaskPool<WaitUntilValueChangedStandardObjectPromise<T, U>> pool;
|
||||||
|
public WaitUntilValueChangedStandardObjectPromise<T, U> NextNode { get; set; }
|
||||||
|
|
||||||
|
static WaitUntilValueChangedStandardObjectPromise()
|
||||||
|
{
|
||||||
|
TaskPool.RegisterSizeGetter(typeof(WaitUntilValueChangedStandardObjectPromise<T, U>), () => pool.Size);
|
||||||
|
}
|
||||||
|
|
||||||
WeakReference<T> target;
|
WeakReference<T> target;
|
||||||
U currentValue;
|
U currentValue;
|
||||||
@@ -467,7 +515,10 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return AutoResetUniTaskCompletionSource<U>.CreateFromCanceled(cancellationToken, out token);
|
return AutoResetUniTaskCompletionSource<U>.CreateFromCanceled(cancellationToken, out token);
|
||||||
}
|
}
|
||||||
|
|
||||||
var result = pool.TryRent() ?? new WaitUntilValueChangedStandardObjectPromise<T, U>();
|
if (!pool.TryPop(out var result))
|
||||||
|
{
|
||||||
|
result = new WaitUntilValueChangedStandardObjectPromise<T, U>();
|
||||||
|
}
|
||||||
|
|
||||||
result.target = new WeakReference<T>(target, false); // wrap in WeakReference.
|
result.target = new WeakReference<T>(target, false); // wrap in WeakReference.
|
||||||
result.monitorFunction = monitorFunction;
|
result.monitorFunction = monitorFunction;
|
||||||
@@ -487,12 +538,11 @@ namespace Cysharp.Threading.Tasks
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
TaskTracker.RemoveTracking(this);
|
|
||||||
return core.GetResult(token);
|
return core.GetResult(token);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
pool.TryReturn(this);
|
TryReturn();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -543,19 +593,21 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Reset()
|
bool TryReturn()
|
||||||
{
|
{
|
||||||
|
TaskTracker.RemoveTracking(this);
|
||||||
core.Reset();
|
core.Reset();
|
||||||
target = default;
|
target = default;
|
||||||
currentValue = default;
|
currentValue = default;
|
||||||
monitorFunction = default;
|
monitorFunction = default;
|
||||||
equalityComparer = default;
|
equalityComparer = default;
|
||||||
cancellationToken = default;
|
cancellationToken = default;
|
||||||
|
return pool.TryPush(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
~WaitUntilValueChangedStandardObjectPromise()
|
~WaitUntilValueChangedStandardObjectPromise()
|
||||||
{
|
{
|
||||||
if (pool.TryReturn(this))
|
if (TryReturn())
|
||||||
{
|
{
|
||||||
GC.ReRegisterForFinalize(this);
|
GC.ReRegisterForFinalize(this);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,8 @@
|
|||||||
"name": "UniTask",
|
"name": "UniTask",
|
||||||
"references": [
|
"references": [
|
||||||
"Unity.ResourceManager",
|
"Unity.ResourceManager",
|
||||||
"Unity.TextMeshPro"
|
"Unity.TextMeshPro",
|
||||||
|
"DOTween.Modules"
|
||||||
],
|
],
|
||||||
"includePlatforms": [],
|
"includePlatforms": [],
|
||||||
"excludePlatforms": [],
|
"excludePlatforms": [],
|
||||||
@@ -21,6 +22,11 @@
|
|||||||
"name": "com.unity.textmeshpro",
|
"name": "com.unity.textmeshpro",
|
||||||
"expression": "",
|
"expression": "",
|
||||||
"define": "UNITASK_TEXTMESHPRO_SUPPORT"
|
"define": "UNITASK_TEXTMESHPRO_SUPPORT"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "com.demigiant.dotween",
|
||||||
|
"expression": "",
|
||||||
|
"define": "UNITASK_DOTWEEN_SUPPORT"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"noEngineReferences": false
|
"noEngineReferences": false
|
||||||
|
|||||||
@@ -1,22 +1,22 @@
|
|||||||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
||||||
#pragma warning disable CS0436
|
#pragma warning disable CS0436
|
||||||
|
|
||||||
|
using Cysharp.Threading.Tasks.CompilerServices;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Runtime.ExceptionServices;
|
using System.Runtime.ExceptionServices;
|
||||||
using Cysharp.Threading.Tasks.CompilerServices;
|
using System.Runtime.InteropServices;
|
||||||
using Cysharp.Threading.Tasks.Internal;
|
|
||||||
|
|
||||||
namespace Cysharp.Threading.Tasks
|
namespace Cysharp.Threading.Tasks
|
||||||
{
|
{
|
||||||
internal static class AwaiterActions
|
internal static class AwaiterActions
|
||||||
{
|
{
|
||||||
internal static readonly Action<object> InvokeActionDelegate = InvokeAction;
|
internal static readonly Action<object> InvokeContinuationDelegate = Continuation;
|
||||||
|
|
||||||
[DebuggerHidden]
|
[DebuggerHidden]
|
||||||
static void InvokeAction(object state)
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
static void Continuation(object state)
|
||||||
{
|
{
|
||||||
((Action)state).Invoke();
|
((Action)state).Invoke();
|
||||||
}
|
}
|
||||||
@@ -26,6 +26,7 @@ namespace Cysharp.Threading.Tasks
|
|||||||
/// Lightweight unity specified task-like object.
|
/// Lightweight unity specified task-like object.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[AsyncMethodBuilder(typeof(AsyncUniTaskMethodBuilder))]
|
[AsyncMethodBuilder(typeof(AsyncUniTaskMethodBuilder))]
|
||||||
|
[StructLayout(LayoutKind.Auto)]
|
||||||
public readonly partial struct UniTask
|
public readonly partial struct UniTask
|
||||||
{
|
{
|
||||||
readonly IUniTaskSource source;
|
readonly IUniTaskSource source;
|
||||||
@@ -68,6 +69,24 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return new UniTask<bool>(new IsCanceledSource(source), token);
|
return new UniTask<bool>(new IsCanceledSource(source), token);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if !UNITY_2018_3_OR_NEWER
|
||||||
|
|
||||||
|
public static implicit operator System.Threading.Tasks.ValueTask(in UniTask self)
|
||||||
|
{
|
||||||
|
if (self.source == null)
|
||||||
|
{
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if NETSTANDARD2_0
|
||||||
|
return self.AsValueTask();
|
||||||
|
#else
|
||||||
|
return new System.Threading.Tasks.ValueTask(self.source, self.token);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
if (source == null) return "()";
|
if (source == null) return "()";
|
||||||
@@ -298,7 +317,7 @@ namespace Cysharp.Threading.Tasks
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
task.source.OnCompleted(AwaiterActions.InvokeActionDelegate, continuation, task.token);
|
task.source.OnCompleted(AwaiterActions.InvokeContinuationDelegate, continuation, task.token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -312,7 +331,7 @@ namespace Cysharp.Threading.Tasks
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
task.source.OnCompleted(AwaiterActions.InvokeActionDelegate, continuation, task.token);
|
task.source.OnCompleted(AwaiterActions.InvokeContinuationDelegate, continuation, task.token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -339,6 +358,7 @@ namespace Cysharp.Threading.Tasks
|
|||||||
/// Lightweight unity specified task-like object.
|
/// Lightweight unity specified task-like object.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[AsyncMethodBuilder(typeof(AsyncUniTaskMethodBuilder<>))]
|
[AsyncMethodBuilder(typeof(AsyncUniTaskMethodBuilder<>))]
|
||||||
|
[StructLayout(LayoutKind.Auto)]
|
||||||
public readonly struct UniTask<T>
|
public readonly struct UniTask<T>
|
||||||
{
|
{
|
||||||
readonly IUniTaskSource<T> source;
|
readonly IUniTaskSource<T> source;
|
||||||
@@ -414,6 +434,24 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return self.AsUniTask();
|
return self.AsUniTask();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if !UNITY_2018_3_OR_NEWER
|
||||||
|
|
||||||
|
public static implicit operator System.Threading.Tasks.ValueTask<T>(in UniTask<T> self)
|
||||||
|
{
|
||||||
|
if (self.source == null)
|
||||||
|
{
|
||||||
|
return new System.Threading.Tasks.ValueTask<T>(self.result);
|
||||||
|
}
|
||||||
|
|
||||||
|
#if NETSTANDARD2_0
|
||||||
|
return self.AsValueTask();
|
||||||
|
#else
|
||||||
|
return new System.Threading.Tasks.ValueTask<T>(self.source, self.token);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// returns (bool IsCanceled, T Result) instead of throws OperationCanceledException.
|
/// returns (bool IsCanceled, T Result) instead of throws OperationCanceledException.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -621,7 +659,7 @@ namespace Cysharp.Threading.Tasks
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
s.OnCompleted(AwaiterActions.InvokeActionDelegate, continuation, task.token);
|
s.OnCompleted(AwaiterActions.InvokeContinuationDelegate, continuation, task.token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -636,7 +674,7 @@ namespace Cysharp.Threading.Tasks
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
s.OnCompleted(AwaiterActions.InvokeActionDelegate, continuation, task.token);
|
s.OnCompleted(AwaiterActions.InvokeContinuationDelegate, continuation, task.token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -123,7 +123,14 @@ namespace Cysharp.Threading.Tasks
|
|||||||
{
|
{
|
||||||
// setup result
|
// setup result
|
||||||
this.hasUnhandledError = true;
|
this.hasUnhandledError = true;
|
||||||
this.error = ExceptionDispatchInfo.Capture(error);
|
if (error is OperationCanceledException)
|
||||||
|
{
|
||||||
|
this.error = error;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this.error = ExceptionDispatchInfo.Capture(error);
|
||||||
|
}
|
||||||
|
|
||||||
if (continuation != null || Interlocked.CompareExchange(ref this.continuation, UniTaskCompletionSourceCoreShared.s_sentinel, null) != null)
|
if (continuation != null || Interlocked.CompareExchange(ref this.continuation, UniTaskCompletionSourceCoreShared.s_sentinel, null) != null)
|
||||||
{
|
{
|
||||||
@@ -264,7 +271,7 @@ namespace Cysharp.Threading.Tasks
|
|||||||
{
|
{
|
||||||
if (token != version)
|
if (token != version)
|
||||||
{
|
{
|
||||||
throw new InvalidOperationException("token version is not matched, can not await twice.");
|
throw new InvalidOperationException("Token version is not matched, can not await twice or get Status after await.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -368,9 +375,15 @@ namespace Cysharp.Threading.Tasks
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class AutoResetUniTaskCompletionSource : IUniTaskSource, IPromisePoolItem, IPromise
|
public class AutoResetUniTaskCompletionSource : IUniTaskSource, ITaskPoolNode<AutoResetUniTaskCompletionSource>, IPromise
|
||||||
{
|
{
|
||||||
static readonly PromisePool<AutoResetUniTaskCompletionSource> pool = new PromisePool<AutoResetUniTaskCompletionSource>();
|
static TaskPool<AutoResetUniTaskCompletionSource> pool;
|
||||||
|
public AutoResetUniTaskCompletionSource NextNode { get; set; }
|
||||||
|
|
||||||
|
static AutoResetUniTaskCompletionSource()
|
||||||
|
{
|
||||||
|
TaskPool.RegisterSizeGetter(typeof(AutoResetUniTaskCompletionSource), () => pool.Size);
|
||||||
|
}
|
||||||
|
|
||||||
UniTaskCompletionSourceCore<AsyncUnit> core;
|
UniTaskCompletionSourceCore<AsyncUnit> core;
|
||||||
|
|
||||||
@@ -381,9 +394,12 @@ namespace Cysharp.Threading.Tasks
|
|||||||
[DebuggerHidden]
|
[DebuggerHidden]
|
||||||
public static AutoResetUniTaskCompletionSource Create()
|
public static AutoResetUniTaskCompletionSource Create()
|
||||||
{
|
{
|
||||||
var value = pool.TryRent() ?? new AutoResetUniTaskCompletionSource();
|
if (!pool.TryPop(out var result))
|
||||||
TaskTracker.TrackActiveTask(value, 2);
|
{
|
||||||
return value;
|
result = new AutoResetUniTaskCompletionSource();
|
||||||
|
}
|
||||||
|
TaskTracker.TrackActiveTask(result, 2);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
[DebuggerHidden]
|
[DebuggerHidden]
|
||||||
@@ -445,12 +461,11 @@ namespace Cysharp.Threading.Tasks
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
TaskTracker.RemoveTracking(this);
|
|
||||||
core.GetResult(token);
|
core.GetResult(token);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
pool.TryReturn(this);
|
TryReturn();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -474,14 +489,16 @@ namespace Cysharp.Threading.Tasks
|
|||||||
}
|
}
|
||||||
|
|
||||||
[DebuggerHidden]
|
[DebuggerHidden]
|
||||||
void IPromisePoolItem.Reset()
|
bool TryReturn()
|
||||||
{
|
{
|
||||||
|
TaskTracker.RemoveTracking(this);
|
||||||
core.Reset();
|
core.Reset();
|
||||||
|
return pool.TryPush(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
~AutoResetUniTaskCompletionSource()
|
~AutoResetUniTaskCompletionSource()
|
||||||
{
|
{
|
||||||
if (pool.TryReturn(this))
|
if (TryReturn())
|
||||||
{
|
{
|
||||||
GC.ReRegisterForFinalize(this);
|
GC.ReRegisterForFinalize(this);
|
||||||
return;
|
return;
|
||||||
@@ -584,9 +601,15 @@ namespace Cysharp.Threading.Tasks
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class AutoResetUniTaskCompletionSource<T> : IUniTaskSource<T>, IPromisePoolItem, IPromise<T>
|
public class AutoResetUniTaskCompletionSource<T> : IUniTaskSource<T>, ITaskPoolNode<AutoResetUniTaskCompletionSource<T>>, IPromise<T>
|
||||||
{
|
{
|
||||||
static readonly PromisePool<AutoResetUniTaskCompletionSource<T>> pool = new PromisePool<AutoResetUniTaskCompletionSource<T>>();
|
static TaskPool<AutoResetUniTaskCompletionSource<T>> pool;
|
||||||
|
public AutoResetUniTaskCompletionSource<T> NextNode { get; set; }
|
||||||
|
|
||||||
|
static AutoResetUniTaskCompletionSource()
|
||||||
|
{
|
||||||
|
TaskPool.RegisterSizeGetter(typeof(AutoResetUniTaskCompletionSource<T>), () => pool.Size);
|
||||||
|
}
|
||||||
|
|
||||||
UniTaskCompletionSourceCore<T> core;
|
UniTaskCompletionSourceCore<T> core;
|
||||||
|
|
||||||
@@ -597,7 +620,10 @@ namespace Cysharp.Threading.Tasks
|
|||||||
[DebuggerHidden]
|
[DebuggerHidden]
|
||||||
public static AutoResetUniTaskCompletionSource<T> Create()
|
public static AutoResetUniTaskCompletionSource<T> Create()
|
||||||
{
|
{
|
||||||
var result = pool.TryRent() ?? new AutoResetUniTaskCompletionSource<T>();
|
if (!pool.TryPop(out var result))
|
||||||
|
{
|
||||||
|
result = new AutoResetUniTaskCompletionSource<T>();
|
||||||
|
}
|
||||||
TaskTracker.TrackActiveTask(result, 2);
|
TaskTracker.TrackActiveTask(result, 2);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -661,12 +687,11 @@ namespace Cysharp.Threading.Tasks
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
TaskTracker.RemoveTracking(this);
|
|
||||||
return core.GetResult(token);
|
return core.GetResult(token);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
pool.TryReturn(this);
|
TryReturn();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -695,14 +720,17 @@ namespace Cysharp.Threading.Tasks
|
|||||||
}
|
}
|
||||||
|
|
||||||
[DebuggerHidden]
|
[DebuggerHidden]
|
||||||
void IPromisePoolItem.Reset()
|
bool TryReturn()
|
||||||
{
|
{
|
||||||
|
TaskTracker.RemoveTracking(this);
|
||||||
core.Reset();
|
core.Reset();
|
||||||
|
return pool.TryPush(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
~AutoResetUniTaskCompletionSource()
|
~AutoResetUniTaskCompletionSource()
|
||||||
{
|
{
|
||||||
if (pool.TryReturn(this))
|
if (TryReturn())
|
||||||
{
|
{
|
||||||
GC.ReRegisterForFinalize(this);
|
GC.ReRegisterForFinalize(this);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -559,36 +559,6 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return await continuationFunction();
|
return await continuationFunction();
|
||||||
}
|
}
|
||||||
|
|
||||||
#if UNITY_2018_3_OR_NEWER
|
|
||||||
|
|
||||||
public static async UniTask ConfigureAwait(this Task task, PlayerLoopTiming timing)
|
|
||||||
{
|
|
||||||
await task.ConfigureAwait(false);
|
|
||||||
await UniTask.Yield(timing);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static async UniTask<T> ConfigureAwait<T>(this Task<T> task, PlayerLoopTiming timing)
|
|
||||||
{
|
|
||||||
var v = await task.ConfigureAwait(false);
|
|
||||||
await UniTask.Yield(timing);
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static async UniTask ConfigureAwait(this UniTask task, PlayerLoopTiming timing)
|
|
||||||
{
|
|
||||||
await task;
|
|
||||||
await UniTask.Yield(timing);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static async UniTask<T> ConfigureAwait<T>(this UniTask<T> task, PlayerLoopTiming timing)
|
|
||||||
{
|
|
||||||
var v = await task;
|
|
||||||
await UniTask.Yield(timing);
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
public static async UniTask<T> Unwrap<T>(this UniTask<UniTask<T>> task)
|
public static async UniTask<T> Unwrap<T>(this UniTask<UniTask<T>> task)
|
||||||
{
|
{
|
||||||
return await await task;
|
return await await task;
|
||||||
@@ -601,7 +571,7 @@ namespace Cysharp.Threading.Tasks
|
|||||||
|
|
||||||
#if UNITY_2018_3_OR_NEWER
|
#if UNITY_2018_3_OR_NEWER
|
||||||
|
|
||||||
class ToCoroutineEnumerator : IEnumerator
|
sealed class ToCoroutineEnumerator : IEnumerator
|
||||||
{
|
{
|
||||||
bool completed;
|
bool completed;
|
||||||
UniTask task;
|
UniTask task;
|
||||||
@@ -651,20 +621,19 @@ namespace Cysharp.Threading.Tasks
|
|||||||
|
|
||||||
if (exception != null)
|
if (exception != null)
|
||||||
{
|
{
|
||||||
// throw exception on iterator (main)thread.
|
exception.Throw();
|
||||||
// unfortunately unity test-runner can not handle throw exception on hand-write IEnumerator.MoveNext.
|
return false;
|
||||||
UnityEngine.Debug.LogException(exception.SourceException);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return !completed;
|
return !completed;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Reset()
|
void IEnumerator.Reset()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ToCoroutineEnumerator<T> : IEnumerator
|
sealed class ToCoroutineEnumerator<T> : IEnumerator
|
||||||
{
|
{
|
||||||
bool completed;
|
bool completed;
|
||||||
Action<T> resultHandler = null;
|
Action<T> resultHandler = null;
|
||||||
@@ -722,19 +691,18 @@ namespace Cysharp.Threading.Tasks
|
|||||||
|
|
||||||
if (exception != null)
|
if (exception != null)
|
||||||
{
|
{
|
||||||
// throw exception on iterator (main)thread.
|
exception.Throw();
|
||||||
// unfortunately unity test-runner can not handle throw exception on hand-write IEnumerator.MoveNext.
|
return false;
|
||||||
UnityEngine.Debug.LogException(exception.SourceException);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return !completed;
|
return !completed;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Reset()
|
void IEnumerator.Reset()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ namespace Cysharp.Threading.Tasks
|
|||||||
{
|
{
|
||||||
public static class UniTaskObservableExtensions
|
public static class UniTaskObservableExtensions
|
||||||
{
|
{
|
||||||
public static UniTask<T> ToUniTask<T>(this IObservable<T> source, CancellationToken cancellationToken = default(CancellationToken), bool useFirstValue = false)
|
public static UniTask<T> ToUniTask<T>(this IObservable<T> source, bool useFirstValue = false, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var promise = new UniTaskCompletionSource<T>();
|
var promise = new UniTaskCompletionSource<T>();
|
||||||
var disposable = new SingleAssignmentDisposable();
|
var disposable = new SingleAssignmentDisposable();
|
||||||
|
|||||||
@@ -9,41 +9,11 @@ using Cysharp.Threading.Tasks.CompilerServices;
|
|||||||
namespace Cysharp.Threading.Tasks
|
namespace Cysharp.Threading.Tasks
|
||||||
{
|
{
|
||||||
[AsyncMethodBuilder(typeof(AsyncUniTaskVoidMethodBuilder))]
|
[AsyncMethodBuilder(typeof(AsyncUniTaskVoidMethodBuilder))]
|
||||||
public struct UniTaskVoid
|
public readonly struct UniTaskVoid
|
||||||
{
|
{
|
||||||
public void Forget()
|
public void Forget()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
[DebuggerHidden]
|
|
||||||
public Awaiter GetAwaiter()
|
|
||||||
{
|
|
||||||
return new Awaiter();
|
|
||||||
}
|
|
||||||
|
|
||||||
public struct Awaiter : ICriticalNotifyCompletion
|
|
||||||
{
|
|
||||||
[DebuggerHidden]
|
|
||||||
public bool IsCompleted => true;
|
|
||||||
|
|
||||||
[DebuggerHidden]
|
|
||||||
public void GetResult()
|
|
||||||
{
|
|
||||||
#if UNITY_2018_3_OR_NEWER
|
|
||||||
UnityEngine.Debug.LogWarning("UniTaskVoid can't await, always fire-and-forget. use Forget instead of await.");
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
[DebuggerHidden]
|
|
||||||
public void OnCompleted(Action continuation)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
[DebuggerHidden]
|
|
||||||
public void UnsafeOnCompleted(Action continuation)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,10 +9,11 @@ namespace Cysharp.Threading.Tasks
|
|||||||
{
|
{
|
||||||
public static partial class UnityAsyncExtensions
|
public static partial class UnityAsyncExtensions
|
||||||
{
|
{
|
||||||
public static async UniTask WaitAsync(this JobHandle jobHandle, PlayerLoopTiming waitTiming)
|
public static async UniTask WaitAsync(this JobHandle jobHandle, PlayerLoopTiming waitTiming, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
await UniTask.Yield(waitTiming);
|
await UniTask.Yield(waitTiming);
|
||||||
jobHandle.Complete();
|
jobHandle.Complete();
|
||||||
|
cancellationToken.ThrowIfCancellationRequested(); // call cancel after Complete.
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UniTask.Awaiter GetAwaiter(this JobHandle jobHandle)
|
public static UniTask.Awaiter GetAwaiter(this JobHandle jobHandle)
|
||||||
@@ -29,21 +30,9 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return new UniTask(handler, token).GetAwaiter();
|
return new UniTask(handler, token).GetAwaiter();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UniTask ToUniTask(this JobHandle jobHandle)
|
// can not pass CancellationToken because can't handle JobHandle's Complete and NativeArray.Dispose.
|
||||||
{
|
|
||||||
var handler = JobHandlePromise.Create(jobHandle, out var token);
|
|
||||||
{
|
|
||||||
PlayerLoopHelper.AddAction(PlayerLoopTiming.EarlyUpdate, handler);
|
|
||||||
PlayerLoopHelper.AddAction(PlayerLoopTiming.PreUpdate, handler);
|
|
||||||
PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, handler);
|
|
||||||
PlayerLoopHelper.AddAction(PlayerLoopTiming.PreLateUpdate, handler);
|
|
||||||
PlayerLoopHelper.AddAction(PlayerLoopTiming.PostLateUpdate, handler);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new UniTask(handler, token);
|
public static UniTask ToUniTask(this JobHandle jobHandle, PlayerLoopTiming waitTiming)
|
||||||
}
|
|
||||||
|
|
||||||
public static UniTask ConfigureAwait(this JobHandle jobHandle, PlayerLoopTiming waitTiming)
|
|
||||||
{
|
{
|
||||||
var handler = JobHandlePromise.Create(jobHandle, out var token);
|
var handler = JobHandlePromise.Create(jobHandle, out var token);
|
||||||
{
|
{
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -37,7 +37,7 @@ namespace Cysharp.Threading.Tasks
|
|||||||
<# if(t.returnType == "UnityWebRequest") { #>
|
<# if(t.returnType == "UnityWebRequest") { #>
|
||||||
#if ENABLE_UNITYWEBREQUEST
|
#if ENABLE_UNITYWEBREQUEST
|
||||||
<# } #>
|
<# } #>
|
||||||
#region <#= t.typeName #>
|
#region <#= t.typeName #>
|
||||||
|
|
||||||
public static <#= t.typeName #>Awaiter GetAwaiter(this <#= t.typeName #> asyncOperation)
|
public static <#= t.typeName #>Awaiter GetAwaiter(this <#= t.typeName #> asyncOperation)
|
||||||
{
|
{
|
||||||
@@ -45,18 +45,18 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return new <#= t.typeName #>Awaiter(asyncOperation);
|
return new <#= t.typeName #>Awaiter(asyncOperation);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <#= ToUniTaskReturnType(t.returnType) #> ToUniTask(this <#= t.typeName #> asyncOperation)
|
public static <#= ToUniTaskReturnType(t.returnType) #> WithCancellation(this <#= t.typeName #> asyncOperation, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
||||||
|
if (asyncOperation.isDone) return <#= IsVoid(t) ? "UniTask.CompletedTask" : $"UniTask.FromResult(asyncOperation.{t.returnField})" #>;
|
||||||
return new <#= ToUniTaskReturnType(t.returnType) #>(<#= t.typeName #>ConfiguredSource.Create(asyncOperation, PlayerLoopTiming.Update, null, CancellationToken.None, out var token), token);
|
return new <#= ToUniTaskReturnType(t.returnType) #>(<#= t.typeName #>WithCancellationSource.Create(asyncOperation, cancellationToken, out var token), token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <#= ToUniTaskReturnType(t.returnType) #> ConfigureAwait(this <#= t.typeName #> asyncOperation, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellation = default(CancellationToken))
|
public static <#= ToUniTaskReturnType(t.returnType) #> ToUniTask(this <#= t.typeName #> asyncOperation, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
{
|
{
|
||||||
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
||||||
|
if (asyncOperation.isDone) return <#= IsVoid(t) ? "UniTask.CompletedTask" : $"UniTask.FromResult(asyncOperation.{t.returnField})" #>;
|
||||||
return new <#= ToUniTaskReturnType(t.returnType) #>(<#= t.typeName #>ConfiguredSource.Create(asyncOperation, timing, progress, cancellation, out var token), token);
|
return new <#= ToUniTaskReturnType(t.returnType) #>(<#= t.typeName #>ConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct <#= t.typeName #>Awaiter : ICriticalNotifyCompletion
|
public struct <#= t.typeName #>Awaiter : ICriticalNotifyCompletion
|
||||||
@@ -106,14 +106,153 @@ namespace Cysharp.Threading.Tasks
|
|||||||
public void UnsafeOnCompleted(Action continuation)
|
public void UnsafeOnCompleted(Action continuation)
|
||||||
{
|
{
|
||||||
Error.ThrowWhenContinuationIsAlreadyRegistered(continuationAction);
|
Error.ThrowWhenContinuationIsAlreadyRegistered(continuationAction);
|
||||||
continuationAction = continuation.AsFuncOfT<AsyncOperation>(); // allocate delegate.
|
continuationAction = PooledDelegate<AsyncOperation>.Create(continuation);
|
||||||
asyncOperation.completed += continuationAction;
|
asyncOperation.completed += continuationAction;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class <#= t.typeName #>ConfiguredSource : <#= ToIUniTaskSourceReturnType(t.returnType) #>, IPlayerLoopItem, IPromisePoolItem
|
sealed class <#= t.typeName #>WithCancellationSource : <#= ToIUniTaskSourceReturnType(t.returnType) #>, IPlayerLoopItem, ITaskPoolNode<<#= t.typeName #>WithCancellationSource>
|
||||||
{
|
{
|
||||||
static readonly PromisePool<<#= t.typeName #>ConfiguredSource> pool = new PromisePool<<#= t.typeName #>ConfiguredSource>();
|
static TaskPool<<#= t.typeName #>WithCancellationSource> pool;
|
||||||
|
public <#= t.typeName #>WithCancellationSource NextNode { get; set; }
|
||||||
|
|
||||||
|
static <#= t.typeName #>WithCancellationSource()
|
||||||
|
{
|
||||||
|
TaskPool.RegisterSizeGetter(typeof(<#= t.typeName #>WithCancellationSource), () => pool.Size);
|
||||||
|
}
|
||||||
|
|
||||||
|
readonly Action<AsyncOperation> continuationAction;
|
||||||
|
<#= t.typeName #> asyncOperation;
|
||||||
|
CancellationToken cancellationToken;
|
||||||
|
bool completed;
|
||||||
|
|
||||||
|
UniTaskCompletionSourceCore<<#= IsVoid(t) ? "AsyncUnit" : t.returnType #>> core;
|
||||||
|
|
||||||
|
<#= t.typeName #>WithCancellationSource()
|
||||||
|
{
|
||||||
|
continuationAction = Continuation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <#= ToIUniTaskSourceReturnType(t.returnType) #> Create(<#= t.typeName #> asyncOperation, CancellationToken cancellationToken, out short token)
|
||||||
|
{
|
||||||
|
if (cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
return AutoResetUniTaskCompletionSource<#= IsVoid(t) ? "" : $"<{t.returnType}>" #>.CreateFromCanceled(cancellationToken, out token);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!pool.TryPop(out var result))
|
||||||
|
{
|
||||||
|
result = new <#= t.typeName #>WithCancellationSource();
|
||||||
|
}
|
||||||
|
|
||||||
|
result.asyncOperation = asyncOperation;
|
||||||
|
result.cancellationToken = cancellationToken;
|
||||||
|
result.completed = false;
|
||||||
|
|
||||||
|
TaskTracker.TrackActiveTask(result, 3);
|
||||||
|
|
||||||
|
PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, result);
|
||||||
|
|
||||||
|
asyncOperation.completed += result.continuationAction;
|
||||||
|
|
||||||
|
token = result.core.Version;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Continuation(AsyncOperation _)
|
||||||
|
{
|
||||||
|
asyncOperation.completed -= continuationAction;
|
||||||
|
|
||||||
|
if (completed)
|
||||||
|
{
|
||||||
|
TryReturn();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
completed = true;
|
||||||
|
core.TrySetResult(<#= IsVoid(t) ? "AsyncUnit.Default" : $"asyncOperation.{t.returnField}" #>);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public <#= t.returnType #> GetResult(short token)
|
||||||
|
{
|
||||||
|
<# if (!IsVoid(t)) { #>
|
||||||
|
return core.GetResult(token);
|
||||||
|
<# } else { #>
|
||||||
|
core.GetResult(token);
|
||||||
|
<# } #>
|
||||||
|
}
|
||||||
|
|
||||||
|
<# if (!IsVoid(t)) { #>
|
||||||
|
void IUniTaskSource.GetResult(short token)
|
||||||
|
{
|
||||||
|
GetResult(token);
|
||||||
|
}
|
||||||
|
<# } #>
|
||||||
|
|
||||||
|
public UniTaskStatus GetStatus(short token)
|
||||||
|
{
|
||||||
|
return core.GetStatus(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTaskStatus UnsafeGetStatus()
|
||||||
|
{
|
||||||
|
return core.UnsafeGetStatus();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnCompleted(Action<object> continuation, object state, short token)
|
||||||
|
{
|
||||||
|
core.OnCompleted(continuation, state, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool MoveNext()
|
||||||
|
{
|
||||||
|
if (completed)
|
||||||
|
{
|
||||||
|
TryReturn();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
completed = true;
|
||||||
|
<# if(t.returnType == "UnityWebRequest") { #>
|
||||||
|
asyncOperation.webRequest.Abort();
|
||||||
|
<# } #>
|
||||||
|
core.TrySetCanceled(cancellationToken);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool TryReturn()
|
||||||
|
{
|
||||||
|
TaskTracker.RemoveTracking(this);
|
||||||
|
core.Reset();
|
||||||
|
asyncOperation = default;
|
||||||
|
cancellationToken = default;
|
||||||
|
return pool.TryPush(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
~<#= t.typeName #>WithCancellationSource()
|
||||||
|
{
|
||||||
|
if (TryReturn())
|
||||||
|
{
|
||||||
|
GC.ReRegisterForFinalize(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sealed class <#= t.typeName #>ConfiguredSource : <#= ToIUniTaskSourceReturnType(t.returnType) #>, IPlayerLoopItem, ITaskPoolNode<<#= t.typeName #>ConfiguredSource>
|
||||||
|
{
|
||||||
|
static TaskPool<<#= t.typeName #>ConfiguredSource> pool;
|
||||||
|
public <#= t.typeName #>ConfiguredSource NextNode { get; set; }
|
||||||
|
|
||||||
|
static <#= t.typeName #>ConfiguredSource()
|
||||||
|
{
|
||||||
|
TaskPool.RegisterSizeGetter(typeof(<#= t.typeName #>ConfiguredSource), () => pool.Size);
|
||||||
|
}
|
||||||
|
|
||||||
<#= t.typeName #> asyncOperation;
|
<#= t.typeName #> asyncOperation;
|
||||||
IProgress<float> progress;
|
IProgress<float> progress;
|
||||||
@@ -133,7 +272,10 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return AutoResetUniTaskCompletionSource<#= IsVoid(t) ? "" : $"<{t.returnType}>" #>.CreateFromCanceled(cancellationToken, out token);
|
return AutoResetUniTaskCompletionSource<#= IsVoid(t) ? "" : $"<{t.returnType}>" #>.CreateFromCanceled(cancellationToken, out token);
|
||||||
}
|
}
|
||||||
|
|
||||||
var result = pool.TryRent() ?? new <#= t.typeName #>ConfiguredSource();
|
if (!pool.TryPop(out var result))
|
||||||
|
{
|
||||||
|
result = new <#= t.typeName #>ConfiguredSource();
|
||||||
|
}
|
||||||
|
|
||||||
result.asyncOperation = asyncOperation;
|
result.asyncOperation = asyncOperation;
|
||||||
result.progress = progress;
|
result.progress = progress;
|
||||||
@@ -151,8 +293,6 @@ namespace Cysharp.Threading.Tasks
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
TaskTracker.RemoveTracking(this);
|
|
||||||
|
|
||||||
<# if (!IsVoid(t)) { #>
|
<# if (!IsVoid(t)) { #>
|
||||||
return core.GetResult(token);
|
return core.GetResult(token);
|
||||||
<# } else { #>
|
<# } else { #>
|
||||||
@@ -161,7 +301,7 @@ namespace Cysharp.Threading.Tasks
|
|||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
pool.TryReturn(this);
|
TryReturn();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -191,6 +331,9 @@ namespace Cysharp.Threading.Tasks
|
|||||||
{
|
{
|
||||||
if (cancellationToken.IsCancellationRequested)
|
if (cancellationToken.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
|
<# if(t.returnType == "UnityWebRequest") { #>
|
||||||
|
asyncOperation.webRequest.Abort();
|
||||||
|
<# } #>
|
||||||
core.TrySetCanceled(cancellationToken);
|
core.TrySetCanceled(cancellationToken);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -209,24 +352,26 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Reset()
|
bool TryReturn()
|
||||||
{
|
{
|
||||||
|
TaskTracker.RemoveTracking(this);
|
||||||
core.Reset();
|
core.Reset();
|
||||||
asyncOperation = default;
|
asyncOperation = default;
|
||||||
progress = default;
|
progress = default;
|
||||||
cancellationToken = default;
|
cancellationToken = default;
|
||||||
|
return pool.TryPush(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
~<#= t.typeName #>ConfiguredSource()
|
~<#= t.typeName #>ConfiguredSource()
|
||||||
{
|
{
|
||||||
if (pool.TryReturn(this))
|
if (TryReturn())
|
||||||
{
|
{
|
||||||
GC.ReRegisterForFinalize(this);
|
GC.ReRegisterForFinalize(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# endregion
|
#endregion
|
||||||
<# if(t.returnType == "UnityWebRequest") { #>
|
<# if(t.returnType == "UnityWebRequest") { #>
|
||||||
#endif
|
#endif
|
||||||
<# } #>
|
<# } #>
|
||||||
|
|||||||
@@ -2,8 +2,6 @@
|
|||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using Cysharp.Threading.Tasks.Internal;
|
|
||||||
using Cysharp.Threading.Tasks.Linq;
|
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.Events;
|
using UnityEngine.Events;
|
||||||
using UnityEngine.UI;
|
using UnityEngine.UI;
|
||||||
@@ -27,6 +25,21 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return new UnityEventHandlerAsyncEnumerable(unityEvent, cancellationToken);
|
return new UnityEventHandlerAsyncEnumerable(unityEvent, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static AsyncUnityEventHandler<T> GetAsyncEventHandler<T>(this UnityEvent<T> unityEvent, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return new AsyncUnityEventHandler<T>(unityEvent, cancellationToken, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UniTask<T> OnInvokeAsync<T>(this UnityEvent<T> unityEvent, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return new AsyncUnityEventHandler<T>(unityEvent, cancellationToken, true).OnInvokeAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IUniTaskAsyncEnumerable<T> OnInvokeAsAsyncEnumerable<T>(this UnityEvent<T> unityEvent, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return new UnityEventHandlerAsyncEnumerable<T>(unityEvent, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
public static IAsyncClickEventHandler GetAsyncClickEventHandler(this Button button)
|
public static IAsyncClickEventHandler GetAsyncClickEventHandler(this Button button)
|
||||||
{
|
{
|
||||||
return new AsyncUnityEventHandler(button.onClick, button.GetCancellationTokenOnDestroy(), false);
|
return new AsyncUnityEventHandler(button.onClick, button.GetCancellationTokenOnDestroy(), false);
|
||||||
@@ -207,6 +220,36 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return new UnityEventHandlerAsyncEnumerable<string>(inputField.onEndEdit, cancellationToken);
|
return new UnityEventHandlerAsyncEnumerable<string>(inputField.onEndEdit, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static IAsyncValueChangedEventHandler<string> GetAsyncValueChangedEventHandler(this InputField inputField)
|
||||||
|
{
|
||||||
|
return new AsyncUnityEventHandler<string>(inputField.onValueChanged, inputField.GetCancellationTokenOnDestroy(), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IAsyncValueChangedEventHandler<string> GetAsyncValueChangedEventHandler(this InputField inputField, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return new AsyncUnityEventHandler<string>(inputField.onValueChanged, cancellationToken, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UniTask<string> OnValueChangedAsync(this InputField inputField)
|
||||||
|
{
|
||||||
|
return new AsyncUnityEventHandler<string>(inputField.onValueChanged, inputField.GetCancellationTokenOnDestroy(), true).OnInvokeAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UniTask<string> OnValueChangedAsync(this InputField inputField, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return new AsyncUnityEventHandler<string>(inputField.onValueChanged, cancellationToken, true).OnInvokeAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IUniTaskAsyncEnumerable<string> OnValueChangedAsAsyncEnumerable(this InputField inputField)
|
||||||
|
{
|
||||||
|
return new UnityEventHandlerAsyncEnumerable<string>(inputField.onValueChanged, inputField.GetCancellationTokenOnDestroy());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IUniTaskAsyncEnumerable<string> OnValueChangedAsAsyncEnumerable(this InputField inputField, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return new UnityEventHandlerAsyncEnumerable<string>(inputField.onValueChanged, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
public static IAsyncValueChangedEventHandler<int> GetAsyncValueChangedEventHandler(this Dropdown dropdown)
|
public static IAsyncValueChangedEventHandler<int> GetAsyncValueChangedEventHandler(this Dropdown dropdown)
|
||||||
{
|
{
|
||||||
return new AsyncUnityEventHandler<int>(dropdown.onValueChanged, dropdown.GetCancellationTokenOnDestroy(), false);
|
return new AsyncUnityEventHandler<int>(dropdown.onValueChanged, dropdown.GetCancellationTokenOnDestroy(), false);
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
|
[assembly: InternalsVisibleTo("UniTask.Linq")]
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 8507e97eb606fad4b99c6edf92e19cb8
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
{
|
{
|
||||||
"name": "com.cysharp.unitask",
|
"name": "com.cysharp.unitask",
|
||||||
"displayName": "UniTask",
|
"displayName": "UniTask",
|
||||||
"version": "2.0.7-rc4",
|
"version": "2.0.15",
|
||||||
"unity": "2018.3",
|
"unity": "2018.4",
|
||||||
"description": "Provides an efficient async/await integration to Unity.",
|
"description": "Provides an efficient async/await integration to Unity.",
|
||||||
"keywords": ["async/await", "async", "Task", "UniTask"],
|
"keywords": [ "async/await", "async", "Task", "UniTask" ],
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"category": "Task",
|
"category": "Task",
|
||||||
"dependencies": {}
|
"dependencies": {}
|
||||||
|
|||||||
@@ -21,13 +21,15 @@ public class ExceptionExamples : MonoBehaviour
|
|||||||
|
|
||||||
private void Start()
|
private void Start()
|
||||||
{
|
{
|
||||||
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
|
UnityEngine.Debug.Log("ExceptionScene, LoopType:" + PlayerLoopInfo.CurrentLoopType + ":" + Time.frameCount);
|
||||||
|
|
||||||
ThrowFromAsyncVoid();
|
//TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
|
||||||
_ = ThrowFromTask();
|
|
||||||
_ = ThrowFromUniTask();
|
|
||||||
|
|
||||||
ThrowFromNonAsync();
|
//ThrowFromAsyncVoid();
|
||||||
|
//_ = ThrowFromTask();
|
||||||
|
//_ = ThrowFromUniTask();
|
||||||
|
|
||||||
|
//ThrowFromNonAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
|
private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using Cysharp.Threading.Tasks;
|
using Cysharp.Threading.Tasks;
|
||||||
|
using System.Linq;
|
||||||
using Cysharp.Threading.Tasks.Linq;
|
using Cysharp.Threading.Tasks.Linq;
|
||||||
using Cysharp.Threading.Tasks.Triggers;
|
using Cysharp.Threading.Tasks.Triggers;
|
||||||
using System;
|
using System;
|
||||||
@@ -10,7 +11,13 @@ using System.Threading.Tasks;
|
|||||||
using Unity.Collections;
|
using Unity.Collections;
|
||||||
using Unity.Jobs;
|
using Unity.Jobs;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using UnityEngine.LowLevel;
|
||||||
|
using UnityEngine.Networking;
|
||||||
using UnityEngine.UI;
|
using UnityEngine.UI;
|
||||||
|
using UnityEngine.SceneManagement;
|
||||||
|
|
||||||
|
|
||||||
|
// using DG.Tweening;
|
||||||
|
|
||||||
public struct MyJob : IJob
|
public struct MyJob : IJob
|
||||||
{
|
{
|
||||||
@@ -35,8 +42,33 @@ public enum MyEnum
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public class SimplePresenter
|
||||||
|
{
|
||||||
|
// View
|
||||||
|
public UnityEngine.UI.InputField Input;
|
||||||
|
|
||||||
|
|
||||||
|
// Presenter
|
||||||
|
|
||||||
|
|
||||||
|
public SimplePresenter()
|
||||||
|
{
|
||||||
|
//Input.OnValueChangedAsAsyncEnumerable()
|
||||||
|
// .Queue()
|
||||||
|
// .SelectAwait(async x =>
|
||||||
|
// {
|
||||||
|
// await UniTask.Delay(TimeSpan.FromSeconds(1));
|
||||||
|
// return x;
|
||||||
|
// })
|
||||||
|
// .Select(x=> x.ToUpper())
|
||||||
|
// .BindTo(
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -87,7 +119,7 @@ public class SandboxMain : MonoBehaviour
|
|||||||
{
|
{
|
||||||
public Button okButton;
|
public Button okButton;
|
||||||
public Button cancelButton;
|
public Button cancelButton;
|
||||||
public Text text;
|
|
||||||
|
|
||||||
CancellationTokenSource cts;
|
CancellationTokenSource cts;
|
||||||
|
|
||||||
@@ -110,6 +142,48 @@ public class SandboxMain : MonoBehaviour
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public class Model
|
||||||
|
{
|
||||||
|
// State<int> Hp { get; }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public Model()
|
||||||
|
{
|
||||||
|
// hp = new AsyncReactiveProperty<int>();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//setHp = Hp.GetSetter();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Increment(int value)
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
// setHp(Hp.Value += value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public Text text;
|
||||||
|
public Button button;
|
||||||
|
|
||||||
|
|
||||||
|
void Start2()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
async UniTask RunStandardDelayAsync()
|
async UniTask RunStandardDelayAsync()
|
||||||
{
|
{
|
||||||
UnityEngine.Debug.Log("DEB");
|
UnityEngine.Debug.Log("DEB");
|
||||||
@@ -122,19 +196,20 @@ public class SandboxMain : MonoBehaviour
|
|||||||
async UniTask RunJobAsync()
|
async UniTask RunJobAsync()
|
||||||
{
|
{
|
||||||
var job = new MyJob() { loopCount = 999, inOut = new NativeArray<int>(1, Allocator.TempJob) };
|
var job = new MyJob() { loopCount = 999, inOut = new NativeArray<int>(1, Allocator.TempJob) };
|
||||||
JobHandle.ScheduleBatchedJobs();
|
try
|
||||||
|
{
|
||||||
|
JobHandle.ScheduleBatchedJobs();
|
||||||
|
|
||||||
var scheduled = job.Schedule();
|
var scheduled = job.Schedule();
|
||||||
|
|
||||||
|
UnityEngine.Debug.Log("OK");
|
||||||
|
await scheduled; // .ConfigureAwait(PlayerLoopTiming.Update); // .WaitAsync(PlayerLoopTiming.Update);
|
||||||
|
UnityEngine.Debug.Log("OK2");
|
||||||
|
}
|
||||||
UnityEngine.Debug.Log("OK");
|
finally
|
||||||
await scheduled; // .ConfigureAwait(PlayerLoopTiming.Update); // .WaitAsync(PlayerLoopTiming.Update);
|
{
|
||||||
UnityEngine.Debug.Log("OK2");
|
job.inOut.Dispose();
|
||||||
|
}
|
||||||
job.inOut.Dispose();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -170,38 +245,464 @@ public class SandboxMain : MonoBehaviour
|
|||||||
Debug.Log("Done");
|
Debug.Log("Done");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int MyProperty { get; set; }
|
||||||
|
|
||||||
|
public class MyClass
|
||||||
|
{
|
||||||
|
public int MyProperty { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task Test1()
|
||||||
|
{
|
||||||
|
var r = await TcsAsync("https://bing.com/");
|
||||||
|
Debug.Log("TASKASYNC");
|
||||||
|
}
|
||||||
|
|
||||||
|
async UniTaskVoid Test2()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//var cts = new CancellationTokenSource();
|
||||||
|
//var r = UniAsync("https://bing.com/", cts.Token);
|
||||||
|
//cts.Cancel();
|
||||||
|
//await r;
|
||||||
|
Debug.Log("SendWebRequestDone:" + PlayerLoopInfo.CurrentLoopType);
|
||||||
|
|
||||||
|
|
||||||
|
// var foo = await UnityWebRequest.Get("https://bing.com/").SendWebRequest();
|
||||||
|
// foo.downloadHandler.text;
|
||||||
|
//
|
||||||
|
_ = await UnityWebRequest.Get("https://bing.com/").SendWebRequest().WithCancellation(CancellationToken.None);
|
||||||
|
Debug.Log("SendWebRequestWithCancellationDone:" + PlayerLoopInfo.CurrentLoopType);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
Debug.Log("Canceled");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
IEnumerator Test3(string url)
|
||||||
|
{
|
||||||
|
var req = UnityWebRequest.Get(url).SendWebRequest();
|
||||||
|
yield return req;
|
||||||
|
Debug.Log("COROUTINE");
|
||||||
|
}
|
||||||
|
|
||||||
|
static async Task<UnityWebRequest> TcsAsync(string url)
|
||||||
|
{
|
||||||
|
var req = await UnityWebRequest.Get(url).SendWebRequest();
|
||||||
|
return req;
|
||||||
|
}
|
||||||
|
|
||||||
|
static async UniTask<UnityWebRequest> UniAsync(string url, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var req = await UnityWebRequest.Get(url).SendWebRequest().WithCancellation(cancellationToken);
|
||||||
|
return req;
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task<int> Test()
|
||||||
|
{
|
||||||
|
await Task.Yield();
|
||||||
|
return 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
async UniTask<int> Ex()
|
||||||
|
{
|
||||||
|
await UniTask.Yield();
|
||||||
|
//throw new Exception();
|
||||||
|
await UniTask.Delay(TimeSpan.FromSeconds(15));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
IEnumerator CoroutineRun()
|
||||||
|
{
|
||||||
|
UnityEngine.Debug.Log("Before Coroutine yield return null," + Time.frameCount + ", " + PlayerLoopInfo.CurrentLoopType);
|
||||||
|
yield return null;
|
||||||
|
UnityEngine.Debug.Log("After Coroutine yield return null," + Time.frameCount + ", " + PlayerLoopInfo.CurrentLoopType);
|
||||||
|
}
|
||||||
|
|
||||||
|
IEnumerator CoroutineRun2()
|
||||||
|
{
|
||||||
|
UnityEngine.Debug.Log("Before Coroutine yield return WaitForEndOfFrame," + Time.frameCount);
|
||||||
|
yield return new WaitForEndOfFrame();
|
||||||
|
UnityEngine.Debug.Log("After Coroutine yield return WaitForEndOfFrame," + Time.frameCount + ", " + PlayerLoopInfo.CurrentLoopType);
|
||||||
|
yield return new WaitForEndOfFrame();
|
||||||
|
UnityEngine.Debug.Log("Onemore After Coroutine yield return WaitForEndOfFrame," + Time.frameCount + ", " + PlayerLoopInfo.CurrentLoopType);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async UniTaskVoid AsyncRun()
|
||||||
|
{
|
||||||
|
UnityEngine.Debug.Log("Before async Yield(default)," + Time.frameCount);
|
||||||
|
await UniTask.Yield();
|
||||||
|
UnityEngine.Debug.Log("After async Yield(default)," + Time.frameCount + ", " + PlayerLoopInfo.CurrentLoopType);
|
||||||
|
}
|
||||||
|
|
||||||
|
async UniTaskVoid AsyncLastUpdate()
|
||||||
|
{
|
||||||
|
UnityEngine.Debug.Log("Before async Yield(LastUpdate)," + Time.frameCount);
|
||||||
|
await UniTask.Yield(PlayerLoopTiming.LastUpdate);
|
||||||
|
UnityEngine.Debug.Log("After async Yield(LastUpdate)," + Time.frameCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
async UniTaskVoid AsyncLastLast()
|
||||||
|
{
|
||||||
|
UnityEngine.Debug.Log("Before async Yield(LastPostLateUpdate)," + Time.frameCount);
|
||||||
|
await UniTask.Yield(PlayerLoopTiming.LastPostLateUpdate);
|
||||||
|
UnityEngine.Debug.Log("After async Yield(LastPostLateUpdate)," + Time.frameCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
async UniTaskVoid Yieldding()
|
||||||
|
{
|
||||||
|
await UniTask.Yield(PlayerLoopTiming.PreUpdate);
|
||||||
|
StartCoroutine(CoroutineRun());
|
||||||
|
}
|
||||||
|
|
||||||
|
async UniTaskVoid AsyncFixedUpdate()
|
||||||
|
{
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
await UniTask.WaitForFixedUpdate();
|
||||||
|
Debug.Log("Async:" + Time.frameCount + ", " + PlayerLoopInfo.CurrentLoopType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
IEnumerator CoroutineFixedUpdate()
|
||||||
|
{
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
yield return new WaitForFixedUpdate();
|
||||||
|
Debug.Log("Coroutine:" + Time.frameCount + ", " + PlayerLoopInfo.CurrentLoopType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void FixedUpdate()
|
||||||
|
{
|
||||||
|
// Debug.Log("FixedUpdate:" + Time.frameCount + ", " + PlayerLoopInfo.CurrentLoopType);
|
||||||
|
}
|
||||||
|
|
||||||
|
async UniTaskVoid DelayFrame3_Pre()
|
||||||
|
{
|
||||||
|
await UniTask.Yield(PlayerLoopTiming.PreUpdate);
|
||||||
|
Debug.Log("Before framecount:" + Time.frameCount);
|
||||||
|
await UniTask.DelayFrame(3);
|
||||||
|
Debug.Log("After framecount:" + Time.frameCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
async UniTaskVoid DelayFrame3_Post()
|
||||||
|
{
|
||||||
|
await UniTask.Yield(PlayerLoopTiming.PostLateUpdate);
|
||||||
|
Debug.Log("Before framecount:" + Time.frameCount);
|
||||||
|
await UniTask.DelayFrame(3);
|
||||||
|
Debug.Log("After framecount:" + Time.frameCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
async UniTask TestCoroutine()
|
||||||
|
{
|
||||||
|
await UniTask.Yield();
|
||||||
|
throw new Exception("foobarbaz");
|
||||||
|
}
|
||||||
|
|
||||||
|
async UniTask DelayCheck()
|
||||||
|
{
|
||||||
|
await UniTask.Yield(PlayerLoopTiming.PreUpdate);
|
||||||
|
Debug.Log("before");
|
||||||
|
var t = UniTask.Delay(TimeSpan.FromSeconds(1), ignoreTimeScale: false);
|
||||||
|
|
||||||
|
await t;
|
||||||
|
Debug.Log("after");
|
||||||
|
}
|
||||||
|
|
||||||
|
private async UniTaskVoid ExecuteAsync()
|
||||||
|
{
|
||||||
|
Debug.Log("1");
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.TimerFrame(1).ToArrayAsync();
|
||||||
|
}
|
||||||
|
Debug.Log("------------------");
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.TimerFrame(1).ToArrayAsync();
|
||||||
|
Debug.Log("2");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void Start()
|
void Start()
|
||||||
{
|
{
|
||||||
//var rp = new AsyncReactiveProperty<int>(10);
|
PlayerLoopInfo.Inject();
|
||||||
|
|
||||||
//Running(rp).Forget();
|
//_ = AsyncFixedUpdate();
|
||||||
|
//StartCoroutine(CoroutineFixedUpdate());
|
||||||
|
|
||||||
//await UniTaskAsyncEnumerable.EveryUpdate().Take(10).ForEachAsync((x, i) => rp.Value = i);
|
//StartCoroutine(TestCoroutine().ToCoroutine());
|
||||||
|
|
||||||
//rp.Dispose();
|
// Application.logMessageReceived += Application_logMessageReceived;
|
||||||
|
|
||||||
//var channel = Channel.CreateSingleConsumerUnbounded<int>();
|
|
||||||
//Debug.Log("wait channel");
|
|
||||||
//await channel.Reader.ReadAllAsync(this.GetCancellationTokenOnDestroy()).ForEachAsync(_ => { });
|
|
||||||
|
|
||||||
|
|
||||||
var pubsub = new AsyncMessageBroker<int>();
|
|
||||||
|
|
||||||
pubsub.Subscribe().ForEachAsync(x => Debug.Log("A:" + x)).Forget();
|
|
||||||
pubsub.Subscribe().ForEachAsync(x => Debug.Log("B:" + x)).Forget();
|
|
||||||
|
|
||||||
|
|
||||||
int i = 0;
|
|
||||||
okButton.OnClickAsAsyncEnumerable().ForEachAsync(_ =>
|
|
||||||
|
okButton.onClick.AddListener(UniTask.UnityAction(async () =>
|
||||||
{
|
{
|
||||||
|
_ = ExecuteAsync();
|
||||||
|
|
||||||
Debug.Log("foo");
|
await UniTask.Yield();
|
||||||
pubsub.Publish(i++);
|
|
||||||
|
//await DelayCheck();
|
||||||
|
/*
|
||||||
|
UnityEngine.Debug.Log("click:" + PlayerLoopInfo.CurrentLoopType);
|
||||||
|
StartCoroutine(CoroutineRun());
|
||||||
|
StartCoroutine(CoroutineRun2());
|
||||||
|
_ = AsyncRun();
|
||||||
|
_ = AsyncLastUpdate();
|
||||||
|
_ = AsyncLastLast();
|
||||||
|
*/
|
||||||
|
//await UniTask.Yield();
|
||||||
|
//_ = Test2();
|
||||||
|
// EarlyUpdate.ExecuteMainThreadJobs
|
||||||
|
// _ = Test2();
|
||||||
|
|
||||||
|
//var t = await Resources.LoadAsync<TextAsset>(Application.streamingAssetsPath + "test.txt");
|
||||||
|
//Debug.Log("LoadEnd" + PlayerLoopInfo.CurrentLoopType + ", " + (t != null));
|
||||||
|
//Debug.Log("LoadEnd" + PlayerLoopInfo.CurrentLoopType + ", " + ((TextAsset)t).text);
|
||||||
|
|
||||||
|
|
||||||
}).Forget();
|
//await UniTask.Yield(PlayerLoopTiming.LastUpdate);
|
||||||
|
//UnityEngine.Debug.Log("after update:" + Time.frameCount);
|
||||||
|
////await UniTask.NextFrame();
|
||||||
|
////await UniTask.Yield();
|
||||||
|
////UnityEngine.Debug.Log("after update nextframe:" + Time.frameCount);
|
||||||
|
|
||||||
|
//StartCoroutine(CoroutineRun2());
|
||||||
|
////StartCoroutine(CoroutineRun());
|
||||||
|
//UnityEngine.Debug.Log("FOO?");
|
||||||
|
|
||||||
|
//_ = DelayFrame3_Pre();
|
||||||
|
//await UniTask.Yield();
|
||||||
|
|
||||||
|
}));
|
||||||
|
|
||||||
|
cancelButton.onClick.AddListener(UniTask.UnityAction(async () =>
|
||||||
|
{
|
||||||
|
_ = DelayFrame3_Post();
|
||||||
|
await UniTask.Yield();
|
||||||
|
|
||||||
|
//await UniTask.Yield(PlayerLoopTiming.LastPreUpdate);
|
||||||
|
//UnityEngine.Debug.Log("before update:" + Time.frameCount);
|
||||||
|
//await UniTask.NextFrame();
|
||||||
|
//await UniTask.Yield();
|
||||||
|
//UnityEngine.Debug.Log("before update nextframe:" + Time.frameCount);
|
||||||
|
|
||||||
|
//StartCoroutine(CoroutineRun());
|
||||||
|
|
||||||
|
//UnityEngine.Debug.Log("click:" + PlayerLoopInfo.CurrentLoopType);
|
||||||
|
//_ = Yieldding();
|
||||||
|
|
||||||
|
//var cts = new CancellationTokenSource();
|
||||||
|
|
||||||
|
//UnityEngine.Debug.Log("click:" + PlayerLoopInfo.CurrentLoopType + ":" + Time.frameCount);
|
||||||
|
//var la = SceneManager.LoadSceneAsync("Scenes/ExceptionExamples").WithCancellation(cts.Token);
|
||||||
|
////cts.Cancel();
|
||||||
|
//await la;
|
||||||
|
//UnityEngine.Debug.Log("End LoadSceneAsync" + PlayerLoopInfo.CurrentLoopType + ":" + Time.frameCount);
|
||||||
|
}));
|
||||||
|
|
||||||
|
//return;
|
||||||
|
//await UniTask.SwitchToMainThread();
|
||||||
|
|
||||||
|
//UniTaskAsyncEnumerable.EveryValueChanged(mcc, x => x.MyProperty)
|
||||||
|
// .Do(_ => { }, () => Debug.Log("COMPLETED"))
|
||||||
|
// .ForEachAsync(x =>
|
||||||
|
// {
|
||||||
|
// Debug.Log("VALUE_CHANGED:" + x);
|
||||||
|
// })
|
||||||
|
// .Forget();
|
||||||
|
|
||||||
|
//_ = Test1();
|
||||||
|
//Test2().Forget();
|
||||||
|
//StartCoroutine(Test3("https://bing.com/"));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//bool flip = false;
|
||||||
|
//var rect = cancelButton.GetComponent<RectTransform>();
|
||||||
|
//var cts = new CancellationTokenSource();
|
||||||
|
//var ct = cts.Token;
|
||||||
|
//okButton.onClick.AddListener(UniTask.UnityAction(async () =>
|
||||||
|
//{
|
||||||
|
// await rect.DOMoveX(10f * (flip ? -1 : 1), 3).OnUpdate(() => { Debug.Log("UPDATE YEAH"); }).WithCancellation(ct);
|
||||||
|
// flip = !flip;
|
||||||
|
// // ok.
|
||||||
|
//}));
|
||||||
|
//cancelButton.onClick.AddListener(() =>
|
||||||
|
//{
|
||||||
|
// cts.Cancel();
|
||||||
|
//});
|
||||||
|
|
||||||
|
|
||||||
|
// DG.Tweening.Core.TweenerCore<int>
|
||||||
|
//Debug.Log("GO MOVEX");
|
||||||
|
//await okButton.GetComponent<RectTransform>().DOMoveX(-10.2f, 3).WithCancellation(CancellationToken.None);
|
||||||
|
//Debug.Log("END MOVEX");
|
||||||
|
|
||||||
|
|
||||||
|
//Debug.Log("AGAIN MOVE");
|
||||||
|
//await okButton.GetComponent<RectTransform>().DOMoveY(10.2f, 3).WithCancellation(CancellationToken.None);
|
||||||
|
//Debug.Log("AGAIN END MOVE");
|
||||||
|
|
||||||
|
//Debug.Log(Test().GetType().FullName);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// check stacktrace
|
||||||
|
// await UniTaskAsyncEnumerable.EveryUpdate().Where((x, i) => i % 2 == 0).Select(x => x).DistinctUntilChanged().ForEachAsync(x =>
|
||||||
|
//{
|
||||||
|
// Debug.Log("test");
|
||||||
|
//});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//// DOTween.To(
|
||||||
|
|
||||||
|
//var cts = new CancellationTokenSource();
|
||||||
|
|
||||||
|
////var tween = okButton.GetComponent<RectTransform>().DOLocalMoveX(100, 5.0f);
|
||||||
|
|
||||||
|
//cancelButton.OnClickAsAsyncEnumerable().ForEachAsync(_ =>
|
||||||
|
//{
|
||||||
|
// cts.Cancel();
|
||||||
|
//}).Forget();
|
||||||
|
|
||||||
|
|
||||||
|
//// await tween.ToUniTask(TweenCancelBehaviour.KillAndCancelAwait, cts.Token);
|
||||||
|
|
||||||
|
////tween.SetRecyclable(true);
|
||||||
|
|
||||||
|
//Debug.Log("END");
|
||||||
|
|
||||||
|
//// tween.Play();
|
||||||
|
|
||||||
|
//// DOTween.
|
||||||
|
|
||||||
|
//// DOVirtual.Float(0, 1, 1, x => { }).ToUniTask();
|
||||||
|
|
||||||
|
|
||||||
|
//await foreach (var _ in UniTaskAsyncEnumerable.EveryUpdate())
|
||||||
|
//{
|
||||||
|
// Debug.Log("Update() " + Time.frameCount);
|
||||||
|
//}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//await okButton.OnClickAsAsyncEnumerable().Where((x, i) => i % 2 == 0).ForEachAsync(_ =>
|
||||||
|
//{
|
||||||
|
//});
|
||||||
|
|
||||||
|
|
||||||
|
//okButton.OnClickAsAsyncEnumerable().ForEachAsync(_ =>
|
||||||
|
//{
|
||||||
|
|
||||||
|
|
||||||
|
//foreach (var (type, size) in TaskPool.GetCacheSizeInfo())
|
||||||
|
//{
|
||||||
|
// Debug.Log(type + ":" + size);
|
||||||
|
//}
|
||||||
|
|
||||||
|
|
||||||
|
//}).Forget();
|
||||||
|
|
||||||
|
//CloseAsync(this.GetCancellationTokenOnDestroy()).Forget();
|
||||||
|
|
||||||
|
//okButton.onClick.AddListener(UniTask.UnityAction(async () => await UniTask.Yield()));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//UpdateUniTask().Forget();
|
||||||
|
|
||||||
|
//StartCoroutine(Coroutine());
|
||||||
|
|
||||||
|
//await UniTask.Delay(TimeSpan.FromSeconds(1));
|
||||||
|
|
||||||
|
|
||||||
|
// _ = ReturnToMainThreadTest();
|
||||||
|
|
||||||
|
//GameObject.Destroy(this.gameObject);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Application_logMessageReceived2(string condition, string stackTrace, LogType type)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Application_logMessageReceived1(string condition, string stackTrace, LogType type)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
async UniTaskVoid UpdateUniTask()
|
||||||
|
{
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
await UniTask.Yield();
|
||||||
|
UnityEngine.Debug.Log("UniTaskYield:" + PlayerLoopInfo.CurrentLoopType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async UniTaskVoid ReturnToMainThreadTest()
|
||||||
|
{
|
||||||
|
var d = UniTask.ReturnToCurrentSynchronizationContext();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
UnityEngine.Debug.Log("In MainThread?" + Thread.CurrentThread.ManagedThreadId);
|
||||||
|
UnityEngine.Debug.Log("SyncContext is null?" + (SynchronizationContext.Current == null));
|
||||||
|
await UniTask.SwitchToThreadPool();
|
||||||
|
UnityEngine.Debug.Log("In ThreadPool?" + Thread.CurrentThread.ManagedThreadId);
|
||||||
|
UnityEngine.Debug.Log("SyncContext is null?" + (SynchronizationContext.Current == null));
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
await d.DisposeAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
UnityEngine.Debug.Log("In ThreadPool?" + Thread.CurrentThread.ManagedThreadId);
|
||||||
|
UnityEngine.Debug.Log("SyncContext is null2" + (SynchronizationContext.Current == null));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void Update()
|
||||||
|
{
|
||||||
|
// UnityEngine.Debug.Log("Update:" + PlayerLoopInfo.CurrentLoopType);
|
||||||
|
}
|
||||||
|
|
||||||
|
IEnumerator Coroutine()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
yield return null;
|
||||||
|
//UnityEngine.Debug.Log("Coroutine null:" + PlayerLoopInfo.CurrentLoopType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
UnityEngine.Debug.Log("Coroutine Finally");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async UniTaskVoid CloseAsync(CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
await UniTask.Yield(PlayerLoopTiming.Update, cancellationToken);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async UniTaskVoid Running(CancellationToken ct)
|
async UniTaskVoid Running(CancellationToken ct)
|
||||||
@@ -434,9 +935,9 @@ public class SandboxMain : MonoBehaviour
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ShowPlayerLoop
|
public class PlayerLoopInfo
|
||||||
{
|
{
|
||||||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
|
// [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
|
||||||
static void Init()
|
static void Init()
|
||||||
{
|
{
|
||||||
var playerLoop = UnityEngine.LowLevel.PlayerLoop.GetDefaultPlayerLoop();
|
var playerLoop = UnityEngine.LowLevel.PlayerLoop.GetDefaultPlayerLoop();
|
||||||
@@ -465,4 +966,45 @@ public class ShowPlayerLoop
|
|||||||
|
|
||||||
UnityEngine.Debug.Log(sb.ToString());
|
UnityEngine.Debug.Log(sb.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Type CurrentLoopType { get; private set; }
|
||||||
|
|
||||||
|
public static void Inject()
|
||||||
|
{
|
||||||
|
var system = PlayerLoop.GetCurrentPlayerLoop();
|
||||||
|
|
||||||
|
for (int i = 0; i < system.subSystemList.Length; i++)
|
||||||
|
{
|
||||||
|
var loop = system.subSystemList[i].subSystemList.SelectMany(x =>
|
||||||
|
{
|
||||||
|
var t = typeof(WrapLoop<>).MakeGenericType(x.type);
|
||||||
|
var instance = (ILoopRunner)Activator.CreateInstance(t, x.type);
|
||||||
|
return new[] { new PlayerLoopSystem { type = t, updateDelegate = instance.Run }, x };
|
||||||
|
}).ToArray();
|
||||||
|
|
||||||
|
system.subSystemList[i].subSystemList = loop;
|
||||||
|
}
|
||||||
|
|
||||||
|
PlayerLoop.SetPlayerLoop(system);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ILoopRunner
|
||||||
|
{
|
||||||
|
void Run();
|
||||||
|
}
|
||||||
|
|
||||||
|
class WrapLoop<T> : ILoopRunner
|
||||||
|
{
|
||||||
|
readonly Type type;
|
||||||
|
|
||||||
|
public WrapLoop(Type type)
|
||||||
|
{
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Run()
|
||||||
|
{
|
||||||
|
CurrentLoopType = type;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -124,18 +124,17 @@ namespace Cysharp.Threading.TasksTests
|
|||||||
public IEnumerator BothEnumeratorCheck() => UniTask.ToCoroutine(async () =>
|
public IEnumerator BothEnumeratorCheck() => UniTask.ToCoroutine(async () =>
|
||||||
{
|
{
|
||||||
await ToaruCoroutineEnumerator(); // wait 5 frame:)
|
await ToaruCoroutineEnumerator(); // wait 5 frame:)
|
||||||
await ToaruCoroutineEnumerator().ConfigureAwait(PlayerLoopTiming.PostLateUpdate);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
[UnityTest]
|
//[UnityTest]
|
||||||
public IEnumerator JobSystem() => UniTask.ToCoroutine(async () =>
|
//public IEnumerator JobSystem() => UniTask.ToCoroutine(async () =>
|
||||||
{
|
//{
|
||||||
var job = new MyJob() { loopCount = 999, inOut = new NativeArray<int>(1, Allocator.TempJob) };
|
// var job = new MyJob() { loopCount = 999, inOut = new NativeArray<int>(1, Allocator.TempJob) };
|
||||||
JobHandle.ScheduleBatchedJobs();
|
// JobHandle.ScheduleBatchedJobs();
|
||||||
await job.Schedule();
|
// await job.Schedule();
|
||||||
job.inOut[0].Should().Be(999);
|
// job.inOut[0].Should().Be(999);
|
||||||
job.inOut.Dispose();
|
// job.inOut.Dispose();
|
||||||
});
|
//});
|
||||||
|
|
||||||
class MyMyClass
|
class MyMyClass
|
||||||
{
|
{
|
||||||
@@ -198,7 +197,7 @@ namespace Cysharp.Threading.TasksTests
|
|||||||
//await UniTask.SwitchToThreadPool();
|
//await UniTask.SwitchToThreadPool();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -270,7 +269,8 @@ namespace Cysharp.Threading.TasksTests
|
|||||||
var first = Time.frameCount;
|
var first = Time.frameCount;
|
||||||
var canceled = await UniTask.DelayFrame(100, cancellationToken: cts.Token).SuppressCancellationThrow();
|
var canceled = await UniTask.DelayFrame(100, cancellationToken: cts.Token).SuppressCancellationThrow();
|
||||||
|
|
||||||
(Time.frameCount - first).Should().Be(11); // 10 frame canceled
|
var r = (Time.frameCount - first);
|
||||||
|
(9 < r && r < 11).Should().BeTrue();
|
||||||
canceled.Should().Be(true);
|
canceled.Should().Be(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -370,6 +370,24 @@ namespace Cysharp.Threading.TasksTests
|
|||||||
throw new Exception("MyException");
|
throw new Exception("MyException");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[UnityTest]
|
||||||
|
public IEnumerator NextFrame1() => UniTask.ToCoroutine(async () =>
|
||||||
|
{
|
||||||
|
await UniTask.Yield(PlayerLoopTiming.LastUpdate);
|
||||||
|
var frame = Time.frameCount;
|
||||||
|
await UniTask.NextFrame();
|
||||||
|
Time.frameCount.Should().Be(frame + 1);
|
||||||
|
});
|
||||||
|
|
||||||
|
[UnityTest]
|
||||||
|
public IEnumerator NextFrame2() => UniTask.ToCoroutine(async () =>
|
||||||
|
{
|
||||||
|
await UniTask.Yield(PlayerLoopTiming.PreUpdate);
|
||||||
|
var frame = Time.frameCount;
|
||||||
|
await UniTask.NextFrame();
|
||||||
|
Time.frameCount.Should().Be(frame + 1);
|
||||||
|
});
|
||||||
|
|
||||||
[UnityTest]
|
[UnityTest]
|
||||||
public IEnumerator NestedEnumerator() => UniTask.ToCoroutine(async () =>
|
public IEnumerator NestedEnumerator() => UniTask.ToCoroutine(async () =>
|
||||||
{
|
{
|
||||||
|
|||||||
97
src/UniTask/Assets/Tests/CachelikeTest.cs
Normal file
97
src/UniTask/Assets/Tests/CachelikeTest.cs
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
using Cysharp.Threading.Tasks;
|
||||||
|
using Cysharp.Threading.Tasks.Linq;
|
||||||
|
using FluentAssertions;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using UnityEngine;
|
||||||
|
using UnityEngine.TestTools;
|
||||||
|
|
||||||
|
namespace Cysharp.Threading.TasksTests
|
||||||
|
{
|
||||||
|
public class Cachelike
|
||||||
|
{
|
||||||
|
[UnityTest]
|
||||||
|
public IEnumerator Check() => UniTask.ToCoroutine(async () =>
|
||||||
|
{
|
||||||
|
{
|
||||||
|
var v = await CachedCheck("foo", 10);
|
||||||
|
v.Should().Be(10);
|
||||||
|
|
||||||
|
var v2 = await CachedCheck("bar", 20);
|
||||||
|
v2.Should().Be(20);
|
||||||
|
|
||||||
|
var v3 = await CachedCheck("baz", 30);
|
||||||
|
v3.Should().Be(30);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var v = await CachedCheck("foo", 10);
|
||||||
|
v.Should().Be(10);
|
||||||
|
|
||||||
|
var v2 = await CachedCheck("bar", 20);
|
||||||
|
v2.Should().Be(20);
|
||||||
|
|
||||||
|
var v3 = await CachedCheck("baz", 30);
|
||||||
|
v3.Should().Be(30);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var v = CachedCheck("foo", 10);
|
||||||
|
var v2 = CachedCheck("bar", 20);
|
||||||
|
var v3 = CachedCheck("baz", 30);
|
||||||
|
|
||||||
|
(await v).Should().Be(10);
|
||||||
|
(await v2).Should().Be(20);
|
||||||
|
(await v3).Should().Be(30);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var v = CachedCheck("foo", 10, true);
|
||||||
|
var v2 = CachedCheck("bar", 20, true);
|
||||||
|
var v3 = CachedCheck("baz", 30, true);
|
||||||
|
|
||||||
|
(await v).Should().Be(10);
|
||||||
|
(await v2).Should().Be(20);
|
||||||
|
(await v3).Should().Be(30);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
static Dictionary<string, int> cacheDict = new Dictionary<string, int>();
|
||||||
|
|
||||||
|
async UniTask<int> CachedCheck(string cache, int value, bool yield = false)
|
||||||
|
{
|
||||||
|
if (!cacheDict.ContainsKey(cache))
|
||||||
|
{
|
||||||
|
await UniTask.Yield();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (yield)
|
||||||
|
{
|
||||||
|
await UniTask.Yield();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cacheDict.TryGetValue(cache, out var v))
|
||||||
|
{
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
cacheDict.Add(cache, value);
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
11
src/UniTask/Assets/Tests/CachelikeTest.cs.meta
Normal file
11
src/UniTask/Assets/Tests/CachelikeTest.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 416e0e77b4408b0498792eb218ed2870
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
394
src/UniTask/Assets/Tests/DelayTest.cs
Normal file
394
src/UniTask/Assets/Tests/DelayTest.cs
Normal file
@@ -0,0 +1,394 @@
|
|||||||
|
using Cysharp.Threading.Tasks;
|
||||||
|
using Cysharp.Threading.Tasks.Linq;
|
||||||
|
using FluentAssertions;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using UnityEngine;
|
||||||
|
using UnityEngine.TestTools;
|
||||||
|
|
||||||
|
namespace Cysharp.Threading.TasksTests
|
||||||
|
{
|
||||||
|
public class DelayTest
|
||||||
|
{
|
||||||
|
//[UnityTest]
|
||||||
|
//public IEnumerator DelayFrame() => UniTask.ToCoroutine(async () =>
|
||||||
|
//{
|
||||||
|
// for (int i = 1; i < 5; i++)
|
||||||
|
// {
|
||||||
|
// await UniTask.Yield(PlayerLoopTiming.PreUpdate);
|
||||||
|
// var frameCount = Time.frameCount;
|
||||||
|
// await UniTask.DelayFrame(i);
|
||||||
|
// Time.frameCount.Should().Be(frameCount + i);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// for (int i = 1; i < 5; i++)
|
||||||
|
// {
|
||||||
|
// await UniTask.Yield(PlayerLoopTiming.PostLateUpdate);
|
||||||
|
// var frameCount = Time.frameCount;
|
||||||
|
// await UniTask.DelayFrame(i);
|
||||||
|
// Time.frameCount.Should().Be(frameCount + i);
|
||||||
|
// }
|
||||||
|
//});
|
||||||
|
|
||||||
|
//[UnityTest]
|
||||||
|
//public IEnumerator DelayFrameZero() => UniTask.ToCoroutine(async () =>
|
||||||
|
//{
|
||||||
|
// {
|
||||||
|
// await UniTask.Yield(PlayerLoopTiming.PreUpdate);
|
||||||
|
// var frameCount = Time.frameCount;
|
||||||
|
// await UniTask.DelayFrame(0);
|
||||||
|
// Time.frameCount.Should().Be(frameCount); // same frame
|
||||||
|
// }
|
||||||
|
// {
|
||||||
|
// await UniTask.Yield(PlayerLoopTiming.PostLateUpdate);
|
||||||
|
// var frameCount = Time.frameCount;
|
||||||
|
// await UniTask.DelayFrame(0);
|
||||||
|
// Time.frameCount.Should().Be(frameCount + 1); // next frame
|
||||||
|
// }
|
||||||
|
//});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//[UnityTest]
|
||||||
|
//public IEnumerator TimerFramePre() => UniTask.ToCoroutine(async () =>
|
||||||
|
//{
|
||||||
|
// await UniTask.Yield(PlayerLoopTiming.PreUpdate);
|
||||||
|
|
||||||
|
// var initialFrame = Time.frameCount;
|
||||||
|
// var xs = await UniTaskAsyncEnumerable.TimerFrame(2, 3).Take(5).Select(_ => Time.frameCount).ToArrayAsync();
|
||||||
|
|
||||||
|
// xs[0].Should().Be(initialFrame + 2);
|
||||||
|
// xs[1].Should().Be(initialFrame + 2 + (3 * 1));
|
||||||
|
// xs[2].Should().Be(initialFrame + 2 + (3 * 2));
|
||||||
|
// xs[3].Should().Be(initialFrame + 2 + (3 * 3));
|
||||||
|
// xs[4].Should().Be(initialFrame + 2 + (3 * 4));
|
||||||
|
//});
|
||||||
|
|
||||||
|
|
||||||
|
//[UnityTest]
|
||||||
|
//public IEnumerator TimerFramePost() => UniTask.ToCoroutine(async () =>
|
||||||
|
//{
|
||||||
|
// await UniTask.Yield(PlayerLoopTiming.PostLateUpdate);
|
||||||
|
|
||||||
|
// var initialFrame = Time.frameCount;
|
||||||
|
// var xs = await UniTaskAsyncEnumerable.TimerFrame(2, 3).Take(5).Select(_ => Time.frameCount).ToArrayAsync();
|
||||||
|
|
||||||
|
// xs[0].Should().Be(initialFrame + 2);
|
||||||
|
// xs[1].Should().Be(initialFrame + 2 + (3 * 1));
|
||||||
|
// xs[2].Should().Be(initialFrame + 2 + (3 * 2));
|
||||||
|
// xs[3].Should().Be(initialFrame + 2 + (3 * 3));
|
||||||
|
// xs[4].Should().Be(initialFrame + 2 + (3 * 4));
|
||||||
|
//});
|
||||||
|
|
||||||
|
|
||||||
|
//[UnityTest]
|
||||||
|
//public IEnumerator TimerFrameTest() => UniTask.ToCoroutine(async () =>
|
||||||
|
//{
|
||||||
|
// await UniTask.Yield(PlayerLoopTiming.PreUpdate);
|
||||||
|
|
||||||
|
// var initialFrame = Time.frameCount;
|
||||||
|
// var xs = await UniTaskAsyncEnumerable.TimerFrame(0, 0).Take(5).Select(_ => Time.frameCount).ToArrayAsync();
|
||||||
|
|
||||||
|
// xs[0].Should().Be(initialFrame);
|
||||||
|
// xs[1].Should().Be(initialFrame + 1);
|
||||||
|
// xs[2].Should().Be(initialFrame + 2);
|
||||||
|
// xs[3].Should().Be(initialFrame + 3);
|
||||||
|
// xs[4].Should().Be(initialFrame + 4);
|
||||||
|
//});
|
||||||
|
|
||||||
|
[UnityTest]
|
||||||
|
public IEnumerator TimerFrameSinglePre2() => UniTask.ToCoroutine(async () =>
|
||||||
|
{
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.TimerFrame(1).ToArrayAsync();
|
||||||
|
}
|
||||||
|
//Debug.Log("------------------");
|
||||||
|
//{
|
||||||
|
// var xs = await UniTaskAsyncEnumerable.TimerFrame(1).ToArrayAsync();
|
||||||
|
//}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
//[UnityTest]
|
||||||
|
//public IEnumerator TimerFrameSinglePre2() => UniTask.ToCoroutine(async () =>
|
||||||
|
//{
|
||||||
|
// {
|
||||||
|
// var initialFrame = Time.frameCount;
|
||||||
|
// var xs = await new MyTimerFrame(0, null)/*.Select(_ => Time.frameCount)*/.ToArrayAsync();
|
||||||
|
// Debug.Log("OK 0 ------------------");
|
||||||
|
// }
|
||||||
|
// {
|
||||||
|
// var xs = await new MyTimerFrame(1, null)/*.Select(_ =>
|
||||||
|
// {
|
||||||
|
// var t = Time.frameCount;
|
||||||
|
// UnityEngine.Debug.Log("store frameCount:" + t);
|
||||||
|
// return t;
|
||||||
|
// })*/.ToArrayAsync();
|
||||||
|
// }
|
||||||
|
//});
|
||||||
|
|
||||||
|
//[UnityTest]
|
||||||
|
//public IEnumerator TimerFrameSinglePre() => UniTask.ToCoroutine(async () =>
|
||||||
|
//{
|
||||||
|
// {
|
||||||
|
// await UniTask.Yield(PlayerLoopTiming.PreUpdate);
|
||||||
|
// var initialFrame = Time.frameCount;
|
||||||
|
// var xs = await UniTaskAsyncEnumerable.Return(UniTask.Yield(PlayerLoopTiming.Update, CancellationToken.None))/*.Select(_ => Time.frameCount)*/.ToArrayAsync();
|
||||||
|
// xs[0].Should().Be(initialFrame);
|
||||||
|
// Debug.Log("OK 0 ------------------");
|
||||||
|
// }
|
||||||
|
// {
|
||||||
|
// await UniTask.Yield(PlayerLoopTiming.PreUpdate);
|
||||||
|
// var initialFrame = Time.frameCount;
|
||||||
|
// Debug.Log("initialFrame:" + initialFrame);
|
||||||
|
// var xs = await UniTaskAsyncEnumerable.Return(UniTask.Yield(PlayerLoopTiming.Update, CancellationToken.None))/*.Select(_ =>
|
||||||
|
// {
|
||||||
|
// var t = Time.frameCount;
|
||||||
|
// UnityEngine.Debug.Log("store frameCount:" + t);
|
||||||
|
// return t;
|
||||||
|
// })*/.ToArrayAsync();
|
||||||
|
// Debug.Log("xs len:" + xs.Length);
|
||||||
|
// Debug.Log("xs[0]:" + xs[0]);
|
||||||
|
|
||||||
|
// xs[0].Should().Be(initialFrame + 1);
|
||||||
|
// Debug.Log("OK 1");
|
||||||
|
// }
|
||||||
|
// {
|
||||||
|
// //await UniTask.Yield(PlayerLoopTiming.PreUpdate);
|
||||||
|
// var initialFrame = Time.frameCount;
|
||||||
|
// var xs = await UniTaskAsyncEnumerable.TimerFrame(2).Select(_ => Time.frameCount).ToArrayAsync();
|
||||||
|
// xs[0].Should().Be(initialFrame + 2);
|
||||||
|
// Debug.Log("OK 2");
|
||||||
|
// }
|
||||||
|
//});
|
||||||
|
|
||||||
|
|
||||||
|
//[UnityTest]
|
||||||
|
//public IEnumerator TimerFrameSinglePost() => UniTask.ToCoroutine(async () =>
|
||||||
|
//{
|
||||||
|
// {
|
||||||
|
// //await UniTask.Yield(PlayerLoopTiming.PostLateUpdate);
|
||||||
|
// //var initialFrame = Time.frameCount;
|
||||||
|
// //var xs = await UniTaskAsyncEnumerable.TimerFrame(0).Select(_ => Time.frameCount).ToArrayAsync();
|
||||||
|
// //xs[0].Should().Be(initialFrame);
|
||||||
|
// }
|
||||||
|
// {
|
||||||
|
// //await UniTask.Yield(PlayerLoopTiming.PostLateUpdate);
|
||||||
|
// var initialFrame = Time.frameCount;
|
||||||
|
// var xs = await UniTaskAsyncEnumerable.TimerFrame(1).Select(_ => Time.frameCount).ToArrayAsync();
|
||||||
|
// xs[0].Should().Be(initialFrame + 1);
|
||||||
|
// }
|
||||||
|
// {
|
||||||
|
// //await UniTask.Yield(PlayerLoopTiming.PostLateUpdate);
|
||||||
|
// var initialFrame = Time.frameCount;
|
||||||
|
// var xs = await UniTaskAsyncEnumerable.TimerFrame(2).Select(_ => Time.frameCount).ToArrayAsync();
|
||||||
|
// xs[0].Should().Be(initialFrame + 2);
|
||||||
|
// }
|
||||||
|
//});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//[UnityTest]
|
||||||
|
//public IEnumerator Timer() => UniTask.ToCoroutine(async () =>
|
||||||
|
//{
|
||||||
|
// await UniTask.Yield(PlayerLoopTiming.PreUpdate);
|
||||||
|
|
||||||
|
// {
|
||||||
|
// var initialSeconds = Time.realtimeSinceStartup;
|
||||||
|
// var xs = await UniTaskAsyncEnumerable.Timer(TimeSpan.FromSeconds(2)).Select(_ => Time.realtimeSinceStartup).ToArrayAsync();
|
||||||
|
|
||||||
|
// Mathf.Approximately(initialSeconds, xs[0]).Should().BeFalse();
|
||||||
|
// Debug.Log("Init:" + initialSeconds);
|
||||||
|
// Debug.Log("After:" + xs[0]);
|
||||||
|
// }
|
||||||
|
//});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public class DelayTest2
|
||||||
|
{
|
||||||
|
[UnityTest]
|
||||||
|
public IEnumerator TimerFrameSinglePre2() => UniTask.ToCoroutine(async () =>
|
||||||
|
{
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.TimerFrame(1).ToArrayAsync();
|
||||||
|
}
|
||||||
|
Debug.Log("------------------");
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.TimerFrame(1).ToArrayAsync();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public class ThreadRunner
|
||||||
|
{
|
||||||
|
Thread thread;
|
||||||
|
|
||||||
|
public void Start(IPlayerLoopItem runner)
|
||||||
|
{
|
||||||
|
thread = new Thread(() =>
|
||||||
|
{
|
||||||
|
Thread.Sleep(30);
|
||||||
|
while (runner.MoveNext())
|
||||||
|
{
|
||||||
|
Thread.Sleep(30);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
thread.Start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal class MyTimerFrame : IUniTaskAsyncEnumerable<AsyncUnit>
|
||||||
|
{
|
||||||
|
//readonly PlayerLoopTiming updateTiming;
|
||||||
|
readonly int dueTimeFrameCount;
|
||||||
|
readonly int? periodFrameCount;
|
||||||
|
|
||||||
|
public MyTimerFrame(int dueTimeFrameCount, int? periodFrameCount)
|
||||||
|
{
|
||||||
|
//this.updateTiming = updateTiming;
|
||||||
|
this.dueTimeFrameCount = dueTimeFrameCount;
|
||||||
|
this.periodFrameCount = periodFrameCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IUniTaskAsyncEnumerator<AsyncUnit> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
return new _TimerFrame(dueTimeFrameCount, periodFrameCount, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
class _TimerFrame : MoveNextSource, IUniTaskAsyncEnumerator<AsyncUnit>, IPlayerLoopItem
|
||||||
|
{
|
||||||
|
readonly int dueTimeFrameCount;
|
||||||
|
readonly int? periodFrameCount;
|
||||||
|
CancellationToken cancellationToken;
|
||||||
|
|
||||||
|
int initialFrame;
|
||||||
|
int currentFrame;
|
||||||
|
bool dueTimePhase;
|
||||||
|
bool completed;
|
||||||
|
bool disposed;
|
||||||
|
ThreadRunner runner;
|
||||||
|
|
||||||
|
public _TimerFrame(int dueTimeFrameCount, int? periodFrameCount, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
if (dueTimeFrameCount <= 0) dueTimeFrameCount = 0;
|
||||||
|
if (periodFrameCount != null)
|
||||||
|
{
|
||||||
|
if (periodFrameCount <= 0) periodFrameCount = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//this.initialFrame = Time.frameCount;
|
||||||
|
this.dueTimePhase = true;
|
||||||
|
this.dueTimeFrameCount = dueTimeFrameCount;
|
||||||
|
this.periodFrameCount = periodFrameCount;
|
||||||
|
|
||||||
|
//TaskTracker.TrackActiveTask(this, 2);
|
||||||
|
//PlayerLoopHelper.AddAction(updateTiming, this);
|
||||||
|
|
||||||
|
runner = new ThreadRunner();
|
||||||
|
runner.Start(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public AsyncUnit Current => default;
|
||||||
|
|
||||||
|
public UniTask<bool> MoveNextAsync()
|
||||||
|
{
|
||||||
|
// return false instead of throw
|
||||||
|
if (disposed || cancellationToken.IsCancellationRequested || completed) return default;
|
||||||
|
|
||||||
|
|
||||||
|
// reset value here.
|
||||||
|
this.currentFrame = 0;
|
||||||
|
|
||||||
|
completionSource.Reset();
|
||||||
|
return new UniTask<bool>(this, completionSource.Version);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTask DisposeAsync()
|
||||||
|
{
|
||||||
|
if (!disposed)
|
||||||
|
{
|
||||||
|
disposed = true;
|
||||||
|
TaskTracker.RemoveTracking(this);
|
||||||
|
}
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool MoveNext()
|
||||||
|
{
|
||||||
|
UnityEngine.Debug.Log("Called MoveNext");
|
||||||
|
if (disposed || cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
UnityEngine.Debug.Log("Disposing");
|
||||||
|
completionSource.TrySetResult(false);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dueTimePhase)
|
||||||
|
{
|
||||||
|
UnityEngine.Debug.Log("In DueTime Phase");
|
||||||
|
if (currentFrame == 0)
|
||||||
|
{
|
||||||
|
if (dueTimeFrameCount == 0)
|
||||||
|
{
|
||||||
|
dueTimePhase = false;
|
||||||
|
completionSource.TrySetResult(true);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// skip in initial frame.
|
||||||
|
/*
|
||||||
|
UnityEngine.Debug.Log("(Init, frameConut)" + (initialFrame, Time.frameCount));
|
||||||
|
if (initialFrame == Time.frameCount)
|
||||||
|
{
|
||||||
|
UnityEngine.Debug.Log("Skip Here");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
UnityEngine.Debug.Log("Which Go?");
|
||||||
|
if (++currentFrame >= dueTimeFrameCount)
|
||||||
|
{
|
||||||
|
UnityEngine.Debug.Log("END Go?");
|
||||||
|
dueTimePhase = false;
|
||||||
|
completionSource.TrySetResult(true);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
UnityEngine.Debug.Log("NG Go?");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (periodFrameCount == null)
|
||||||
|
{
|
||||||
|
UnityEngine.Debug.Log("PERIOD");
|
||||||
|
completed = true;
|
||||||
|
completionSource.TrySetResult(false);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (++currentFrame >= periodFrameCount)
|
||||||
|
{
|
||||||
|
completionSource.TrySetResult(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
11
src/UniTask/Assets/Tests/DelayTest.cs.meta
Normal file
11
src/UniTask/Assets/Tests/DelayTest.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 1376b93d9e1083a4a9b4081e08f3a7b7
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@@ -1,401 +1,400 @@
|
|||||||
#if !(UNITY_4_5 || UNITY_4_6 || UNITY_4_7 || UNITY_5_0 || UNITY_5_1 || UNITY_5_2)
|
//#if !(UNITY_4_5 || UNITY_4_6 || UNITY_4_7 || UNITY_5_0 || UNITY_5_1 || UNITY_5_2)
|
||||||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
//#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
||||||
|
|
||||||
using UnityEngine;
|
//using UnityEngine;
|
||||||
using System;
|
//using System;
|
||||||
using System.Collections;
|
//using System.Collections;
|
||||||
using System.Collections.Generic;
|
//using System.Collections.Generic;
|
||||||
using System.Linq;
|
//using System.Linq;
|
||||||
using System.Text;
|
//using System.Text;
|
||||||
using UnityEngine.UI;
|
//using UnityEngine.UI;
|
||||||
using UnityEngine.Scripting;
|
//using UnityEngine.Scripting;
|
||||||
using Cysharp.Threading.Tasks;
|
//using Cysharp.Threading.Tasks;
|
||||||
using UnityEngine.SceneManagement;
|
//using UnityEngine.SceneManagement;
|
||||||
#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6))
|
//#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6))
|
||||||
using System.Threading.Tasks;
|
//using System.Threading.Tasks;
|
||||||
#endif
|
//#endif
|
||||||
using UnityEngine.Networking;
|
//using UnityEngine.Networking;
|
||||||
|
|
||||||
#if !UNITY_2019_3_OR_NEWER
|
//#if !UNITY_2019_3_OR_NEWER
|
||||||
using UnityEngine.Experimental.LowLevel;
|
//using UnityEngine.Experimental.LowLevel;
|
||||||
#else
|
//#else
|
||||||
using UnityEngine.LowLevel;
|
//using UnityEngine.LowLevel;
|
||||||
#endif
|
//#endif
|
||||||
|
|
||||||
#if !UNITY_WSA
|
//#if !UNITY_WSA
|
||||||
using Unity.Jobs;
|
//using Unity.Jobs;
|
||||||
#endif
|
//#endif
|
||||||
using Unity.Collections;
|
//using Unity.Collections;
|
||||||
using System.Threading;
|
//using System.Threading;
|
||||||
using NUnit.Framework;
|
//using NUnit.Framework;
|
||||||
using UnityEngine.TestTools;
|
//using UnityEngine.TestTools;
|
||||||
using FluentAssertions;
|
//using FluentAssertions;
|
||||||
|
|
||||||
namespace Cysharp.Threading.TasksTests
|
//namespace Cysharp.Threading.TasksTests
|
||||||
{
|
//{
|
||||||
public class AsyncTest
|
// public class AsyncTest
|
||||||
{
|
// {
|
||||||
#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6))
|
//#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6))
|
||||||
#if !UNITY_WSA
|
//#if !UNITY_WSA
|
||||||
|
|
||||||
public struct MyJob : IJob
|
// public struct MyJob : IJob
|
||||||
{
|
// {
|
||||||
public int loopCount;
|
// public int loopCount;
|
||||||
public NativeArray<int> inOut;
|
// public NativeArray<int> inOut;
|
||||||
public int result;
|
// public int result;
|
||||||
|
|
||||||
public void Execute()
|
// public void Execute()
|
||||||
{
|
// {
|
||||||
result = 0;
|
// result = 0;
|
||||||
for (int i = 0; i < loopCount; i++)
|
// for (int i = 0; i < loopCount; i++)
|
||||||
{
|
// {
|
||||||
result++;
|
// result++;
|
||||||
}
|
// }
|
||||||
inOut[0] = result;
|
// inOut[0] = result;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
[UnityTest]
|
// [UnityTest]
|
||||||
public IEnumerator DelayAnd() => UniTask.ToCoroutine(async () =>
|
// public IEnumerator DelayAnd() => UniTask.ToCoroutine(async () =>
|
||||||
{
|
// {
|
||||||
await UniTask.Yield(PlayerLoopTiming.PostLateUpdate);
|
// await UniTask.Yield(PlayerLoopTiming.PostLateUpdate);
|
||||||
|
|
||||||
var time = Time.realtimeSinceStartup;
|
// var time = Time.realtimeSinceStartup;
|
||||||
|
|
||||||
Time.timeScale = 0.5f;
|
// Time.timeScale = 0.5f;
|
||||||
try
|
// try
|
||||||
{
|
// {
|
||||||
await UniTask.Delay(TimeSpan.FromSeconds(3));
|
// await UniTask.Delay(TimeSpan.FromSeconds(3));
|
||||||
|
|
||||||
var elapsed = Time.realtimeSinceStartup - time;
|
// var elapsed = Time.realtimeSinceStartup - time;
|
||||||
((int)Math.Round(TimeSpan.FromSeconds(elapsed).TotalSeconds, MidpointRounding.ToEven)).Should().Be(6);
|
// ((int)Math.Round(TimeSpan.FromSeconds(elapsed).TotalSeconds, MidpointRounding.ToEven)).Should().Be(6);
|
||||||
}
|
// }
|
||||||
finally
|
// finally
|
||||||
{
|
// {
|
||||||
Time.timeScale = 1.0f;
|
// Time.timeScale = 1.0f;
|
||||||
}
|
// }
|
||||||
});
|
// });
|
||||||
|
|
||||||
[UnityTest]
|
// [UnityTest]
|
||||||
public IEnumerator DelayIgnore() => UniTask.ToCoroutine(async () =>
|
// public IEnumerator DelayIgnore() => UniTask.ToCoroutine(async () =>
|
||||||
{
|
// {
|
||||||
var time = Time.realtimeSinceStartup;
|
// var time = Time.realtimeSinceStartup;
|
||||||
|
|
||||||
Time.timeScale = 0.5f;
|
// Time.timeScale = 0.5f;
|
||||||
try
|
// try
|
||||||
{
|
// {
|
||||||
await UniTask.Delay(TimeSpan.FromSeconds(3), ignoreTimeScale: true);
|
// await UniTask.Delay(TimeSpan.FromSeconds(3), ignoreTimeScale: true);
|
||||||
|
|
||||||
var elapsed = Time.realtimeSinceStartup - time;
|
// var elapsed = Time.realtimeSinceStartup - time;
|
||||||
((int)Math.Round(TimeSpan.FromSeconds(elapsed).TotalSeconds, MidpointRounding.ToEven)).Should().Be(3);
|
// ((int)Math.Round(TimeSpan.FromSeconds(elapsed).TotalSeconds, MidpointRounding.ToEven)).Should().Be(3);
|
||||||
}
|
// }
|
||||||
finally
|
// finally
|
||||||
{
|
// {
|
||||||
Time.timeScale = 1.0f;
|
// Time.timeScale = 1.0f;
|
||||||
}
|
// }
|
||||||
});
|
// });
|
||||||
|
|
||||||
[UnityTest]
|
// [UnityTest]
|
||||||
public IEnumerator WhenAll() => UniTask.ToCoroutine(async () =>
|
// public IEnumerator WhenAll() => UniTask.ToCoroutine(async () =>
|
||||||
{
|
// {
|
||||||
var a = UniTask.FromResult(999);
|
// var a = UniTask.FromResult(999);
|
||||||
var b = UniTask.Yield(PlayerLoopTiming.Update, CancellationToken.None).AsAsyncUnitUniTask();
|
// var b = UniTask.Yield(PlayerLoopTiming.Update, CancellationToken.None).AsAsyncUnitUniTask();
|
||||||
var c = UniTask.DelayFrame(99).AsAsyncUnitUniTask();
|
// var c = UniTask.DelayFrame(99).AsAsyncUnitUniTask();
|
||||||
|
|
||||||
var (a2, b2, c2) = await UniTask.WhenAll(a, b, c);
|
// var (a2, b2, c2) = await UniTask.WhenAll(a, b, c);
|
||||||
a2.Should().Be(999);
|
// a2.Should().Be(999);
|
||||||
b2.Should().Be(AsyncUnit.Default);
|
// b2.Should().Be(AsyncUnit.Default);
|
||||||
c2.Should().Be(AsyncUnit.Default);
|
// c2.Should().Be(AsyncUnit.Default);
|
||||||
});
|
// });
|
||||||
|
|
||||||
[UnityTest]
|
// [UnityTest]
|
||||||
public IEnumerator WhenAny() => UniTask.ToCoroutine(async () =>
|
// public IEnumerator WhenAny() => UniTask.ToCoroutine(async () =>
|
||||||
{
|
// {
|
||||||
var a = UniTask.FromResult(999);
|
// var a = UniTask.FromResult(999);
|
||||||
var b = UniTask.Yield(PlayerLoopTiming.Update, CancellationToken.None).AsAsyncUnitUniTask();
|
// var b = UniTask.Yield(PlayerLoopTiming.Update, CancellationToken.None).AsAsyncUnitUniTask();
|
||||||
var c = UniTask.DelayFrame(99).AsAsyncUnitUniTask();
|
// var c = UniTask.DelayFrame(99).AsAsyncUnitUniTask();
|
||||||
|
|
||||||
var (win, a2, b2, c2) = await UniTask.WhenAny(a, b, c);
|
// var (win, a2, b2, c2) = await UniTask.WhenAny(a, b, c);
|
||||||
win.Should().Be(0);
|
// win.Should().Be(0);
|
||||||
a2.Should().Be(999);
|
// a2.Should().Be(999);
|
||||||
});
|
// });
|
||||||
|
|
||||||
[UnityTest]
|
// [UnityTest]
|
||||||
public IEnumerator BothEnumeratorCheck() => UniTask.ToCoroutine(async () =>
|
// public IEnumerator BothEnumeratorCheck() => UniTask.ToCoroutine(async () =>
|
||||||
{
|
// {
|
||||||
await ToaruCoroutineEnumerator(); // wait 5 frame:)
|
// await ToaruCoroutineEnumerator(); // wait 5 frame:)
|
||||||
await ToaruCoroutineEnumerator().ConfigureAwait(PlayerLoopTiming.PostLateUpdate);
|
// });
|
||||||
});
|
|
||||||
|
|
||||||
[UnityTest]
|
// [UnityTest]
|
||||||
public IEnumerator JobSystem() => UniTask.ToCoroutine(async () =>
|
// public IEnumerator JobSystem() => UniTask.ToCoroutine(async () =>
|
||||||
{
|
// {
|
||||||
var job = new MyJob() { loopCount = 999, inOut = new NativeArray<int>(1, Allocator.TempJob) };
|
// var job = new MyJob() { loopCount = 999, inOut = new NativeArray<int>(1, Allocator.TempJob) };
|
||||||
JobHandle.ScheduleBatchedJobs();
|
// JobHandle.ScheduleBatchedJobs();
|
||||||
await job.Schedule();
|
// await job.Schedule();
|
||||||
job.inOut[0].Should().Be(999);
|
// job.inOut[0].Should().Be(999);
|
||||||
job.inOut.Dispose();
|
// job.inOut.Dispose();
|
||||||
});
|
// });
|
||||||
|
|
||||||
class MyMyClass
|
// class MyMyClass
|
||||||
{
|
// {
|
||||||
public int MyProperty { get; set; }
|
// public int MyProperty { get; set; }
|
||||||
}
|
// }
|
||||||
|
|
||||||
[UnityTest]
|
// [UnityTest]
|
||||||
public IEnumerator WaitUntil() => UniTask.ToCoroutine(async () =>
|
// public IEnumerator WaitUntil() => UniTask.ToCoroutine(async () =>
|
||||||
{
|
// {
|
||||||
bool t = false;
|
// bool t = false;
|
||||||
|
|
||||||
await UniTask.Yield(PlayerLoopTiming.PostLateUpdate);
|
// await UniTask.Yield(PlayerLoopTiming.PostLateUpdate);
|
||||||
|
|
||||||
UniTask.DelayFrame(10,PlayerLoopTiming.PostLateUpdate).ContinueWith(() => t = true).Forget();
|
// UniTask.DelayFrame(10,PlayerLoopTiming.PostLateUpdate).ContinueWith(() => t = true).Forget();
|
||||||
|
|
||||||
var startFrame = Time.frameCount;
|
// var startFrame = Time.frameCount;
|
||||||
await UniTask.WaitUntil(() => t, PlayerLoopTiming.EarlyUpdate);
|
// await UniTask.WaitUntil(() => t, PlayerLoopTiming.EarlyUpdate);
|
||||||
|
|
||||||
var diff = Time.frameCount - startFrame;
|
// var diff = Time.frameCount - startFrame;
|
||||||
diff.Should().Be(11);
|
// diff.Should().Be(11);
|
||||||
});
|
// });
|
||||||
|
|
||||||
[UnityTest]
|
// [UnityTest]
|
||||||
public IEnumerator WaitWhile() => UniTask.ToCoroutine(async () =>
|
// public IEnumerator WaitWhile() => UniTask.ToCoroutine(async () =>
|
||||||
{
|
// {
|
||||||
bool t = true;
|
// bool t = true;
|
||||||
|
|
||||||
UniTask.DelayFrame(10, PlayerLoopTiming.PostLateUpdate).ContinueWith(() => t = false).Forget();
|
// UniTask.DelayFrame(10, PlayerLoopTiming.PostLateUpdate).ContinueWith(() => t = false).Forget();
|
||||||
|
|
||||||
var startFrame = Time.frameCount;
|
// var startFrame = Time.frameCount;
|
||||||
await UniTask.WaitWhile(() => t, PlayerLoopTiming.EarlyUpdate);
|
// await UniTask.WaitWhile(() => t, PlayerLoopTiming.EarlyUpdate);
|
||||||
|
|
||||||
var diff = Time.frameCount - startFrame;
|
// var diff = Time.frameCount - startFrame;
|
||||||
diff.Should().Be(11);
|
// diff.Should().Be(11);
|
||||||
});
|
// });
|
||||||
|
|
||||||
[UnityTest]
|
// [UnityTest]
|
||||||
public IEnumerator WaitUntilValueChanged() => UniTask.ToCoroutine(async () =>
|
// public IEnumerator WaitUntilValueChanged() => UniTask.ToCoroutine(async () =>
|
||||||
{
|
// {
|
||||||
var v = new MyMyClass { MyProperty = 99 };
|
// var v = new MyMyClass { MyProperty = 99 };
|
||||||
|
|
||||||
UniTask.DelayFrame(10, PlayerLoopTiming.PostLateUpdate).ContinueWith(() => v.MyProperty = 1000).Forget();
|
// UniTask.DelayFrame(10, PlayerLoopTiming.PostLateUpdate).ContinueWith(() => v.MyProperty = 1000).Forget();
|
||||||
|
|
||||||
var startFrame = Time.frameCount;
|
// var startFrame = Time.frameCount;
|
||||||
await UniTask.WaitUntilValueChanged(v, x => x.MyProperty, PlayerLoopTiming.EarlyUpdate);
|
// await UniTask.WaitUntilValueChanged(v, x => x.MyProperty, PlayerLoopTiming.EarlyUpdate);
|
||||||
|
|
||||||
var diff = Time.frameCount - startFrame;
|
// var diff = Time.frameCount - startFrame;
|
||||||
diff.Should().Be(11);
|
// diff.Should().Be(11);
|
||||||
});
|
// });
|
||||||
|
|
||||||
[UnityTest]
|
// [UnityTest]
|
||||||
public IEnumerator SwitchTo() => UniTask.ToCoroutine(async () =>
|
// public IEnumerator SwitchTo() => UniTask.ToCoroutine(async () =>
|
||||||
{
|
// {
|
||||||
await UniTask.Yield();
|
// await UniTask.Yield();
|
||||||
|
|
||||||
var currentThreadId = Thread.CurrentThread.ManagedThreadId;
|
// var currentThreadId = Thread.CurrentThread.ManagedThreadId;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
await UniTask.SwitchToThreadPool();
|
// await UniTask.SwitchToThreadPool();
|
||||||
//await UniTask.SwitchToThreadPool();
|
// //await UniTask.SwitchToThreadPool();
|
||||||
//await UniTask.SwitchToThreadPool();
|
// //await UniTask.SwitchToThreadPool();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var switchedThreadId = Thread.CurrentThread.ManagedThreadId;
|
// var switchedThreadId = Thread.CurrentThread.ManagedThreadId;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
currentThreadId.Should().NotBe(switchedThreadId);
|
// currentThreadId.Should().NotBe(switchedThreadId);
|
||||||
|
|
||||||
|
|
||||||
await UniTask.Yield();
|
// await UniTask.Yield();
|
||||||
|
|
||||||
var switchedThreadId2 = Thread.CurrentThread.ManagedThreadId;
|
// var switchedThreadId2 = Thread.CurrentThread.ManagedThreadId;
|
||||||
|
|
||||||
currentThreadId.Should().Be(switchedThreadId2);
|
// currentThreadId.Should().Be(switchedThreadId2);
|
||||||
});
|
// });
|
||||||
|
|
||||||
//[UnityTest]
|
// //[UnityTest]
|
||||||
//public IEnumerator ObservableConversion() => UniTask.ToCoroutine(async () =>
|
// //public IEnumerator ObservableConversion() => UniTask.ToCoroutine(async () =>
|
||||||
//{
|
// //{
|
||||||
// var v = await Observable.Range(1, 10).ToUniTask();
|
// // var v = await Observable.Range(1, 10).ToUniTask();
|
||||||
// v.Is(10);
|
// // v.Is(10);
|
||||||
|
|
||||||
// v = await Observable.Range(1, 10).ToUniTask(useFirstValue: true);
|
// // v = await Observable.Range(1, 10).ToUniTask(useFirstValue: true);
|
||||||
// v.Is(1);
|
// // v.Is(1);
|
||||||
|
|
||||||
// v = await UniTask.DelayFrame(10).ToObservable().ToTask();
|
// // v = await UniTask.DelayFrame(10).ToObservable().ToTask();
|
||||||
// v.Is(10);
|
// // v.Is(10);
|
||||||
|
|
||||||
// v = await UniTask.FromResult(99).ToObservable();
|
// // v = await UniTask.FromResult(99).ToObservable();
|
||||||
// v.Is(99);
|
// // v.Is(99);
|
||||||
//});
|
// //});
|
||||||
|
|
||||||
//[UnityTest]
|
// //[UnityTest]
|
||||||
//public IEnumerator AwaitableReactiveProperty() => UniTask.ToCoroutine(async () =>
|
// //public IEnumerator AwaitableReactiveProperty() => UniTask.ToCoroutine(async () =>
|
||||||
//{
|
// //{
|
||||||
// var rp1 = new ReactiveProperty<int>(99);
|
// // var rp1 = new ReactiveProperty<int>(99);
|
||||||
|
|
||||||
// UniTask.DelayFrame(100).ContinueWith(x => rp1.Value = x).Forget();
|
// // UniTask.DelayFrame(100).ContinueWith(x => rp1.Value = x).Forget();
|
||||||
|
|
||||||
// await rp1;
|
// // await rp1;
|
||||||
|
|
||||||
// rp1.Value.Is(100);
|
// // rp1.Value.Is(100);
|
||||||
|
|
||||||
// // var delay2 = UniTask.DelayFrame(10);
|
// // // var delay2 = UniTask.DelayFrame(10);
|
||||||
// // var (a, b ) = await UniTask.WhenAll(rp1.WaitUntilValueChangedAsync(), delay2);
|
// // // var (a, b ) = await UniTask.WhenAll(rp1.WaitUntilValueChangedAsync(), delay2);
|
||||||
|
|
||||||
//});
|
// //});
|
||||||
|
|
||||||
//[UnityTest]
|
// //[UnityTest]
|
||||||
//public IEnumerator AwaitableReactiveCommand() => UniTask.ToCoroutine(async () =>
|
// //public IEnumerator AwaitableReactiveCommand() => UniTask.ToCoroutine(async () =>
|
||||||
//{
|
// //{
|
||||||
// var rc = new ReactiveCommand<int>();
|
// // var rc = new ReactiveCommand<int>();
|
||||||
|
|
||||||
// UniTask.DelayFrame(100).ContinueWith(x => rc.Execute(x)).Forget();
|
// // UniTask.DelayFrame(100).ContinueWith(x => rc.Execute(x)).Forget();
|
||||||
|
|
||||||
// var v = await rc;
|
// // var v = await rc;
|
||||||
|
|
||||||
// v.Is(100);
|
// // v.Is(100);
|
||||||
//});
|
// //});
|
||||||
|
|
||||||
[UnityTest]
|
// [UnityTest]
|
||||||
public IEnumerator ExceptionlessCancellation() => UniTask.ToCoroutine(async () =>
|
// public IEnumerator ExceptionlessCancellation() => UniTask.ToCoroutine(async () =>
|
||||||
{
|
// {
|
||||||
var cts = new CancellationTokenSource();
|
// var cts = new CancellationTokenSource();
|
||||||
|
|
||||||
UniTask.DelayFrame(10).ContinueWith(() => cts.Cancel()).Forget();
|
// UniTask.DelayFrame(10).ContinueWith(() => cts.Cancel()).Forget();
|
||||||
|
|
||||||
var first = Time.frameCount;
|
// var first = Time.frameCount;
|
||||||
var canceled = await UniTask.DelayFrame(100, cancellationToken: cts.Token).SuppressCancellationThrow();
|
// var canceled = await UniTask.DelayFrame(100, cancellationToken: cts.Token).SuppressCancellationThrow();
|
||||||
|
|
||||||
(Time.frameCount - first).Should().Be(11); // 10 frame canceled
|
// (Time.frameCount - first).Should().Be(11); // 10 frame canceled
|
||||||
canceled.Should().Be(true);
|
// canceled.Should().Be(true);
|
||||||
});
|
// });
|
||||||
|
|
||||||
[UnityTest]
|
// [UnityTest]
|
||||||
public IEnumerator ExceptionCancellation() => UniTask.ToCoroutine(async () =>
|
// public IEnumerator ExceptionCancellation() => UniTask.ToCoroutine(async () =>
|
||||||
{
|
// {
|
||||||
var cts = new CancellationTokenSource();
|
// var cts = new CancellationTokenSource();
|
||||||
|
|
||||||
UniTask.DelayFrame(10).ContinueWith(() => cts.Cancel()).Forget();
|
// UniTask.DelayFrame(10).ContinueWith(() => cts.Cancel()).Forget();
|
||||||
|
|
||||||
bool occur = false;
|
// bool occur = false;
|
||||||
try
|
// try
|
||||||
{
|
// {
|
||||||
await UniTask.DelayFrame(100, cancellationToken: cts.Token);
|
// await UniTask.DelayFrame(100, cancellationToken: cts.Token);
|
||||||
}
|
// }
|
||||||
catch (OperationCanceledException)
|
// catch (OperationCanceledException)
|
||||||
{
|
// {
|
||||||
occur = true;
|
// occur = true;
|
||||||
}
|
// }
|
||||||
occur.Should().BeTrue();
|
// occur.Should().BeTrue();
|
||||||
});
|
// });
|
||||||
|
|
||||||
IEnumerator ToaruCoroutineEnumerator()
|
// IEnumerator ToaruCoroutineEnumerator()
|
||||||
{
|
// {
|
||||||
yield return null;
|
// yield return null;
|
||||||
yield return null;
|
// yield return null;
|
||||||
yield return null;
|
// yield return null;
|
||||||
yield return null;
|
// yield return null;
|
||||||
yield return null;
|
// yield return null;
|
||||||
}
|
// }
|
||||||
|
|
||||||
[UnityTest]
|
// [UnityTest]
|
||||||
public IEnumerator ExceptionUnobserved1() => UniTask.ToCoroutine(async () =>
|
// public IEnumerator ExceptionUnobserved1() => UniTask.ToCoroutine(async () =>
|
||||||
{
|
// {
|
||||||
bool calledEx = false;
|
// bool calledEx = false;
|
||||||
Action<Exception> action = exx =>
|
// Action<Exception> action = exx =>
|
||||||
{
|
// {
|
||||||
calledEx = true;
|
// calledEx = true;
|
||||||
exx.Message.Should().Be("MyException");
|
// exx.Message.Should().Be("MyException");
|
||||||
};
|
// };
|
||||||
|
|
||||||
UniTaskScheduler.UnobservedTaskException += action;
|
// UniTaskScheduler.UnobservedTaskException += action;
|
||||||
|
|
||||||
var ex = InException1();
|
// var ex = InException1();
|
||||||
ex = default(UniTask);
|
// ex = default(UniTask);
|
||||||
|
|
||||||
await UniTask.DelayFrame(3);
|
// await UniTask.DelayFrame(3);
|
||||||
|
|
||||||
GC.Collect();
|
// GC.Collect();
|
||||||
GC.WaitForPendingFinalizers();
|
// GC.WaitForPendingFinalizers();
|
||||||
GC.Collect();
|
// GC.Collect();
|
||||||
|
|
||||||
await UniTask.DelayFrame(1);
|
// await UniTask.DelayFrame(1);
|
||||||
|
|
||||||
calledEx.Should().BeTrue();
|
// calledEx.Should().BeTrue();
|
||||||
|
|
||||||
UniTaskScheduler.UnobservedTaskException -= action;
|
// UniTaskScheduler.UnobservedTaskException -= action;
|
||||||
});
|
// });
|
||||||
|
|
||||||
[UnityTest]
|
// [UnityTest]
|
||||||
public IEnumerator ExceptionUnobserved2() => UniTask.ToCoroutine(async () =>
|
// public IEnumerator ExceptionUnobserved2() => UniTask.ToCoroutine(async () =>
|
||||||
{
|
// {
|
||||||
bool calledEx = false;
|
// bool calledEx = false;
|
||||||
Action<Exception> action = exx =>
|
// Action<Exception> action = exx =>
|
||||||
{
|
// {
|
||||||
calledEx = true;
|
// calledEx = true;
|
||||||
exx.Message.Should().Be("MyException");
|
// exx.Message.Should().Be("MyException");
|
||||||
};
|
// };
|
||||||
|
|
||||||
UniTaskScheduler.UnobservedTaskException += action;
|
// UniTaskScheduler.UnobservedTaskException += action;
|
||||||
|
|
||||||
var ex = InException2();
|
// var ex = InException2();
|
||||||
ex = default(UniTask<int>);
|
// ex = default(UniTask<int>);
|
||||||
|
|
||||||
await UniTask.DelayFrame(3);
|
// await UniTask.DelayFrame(3);
|
||||||
|
|
||||||
GC.Collect();
|
// GC.Collect();
|
||||||
GC.WaitForPendingFinalizers();
|
// GC.WaitForPendingFinalizers();
|
||||||
GC.Collect();
|
// GC.Collect();
|
||||||
|
|
||||||
await UniTask.DelayFrame(1);
|
// await UniTask.DelayFrame(1);
|
||||||
|
|
||||||
calledEx.Should().BeTrue();
|
// calledEx.Should().BeTrue();
|
||||||
|
|
||||||
UniTaskScheduler.UnobservedTaskException -= action;
|
// UniTaskScheduler.UnobservedTaskException -= action;
|
||||||
});
|
// });
|
||||||
|
|
||||||
async UniTask InException1()
|
// async UniTask InException1()
|
||||||
{
|
// {
|
||||||
await UniTask.Yield();
|
// await UniTask.Yield();
|
||||||
throw new Exception("MyException");
|
// throw new Exception("MyException");
|
||||||
}
|
// }
|
||||||
|
|
||||||
async UniTask<int> InException2()
|
// async UniTask<int> InException2()
|
||||||
{
|
// {
|
||||||
await UniTask.Yield();
|
// await UniTask.Yield();
|
||||||
throw new Exception("MyException");
|
// throw new Exception("MyException");
|
||||||
}
|
// }
|
||||||
|
|
||||||
[UnityTest]
|
// [UnityTest]
|
||||||
public IEnumerator NestedEnumerator() => UniTask.ToCoroutine(async () =>
|
// public IEnumerator NestedEnumerator() => UniTask.ToCoroutine(async () =>
|
||||||
{
|
// {
|
||||||
var time = Time.realtimeSinceStartup;
|
// var time = Time.realtimeSinceStartup;
|
||||||
|
|
||||||
await ParentCoroutineEnumerator();
|
// await ParentCoroutineEnumerator();
|
||||||
|
|
||||||
var elapsed = Time.realtimeSinceStartup - time;
|
// var elapsed = Time.realtimeSinceStartup - time;
|
||||||
((int)Math.Round(TimeSpan.FromSeconds(elapsed).TotalSeconds, MidpointRounding.ToEven)).Should().Be(3);
|
// ((int)Math.Round(TimeSpan.FromSeconds(elapsed).TotalSeconds, MidpointRounding.ToEven)).Should().Be(3);
|
||||||
});
|
// });
|
||||||
|
|
||||||
IEnumerator ParentCoroutineEnumerator()
|
// IEnumerator ParentCoroutineEnumerator()
|
||||||
{
|
// {
|
||||||
yield return ChildCoroutineEnumerator();
|
// yield return ChildCoroutineEnumerator();
|
||||||
}
|
// }
|
||||||
|
|
||||||
IEnumerator ChildCoroutineEnumerator()
|
// IEnumerator ChildCoroutineEnumerator()
|
||||||
{
|
// {
|
||||||
yield return new WaitForSeconds(3);
|
// yield return new WaitForSeconds(3);
|
||||||
}
|
// }
|
||||||
|
|
||||||
#endif
|
//#endif
|
||||||
#endif
|
//#endif
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
|
||||||
#endif
|
//#endif
|
||||||
@@ -1,95 +1,95 @@
|
|||||||
#if !(UNITY_4_5 || UNITY_4_6 || UNITY_4_7 || UNITY_5_0 || UNITY_5_1 || UNITY_5_2)
|
//#if !(UNITY_4_5 || UNITY_4_6 || UNITY_4_7 || UNITY_5_0 || UNITY_5_1 || UNITY_5_2)
|
||||||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
//#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
||||||
|
|
||||||
using UnityEngine;
|
//using UnityEngine;
|
||||||
using System;
|
//using System;
|
||||||
using System.Collections;
|
//using System.Collections;
|
||||||
using System.Collections.Generic;
|
//using System.Collections.Generic;
|
||||||
using System.Linq;
|
//using System.Linq;
|
||||||
using System.Text;
|
//using System.Text;
|
||||||
using UnityEngine.UI;
|
//using UnityEngine.UI;
|
||||||
using UnityEngine.Scripting;
|
//using UnityEngine.Scripting;
|
||||||
using Cysharp.Threading.Tasks;
|
//using Cysharp.Threading.Tasks;
|
||||||
using UnityEngine.SceneManagement;
|
//using UnityEngine.SceneManagement;
|
||||||
#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6))
|
//#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6))
|
||||||
using System.Threading.Tasks;
|
//using System.Threading.Tasks;
|
||||||
#endif
|
//#endif
|
||||||
using UnityEngine.Networking;
|
//using UnityEngine.Networking;
|
||||||
|
|
||||||
#if !UNITY_2019_3_OR_NEWER
|
//#if !UNITY_2019_3_OR_NEWER
|
||||||
using UnityEngine.Experimental.LowLevel;
|
//using UnityEngine.Experimental.LowLevel;
|
||||||
#else
|
//#else
|
||||||
using UnityEngine.LowLevel;
|
//using UnityEngine.LowLevel;
|
||||||
#endif
|
//#endif
|
||||||
|
|
||||||
#if !UNITY_WSA
|
//#if !UNITY_WSA
|
||||||
using Unity.Jobs;
|
//using Unity.Jobs;
|
||||||
#endif
|
//#endif
|
||||||
using Unity.Collections;
|
//using Unity.Collections;
|
||||||
using System.Threading;
|
//using System.Threading;
|
||||||
using NUnit.Framework;
|
//using NUnit.Framework;
|
||||||
using UnityEngine.TestTools;
|
//using UnityEngine.TestTools;
|
||||||
using FluentAssertions;
|
//using FluentAssertions;
|
||||||
|
|
||||||
namespace Cysharp.Threading.TasksTests
|
//namespace Cysharp.Threading.TasksTests
|
||||||
{
|
//{
|
||||||
public class RunTest
|
// public class RunTest
|
||||||
{
|
// {
|
||||||
#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6))
|
//#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6))
|
||||||
#if !UNITY_WSA
|
//#if !UNITY_WSA
|
||||||
|
|
||||||
//[UnityTest]
|
// //[UnityTest]
|
||||||
//public IEnumerator RunThread() => UniTask.ToCoroutine(async () =>
|
// //public IEnumerator RunThread() => UniTask.ToCoroutine(async () =>
|
||||||
//{
|
// //{
|
||||||
// var main = Thread.CurrentThread.ManagedThreadId;
|
// // var main = Thread.CurrentThread.ManagedThreadId;
|
||||||
// var v = await UniTask.Run(() => { return System.Threading.Thread.CurrentThread.ManagedThreadId; }, false);
|
// // var v = await UniTask.Run(() => { return System.Threading.Thread.CurrentThread.ManagedThreadId; }, false);
|
||||||
// UnityEngine.Debug.Log("Ret Value is:" + v);
|
// // UnityEngine.Debug.Log("Ret Value is:" + v);
|
||||||
// UnityEngine.Debug.Log("Run Here and id:" + System.Threading.Thread.CurrentThread.ManagedThreadId);
|
// // UnityEngine.Debug.Log("Run Here and id:" + System.Threading.Thread.CurrentThread.ManagedThreadId);
|
||||||
// //v.Should().Be(3);
|
// // //v.Should().Be(3);
|
||||||
// main.Should().NotBe(Thread.CurrentThread.ManagedThreadId);
|
// // main.Should().NotBe(Thread.CurrentThread.ManagedThreadId);
|
||||||
//});
|
// //});
|
||||||
|
|
||||||
[UnityTest]
|
// [UnityTest]
|
||||||
public IEnumerator RunThreadConfigure() => UniTask.ToCoroutine(async () =>
|
// public IEnumerator RunThreadConfigure() => UniTask.ToCoroutine(async () =>
|
||||||
{
|
// {
|
||||||
var main = Thread.CurrentThread.ManagedThreadId;
|
// var main = Thread.CurrentThread.ManagedThreadId;
|
||||||
var v = await UniTask.Run(() => 3, true);
|
// var v = await UniTask.Run(() => 3, true);
|
||||||
v.Should().Be(3);
|
// v.Should().Be(3);
|
||||||
main.Should().Be(Thread.CurrentThread.ManagedThreadId);
|
// main.Should().Be(Thread.CurrentThread.ManagedThreadId);
|
||||||
});
|
// });
|
||||||
|
|
||||||
//[UnityTest]
|
// //[UnityTest]
|
||||||
//public IEnumerator RunThreadException() => UniTask.ToCoroutine(async () =>
|
// //public IEnumerator RunThreadException() => UniTask.ToCoroutine(async () =>
|
||||||
//{
|
// //{
|
||||||
// var main = Thread.CurrentThread.ManagedThreadId;
|
// // var main = Thread.CurrentThread.ManagedThreadId;
|
||||||
// try
|
// // try
|
||||||
// {
|
// // {
|
||||||
// await UniTask.Run<int>(() => throw new Exception(), false);
|
// // await UniTask.Run<int>(() => throw new Exception(), false);
|
||||||
// }
|
// // }
|
||||||
// catch
|
// // catch
|
||||||
// {
|
// // {
|
||||||
// main.Should().NotBe(Thread.CurrentThread.ManagedThreadId);
|
// // main.Should().NotBe(Thread.CurrentThread.ManagedThreadId);
|
||||||
// }
|
// // }
|
||||||
//});
|
// //});
|
||||||
|
|
||||||
[UnityTest]
|
// [UnityTest]
|
||||||
public IEnumerator RunThreadExceptionConfigure() => UniTask.ToCoroutine(async () =>
|
// public IEnumerator RunThreadExceptionConfigure() => UniTask.ToCoroutine(async () =>
|
||||||
{
|
// {
|
||||||
var main = Thread.CurrentThread.ManagedThreadId;
|
// var main = Thread.CurrentThread.ManagedThreadId;
|
||||||
try
|
// try
|
||||||
{
|
// {
|
||||||
await UniTask.Run<int>(() => throw new Exception(), true);
|
// await UniTask.Run<int>(() => throw new Exception(), true);
|
||||||
}
|
// }
|
||||||
catch
|
// catch
|
||||||
{
|
// {
|
||||||
main.Should().Be(Thread.CurrentThread.ManagedThreadId);
|
// main.Should().Be(Thread.CurrentThread.ManagedThreadId);
|
||||||
}
|
// }
|
||||||
});
|
// });
|
||||||
|
|
||||||
|
|
||||||
#endif
|
//#endif
|
||||||
#endif
|
//#endif
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
|
||||||
#endif
|
//#endif
|
||||||
@@ -5,7 +5,8 @@
|
|||||||
"UnityEditor.TestRunner",
|
"UnityEditor.TestRunner",
|
||||||
"UniTask.Tests",
|
"UniTask.Tests",
|
||||||
"UniTask",
|
"UniTask",
|
||||||
"Unity.ResourceManager"
|
"Unity.ResourceManager",
|
||||||
|
"DOTween.Modules"
|
||||||
],
|
],
|
||||||
"includePlatforms": [
|
"includePlatforms": [
|
||||||
"Editor"
|
"Editor"
|
||||||
@@ -14,7 +15,8 @@
|
|||||||
"allowUnsafeCode": false,
|
"allowUnsafeCode": false,
|
||||||
"overrideReferences": true,
|
"overrideReferences": true,
|
||||||
"precompiledReferences": [
|
"precompiledReferences": [
|
||||||
"nunit.framework.dll"
|
"nunit.framework.dll",
|
||||||
|
"DOTween.dll"
|
||||||
],
|
],
|
||||||
"autoReferenced": false,
|
"autoReferenced": false,
|
||||||
"defineConstraints": [
|
"defineConstraints": [
|
||||||
|
|||||||
@@ -1,43 +1,43 @@
|
|||||||
#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6))
|
//#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6))
|
||||||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
//#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
||||||
|
|
||||||
using UnityEngine;
|
//using UnityEngine;
|
||||||
using System;
|
//using System;
|
||||||
using System.Collections;
|
//using System.Collections;
|
||||||
using System.Collections.Generic;
|
//using System.Collections.Generic;
|
||||||
using System.Linq;
|
//using System.Linq;
|
||||||
using System.Text;
|
//using System.Text;
|
||||||
using UnityEngine.UI;
|
//using UnityEngine.UI;
|
||||||
using UnityEngine.Scripting;
|
//using UnityEngine.Scripting;
|
||||||
using Cysharp.Threading.Tasks;
|
//using Cysharp.Threading.Tasks;
|
||||||
using Unity.Collections;
|
//using Unity.Collections;
|
||||||
using System.Threading;
|
//using System.Threading;
|
||||||
using NUnit.Framework;
|
//using NUnit.Framework;
|
||||||
using UnityEngine.TestTools;
|
//using UnityEngine.TestTools;
|
||||||
using FluentAssertions;
|
//using FluentAssertions;
|
||||||
|
|
||||||
namespace Cysharp.Threading.TasksTests
|
//namespace Cysharp.Threading.TasksTests
|
||||||
{
|
//{
|
||||||
public class WhenAnyTest
|
// public class WhenAnyTest
|
||||||
{
|
// {
|
||||||
[UnityTest]
|
// [UnityTest]
|
||||||
public IEnumerator WhenAnyCanceled() => UniTask.ToCoroutine(async () =>
|
// public IEnumerator WhenAnyCanceled() => UniTask.ToCoroutine(async () =>
|
||||||
{
|
// {
|
||||||
var cts = new CancellationTokenSource();
|
// var cts = new CancellationTokenSource();
|
||||||
var successDelayTask = UniTask.Delay(TimeSpan.FromSeconds(1));
|
// var successDelayTask = UniTask.Delay(TimeSpan.FromSeconds(1));
|
||||||
var cancelTask = UniTask.Delay(TimeSpan.FromSeconds(1), cancellationToken: cts.Token);
|
// var cancelTask = UniTask.Delay(TimeSpan.FromSeconds(1), cancellationToken: cts.Token);
|
||||||
cts.CancelAfterSlim(200);
|
// cts.CancelAfterSlim(200);
|
||||||
|
|
||||||
try
|
// try
|
||||||
{
|
// {
|
||||||
var r = await UniTask.WhenAny(new[] { successDelayTask, cancelTask });
|
// var r = await UniTask.WhenAny(new[] { successDelayTask, cancelTask });
|
||||||
}
|
// }
|
||||||
catch (Exception ex)
|
// catch (Exception ex)
|
||||||
{
|
// {
|
||||||
ex.Should().BeAssignableTo<OperationCanceledException>();
|
// ex.Should().BeAssignableTo<OperationCanceledException>();
|
||||||
}
|
// }
|
||||||
});
|
// });
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
|
||||||
#endif
|
//#endif
|
||||||
165
src/UniTask/Assets/Tests/GenericsWhenAllAny.cs
Normal file
165
src/UniTask/Assets/Tests/GenericsWhenAllAny.cs
Normal file
@@ -0,0 +1,165 @@
|
|||||||
|
#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6))
|
||||||
|
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
||||||
|
|
||||||
|
using UnityEngine;
|
||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using UnityEngine.UI;
|
||||||
|
using UnityEngine.Scripting;
|
||||||
|
using Cysharp.Threading.Tasks;
|
||||||
|
using Unity.Collections;
|
||||||
|
using System.Threading;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using UnityEngine.TestTools;
|
||||||
|
using FluentAssertions;
|
||||||
|
|
||||||
|
namespace Cysharp.Threading.TasksTests
|
||||||
|
{
|
||||||
|
public class GenericsWhenAllAny
|
||||||
|
{
|
||||||
|
|
||||||
|
[UnityTest]
|
||||||
|
public IEnumerator WhenAllT15() => UniTask.ToCoroutine(async () =>
|
||||||
|
{
|
||||||
|
var t01 = Tes<int>();
|
||||||
|
var t02 = Tes<int>();
|
||||||
|
var t03 = Tes<int>();
|
||||||
|
var t04 = Tes<int>();
|
||||||
|
var t05 = Tes<int>();
|
||||||
|
var t06 = Tes<int>();
|
||||||
|
var t07 = Tes<int>();
|
||||||
|
var t08 = Tes<int>();
|
||||||
|
var t09 = Tes<int>();
|
||||||
|
var t10 = Tes<int>();
|
||||||
|
var t11 = Tes<int>();
|
||||||
|
var t12 = Tes<int>();
|
||||||
|
var t13 = Tes<int>();
|
||||||
|
var t14 = Tes<int>();
|
||||||
|
var t15 = Tes<int>();
|
||||||
|
|
||||||
|
await UniTask.WhenAll(t01, t02, t03, t04, t05, t06, t07, t08, t09, t10, t11, t12, t13, t14, t15);
|
||||||
|
});
|
||||||
|
|
||||||
|
[UnityTest]
|
||||||
|
public IEnumerator WhenAllT01_Generics1() => UniTask.ToCoroutine(async () =>
|
||||||
|
{
|
||||||
|
var t01 = Tes<MyGenerics<int>>();
|
||||||
|
|
||||||
|
await UniTask.WhenAll(t01);
|
||||||
|
});
|
||||||
|
|
||||||
|
[UnityTest]
|
||||||
|
public IEnumerator WhenAllT02_Generics1() => UniTask.ToCoroutine(async () =>
|
||||||
|
{
|
||||||
|
var t01 = Tes<MyGenerics<int>>();
|
||||||
|
var t02 = Tes<MyGenerics<int>>();
|
||||||
|
|
||||||
|
await UniTask.WhenAll(t01, t02);
|
||||||
|
});
|
||||||
|
|
||||||
|
[UnityTest]
|
||||||
|
public IEnumerator WhenAllT03_Generics1() => UniTask.ToCoroutine(async () =>
|
||||||
|
{
|
||||||
|
var t01 = Tes<MyGenerics<int>>();
|
||||||
|
var t02 = Tes<MyGenerics<int>>();
|
||||||
|
var t03 = Tes<MyGenerics<int>>();
|
||||||
|
|
||||||
|
await UniTask.WhenAll(t01, t02, t03);
|
||||||
|
});
|
||||||
|
|
||||||
|
[UnityTest]
|
||||||
|
public IEnumerator WhenAllT04_Generics1() => UniTask.ToCoroutine(async () =>
|
||||||
|
{
|
||||||
|
var t01 = Tes<MyGenerics<int>>();
|
||||||
|
var t02 = Tes<MyGenerics<int>>();
|
||||||
|
var t03 = Tes<MyGenerics<int>>();
|
||||||
|
var t04 = Tes<MyGenerics<int>>();
|
||||||
|
|
||||||
|
await UniTask.WhenAll(t01, t02, t03, t04);
|
||||||
|
});
|
||||||
|
|
||||||
|
// will fail.
|
||||||
|
|
||||||
|
//[UnityTest]
|
||||||
|
//public IEnumerator WhenAllT05_Generics1() => UniTask.ToCoroutine(async () =>
|
||||||
|
//{
|
||||||
|
// var t01 = Tes<MyGenerics<int>>();
|
||||||
|
// var t02 = Tes<MyGenerics<int>>();
|
||||||
|
// var t03 = Tes<MyGenerics<int>>();
|
||||||
|
// var t04 = Tes<MyGenerics<int>>();
|
||||||
|
// var t05 = Tes<MyGenerics<int>>();
|
||||||
|
|
||||||
|
// await UniTask.WhenAll(t01, t02, t03, t04, t05);
|
||||||
|
//});
|
||||||
|
|
||||||
|
//[UnityTest]
|
||||||
|
//public IEnumerator WhenAllT06_Generics1() => UniTask.ToCoroutine(async () =>
|
||||||
|
//{
|
||||||
|
// var t01 = Tes<MyGenerics<int>>();
|
||||||
|
// var t02 = Tes<MyGenerics<int>>();
|
||||||
|
// var t03 = Tes<MyGenerics<int>>();
|
||||||
|
// var t04 = Tes<MyGenerics<int>>();
|
||||||
|
// var t05 = Tes<MyGenerics<int>>();
|
||||||
|
// var t06 = Tes<MyGenerics<int>>();
|
||||||
|
|
||||||
|
// await UniTask.WhenAll(t01, t02, t03, t04, t05, t06);
|
||||||
|
//});
|
||||||
|
|
||||||
|
//[UnityTest]
|
||||||
|
//public IEnumerator WhenAllT07_Generics1() => UniTask.ToCoroutine(async () =>
|
||||||
|
//{
|
||||||
|
// var t01 = Tes<MyGenerics<int>>();
|
||||||
|
// var t02 = Tes<MyGenerics<int>>();
|
||||||
|
// var t03 = Tes<MyGenerics<int>>();
|
||||||
|
// var t04 = Tes<MyGenerics<int>>();
|
||||||
|
// var t05 = Tes<MyGenerics<int>>();
|
||||||
|
// var t06 = Tes<MyGenerics<int>>();
|
||||||
|
// var t07 = Tes<MyGenerics<int>>();
|
||||||
|
|
||||||
|
// await UniTask.WhenAll(t01, t02, t03, t04, t05, t06, t07);
|
||||||
|
//});
|
||||||
|
|
||||||
|
//[UnityTest]
|
||||||
|
//public IEnumerator WhenAllT15_Generics1() => UniTask.ToCoroutine(async () =>
|
||||||
|
//{
|
||||||
|
// var t01 = Tes<MyGenerics<int>>();
|
||||||
|
// var t02 = Tes<MyGenerics<int>>();
|
||||||
|
// var t03 = Tes<MyGenerics<int>>();
|
||||||
|
// var t04 = Tes<MyGenerics<int>>();
|
||||||
|
// var t05 = Tes<MyGenerics<int>>();
|
||||||
|
// var t06 = Tes<MyGenerics<int>>();
|
||||||
|
// var t07 = Tes<MyGenerics<int>>();
|
||||||
|
// var t08 = Tes<MyGenerics<int>>();
|
||||||
|
// var t09 = Tes<MyGenerics<int>>();
|
||||||
|
// var t10 = Tes<MyGenerics<int>>();
|
||||||
|
// var t11 = Tes<MyGenerics<int>>();
|
||||||
|
// var t12 = Tes<MyGenerics<int>>();
|
||||||
|
// var t13 = Tes<MyGenerics<int>>();
|
||||||
|
// var t14 = Tes<MyGenerics<int>>();
|
||||||
|
// var t15 = Tes<MyGenerics<int>>();
|
||||||
|
|
||||||
|
// await UniTask.WhenAll(t01, t02, t03, t04, t05, t06, t07, t08, t09, t10, t11, t12, t13, t14, t15);
|
||||||
|
//});
|
||||||
|
|
||||||
|
async UniTask<T> Tes<T>()
|
||||||
|
{
|
||||||
|
await UniTask.Yield();
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class MyGenerics<T>
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public class MyGenerics2
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
11
src/UniTask/Assets/Tests/GenericsWhenAllAny.cs.meta
Normal file
11
src/UniTask/Assets/Tests/GenericsWhenAllAny.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 6ce87069a3c0ebb47b26dca280a07756
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@@ -6,13 +6,20 @@ using System.Collections;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using UnityEngine;
|
||||||
using UnityEngine.TestTools;
|
using UnityEngine.TestTools;
|
||||||
|
|
||||||
namespace Cysharp.Threading.TasksTests
|
namespace Cysharp.Threading.TasksTests
|
||||||
{
|
{
|
||||||
public class Preserve
|
public class Preserve
|
||||||
{
|
{
|
||||||
|
public Preserve()
|
||||||
|
{
|
||||||
|
// TaskPool.SetMaxPoolSize(0);
|
||||||
|
}
|
||||||
|
|
||||||
[UnityTest]
|
[UnityTest]
|
||||||
public IEnumerator AwaitTwice() => UniTask.ToCoroutine(async () =>
|
public IEnumerator AwaitTwice() => UniTask.ToCoroutine(async () =>
|
||||||
{
|
{
|
||||||
@@ -33,12 +40,17 @@ namespace Cysharp.Threading.TasksTests
|
|||||||
[UnityTest]
|
[UnityTest]
|
||||||
public IEnumerator PreserveAllowTwice() => UniTask.ToCoroutine(async () =>
|
public IEnumerator PreserveAllowTwice() => UniTask.ToCoroutine(async () =>
|
||||||
{
|
{
|
||||||
|
await UniTask.Yield(PlayerLoopTiming.Update);
|
||||||
|
|
||||||
var delay = UniTask.DelayFrame(5, PlayerLoopTiming.PostLateUpdate).Preserve();
|
var delay = UniTask.DelayFrame(5, PlayerLoopTiming.PostLateUpdate).Preserve();
|
||||||
var before = UnityEngine.Time.frameCount;
|
|
||||||
|
var before = UnityEngine.Time.frameCount; // 0
|
||||||
|
|
||||||
await delay;
|
await delay;
|
||||||
var afterOne = UnityEngine.Time.frameCount;
|
var afterOne = UnityEngine.Time.frameCount; // 5
|
||||||
|
|
||||||
await delay;
|
await delay;
|
||||||
var afterTwo = UnityEngine.Time.frameCount;
|
var afterTwo = UnityEngine.Time.frameCount; // 5
|
||||||
|
|
||||||
(afterOne - before).Should().Be(5);
|
(afterOne - before).Should().Be(5);
|
||||||
afterOne.Should().Be(afterTwo);
|
afterOne.Should().Be(afterTwo);
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user