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

Add Javascript proxy support #3

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

amaitland
Copy link

@amaitland amaitland commented Mar 5, 2023

I had this idea a while ago to use a Proxy in javascript to intercept method calls and dispatch them to .Net using Promises to fulfil the method calls. This is a basic prototype of that idea, should be something that can be made to work on all supported platforms.

  • Add support for creating a javascript proxy that will intercept method calls and dispatch them to .Net (returning a promise).
  • Replace JSInvokeTarget with HybridWebViewObjectHost that supports multiple objects (using a string key)
  • Method invocation in javascript now returns a Promise that can be awaited to obtain the result.
  • Methods in .Net can now return a Task that is awaited and the result sent back to javascript.
  • OnMessageReceived was made protected as shouldn't be called directly by user
// .Net
// Add an object called 'host' that can be called from JavaScript
myHybridWebView.ObjectHost.AddObject("host", new MyJSInvokeTarget(this));
// JavaScript
// Directly call a .Net method called CallMeFromScript
HybridWebView.SendInvokeMessageToDotNet("host", "CallMeFromScript", ["msg from js", 987]);
// Create a proxy
const myDotNetObjectProxy = HybridWebView.CreateProxy("host");
// Call the method, await the result (promise) to get the returned value.
const result = await myDotNetObjectProxy.CallMeFromScriptReturn("Hello", 42);

Still lots of things I'd like to see added/supported

  • ParamArray support
  • Javascript -> Net method name conversion (should the conversion be done in javascript or .Net?)
  • Javascript method names should probably be changed to start with lowercase to match javascript convention
  • Test on all supported platforms (I've only tested on Windows/Android Emulator).
  • Review method/event names and properly document public API.
  • Improved error handling
  • Review Threading, should methods be invoked on Worker Thread?
  • Unit tests

- Add support for creating a javascript proxy that will intercept method calls
  and dispatch them to .Net (returning a promise).
- Replace JSInvokeTarget with HybridWebViewObjectHost that supports
  multiple objects (using a string key)
@amaitland amaitland changed the title Add Javascript proxy Add Javascript proxy support Mar 5, 2023
@amaitland
Copy link
Author

Android should now work (tested only in the emulator).

@saber-wang
Copy link

Android 9.0 execution failed(Chrome69)

@amaitland
Copy link
Author

Android 9.0 execution failed(Chrome69)

That's expected, the browser is too old for the JavaScript in it's current form. In theory a minimum of version Chromium 74 would be required currently.

- Change from using private fields
- Remove static field
@amaitland
Copy link
Author

I've changed the syntax slightly to lower the minimum required javascript version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants