tjanczuk / edge

Run .NET and Node.js code in-process on Windows, MacOS, and Linux
http://tjanczuk.github.io/edge
Other
5.41k stars 642 forks source link

Can I nest C# in NodeJS in C#? Or can I replace NodeJS function by C# method, similar to Jurrasic? #699

Open GuzioMG opened 4 years ago

GuzioMG commented 4 years ago

Will something like this simply work:

using System;
using System.Threading.Tasks;
using System.Threading;
using EdgeJs;

namespace hub.guzio.AnExample{  class Program
{
    public static async Task Start()
    {
        var func = Edge.Func(@"
            return function (data, callback) {
                var edge = require('edge');

                var helloWorld = edge.func(function () {/*
                     async (input) => { while(true){
                         hub.guzio.AnExample.Program.Say("Hello, World!");}  //The most CRUCIAL line.
                         return ""; 
                     }}
                */});

                helloWorld('JavaScript', function (error, result) {
                    if (error) throw error;
                    console.log(result);
                });
            }
        ");
    }

    public static void Say(string txt)
    {
        Console.WriteLine(txt);
    }

    static void Main(string[] args)
    {
        Console.WriteLine("Starting...)
        Start().Wait();
        Thread.sleep(1000);
        Console.WriteLine("It's running for over a 1000ms now. I wonder, how many Hello, Worlds did it print so far..."
    }
}}

Probably not...

Why do I need that?

I'm not a scripting person and DEFINITELY not a networking person. I have no idea, how is your program accessing the code in /* comment */. The example you provided, for something probably similar, seems like ROCKET SCIENCE to me. All I want to do is access one NodeJS library from an UWP (more on that later) app. But it would make sense if I could communicate with my NodeJS code, wouldn't it? And no, simple "return" statement won't work here at all, and my loop example illustrates it the best way possible. So if you know a way to do that (or mine actually works, which would be weird), then I'd love to hear that.

Can't I look for an alternative?

Nope.

However, there is an alternative WAY of doing that. I can replace JS function with a C# method and simply refer to that function from my JS code, similar to what you can achieve using Jurrasic or similar kind or engine. That would be even more convenient in my case. (Not to mention, I wouldn't have to (this is grammatically incorrect, I know) type

 var NAME = edge.func(function () {/*
        async (input) => {
               Reference.To.method("argument", 1, "and argument", 2);}
               return ""; 
         }
*/});

NAME('JavaScript', function (error, result) {
          if (error) throw error;
          console.log(result);
});

every time, but this can be easily fixed with a function, so it's not a big deal)

More on that later now.

Yes, I need to run it on UWP. And I ALMOST asked a question about it here, but this issue arleady covers two topics, which is not good, so I opened #698 instead.

Thanks!