Skip to content

Commit 092789a

Browse files
committed
feat: Add overloaded methods in IpcMain that supports listeners with a return type of Task<object>.
1 parent bff3fff commit 092789a

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

src/ElectronNET.API/API/IpcMain.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,29 @@ public void OnSync(string channel, Func<object, object> listener)
102102
});
103103
}
104104

105+
/// <summary>
106+
/// Send a message to the renderer process synchronously via channel,
107+
/// you can also send arbitrary arguments.
108+
///
109+
/// Note: Sending a synchronous message will block the whole renderer process,
110+
/// unless you know what you are doing you should never use it.
111+
/// </summary>
112+
/// <param name="channel"></param>
113+
/// <param name="listener"></param>
114+
public void OnSync(string channel, Func<object, Task<object>> listener)
115+
{
116+
BridgeConnector.Socket.Emit("registerSyncIpcMainChannel", channel);
117+
BridgeConnector.Socket.On<JsonElement>(channel, (args) =>
118+
{
119+
Task.Run(async () =>
120+
{
121+
var arg = FormatArguments(args);
122+
var result = await listener(arg);
123+
BridgeConnector.Socket.Emit(channel + "Sync", result);
124+
});
125+
});
126+
}
127+
105128
/// <summary>
106129
/// Adds a one time listener method for the event. This listener is invoked only
107130
/// the next time a message is sent to channel, after which it is removed.
@@ -172,6 +195,26 @@ public void Handle(string channel, Func<object, object> listener)
172195
});
173196
}
174197

198+
/// <summary>
199+
/// Adds a handler for an invokeable IPC. This handler will be called
200+
/// whenever a renderer calls ipcRenderer.invoke(channel, ...args).
201+
/// </summary>
202+
/// <param name="channel">Channelname.</param>
203+
/// <param name="listener">Callback Method.</param>
204+
public void Handle(string channel, Func<object, Task<object>> listener)
205+
{
206+
BridgeConnector.Socket.Emit("registerHandleIpcMainChannel", channel);
207+
BridgeConnector.Socket.On<JsonElement>(channel, (args) =>
208+
{
209+
Task.Run(async () =>
210+
{
211+
var arg = FormatArguments(args);
212+
var result = await listener(arg);
213+
BridgeConnector.Socket.Emit(channel + "Handle", result);
214+
});
215+
});
216+
}
217+
175218
/// <summary>
176219
/// Handles a single invokeable IPC message, then removes the listener.
177220
/// See ipcMain.handle(channel, listener).
@@ -189,6 +232,26 @@ public void HandleOnce(string channel, Func<object, object> listener)
189232
});
190233
}
191234

235+
/// <summary>
236+
/// Handles a single invokeable IPC message, then removes the listener.
237+
/// See ipcMain.handle(channel, listener).
238+
/// </summary>
239+
/// <param name="channel">Channelname.</param>
240+
/// <param name="listener">Callback Method.</param>
241+
public void HandleOnce(string channel, Func<object, Task<object>> listener)
242+
{
243+
BridgeConnector.Socket.Emit("registerHandleOnceIpcMainChannel", channel);
244+
BridgeConnector.Socket.Once<JsonElement>(channel, (args) =>
245+
{
246+
Task.Run(async () =>
247+
{
248+
var arg = FormatArguments(args);
249+
var result = await listener(arg);
250+
BridgeConnector.Socket.Emit(channel + "HandleOnce", result);
251+
});
252+
});
253+
}
254+
192255
/// <summary>
193256
/// Removes any handler for channel, if present.
194257
/// </summary>

0 commit comments

Comments
 (0)