This commit is contained in:
theolivenbaum
2021-07-12 21:33:35 +02:00
parent e6214491cf
commit b03bc7c9eb
14 changed files with 64 additions and 110 deletions

View File

@@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using SocketIOClient;
using SocketIOClient.Newtonsoft.Json;
@@ -10,21 +11,22 @@ namespace ElectronNET.API
private static object _syncRoot = new object();
public static void Emit(string eventString, params object[] args) => Socket.EmitAsync(eventString, args);
public static void Emit(string eventString, params object[] args)
{
//We don't care about waiting for the event to be emitted, so this doesn't need to be async
Task.Run(() => Socket.EmitAsync(eventString, args));
}
public static void Off(string eventString) => Socket.Off(eventString);
public static void On(string eventString, Action fn) => Socket.On(eventString, _ => fn());
public static void On<T>(string eventString, Action<T> fn) => Socket.On(eventString, (o) =>
{
fn(o.GetValue<T>(0));
});
public static void On<T>(string eventString, Action<T> fn) => Socket.On(eventString, (o) => fn(o.GetValue<T>(0)));
public static void Once<T>(string eventString, Action<T> fn) => Socket.On(eventString, (o) =>
{
Socket.Off(eventString);
fn(o.GetValue<T>(0));
});
private static SocketIO Socket
{
get
@@ -67,55 +69,4 @@ namespace ElectronNET.API
}
}
}
public interface IListener : System.IComparable<IListener>
{
int GetId();
void Call(params object[] args);
}
public class ListenerImpl : IListener
{
private static int id_counter = 0;
private int Id;
private readonly Action fn1;
private readonly Action<object> fn;
public ListenerImpl(Action<object> fn)
{
this.fn = fn;
this.Id = id_counter++;
}
public ListenerImpl(Action fn)
{
this.fn1 = fn;
this.Id = id_counter++;
}
public void Call(params object[] args)
{
if (fn != null)
{
var arg = args.Length > 0 ? args[0] : null;
fn(arg);
}
else
{
fn1();
}
}
public int CompareTo(IListener other)
{
return this.GetId().CompareTo(other.GetId());
}
public int GetId()
{
return Id;
}
}
}