mirror of
https://github.com/ElectronNET/Electron.NET.git
synced 2026-10-01 03:11:45 +00:00
Clean-up
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user