Skip to content

Instantly share code, notes, and snippets.

@smx-smx
Last active April 24, 2018 19:55
Show Gist options
  • Save smx-smx/ed828fbea57bb5b0c15c3f070ff14684 to your computer and use it in GitHub Desktop.
Save smx-smx/ed828fbea57bb5b0c15c3f070ff14684 to your computer and use it in GitHub Desktop.
edge-js within Bridge.Net
namespace Electron
{
public class App
{
private const string ASSEMBLIES_RELPATH = "generated/assemblies/";
private static readonly string AdapterAssembly = path.resolve(ASSEMBLIES_RELPATH + "/Electron.Adapter.dll");
private const string AdapterType = "Foo.Adapter.ElectronAdapter";
private static void OnNotify(object data, EdgeCallback cb)
{
//first arg: error if any
//second arg: result (any type)
cb(null, "AnyResult");
}
public void Test(){
SharpAssembly x = new SharpAssembly(AdapterAssembly);
SharpFunction CreateAdapter = x.GetFunction(AdapterType, "CreateAdapter");
CreateAdapter.InvokeAsync(new {
foo = "Hello World from Bridge.NET",
notify = new Action<object, EdgeCallback>(OnNotify)
}).then<object>((result) => {
Console.WriteLine(Retyped.Primitive.Object.keys(result));
return null;
}, (error) => {
// TODO: untested
es5.Error obj = (es5.Error)error;
throw new Exception(obj.message);
});
}
}
}
using Bridge;
using Retyped;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Electron.edge_js.SharpFunction;
namespace Electron.edge_js
{
public class SharpAssembly
{
[Init(InitPosition.Top)]
public static void InitGlobals()
{
// The call below is required to initialize a global var 'Edge'.
var Edge = node.require.Self("electron-edge-js");
}
[Template("Edge")]
public static object Edge;
private string assembly;
public SharpAssembly(string assemblyFile)
{
this.assembly = assemblyFile;
}
public SharpFunction GetFunction(string typeName, string methodName)
{
Action<object, EdgeCallback> clrMethod = Edge.ToDynamic().func(
new
{
assemblyFile = this.assembly,
typeName = typeName,
methodName = methodName
});
return new SharpFunction(clrMethod);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Retyped.es6_promise;
namespace Electron.edge_js
{
public class SharpFunction
{
public delegate void EdgeCallback(object error, object result);
private Action<object, EdgeCallback> func;
public SharpFunction(Action<object, EdgeCallback> func)
{
this.func = func;
}
public Promise<object> InvokeAsync(object arg)
{
return new Promise<object>((resolve, reject) => {
func(arg, (error, result) => {
if (error != null) {
reject(error);
return;
}
resolve(result);
});
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment