예제 #1
0
            public UniTask <bool> MoveNextAsync()
            {
                cancellationToken.ThrowIfCancellationRequested();
                completionSource.Reset();

                if (enumerator == null)
                {
                    if (state == State.RequireAppend)
                    {
                        Current = element;
                        state   = State.None;
                        return(CompletedTasks.True);
                    }

                    enumerator = source.GetAsyncEnumerator(cancellationToken);
                }

                if (state == State.Completed)
                {
                    return(CompletedTasks.False);
                }

                awaiter = enumerator.MoveNextAsync().GetAwaiter();

                if (awaiter.IsCompleted)
                {
                    MoveNextCoreDelegate(this);
                }
                else
                {
                    awaiter.SourceOnCompleted(MoveNextCoreDelegate, this);
                }

                return(new UniTask <bool>(this, completionSource.Version));
            }
예제 #2
0
            void SourceMoveNext()
            {
                try
                {
LOOP:
                    awaiter = enumerator.MoveNextAsync().GetAwaiter();
                    if (awaiter.IsCompleted)
                    {
                        continueNext = true;
                        MoveNextCore(this);
                        if (continueNext)
                        {
                            continueNext = false;
                            goto LOOP;
                        }
                    }
                    else
                    {
                        awaiter.SourceOnCompleted(MoveNextCoreDelegate, this);
                    }
                }
                catch (Exception ex)
                {
                    completionSource.TrySetException(ex);
                }
            }
예제 #3
0
        protected void SourceMoveNext()
        {
CONTINUE:
            sourceMoveNext = enumerator.MoveNextAsync().GetAwaiter();
            if (sourceMoveNext.IsCompleted)
            {
                bool result = false;
                try
                {
                    if (!TryMoveNextCore(sourceMoveNext.GetResult(), out result))
                    {
                        goto CONTINUE;
                    }
                }
                catch (Exception ex)
                {
                    completionSource.TrySetException(ex);
                    return;
                }

                if (cancellationToken.IsCancellationRequested)
                {
                    completionSource.TrySetCanceled(cancellationToken);
                }
                else
                {
                    completionSource.TrySetResult(result);
                }
            }
            else
            {
                sourceMoveNext.SourceOnCompleted(moveNextCallbackDelegate, this);
            }
        }
예제 #4
0
            void StartIterate()
            {
                if (enumerator == null)
                {
                    if (iteratingState == IteratingState.IteratingFirst)
                    {
                        enumerator = first.GetAsyncEnumerator(cancellationToken);
                    }
                    else if (iteratingState == IteratingState.IteratingSecond)
                    {
                        enumerator = second.GetAsyncEnumerator(cancellationToken);
                    }
                }

                try {
                    awaiter = enumerator.MoveNextAsync().GetAwaiter();
                }
                catch (Exception ex) {
                    completionSource.TrySetException(ex);
                    return;
                }

                if (awaiter.IsCompleted)
                {
                    MoveNextCoreDelegate(this);
                }
                else
                {
                    awaiter.SourceOnCompleted(MoveNextCoreDelegate, this);
                }
            }
예제 #5
0
            public UniTask <bool> MoveNextAsync()
            {
                cancellationToken.ThrowIfCancellationRequested();
                completionSource.Reset();

                bool isCompleted = false;

                try {
                    if (enumerator == null)
                    {
                        enumerator = source.GetAsyncEnumerator(cancellationToken);
                    }

                    awaiter     = enumerator.MoveNextAsync().GetAwaiter();
                    isCompleted = awaiter.IsCompleted;
                }
                catch (Exception ex) {
                    CallTrySetExceptionAfterNotification(ex);
                    return(new UniTask <bool>(this, completionSource.Version));
                }

                if (isCompleted)
                {
                    MoveNextCore(this);
                }
                else
                {
                    awaiter.SourceOnCompleted(MoveNextCoreDelegate, this);
                }

                return(new UniTask <bool>(this, completionSource.Version));
            }
예제 #6
0
            public UniTask <bool> MoveNextAsync()
            {
                cancellationToken.ThrowIfCancellationRequested();

                if (sourceEnumerator == null)
                {
                    sourceEnumerator = source.GetAsyncEnumerator(cancellationToken);
                    channel          = Channel.CreateSingleConsumerUnbounded <TSource>();

                    channelEnumerator = channel.Reader.ReadAllAsync().GetAsyncEnumerator(cancellationToken);

                    ConsumeAll(this, sourceEnumerator, channel).Forget();
                }

                return(channelEnumerator.MoveNextAsync());
            }
예제 #7
0
 void SourceMoveNext()
 {
     try {
         awaiter = enumerator.MoveNextAsync().GetAwaiter();
         if (awaiter.IsCompleted)
         {
             MoveNextCore(this);
         }
         else
         {
             awaiter.SourceOnCompleted(MoveNextCoreDelegate, this);
         }
     }
     catch (Exception ex) {
         completionSource.TrySetException(ex);
     }
 }
예제 #8
0
        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;
            }
        }
