Start a new topic

DuoCode.Runtime Help

I really need some sort of documentation or some kinda example or writeup or something that tells me how and when to use the Js apis...


Also...


What is the rule of thumb for binding and 'dynamic'.


i am afraid i might be OVER doing it a bit on the use of dynamic in my bindings.


For Example:  Several Webix Javascript APIs use object like so:

object clone(object source);



i would bind this as:


public static extern dynamic copy(dynamic source);


Now i like the dynamic because i can easily assign to a strong type object.


But should i be doing something like 


public static extern object copy(object source);


or even something like:


[Js(OmitGenericArgs = true)]

public static extern T copy<T>(T source);



ALSO... What about functions... I end mapping things like attachEvent like so (i am afraid also might be over doing the overrides to be able to handle many parameter):


public extern dynamic attachEvent(string eventName, Action func, dynamic id = null);
public extern dynamic attachEvent(string eventName, Action<dynamic> func, dynamic id = null);
[Js(OmitGenericArgs = true)]
public extern dynamic attachEvent<T>(string eventName, Action<T> func, dynamic id = null);
[Js(OmitGenericArgs = true)]
public extern dynamic attachEvent<T1, T2>(string eventName, Action<T1, T2> func, dynamic id = null);
[Js(OmitGenericArgs = true)]
public extern dynamic attachEvent<T1, T2, T3>(string eventName, Action<T1, T2, T3> func, dynamic id = null);
[Js(OmitGenericArgs = true)]
public extern dynamic attachEvent<T1, T2, T3, T4>(string eventName, Action<T1, T2, T3, T4> func, dynamic id = null);
[Js(OmitGenericArgs = true)]
public extern dynamic attachEvent<T1, T2, T3, T4, T5>(string eventName, Action<T1, T2, T3, T4, T5> func, dynamic id = null);
[Js(OmitGenericArgs = true)]
public extern dynamic attachEvent<T1, T2, T3, T4, T5, T6>(string eventName, Action<T1, T2, T3, T4, T5, T6> func, dynamic id = null);
[Js(OmitGenericArgs = true)]
public extern dynamic attachEvent<T1, T2, T3, T4, T5, T6, T7>(string eventName, Action<T1, T2, T3, T4, T5, T6, T7> func, dynamic id = null);
[Js(OmitGenericArgs = true)]
public extern dynamic attachEvent<T1, T2, T3, T4, T5, T6, T7, T8>(string eventName, Action<T1, T2, T3, T4, T5, T6, T7, T8> func, dynamic id = null);



Should i be doing all this to provide attachEvent with up to eight different param types?



Kool... Im using Delegate now... But the github-electron.d.cs uses JsFunction everywhere. Thats why

i thought that was the NORM

Internally (in mscorlib) Delegate inherits from JsFunction, so you can pass any delegate or Action to these methods.

But I will suggest to replace JsFunction to Delegate on bindings definitions, more close to classic C#.


So how do we use JsFunction?


[Js]

public static extern SomeHandler(JsFunction func);



So how do i use in C#... What wraps or creates a JsFunction... Can i use like a Action<T> lambda?

@David @Mackey The tool converts from the .t.cs. It's highly experimental, that's why it's not public yet... Its output still requires quite a few manual fixes.


@David It doesn't handle the constructor correctly. I guess the correct way would be something like this:

 

public extern BrowserWindow(BrowserWindowOptions options = default(BrowserWindowOptions));

 

@Yoav Conversion tool huh?  Does that convert from the JS or from the TypeScript .t.cs?


I noticed that for, say, class BrowserWindow, it created no constructor.  But it did create this metnod:


  public extern dynamic constructor(BrowserWindowOptions options = default(BrowserWindowOptions));


Can you explain what the thinking is there?


It's very different form how I was creating my binding file and I'm not sure what's what.


David

David - I've attached a sample of a .d.cs file matching the type definitions that you mentioned. I've used an internal conversion tool to make it - the tool is very experimental and the output file is probably not something you can use directly without tweaking it. But it's a good start... I hope it helps.


WOW... Any kind of conversion tool (Even if you gotta clean up code after) would still BE AWESOME... When i comes to binding a complete library (like im trying to do witht he whole webix library)... can be a daunting task to say the least... for now i am just binding the "Webix Common Helpers" since i have to do them all by hand... and that is still a bunch...


If you ever get that conversion tool ready to distribute to your DuoCode customers... Sign me up!!!

 

Wow... that github-electron.d.cs


That is why i need some kind of documentation... To know what we can and cannot do with the JS Runtime...


For example, everywhere i was using and Delegate, you are using JsFunction... But i have know idea how and

when to use JsFunction as apposed to  a Delegate or Action Or Func... What is is the best one to use and why?


Also...if i started using JsFunction... How do i actually use the "Bound" methed using a JsFunction in c#...is it lambda still or what?

Just as an aside: it's possible (even recommended) to distribute source files using NuGet - you can place your code in a directory called content, and they will be added to the project.

This is how Typescript definitions (.d.ts) are added via NuGet, btw.


See this tutorial for more details: http://blogs.clariusconsulting.net/kzu/how-to-create-lightweight-reusable-source-code-with-nuget/


Mackey - there's no problem at all with using as DLL. Both ways are perfectly acceptable, depends on the need, you can bundle typescript-style .d.cs files or DLLs with reference definitions.


David - I've attached a sample of a .d.cs file matching the type definitions that you mentioned. I've used an internal conversion tool to make it - the tool is very experimental and the output file is probably not something you can use directly without tweaking it. But it's a good start... I hope it helps.

cs

Regarding:


I very recommend to use one file .d.cs approach and link to it and not use DLL references for definitions, it's very similar to TypeScript .t.cs.


WHY?


Is there a problem with using as a dll.


I am making a API TOOLKIT... I would HATE to have to ship my .cs files as raw source files for

my toolkit users. Thats the whole point of libraries.

Any chance you all could convert github-electron.d.ts into a electron.d.cs for us?  I'm trying to create my own type definition, but lacking documentation it's been very difficult.  The few examples out there don't cover enough situations.


David

[Js(OmitGenericArgs = true)]

public static extern T copy<T>(T source);

Will be prefer, also may I suggest to use constraints on T as possible, for example:

where T : ICopyable


In the case of multi-delegates, you can simply use Delegate base type:

public extern dynamic attachEvent(string eventName, Delegate func, dynamic id = null);


Tip:

Also you don't have to write [Js(OmitGenericArgs = true)] on extern methods, this is default behavior, see attached qunit.d.cs found in UnitTest template.

This attribute is only required only if you compile your definitions as a class-library DLL and reference to it. We are fixing this in next versions.

I very recommened to use one file .d.cs approach and link to it and not use DLL references for definitions, it's very similar to TypeScript .t.cs.

See:

https://github.com/borisyankov/DefinitelyTyped/blob/master/github-electron/github-electron.d.ts

cs
(1.84 KB)
Login or Signup to post a comment