// 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.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
namespace System.Linq
{
///
/// Provides functionality to evaluate an expression tree representation of a computation over asynchronous enumerable sequences.
///
/// The type of the elements in the sequence.
internal class AsyncEnumerableExecutor
{
private readonly Expression _expression;
private Func> _func;
///
/// Creates a new execution helper instance for the specified expression tree representing a computation over asynchronous enumerable sequences.
///
/// Expression tree representing a computation over asynchronous enumerable sequences.
public AsyncEnumerableExecutor(Expression expression)
{
_expression = expression;
}
///
/// Evaluated the expression tree.
///
/// Token to cancel the evaluation.
/// Task representing the evaluation of the expression tree.
internal Task ExecuteAsync(CancellationToken token)
{
if (_func == null)
{
var expression = Expression.Lambda>>(new AsyncEnumerableRewriter().Visit(_expression), Expression.Parameter(typeof(CancellationToken)));
_func = expression.Compile();
}
return _func(token);
}
}
}