So here's the scenario I'm working with: I have a data coming in from a socket and I'd like to have user provided javascript components processing that data. I have js that conceptually looks like this:
I built this prototype and to my amazement the whole thing just worked, right off the bat. So, amazing job ClearScript team. Really incredible framework you've written.
I am a complete ClearScript newb, so my questions are:
components.init = function(container) {
container.register('typeOfData1', myFunction1);
container.register('typeOfData2', myFunction2);
}
function myFunction1(args) {
// do stuff
}
function myFunction(args) {
// do more stuff
}
Boiled down to essentials my c# code looks something like this:public class Container
{
...
public void register(string dataType, object obj)
{
SaveFunctionObj(dataType, obj);
}
public void Execute(string dataType, string args)
{
object obj = GetFunctionObj(dataType);
dynamic method = obj;
method(args);
}
}
static void Main()
{
using (var engine = new V8ScriptEngine())
{
var container = new Container();
dynamic components = new ExpandoObject();
engine.AddHostObject("components", components);
// get scripts that look like the js above
foreach(string script in GetTheScripts())
{
engine.Evaluate(script);
components.init(container);
}
// start a long-running loop
while (true)
{
var data = GetData();
container.Execute(data.DataType, data.Args);
}
}
}
Sorry for all the code, just wanted to be clear in how I was approaching the problem. I basically have a bunch of scripts which respond to certain data coming in. I have some C# code that holds references to functions that exist in the scripts and runs each as needed. I built this prototype and to my amazement the whole thing just worked, right off the bat. So, amazing job ClearScript team. Really incredible framework you've written.
I am a complete ClearScript newb, so my questions are:
- Is this a good idea? Should I be holding on to references to functions that exist in the script the way I am? Are there bad performance implications of doing so?
- Does it make sense to have one engine for all scripts? From what I've been reading it's expensive to create multiple engines. I may have 20-30 different scripts, so it sounds like I don't want 30 different engines running.
- I'd love to listen for file changes and automatically update the running engine using the method above. I can do book keeping and get rid of my C# references to the js functions for that file. However the engine would still hold those references wouldn't it? Would the engine know to clean those up?