public async Task <IEnumerator <Tuple> > GetEnumeratorAsync(EnumerationContext context, CancellationToken token) { const string enumerationMarker = "Enumerated"; var enumerated = context.GetValue <bool>(this, enumerationMarker); bool onEnumerationExecuted = false; if (!enumerated) { OnBeforeEnumerate(context); } try { context.SetValue(this, enumerationMarker, true); var enumerator = (await OnEnumerateAsync(context, token).ConfigureAwait(false)) .ToEnumerator( () => { if (!enumerated) { OnAfterEnumerate(context); } }); onEnumerationExecuted = true; return(enumerator); } finally { if (!enumerated && !onEnumerationExecuted) { OnAfterEnumerate(context); } } }
private async ValueTask Prepare(bool executeAsync) { enumerationScope = context.BeginEnumeration(); enumerated = context.GetValue <bool>(provider, enumerationMarker); if (!enumerated) { provider.OnBeforeEnumerate(context); context.SetValue(provider, enumerationMarker, true); } try { dataReader = executeAsync ? await provider.OnEnumerateAsync(context, token).ConfigureAwait(false) : provider.OnEnumerate(context); if (isGreedy && !dataReader.IsInMemory) { var tuples = new List <Tuple>(); if (executeAsync) { await using (dataReader.ConfigureAwait(false)) { while (await dataReader.MoveNextAsync().ConfigureAwait(false)) { tuples.Add(dataReader.Current); } } } else { using (dataReader) { while (dataReader.MoveNext()) { tuples.Add(dataReader.Current); } } } dataReader = new DataReader(tuples); } } catch { FinishEnumeration(true); throw; } state = State.Prepared; }
protected void SetValue <T>(EnumerationContext context, string name, T value) where T : class { context.EnsureIsActive(); context.SetValue(this, name, value); }
/// <summary> /// Puts specified <paramref name="value"/> into the cache residing in the provided /// <see cref="EnumerationContext"/> instance using the <paramref name="name"/> as the key. /// </summary> /// <param name="context">The <see cref="EnumerationContext"/> where to cache <paramref name="value"/>.</param> /// <param name="name">The name of the <paramref name="value"/> to be cached.</param> /// <param name="value">The value of <typeparamref name="T"/> type to be cached.</param> /// <typeparam name="T">The type of the provided <paramref name="value"/>.</typeparam> protected void SetValue <T>(EnumerationContext context, string name, T value) where T : class => context.SetValue(this, name, value);