-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Add statically-typed support for wrapping JS interop calls as C# delegates #61721
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
base: main
Are you sure you want to change the base?
Add statically-typed support for wrapping JS interop calls as C# delegates #61721
Conversation
/// <param name="jsObjectReference">The JavaScript object reference that represents the function to be invoked.</param> | ||
/// <returns>A delegate that can be used to invoke the JavaScript function.</returns> | ||
/// <exception cref="ArgumentNullException">Thrown when <paramref name="jsObjectReference"/> is null.</exception> | ||
public static Func<ValueTask> AsVoidFunction(this IJSObjectReference jsObjectReference) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is guess ValueTask
(vs Task
) is here for consistency ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, because the other Invoke*
methods return ValueTask
. The JSRuntime
method which implements the actual call also returns ValueTask
(although it creates it from a regular Task
, for some reason).
How do I Are we going to take the DX convenience to next level ? What about passing JS and C# functions as callbacks to other functions ? |
To create the delegate, you must have had the
You can now pass JS functions (as There is no special support for passing C# functions. You have to create a JS wrapper and pass it the string identifiers of the assembly/method to call a static method, or passing a |
This is a work-in-progress implementation of
AsFunction
extension methods forIJSObjectReference
. These methods create C# delegates that can be used as typed wrappers for invoking referenced JS functions via interop. Currently, it adds overloads for void/value-returning functions with up to 3 arguments.This is a reflection-free alternative to #61673.
The downside for this simpler implementation is the need to introduce multiple overloads into the public API.