| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 | // 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;using System.Collections.Generic;using System.Linq;using System.Threading;using System.Threading.Tasks;namespace System.Linq{    public static partial class AsyncEnumerable    {        public static IAsyncEnumerable<TSource> Using<TSource, TResource>(Func<TResource> resourceFactory, Func<TResource, IAsyncEnumerable<TSource>> enumerableFactory) where TResource : IDisposable        {            if (resourceFactory == null)                throw new ArgumentNullException(nameof(resourceFactory));            if (enumerableFactory == null)                throw new ArgumentNullException(nameof(enumerableFactory));            return new UsingAsyncIterator<TSource, TResource>(resourceFactory, enumerableFactory);        }        private sealed class UsingAsyncIterator<TSource, TResource> : AsyncIterator<TSource> where TResource : IDisposable        {            private readonly Func<TResource, IAsyncEnumerable<TSource>> enumerableFactory;            private readonly Func<TResource> resourceFactory;            private IAsyncEnumerable<TSource> enumerable;            private IAsyncEnumerator<TSource> enumerator;            private TResource resource;            public UsingAsyncIterator(Func<TResource> resourceFactory, Func<TResource, IAsyncEnumerable<TSource>> enumerableFactory)            {                this.resourceFactory = resourceFactory;                this.enumerableFactory = enumerableFactory;            }            public override AsyncIterator<TSource> Clone()            {                return new UsingAsyncIterator<TSource, TResource>(resourceFactory, enumerableFactory);            }            public override void Dispose()            {                if (enumerator != null)                {                    enumerator.Dispose();                    enumerator = null;                }                if (resource != null)                {                    resource.Dispose();                    resource = default(TResource);                }                base.Dispose();            }            protected override async Task<bool> MoveNextCore(CancellationToken cancellationToken)            {                switch (state)                {                    case AsyncIteratorState.Allocated:                        enumerator = enumerable.GetEnumerator();                        state = AsyncIteratorState.Iterating;                        goto case AsyncIteratorState.Iterating;                    case AsyncIteratorState.Iterating:                        while (await enumerator.MoveNext(cancellationToken)                                               .ConfigureAwait(false))                        {                            current = enumerator.Current;                            return true;                        }                        Dispose();                        break;                }                return false;            }            protected override void OnGetEnumerator()            {                resource = resourceFactory();                enumerable = enumerableFactory(resource);                base.OnGetEnumerator();            }        }    }}
 |