| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 | // 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.Diagnostics;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);        }        public static IAsyncEnumerable<TSource> Using<TSource, TResource>(Func<Task<TResource>> resourceFactory, Func<TResource, Task<IAsyncEnumerable<TSource>>> enumerableFactory) where TResource : IDisposable        {            if (resourceFactory == null)                throw new ArgumentNullException(nameof(resourceFactory));            if (enumerableFactory == null)                throw new ArgumentNullException(nameof(enumerableFactory));            return new UsingAsyncIteratorWithTask<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)            {                Debug.Assert(resourceFactory != null);                Debug.Assert(enumerableFactory != null);                this.resourceFactory = resourceFactory;                this.enumerableFactory = enumerableFactory;            }            public override AsyncIterator<TSource> Clone()            {                return new UsingAsyncIterator<TSource, TResource>(resourceFactory, enumerableFactory);            }            public override async Task DisposeAsync()            {                if (enumerator != null)                {                    await enumerator.DisposeAsync().ConfigureAwait(false);                    enumerator = null;                }                if (resource != null)                {                    resource.Dispose();                    resource = default(TResource);                }                await base.DisposeAsync().ConfigureAwait(false);            }            protected override async Task<bool> MoveNextCore()            {                switch (state)                {                    case AsyncIteratorState.Allocated:                        enumerator = enumerable.GetAsyncEnumerator();                        state = AsyncIteratorState.Iterating;                        goto case AsyncIteratorState.Iterating;                    case AsyncIteratorState.Iterating:                        while (await enumerator.MoveNextAsync().ConfigureAwait(false))                        {                            current = enumerator.Current;                            return true;                        }                        await DisposeAsync().ConfigureAwait(false);                        break;                }                return false;            }            protected override void OnGetEnumerator()            {                resource = resourceFactory();                enumerable = enumerableFactory(resource);                base.OnGetEnumerator();            }        }        private sealed class UsingAsyncIteratorWithTask<TSource, TResource> : AsyncIterator<TSource> where TResource : IDisposable        {            private readonly Func<TResource, Task<IAsyncEnumerable<TSource>>> enumerableFactory;            private readonly Func<Task<TResource>> resourceFactory;            private IAsyncEnumerable<TSource> enumerable;            private IAsyncEnumerator<TSource> enumerator;            private TResource resource;            public UsingAsyncIteratorWithTask(Func<Task<TResource>> resourceFactory, Func<TResource, Task<IAsyncEnumerable<TSource>>> enumerableFactory)            {                Debug.Assert(resourceFactory != null);                Debug.Assert(enumerableFactory != null);                this.resourceFactory = resourceFactory;                this.enumerableFactory = enumerableFactory;            }            public override AsyncIterator<TSource> Clone()            {                return new UsingAsyncIteratorWithTask<TSource, TResource>(resourceFactory, enumerableFactory);            }            public override async Task DisposeAsync()            {                if (enumerator != null)                {                    await enumerator.DisposeAsync().ConfigureAwait(false);                    enumerator = null;                }                if (resource != null)                {                    resource.Dispose();                    resource = default(TResource);                }                await base.DisposeAsync().ConfigureAwait(false);            }            protected override async Task<bool> MoveNextCore()            {                switch (state)                {                    case AsyncIteratorState.Allocated:                        resource = await resourceFactory().ConfigureAwait(false);                        enumerable = await enumerableFactory(resource).ConfigureAwait(false);                        enumerator = enumerable.GetAsyncEnumerator();                        state = AsyncIteratorState.Iterating;                        goto case AsyncIteratorState.Iterating;                    case AsyncIteratorState.Iterating:                        while (await enumerator.MoveNextAsync().ConfigureAwait(false))                        {                            current = enumerator.Current;                            return true;                        }                        await DisposeAsync().ConfigureAwait(false);                        break;                }                return false;            }        }    }}
 |