From a8229f2fa957d16b034b7871be77a5d41ad49986 Mon Sep 17 00:00:00 2001 From: rafael-aero Date: Fri, 20 Aug 2021 15:06:58 +0200 Subject: [PATCH] add internal method to emit socket events synchronously, and use it for all exit, quit and relaunch methods --- ElectronNET.API/App.cs | 11 +++++------ ElectronNET.API/AutoUpdater.cs | 2 +- ElectronNET.API/BridgeConnector.cs | 20 ++++++++++++++++++++ 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/ElectronNET.API/App.cs b/ElectronNET.API/App.cs index ddc0e07..b18cfdf 100644 --- a/ElectronNET.API/App.cs +++ b/ElectronNET.API/App.cs @@ -556,7 +556,7 @@ namespace ElectronNET.API /// public void Quit() { - BridgeConnector.Emit("appQuit"); + BridgeConnector.EmitSync("appQuit"); } /// @@ -566,7 +566,7 @@ namespace ElectronNET.API /// Exits immediately with exitCode. exitCode defaults to 0. public void Exit(int exitCode = 0) { - BridgeConnector.Emit("appExit", exitCode); + BridgeConnector.EmitSync("appExit", exitCode); } /// @@ -581,7 +581,7 @@ namespace ElectronNET.API /// public void Relaunch() { - BridgeConnector.Emit("appRelaunch"); + BridgeConnector.EmitSync("appRelaunch"); } /// @@ -599,7 +599,7 @@ namespace ElectronNET.API /// Options for the relaunch. public void Relaunch(RelaunchOptions relaunchOptions) { - BridgeConnector.Emit("appRelaunch", JObject.FromObject(relaunchOptions, _jsonSerializer)); + BridgeConnector.EmitSync("appRelaunch", JObject.FromObject(relaunchOptions, _jsonSerializer)); } /// @@ -698,8 +698,7 @@ namespace ElectronNET.API BridgeConnector.Emit("appGetPath", pathName.GetDescription()); - return await taskCompletionSource.Task - .ConfigureAwait(false); + return await taskCompletionSource.Task.ConfigureAwait(false); } } diff --git a/ElectronNET.API/AutoUpdater.cs b/ElectronNET.API/AutoUpdater.cs index c65b657..95fe432 100644 --- a/ElectronNET.API/AutoUpdater.cs +++ b/ElectronNET.API/AutoUpdater.cs @@ -539,7 +539,7 @@ namespace ElectronNET.API /// Run the app after finish even on silent install. Not applicable for macOS. Ignored if `isSilent` is set to `false`. public void QuitAndInstall(bool isSilent = false, bool isForceRunAfter = false) { - BridgeConnector.Emit("autoUpdaterQuitAndInstall", isSilent, isForceRunAfter); + BridgeConnector.EmitSync("autoUpdaterQuitAndInstall", isSilent, isForceRunAfter); } /// diff --git a/ElectronNET.API/BridgeConnector.cs b/ElectronNET.API/BridgeConnector.cs index f4ab793..f8b06fa 100644 --- a/ElectronNET.API/BridgeConnector.cs +++ b/ElectronNET.API/BridgeConnector.cs @@ -104,6 +104,26 @@ namespace ElectronNET.API }); } + /// + /// This method is only used on places where we need to be sure the event was sent on the socket, such as Quit, Exit, Relaunch and QuitAndInstall methods + /// + /// + /// + internal static void EmitSync(string eventString, params object[] args) + { + if (App.SocketDebug) + { + Console.WriteLine($"Sending event {eventString}"); + } + + Socket.EmitAsync(eventString, args).Wait(); + + if (App.SocketDebug) + { + Console.WriteLine($"Sent event {eventString}"); + } + } + public static void Off(string eventString) { Socket.Off(eventString);