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)); }
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); } }
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); } }
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); } }
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)); }
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()); }
void SourceMoveNext() { try { awaiter = enumerator.MoveNextAsync().GetAwaiter(); if (awaiter.IsCompleted) { MoveNextCore(this); } else { awaiter.SourceOnCompleted(MoveNextCoreDelegate, this); } } catch (Exception ex) { completionSource.TrySetException(ex); } }
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; } }
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); }
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(); } }
void MoveNextSelected() { try { selectedAwaiter = selectedEnumerator.MoveNextAsync().GetAwaiter(); } catch (Exception ex) { completionSource.TrySetException(ex); return; } if (selectedAwaiter.IsCompleted) { SeletedSourceMoveNextCore(this); } else { selectedAwaiter.SourceOnCompleted(selectedSourceMoveNextCoreDelegate, this); } }
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); } }
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); }
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); } }
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()); }