Return completed task rather than null for empty virtual async methods.

This commit is contained in:
Jeremy Hutchinson 2018-10-18 11:56:44 -04:00 committed by Pranav K
parent 5c13452d96
commit 1661d38971
1 changed files with 16 additions and 12 deletions

View File

@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNetCore.Blazor.RenderTree; using Microsoft.AspNetCore.Blazor.RenderTree;
@ -71,9 +71,9 @@ namespace Microsoft.AspNetCore.Blazor.Components
/// Override this method if you will perform an asynchronous operation and /// Override this method if you will perform an asynchronous operation and
/// want the component to refresh when that operation is completed. /// want the component to refresh when that operation is completed.
/// </summary> /// </summary>
/// <returns>A <see cref="Task"/> representing any asynchronous operation, or <see langword="null"/>.</returns> /// <returns>A <see cref="Task"/> representing any asynchronous operation.</returns>
protected virtual Task OnInitAsync() protected virtual Task OnInitAsync()
=> null; => Task.CompletedTask;
/// <summary> /// <summary>
/// Method invoked when the component has received parameters from its parent in /// Method invoked when the component has received parameters from its parent in
@ -87,9 +87,9 @@ namespace Microsoft.AspNetCore.Blazor.Components
/// Method invoked when the component has received parameters from its parent in /// Method invoked when the component has received parameters from its parent in
/// the render tree, and the incoming values have been assigned to properties. /// the render tree, and the incoming values have been assigned to properties.
/// </summary> /// </summary>
/// <returns>A <see cref="Task"/> representing any asynchronous operation, or <see langword="null"/>.</returns> /// <returns>A <see cref="Task"/> representing any asynchronous operation.</returns>
protected virtual Task OnParametersSetAsync() protected virtual Task OnParametersSetAsync()
=> null; => Task.CompletedTask;
/// <summary> /// <summary>
/// Notifies the component that its state has changed. When applicable, this will /// Notifies the component that its state has changed. When applicable, this will
@ -128,9 +128,9 @@ namespace Microsoft.AspNetCore.Blazor.Components
/// not automatically re-render after the completion of any returned <see cref="Task"/>, because /// not automatically re-render after the completion of any returned <see cref="Task"/>, because
/// that would cause an infinite render loop. /// that would cause an infinite render loop.
/// </summary> /// </summary>
/// <returns>A <see cref="Task"/> representing any asynchronous operation, or <see langword="null"/>.</returns> /// <returns>A <see cref="Task"/> representing any asynchronous operation.</returns>
protected virtual Task OnAfterRenderAsync() protected virtual Task OnAfterRenderAsync()
=> null; => Task.CompletedTask;
void IComponent.Init(RenderHandle renderHandle) void IComponent.Init(RenderHandle renderHandle)
{ {
@ -221,8 +221,11 @@ namespace Microsoft.AspNetCore.Blazor.Components
{ {
OnAfterRender(); OnAfterRender();
OnAfterRenderAsync()?.ContinueWith(task => var onAfterRenderTask = OnAfterRenderAsync();
if (onAfterRenderTask != null && onAfterRenderTask.Status != TaskStatus.RanToCompletion)
{ {
onAfterRenderTask.ContinueWith(task =>
{
// Note that we don't call StateHasChanged to trigger a render after // Note that we don't call StateHasChanged to trigger a render after
// handling this, because that would be an infinite loop. The only // handling this, because that would be an infinite loop. The only
// reason we have OnAfterRenderAsync is so that the developer doesn't // reason we have OnAfterRenderAsync is so that the developer doesn't
@ -230,10 +233,11 @@ namespace Microsoft.AspNetCore.Blazor.Components
// the case where they want to start an async task. // the case where they want to start an async task.
if (task.Exception != null) if (task.Exception != null)
{ {
HandleException(task.Exception); HandleException(task.Exception);
} }
}); });
}
} }
} }
} }