Skip to content

Instantly share code, notes, and snippets.

@emelent
Created August 1, 2024 15:58
Show Gist options
  • Save emelent/5e92a7930631877d66b0014f371c759c to your computer and use it in GitHub Desktop.
Save emelent/5e92a7930631877d66b0014f371c759c to your computer and use it in GitHub Desktop.
class Program
{
static class JsRuntime {
public static Task InvokeVoidAsync(string name, params object[] args){
Console.WriteLine($"Running the js function {name} with arguments [{string.Join(", ", args)}]");
return Task.CompletedTask;
}
}
class VariadicFunc<T>(Func<object[], T> func)
{
public T Invoke(params object[] args)
{
return func(args);
}
public T this[params object[] args] => this.Invoke(args);
}
class Thing
{
VariadicFunc<Task> func = new VariadicFunc<Task>(args =>
{
var sArgs = args.Select(x => x.ToString());
var j = string.Join(", ", sArgs);
Console.WriteLine($"You asked for: {j}");
return Task.CompletedTask;
});
public VariadicFunc<Task> this[string name]
{
get
{
return new VariadicFunc<Task>(
args => JsRuntime.InvokeVoidAsync(name, args)
);
}
}
}
static async Task Main(string[] args)
{
var console = new Thing();
// await thing["a", "b", true, false];
// var log = console["log"];
// await log["Jerky"];
await console["addEventListener"]["jerky", "monkey"];
await JsRuntime.InvokeVoidAsync("console.log", "name bro");
// await JsRuntime.InvokeVoidAsync("console.log", "jake");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment