// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.IO;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Microsoft.AspNet.Html;
namespace Microsoft.AspNet.Mvc.Razor
{
///
/// Represents a deferred write operation in a .
///
public class HelperResult : IHtmlContent
{
private readonly Func _asyncAction;
///
/// Creates a new instance of .
///
/// The asynchronous delegate to invoke when
/// is called.
/// Calls to result in a blocking invocation of
/// .
public HelperResult(Func asyncAction)
{
if (asyncAction == null)
{
throw new ArgumentNullException(nameof(asyncAction));
}
_asyncAction = asyncAction;
}
///
/// Gets the asynchronous delegate to invoke when is called.
///
public Func WriteAction
{
get { return _asyncAction; }
}
///
/// Method invoked to produce content from the .
///
/// The instance to write to.
/// The to encode the content.
public virtual void WriteTo(TextWriter writer, HtmlEncoder encoder)
{
if (writer == null)
{
throw new ArgumentNullException(nameof(writer));
}
if (encoder == null)
{
throw new ArgumentNullException(nameof(encoder));
}
_asyncAction(writer).GetAwaiter().GetResult();
}
}
}