Add event handler with sender id

This commit is contained in:
rafael-aero
2021-09-01 20:42:49 +02:00
parent 794248a83c
commit eac14427df
3 changed files with 61 additions and 9 deletions

View File

@@ -47,11 +47,11 @@ namespace ElectronNET.API
{
BridgeConnector.Emit("registerIpcMainChannel", channel);
BridgeConnector.Off(channel);
BridgeConnector.On<object[]>(channel, (args) =>
BridgeConnector.On<object[]>(channel, (args) =>
{
var objectArray = FormatArguments(args);
if(objectArray.Count == 1)
if (objectArray.Count == 1)
{
listener(objectArray.First());
}
@@ -62,6 +62,38 @@ namespace ElectronNET.API
});
}
/// <summary>
/// Listens to channel, when a new message arrives listener would be called with
/// listener(event, args...). This listner will keep the window event sender id
/// </summary>
/// <param name="channel">Channelname.</param>
/// <param name="listener">Callback Method.</param>
public void OnWithId(string channel, Action<int, object> listener)
{
BridgeConnector.Emit("registerIpcMainChannelWithId", channel);
BridgeConnector.Off(channel);
BridgeConnector.On<ArgsAndId>(channel, (data) =>
{
var objectArray = FormatArguments(data.args);
if (objectArray.Count == 1)
{
listener(data.id, objectArray.First());
}
else
{
listener(data.id, objectArray);
}
});
}
private class ArgsAndId
{
public int id { get; set; }
public object[] args { get; set; }
}
private List<object> FormatArguments(object[] objectArray)
{
return objectArray.Where(o => o is object).ToList();