Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Blazor] Compiler runtime APIs needed for bind:get, bind:set, bind:after #40143

Merged
merged 5 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.CompilerServices;

namespace Microsoft.AspNetCore.Components.CompilerServices;

/// <summary>
Expand Down Expand Up @@ -50,4 +52,76 @@ public static EventCallback<T> CreateInferredEventCallback<T>(object receiver, F
{
return EventCallback.Factory.Create<T>(receiver, callback);
}

/// <summary>
/// Not intended for use by application code.
/// </summary>
/// <param name="receiver"></param>
/// <param name="callback"></param>
/// <param name="value"></param>
/// <returns></returns>
//
// This method is used with `@bind-Value` for components. When a component has a generic type, it's
// really messy to write the parameter type for ValueChanged - because it can contain generic
// type parameters. We're using a trick of type inference to generate the proper typing for the delegate
// so that method-group-to-delegate conversion works.
public static EventCallback<T> CreateInferredEventCallback<T>(object receiver, EventCallback<T> callback, T value)
{
return EventCallback.Factory.Create<T>(receiver, callback);
}

/// <summary>
/// Not intended for use by application code.
/// </summary>
/// <param name="callback"></param>
/// <returns></returns>
//
// This method is used with `@bind-Value:after` for components. When :after is provided we don't know the
// type of the expression provided by the developer or if we can invoke it directly, as it can be a lambda
// and unlike in JavaScript, C# doesn't support Immediately Invoked Function Expressions so we need to pass
// the expression to this helper method and invoke it inside.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void InvokeSynchronousDelegate(Action callback)
{
callback();
}

/// <summary>
/// Not intended for use by application code.
/// </summary>
/// <param name="callback"></param>
/// <returns></returns>
//
// This method is used with `@bind-Value:after` for components. When :after is provided we don't know the
// type of the expression provided by the developer or if we can invoke it directly, as it can be a lambda
// and unlike in JavaScript, C# doesn't support Immediately Invoked Function Expressions so we need to pass
// the expression to this helper method and invoke it inside.
// In addition to that, when the receiving target delegate property result is awaitable, we can receive either
// an Action or a Func<Task> and we don't have that information at compile time, so we use this helper to
// normalize both operations into a Task in the same way we do for EventCallback
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Task InvokeAsynchronousDelegate(Action callback)
{
callback();
return Task.CompletedTask;
}

/// <summary>
/// Not intended for use by application code.
/// </summary>
/// <param name="callback"></param>
/// <returns></returns>
//
// This method is used with `@bind-Value:after` for components. When :after is provided we don't know the
// type of the expression provided by the developer or if we can invoke it directly, as it can be a lambda
// and unlike in JavaScript, C# doesn't support Immediately Invoked Function Expressions so we need to pass
// the expression to this helper method and invoke it inside.
// In addition to that, when the receiving target delegate property result is awaitable, we can receive either
// an Action or a Func<Task> and we don't have that information at compile time, so we use this helper to
// normalize both operations into a Task in the same way we do for EventCallback
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Task InvokeAsynchronousDelegate(Func<Task> callback)
{
return callback();
}
}
Loading