kekyo / DupeNukem

WebView attachable full-duplex asynchronous interoperable independent messaging library between .NET and JavaScript.
Apache License 2.0
18 stars 0 forks source link

Support closure functions both .NET and JavaScript. #5

Closed kekyo closed 1 year ago

kekyo commented 2 years ago

.NET proxy

public interface IJavaScriptTargetFunctionsPreDefined
{
    Task<int> add(int a, int b);
    Task<int> sub(int a, int b);
}

// Get (generate) a proxy.
var proxy = messenger.GetProxy<IJavaScriptTargetFunctionsPreDefined>();

var r1 = await proxy.add(1, 2);
var r2 = await proxy.sub(1, 2);
async function add(a, b) {
  return a + b;
}
async function sub(a, b) {
  return a - b;
}

JavaScript proxy

// "calculator" is a proxy.
window.calculator.Add(1, 2);
window.calculator.Sub(1, 2);
// (Already supported)

public sealed class Calculator
{
    public Task<int> Add(int a, int b) =>
        Task.FromResult(a + b);
    public Task<int> Sub(int a, int b) =>
        Task.FromResult(a - b);
}

// Register object AND generate a proxy into JavaScript.
var calculator = new Calculator();
message.RegisterObject("calculator", calculator);
kekyo commented 2 years ago

.NET side proxy bit harder implementation, requires IL emitting or DLR or build time generator.

Maybe easier implements in JavaScript proxy.

kekyo commented 2 years ago

Supported JavaScript proxy object at 0.6.0.

kekyo commented 1 year ago

Pivot closure functions instead of proxy objects.

kekyo commented 1 year ago

Related #6 when supported closure function, will be better to use this infrastructure.