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

Support Invoking Modules/Classes from JS in C# #20

Open
geocine opened this issue May 2, 2023 · 3 comments
Open

Support Invoking Modules/Classes from JS in C# #20

geocine opened this issue May 2, 2023 · 3 comments
Labels
enhancement New feature or request question Further information is requested

Comments

@geocine
Copy link

geocine commented May 2, 2023

I can see this is possible from C# to JS like so

var calculator = new Calculator();
messenger.RegisterObject(calculator)

How do I do this for JS? It seems I can only invoke functions from the window context.

@kekyo
Copy link
Owner

kekyo commented May 3, 2023

Does this mean that you want to call a member function of an object on the JS side as an instance of a class object from the .NET side?

.NET Proxy feature on #5 (comment) for example?

This issue eventually pivoted to being able to pass a callback function. The reason is that I felt that if the callback function could be implemented, it would no longer be necessary. However, the callback function is a different method than the realization of a proxy feature, so it is not exactly a replacement....

Do you think this function should be there?

@kekyo kekyo added question Further information is requested enhancement New feature or request labels May 3, 2023
@geocine
Copy link
Author

geocine commented May 3, 2023

I think not to this extent but I want to be able to call functions from a class/module from JavaScript in C#

Let's say this is JS

let calculator = new Calculator(); // declaring using let will not attach calculator to window
messenger.RegisterObject(calculator) // so we need a way to expose it to C#

I should be able to do this in C#

invokeClientMethod(`calculator.add`, 1,2);

@kekyo
Copy link
Owner

kekyo commented May 17, 2023

@geocine Sorry too later :(

I remembered that I had implemented it to accept that operation, so I wrote some test code:

4132b6b

JavaScript classes like this one:

class JSCalculator {
  async add(a, b) { return a + b; }
  async sub(a, b) { return a - b; }
};
var jscalc = new JSCalculator();

It can be called like this:

            var result_jscalc_add = await messenger.InvokePeerMethodAsync<int>(
                "jscalc.add", 1, 2);

If you could check out this commit and give it a try, you can confirm this. However, it may just be my environment, but now in Edge WebView2, the apply function in the JavaScript is not working correctly, so the call after the Ready event does not complete (I checked all other environments, only Edge WebView2 is abnormal).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants