public static UniTask WhenAll(IEnumerable <UniTask> tasks)
 {
     using (var span = ArrayPoolUtil.Materialize(tasks)) {
         var promise = new WhenAllPromise(span.Array, span.Length); // consumed array in constructor.
         return(new UniTask(promise, 0));
     }
 }
Ejemplo n.º 2
0
        internal static async UniTask <ILookup <TKey, TSource> > ToLookupAsync <TSource, TKey>(IUniTaskAsyncEnumerable <TSource> source, Func <TSource, TKey> keySelector, IEqualityComparer <TKey> comparer, CancellationToken cancellationToken)
        {
            var pool  = ArrayPool <TSource> .Shared;
            var array = pool.Rent(16);

            var e = source.GetAsyncEnumerator(cancellationToken);

            try
            {
                var i = 0;
                while (await e.MoveNextAsync())
                {
                    ArrayPoolUtil.EnsureCapacity(ref array, i, pool);
                    array[i++] = e.Current;
                }

                if (i == 0)
                {
                    return(Lookup <TKey, TSource> .CreateEmpty());
                }
                else
                {
                    return(Lookup <TKey, TSource> .Create(new ArraySegment <TSource>(array, 0, i), keySelector, comparer));
                }
            }
            finally
            {
                pool.Return(array, clearArray: !RuntimeHelpersAbstraction.IsWellKnownNoReferenceContainsType <TSource>());

                if (e != null)
                {
                    await e.DisposeAsync();
                }
            }
        }
Ejemplo n.º 3
0
		public static async UniTask WhenAll(IEnumerable<UniTask> tasks) {
			WhenAllPromise promise;

			using (var span = ArrayPoolUtil.Materialize(tasks)) {
				promise = new WhenAllPromise(span.Array, span.Length);
			}

			await promise;
		}
Ejemplo n.º 4
0
		public static async UniTask<T[]> WhenAll<T>(IEnumerable<UniTask<T>> tasks) {
			WhenAllPromise<T> promise;

			using (var span = ArrayPoolUtil.Materialize(tasks)) {
				promise = new WhenAllPromise<T>(span.Array, span.Length);
			}

			return await promise;
		}
Ejemplo n.º 5
0
        protected UniTask <T> GetOrAddPromise <T>(ref AsyncTriggerPromise <T> promise, ref AsyncTriggerPromiseDictionary <T> promises, CancellationToken cancellationToken)
        {
            if (destroyCalled)
            {
                return(UniTask.FromCanceled <T>());
            }

            if (!cancellationToken.CanBeCanceled)
            {
                if (promise == null)
                {
                    promise = new AsyncTriggerPromise <T>();
                    if (!calledAwake)
                    {
                        PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, new AwakeMonitor(this));
                    }
                }

                return(promise.Task);
            }

            if (promises == null)
            {
                promises = new AsyncTriggerPromiseDictionary <T>();
            }

            if (promises.TryGetValue(cancellationToken, out var cancellablePromise))
            {
                return(cancellablePromise.Task);
            }

            cancellablePromise = new AsyncTriggerPromise <T>();
            promises.Add(cancellationToken, cancellablePromise);
            if (!calledAwake)
            {
                PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, new AwakeMonitor(this));
            }

            var registrationToken =
                cancellationToken.RegisterWithoutCaptureExecutionContext(Callback,
                                                                         Tuple.Create((ICancellationTokenKeyDictionary)promises, (ICancelablePromise)cancellablePromise));

            if (registeredCancellations == null)
            {
                registeredCancellations = ArrayPool <CancellationTokenRegistration> .Shared.Rent(4);
            }
            ArrayPoolUtil.EnsureCapacity(ref registeredCancellations, registeredCancellationsCount + 1, ArrayPool <CancellationTokenRegistration> .Shared);
            registeredCancellations[registeredCancellationsCount++] = registrationToken;

            return(cancellablePromise.Task);
        }
Ejemplo n.º 6
0
        internal static async UniTask <TSource[]> ToArrayAsync <TSource>(IUniTaskAsyncEnumerable <TSource> source, CancellationToken cancellationToken)
        {
            // UnityEngine.Debug.Log("Called ToArray");

            var pool  = ArrayPool <TSource> .Shared;
            var array = pool.Rent(16);

            TSource[] result = default;
            IUniTaskAsyncEnumerator <TSource> e = default;

            try
            {
                e = source.GetAsyncEnumerator(cancellationToken);
                var i = 0;
                while (await e.MoveNextAsync())
                {
                    ArrayPoolUtil.EnsureCapacity(ref array, i, pool);
                    array[i++] = e.Current;
                }

                if (i == 0)
                {
                    result = Array.Empty <TSource>();
                }
                else
                {
                    result = new TSource[i];
                    Array.Copy(array, result, i);
                }
            }
            finally
            {
                pool.Return(array, clearArray: !RuntimeHelpersAbstraction.IsWellKnownNoReferenceContainsType <TSource>());

                if (e != null)
                {
                    await e.DisposeAsync();
                }
            }

            return(result);
        }