|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements.
|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 |
|
| 4 | +using System.Runtime.CompilerServices; |
| 5 | + |
4 | 6 | namespace Microsoft.AspNetCore.Components.CompilerServices;
|
5 | 7 |
|
6 | 8 | /// <summary>
|
@@ -67,4 +69,59 @@ public static EventCallback<T> CreateInferredEventCallback<T>(object receiver, E
|
67 | 69 | {
|
68 | 70 | return EventCallback.Factory.Create<T>(receiver, callback);
|
69 | 71 | }
|
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// Not intended for use by application code. |
| 75 | + /// </summary> |
| 76 | + /// <param name="callback"></param> |
| 77 | + /// <returns></returns> |
| 78 | + // |
| 79 | + // This method is used with `@bind-Value:after` for components. When :after is provided we don't know the |
| 80 | + // type of the expression provided by the developer or if we can invoke it directly, as it can be a lambda |
| 81 | + // and unlike in JavaScript, C# doesn't support Immediately Invoked Function Expressions so we need to pass |
| 82 | + // the expression to this helper method and invoke it inside. |
| 83 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 84 | + public static void InvokeSynchronousDelegate(Action callback) |
| 85 | + { |
| 86 | + callback(); |
| 87 | + } |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// Not intended for use by application code. |
| 91 | + /// </summary> |
| 92 | + /// <param name="callback"></param> |
| 93 | + /// <returns></returns> |
| 94 | + // |
| 95 | + // This method is used with `@bind-Value:after` for components. When :after is provided we don't know the |
| 96 | + // type of the expression provided by the developer or if we can invoke it directly, as it can be a lambda |
| 97 | + // and unlike in JavaScript, C# doesn't support Immediately Invoked Function Expressions so we need to pass |
| 98 | + // the expression to this helper method and invoke it inside. |
| 99 | + // In addition to that, when the receiving target delegate property result is awaitable, we can receive either |
| 100 | + // an Action or a Func<Task> and we don't have that information at compile time, so we use this helper to |
| 101 | + // normalize both operations into a Task in the same way we do for EventCallback |
| 102 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 103 | + public static Task InvokeAsynchronousDelegate(Action callback) |
| 104 | + { |
| 105 | + callback(); |
| 106 | + return Task.CompletedTask; |
| 107 | + } |
| 108 | + |
| 109 | + /// <summary> |
| 110 | + /// Not intended for use by application code. |
| 111 | + /// </summary> |
| 112 | + /// <param name="callback"></param> |
| 113 | + /// <returns></returns> |
| 114 | + // |
| 115 | + // This method is used with `@bind-Value:after` for components. When :after is provided we don't know the |
| 116 | + // type of the expression provided by the developer or if we can invoke it directly, as it can be a lambda |
| 117 | + // and unlike in JavaScript, C# doesn't support Immediately Invoked Function Expressions so we need to pass |
| 118 | + // the expression to this helper method and invoke it inside. |
| 119 | + // In addition to that, when the receiving target delegate property result is awaitable, we can receive either |
| 120 | + // an Action or a Func<Task> and we don't have that information at compile time, so we use this helper to |
| 121 | + // normalize both operations into a Task in the same way we do for EventCallback |
| 122 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 123 | + public static Task InvokeAsynchronousDelegate(Func<Task> callback) |
| 124 | + { |
| 125 | + return callback(); |
| 126 | + } |
70 | 127 | }
|
0 commit comments