| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using System; |
| | 3 | | using System.Threading; |
| | 4 | |
|
| | 5 | | namespace DCL.Helpers |
| | 6 | | { |
| | 7 | | public static class PromiseAsyncExtensions |
| | 8 | | { |
| | 9 | | public static UniTask<T>.Awaiter GetAwaiter<T>(this Promise<T> promise) => |
| | 10 | | promise.ToUniTask().GetAwaiter(); |
| | 11 | |
|
| | 12 | | public static UniTask<T> WithCancellation<T>(this Promise<T> promise, CancellationToken cancellationToken) => |
| | 13 | | promise.ToUniTask(cancellationToken); |
| | 14 | |
|
| | 15 | | public static UniTask<T> ToUniTask<T>(this Promise<T> promise, CancellationToken cancellationToken = default) |
| | 16 | | { |
| | 17 | | if (promise == null) |
| | 18 | | throw new ArgumentNullException(nameof(promise)); |
| | 19 | |
|
| | 20 | | if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled<T>(cancellationToken); |
| | 21 | | if (promise.failed) return UniTask.FromException<T>(promise.exception); |
| | 22 | | if (promise.resolved) return UniTask.FromResult(promise.value); |
| | 23 | |
|
| | 24 | | return new UniTask<T>(PromiseCompletionSource<T>.Create(promise, cancellationToken, out short token), token) |
| | 25 | | } |
| | 26 | |
|
| | 27 | | internal sealed class PromiseCompletionSource<T> : IUniTaskSource<T>, ITaskPoolNode<PromiseCompletionSource<T>> |
| | 28 | | { |
| | 29 | | private static TaskPool<PromiseCompletionSource<T>> pool; |
| | 30 | |
|
| | 31 | | private PromiseCompletionSource<T> nextNode; |
| | 32 | |
|
| 27 | 33 | | public ref PromiseCompletionSource<T> NextNode => ref nextNode; |
| | 34 | |
|
| | 35 | | private Promise<T> innerPromise; |
| | 36 | | private CancellationToken cancellationToken; |
| | 37 | | private CancellationTokenRegistration cancellationTokenRegistration; |
| | 38 | |
|
| | 39 | | private UniTaskCompletionSourceCore<T> core; |
| | 40 | |
|
| | 41 | | static PromiseCompletionSource() |
| | 42 | | { |
| 2 | 43 | | TaskPool.RegisterSizeGetter(typeof(PromiseCompletionSource<T>), () => pool.Size); |
| 2 | 44 | | } |
| | 45 | |
|
| 14 | 46 | | private PromiseCompletionSource() { } |
| | 47 | |
|
| | 48 | | private void SetData(Promise<T> innerPromise, CancellationToken cancellationToken) |
| | 49 | | { |
| 17 | 50 | | this.innerPromise = innerPromise; |
| 17 | 51 | | this.cancellationToken = cancellationToken; |
| | 52 | |
|
| | 53 | | // Subscribe directly to `Promise` instead of calling `MoveNext` to prevent skipping an extra frame |
| 17 | 54 | | innerPromise.onSuccess += SetResult; |
| 17 | 55 | | innerPromise.onError += SetException; |
| | 56 | |
|
| 17 | 57 | | cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(SetCancelled); |
| 17 | 58 | | } |
| | 59 | |
|
| | 60 | | private void SetCancelled() |
| | 61 | | { |
| 16 | 62 | | UnsubscribeBeforeReset(); |
| 16 | 63 | | core.TrySetCanceled(cancellationToken); |
| 16 | 64 | | } |
| | 65 | |
|
| | 66 | | private void SetResult(T result) |
| | 67 | | { |
| 1 | 68 | | UnsubscribeBeforeReset(); |
| 1 | 69 | | core.TrySetResult(result); |
| 1 | 70 | | } |
| | 71 | |
|
| | 72 | | private void SetException(string _) |
| | 73 | | { |
| 0 | 74 | | UnsubscribeBeforeReset(); |
| 0 | 75 | | core.TrySetException(innerPromise.exception); |
| 0 | 76 | | } |
| | 77 | |
|
| | 78 | | private void UnsubscribeBeforeReset() |
| | 79 | | { |
| 17 | 80 | | cancellationTokenRegistration.Dispose(); |
| 17 | 81 | | innerPromise.onSuccess -= SetResult; |
| 17 | 82 | | innerPromise.onError -= SetException; |
| 17 | 83 | | } |
| | 84 | |
|
| | 85 | | public static IUniTaskSource<T> Create(Promise<T> promise, CancellationToken cancellationToken, out short to |
| | 86 | | { |
| 17 | 87 | | if (cancellationToken.IsCancellationRequested) { return AutoResetUniTaskCompletionSource<T>.CreateFromCa |
| | 88 | |
|
| 24 | 89 | | if (!pool.TryPop(out var result)) result = new PromiseCompletionSource<T>(); |
| | 90 | |
|
| 17 | 91 | | result.SetData(promise, cancellationToken); |
| | 92 | |
|
| 17 | 93 | | TaskTracker.TrackActiveTask(result, 3); |
| | 94 | |
|
| 17 | 95 | | token = result.core.Version; |
| | 96 | |
|
| 17 | 97 | | return result; |
| | 98 | | } |
| | 99 | |
|
| | 100 | | public UniTaskStatus GetStatus(short token) => |
| 18 | 101 | | core.GetStatus(token); |
| | 102 | |
|
| | 103 | | public void OnCompleted(Action<object> continuation, object state, short token) |
| | 104 | | { |
| 17 | 105 | | core.OnCompleted(continuation, state, token); |
| 17 | 106 | | } |
| | 107 | |
|
| | 108 | | public T GetResult(short token) |
| | 109 | | { |
| 17 | 110 | | try { return core.GetResult(token); } |
| 34 | 111 | | finally { TryReturn(); } |
| 1 | 112 | | } |
| | 113 | |
|
| | 114 | | void IUniTaskSource.GetResult(short token) |
| | 115 | | { |
| 1 | 116 | | GetResult(token); |
| 1 | 117 | | } |
| | 118 | |
|
| | 119 | | public UniTaskStatus UnsafeGetStatus() => |
| 0 | 120 | | core.UnsafeGetStatus(); |
| | 121 | |
|
| | 122 | | private bool TryReturn() |
| | 123 | | { |
| 17 | 124 | | TaskTracker.RemoveTracking(this); |
| 17 | 125 | | core.Reset(); |
| 17 | 126 | | innerPromise = null; |
| 17 | 127 | | cancellationToken = default; |
| 17 | 128 | | return pool.TryPush(this); |
| | 129 | | } |
| | 130 | | } |
| | 131 | | } |
| | 132 | | } |