예제 #9
0
            void SourceMoveNext()
            {
                try
                {
LOOP:
                    if (valueEnumerator != null)
                    {
                        if (valueEnumerator.MoveNext())
                        {
                            Current = resultSelector(currentOuterValue, valueEnumerator.Current);
                            goto TRY_SET_RESULT_TRUE;
                        }
                        else
                        {
                            valueEnumerator.Dispose();
                            valueEnumerator = null;
                        }
                    }

                    awaiter = enumerator.MoveNextAsync().GetAwaiter();
                    if (awaiter.IsCompleted)
                    {
                        continueNext = true;
                        MoveNextCore(this);
                        if (continueNext)
                        {
                            continueNext = false;
                            goto LOOP; // avoid recursive
                        }
                    }
                    else
                    {
                        awaiter.SourceOnCompleted(MoveNextCoreDelegate, this);
                    }
                }
                catch (Exception ex)
                {
                    completionSource.TrySetException(ex);
                }

                return;

TRY_SET_RESULT_TRUE:
                completionSource.TrySetResult(true);
            }
예제 #10
0
            static async UniTaskVoid ConsumeAll(_Queue self, IUniTaskAsyncEnumerator <TSource> enumerator,
                                                ChannelWriter <TSource> writer)
            {
                try {
                    while (await enumerator.MoveNextAsync())
                    {
                        writer.TryWrite(enumerator.Current);
                    }

                    writer.TryComplete();
                }
                catch (Exception ex) {
                    writer.TryComplete(ex);
                }
                finally {
                    self.channelClosed = true;
                    await enumerator.DisposeAsync();
                }
            }
예제 #11
0
            void MoveNextSelected()
            {
                try {
                    selectedAwaiter = selectedEnumerator.MoveNextAsync().GetAwaiter();
                }
                catch (Exception ex) {
                    completionSource.TrySetException(ex);
                    return;
                }

                if (selectedAwaiter.IsCompleted)
                {
                    SeletedSourceMoveNextCore(this);
                }
                else
                {
                    selectedAwaiter.SourceOnCompleted(selectedSourceMoveNextCoreDelegate, this);
                }
            }
예제 #12
0
            void SourceMoveNext()
            {
                if (completed)
                {
                    if (buffer != null && buffer.Count > 0)
                    {
                        var ret = buffer;
                        buffer  = null;
                        Current = ret;
                        completionSource.TrySetResult(true);
                        return;
                    }
                    else
                    {
                        completionSource.TrySetResult(false);
                        return;
                    }
                }

                try
                {
LOOP:
                    awaiter = enumerator.MoveNextAsync().GetAwaiter();
                    if (awaiter.IsCompleted)
                    {
                        continueNext = true;
                        MoveNextCore(this);
                        if (continueNext)
                        {
                            continueNext = false;
                            goto LOOP; // avoid recursive
                        }
                    }
                    else
                    {
                        awaiter.SourceOnCompleted(MoveNextCoreDelegate, this);
                    }
                }
                catch (Exception ex)
                {
                    completionSource.TrySetException(ex);
                }
            }
예제 #13
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);
        }
예제 #14
0
            void SourceMoveNext()
            {
                if (iterateCompleted)
                {
                    if (queue.Count > 0)
                    {
                        Current = queue.Dequeue();
                        completionSource.TrySetResult(true);
                    }
                    else
                    {
                        completionSource.TrySetResult(false);
                    }

                    return;
                }

                try
                {
LOOP:
                    awaiter = enumerator.MoveNextAsync().GetAwaiter();
                    if (awaiter.IsCompleted)
                    {
                        continueNext = true;
                        MoveNextCore(this);
                        if (continueNext)
                        {
                            continueNext = false;
                            goto LOOP; // avoid recursive
                        }
                    }
                    else
                    {
                        awaiter.SourceOnCompleted(MoveNextCoreDelegate, this);
                    }
                }
                catch (Exception ex)
                {
                    completionSource.TrySetException(ex);
                }
            }
예제 #15
0
        ToLookupAwaitWithCancellationAsync <TSource, TKey, TElement>(IUniTaskAsyncEnumerable <TSource> source,
                                                                     Func <TSource, CancellationToken, UniTask <TKey> > keySelector,
                                                                     Func <TSource, CancellationToken, UniTask <TElement> > elementSelector, IEqualityComparer <TKey> comparer,
                                                                     CancellationToken cancellationToken)
        {
            var pool  = ArrayPool <TSource> .Shared;
            var array = pool.Rent(16);

            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)
                {
                    return(Lookup <TKey, TElement> .CreateEmpty());
                }
                else
                {
                    return(await Lookup <TKey, TElement> .CreateAsync(new ArraySegment <TSource>(array, 0, i), keySelector,
                                                                      elementSelector, comparer, cancellationToken));
                }
            }
            finally {
                pool.Return(array,
                            clearArray: !RuntimeHelpersAbstraction.IsWellKnownNoReferenceContainsType <TSource>());

                if (e != null)
                {
                    await e.DisposeAsync();
                }
            }
        }
 public UniTask <bool> MoveNextAsync()
 {
     return(enumerator.MoveNextAsync());
 }