|
@@ -1,96 +0,0 @@
|
|
|
-// Licensed to the .NET Foundation under one or more agreements.
|
|
|
-// The .NET Foundation licenses this file to you under the Apache 2.0 License.
|
|
|
-// See the LICENSE file in the project root for more information.
|
|
|
-
|
|
|
-using System.Collections.Generic;
|
|
|
-using System.Threading;
|
|
|
-using System.Threading.Tasks;
|
|
|
-
|
|
|
-namespace System.Linq
|
|
|
-{
|
|
|
- internal abstract class AsyncIterator<TSource> : IAsyncEnumerable<TSource>, IAsyncEnumerator<TSource>
|
|
|
- {
|
|
|
- private readonly int _threadId;
|
|
|
-
|
|
|
- private bool _currentIsInvalid = true;
|
|
|
-
|
|
|
- internal TSource current;
|
|
|
- internal AsyncIteratorState state = AsyncIteratorState.New;
|
|
|
- internal CancellationToken cancellationToken;
|
|
|
-
|
|
|
- protected AsyncIterator()
|
|
|
- {
|
|
|
- _threadId = Environment.CurrentManagedThreadId;
|
|
|
- }
|
|
|
-
|
|
|
- public IAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken)
|
|
|
- {
|
|
|
- var enumerator = state == AsyncIteratorState.New && _threadId == Environment.CurrentManagedThreadId
|
|
|
- ? this
|
|
|
- : Clone();
|
|
|
-
|
|
|
- enumerator.state = AsyncIteratorState.Allocated;
|
|
|
- enumerator.cancellationToken = cancellationToken;
|
|
|
-
|
|
|
- return enumerator;
|
|
|
- }
|
|
|
-
|
|
|
- public virtual ValueTask DisposeAsync()
|
|
|
- {
|
|
|
- current = default;
|
|
|
- state = AsyncIteratorState.Disposed;
|
|
|
-
|
|
|
- return TaskExt.CompletedTask;
|
|
|
- }
|
|
|
-
|
|
|
- public TSource Current
|
|
|
- {
|
|
|
- get
|
|
|
- {
|
|
|
- if (_currentIsInvalid)
|
|
|
- throw new InvalidOperationException("Enumerator is in an invalid state");
|
|
|
-
|
|
|
- return current;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public async ValueTask<bool> MoveNextAsync()
|
|
|
- {
|
|
|
- // Note: MoveNext *must* be implemented as an async method to ensure
|
|
|
- // that any exceptions thrown from the MoveNextCore call are handled
|
|
|
- // by the try/catch, whether they're sync or async
|
|
|
-
|
|
|
- if (state == AsyncIteratorState.Disposed)
|
|
|
- {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- try
|
|
|
- {
|
|
|
- var result = await MoveNextCore(cancellationToken).ConfigureAwait(false);
|
|
|
-
|
|
|
- _currentIsInvalid = !result; // if move next is false, invalid otherwise valid
|
|
|
-
|
|
|
- return result;
|
|
|
- }
|
|
|
- catch
|
|
|
- {
|
|
|
- _currentIsInvalid = true;
|
|
|
- await DisposeAsync().ConfigureAwait(false);
|
|
|
- throw;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public abstract AsyncIterator<TSource> Clone();
|
|
|
-
|
|
|
- protected abstract ValueTask<bool> MoveNextCore(CancellationToken cancellationToken);
|
|
|
- }
|
|
|
-
|
|
|
- internal enum AsyncIteratorState
|
|
|
- {
|
|
|
- New = 0,
|
|
|
- Allocated = 1,
|
|
|
- Iterating = 2,
|
|
|
- Disposed = -1,
|
|
|
- }
|
|
|
-}
|