Skip to content

Support DI and Mocking better + Support launching app with file for win and linux #656

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion ElectronNET.API/App.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
using System.Threading;
using System.Threading.Tasks;
using ElectronNET.API.Extensions;
using ElectronNET.API.Interfaces;

namespace ElectronNET.API
{
/// <summary>
/// Control your application's event lifecycle.
/// </summary>
public sealed class App
public sealed class App : IApp
{
/// <summary>
/// Emitted when all windows have been closed.
Expand Down
16 changes: 16 additions & 0 deletions ElectronNET.API/ApplicationSocket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using ElectronNET.API.Interfaces;
using Quobject.SocketIoClientDotNet.Client;

namespace ElectronNET.API
{
/// <summary>
/// Wrapper for the underlying Socket connection
/// </summary>
public class ApplicationSocket : IApplicationSocket
{
/// <summary>
/// Socket used to communicate with main.js
/// </summary>
public Socket Socket { get; internal set; }
}
}
3 changes: 2 additions & 1 deletion ElectronNET.API/AutoUpdater.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using ElectronNET.API.Interfaces;

namespace ElectronNET.API
{
/// <summary>
/// Enable apps to automatically update themselves. Based on electron-updater.
/// </summary>
public sealed class AutoUpdater
public sealed class AutoUpdater : IAutoUpdater
{
/// <summary>
/// Whether to automatically download an update when it is found. (Default is true)
Expand Down
110 changes: 109 additions & 1 deletion ElectronNET.API/BridgeConnector.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using Quobject.SocketIoClientDotNet.Client;
using Newtonsoft.Json.Linq;
using Quobject.SocketIoClientDotNet.Client;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace ElectronNET.API
{
Expand Down Expand Up @@ -40,5 +43,110 @@ public static Socket Socket
return _socket;
}
}

public static async Task<T> GetValueOverSocketAsync<T>(string eventString, string eventCompletedString)
{
CancellationToken cancellationToken = new();
cancellationToken.ThrowIfCancellationRequested();

var taskCompletionSource = new TaskCompletionSource<T>();
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
{
BridgeConnector.Socket.On(eventCompletedString, (value) =>
{
BridgeConnector.Socket.Off(eventCompletedString);

if (value == null)
{
Console.WriteLine($"ERROR: BridgeConnector (event: '{eventString}') returned null. Socket loop hang.");
taskCompletionSource.SetCanceled();
return;
}

try
{
taskCompletionSource.SetResult( new JValue(value).ToObject<T>() );
}
catch (Exception e)
{
Console.WriteLine($"ERROR: BridgeConnector (event: '{eventString}') exception: {e.Message}. Socket loop hung.");
}
});

BridgeConnector.Socket.Emit(eventString);

return await taskCompletionSource.Task.ConfigureAwait(false);
}
}

public static async Task<T> GetObjectOverSocketAsync<T>(string eventString, string eventCompletedString)
{
CancellationToken cancellationToken = new();
cancellationToken.ThrowIfCancellationRequested();

var taskCompletionSource = new TaskCompletionSource<T>();
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
{
BridgeConnector.Socket.On(eventCompletedString, (value) =>
{
BridgeConnector.Socket.Off(eventCompletedString);

if (value == null)
{
Console.WriteLine($"ERROR: BridgeConnector (event: '{eventString}') returned null. Socket loop hang.");
taskCompletionSource.SetCanceled();
return;
}

try
{
taskCompletionSource.SetResult( ((JObject)value).ToObject<T>() );
}
catch (Exception e)
{
Console.WriteLine($"ERROR: BridgeConnector (event: '{eventString}') exception: {e.Message}. Socket loop hung.");
}
});

BridgeConnector.Socket.Emit(eventString);

return await taskCompletionSource.Task.ConfigureAwait(false);
}
}

public static async Task<T> GetArrayOverSocketAsync<T>(string eventString, string eventCompletedString)
{
CancellationToken cancellationToken = new();
cancellationToken.ThrowIfCancellationRequested();

var taskCompletionSource = new TaskCompletionSource<T>();
using (cancellationToken.Register(() => taskCompletionSource.TrySetCanceled()))
{
BridgeConnector.Socket.On(eventCompletedString, (value) =>
{
BridgeConnector.Socket.Off(eventCompletedString);
if (value == null)
{
Console.WriteLine($"ERROR: BridgeConnector (event: '{eventString}') returned null. Socket loop hang.");
taskCompletionSource.SetCanceled();
return;
}

try
{
taskCompletionSource.SetResult( ((JArray)value).ToObject<T>() );
}
catch (Exception e)
{
Console.WriteLine($"ERROR: BridgeConnector (event: '{eventString}') exception: {e.Message}. Socket loop hung.");
}
});

BridgeConnector.Socket.Emit(eventString);

return await taskCompletionSource.Task.ConfigureAwait(false);
}
}

}
}
3 changes: 2 additions & 1 deletion ElectronNET.API/Clipboard.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System.Threading.Tasks;
using ElectronNET.API.Interfaces;

namespace ElectronNET.API
{
/// <summary>
/// Perform copy and paste operations on the system clipboard.
/// </summary>
public sealed class Clipboard
public sealed class Clipboard : IClipboard
{
private static Clipboard _clipboard;
private static object _syncRoot = new object();
Expand Down
3 changes: 2 additions & 1 deletion ElectronNET.API/Dialog.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Web;
using ElectronNET.API.Interfaces;

namespace ElectronNET.API
{
/// <summary>
/// Display native system dialogs for opening and saving files, alerting, etc.
/// </summary>
public sealed class Dialog
public sealed class Dialog : IDialog
{
private static Dialog _dialog;
private static object _syncRoot = new object();
Expand Down
3 changes: 2 additions & 1 deletion ElectronNET.API/Dock.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading.Tasks;
using ElectronNET.API.Entities;
using ElectronNET.API.Extensions;
using ElectronNET.API.Interfaces;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
Expand All @@ -12,7 +13,7 @@ namespace ElectronNET.API
/// <summary>
/// Control your app in the macOS dock.
/// </summary>
public sealed class Dock
public sealed class Dock : IDock
{
private static Dock _dock;
private static object _syncRoot = new object();
Expand Down
5 changes: 5 additions & 0 deletions ElectronNET.API/Electron.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,10 @@ public static class Electron
/// Control your app in the macOS dock.
/// </summary>
public static Dock Dock { get { return Dock.Instance; } }

/// <summary>
/// Electeon extensions to the Nodejs process object.
/// </summary>
public static Process Process { get { return Process.Instance; } }
}
}
Empty file modified ElectronNET.API/ElectronNET.API.csproj
100644 → 100755
Empty file.
10 changes: 10 additions & 0 deletions ElectronNET.API/Entities/ProcessVersions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace ElectronNET.API
{
/// <summary>
/// An object listing the version strings specific to Electron
/// </summary>
/// <param name="Chrome">Value representing Chrome's version string</param>
/// <param name="Electron">Value representing Electron's version string</param>
/// <returns></returns>
public record ProcessVersions(string Chrome, string Electron);
}
3 changes: 2 additions & 1 deletion ElectronNET.API/GlobalShortcut.cs
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using ElectronNET.API.Interfaces;

namespace ElectronNET.API
{
/// <summary>
/// Detect keyboard events when the application does not have keyboard focus.
/// </summary>
public sealed class GlobalShortcut
public sealed class GlobalShortcut : IGlobalShortcut
{
private static GlobalShortcut _globalShortcut;
private static object _syncRoot = new object();
Expand Down
3 changes: 2 additions & 1 deletion ElectronNET.API/HostHook.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Newtonsoft.Json.Serialization;
using System;
using System.Threading.Tasks;
using ElectronNET.API.Interfaces;

namespace ElectronNET.API
{
Expand All @@ -13,7 +14,7 @@ namespace ElectronNET.API
/// ElectronHostHook directory:
/// <c>electronize add HostHook</c>
/// </summary>
public sealed class HostHook
public sealed class HostHook : IHostHook
{
private static HostHook _electronHostHook;
private static object _syncRoot = new object();
Expand Down
Loading