Skip to content
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

Rewrite to signalr #670

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix multiple areas that should return a int, not a string (was also w…
…rong in socketio version) -> Should fix most areas with system.text.json
  • Loading branch information
LORDofDOOM committed Mar 1, 2022
commit e353919342be6ad4810b5af0d6b9d2d0cdc11067
6 changes: 2 additions & 4 deletions ElectronNET.API/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1064,8 +1064,7 @@ public async void SetAppUserModelId(string id)
/// <returns>Result of import. Value of 0 indicates success.</returns>
public async Task<int> ImportCertificateAsync(ImportCertificateOptions options, CancellationToken cancellationToken = default)
{
var signalrResult = await SignalrSerializeHelper.GetSignalrResultString("appImportCertificate", JObject.FromObject(options, _jsonSerializer));
return int.Parse(signalrResult);
return await SignalrSerializeHelper.GetSignalrResultInt("appImportCertificate", JObject.FromObject(options, _jsonSerializer));
}

/// <summary>
Expand Down Expand Up @@ -1115,8 +1114,7 @@ public async Task<bool> SetBadgeCountAsync(int count, CancellationToken cancella
/// <param name="cancellationToken">The cancellation token.</param>
public async Task<int> GetBadgeCountAsync(CancellationToken cancellationToken = default)
{
var signalrResult = await SignalrSerializeHelper.GetSignalrResultString("appGetBadgeCount");
return int.Parse(signalrResult);
return await SignalrSerializeHelper.GetSignalrResultInt("appGetBadgeCount");
}

/// <summary>
Expand Down
3 changes: 1 addition & 2 deletions ElectronNET.API/BrowserWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1876,8 +1876,7 @@ public async void SetParentWindow(BrowserWindow parent)
/// <returns></returns>
public async Task<BrowserWindow> GetParentWindowAsync()
{
string parentId = await SignalrSerializeHelper.GetSignalrResultString("browserWindowGetParentWindow", Id);
var browserWindowId = int.Parse(parentId.ToString());
int browserWindowId = await SignalrSerializeHelper.GetSignalrResultInt("browserWindowGetParentWindow", Id);
var browserWindow = Electron.WindowManager.BrowserWindows.ToList().Single(x => x.Id == browserWindowId);
return browserWindow;
}
Expand Down
3 changes: 1 addition & 2 deletions ElectronNET.API/Dock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ internal static Dock Instance
/// <returns>Return an ID representing the request.</returns>
public async Task<int> BounceAsync(DockBounceType type, CancellationToken cancellationToken = default)
{
var result = await SignalrSerializeHelper.GetSignalrResultString("dock-bounce", type.GetDescription());
return int.Parse(result);
return await SignalrSerializeHelper.GetSignalrResultInt("dock-bounce", type.GetDescription());
}

/// <summary>
Expand Down
140 changes: 140 additions & 0 deletions ElectronNET.API/Extensions/SignalrSerializeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -903,5 +903,145 @@ public static async Task<JArray> GetSignalrResultJArray(string signalrCommand, J
return result;
}

public static async Task<int> GetSignalrResultInt(string signalrCommand)
{
var taskCompletionSource = new TaskCompletionSource<int>();
var guid = Guid.NewGuid();
HubElectron.ClientResponsesInt.TryAdd(guid, taskCompletionSource);
await Electron.SignalrElectron.Clients.All.SendAsync(signalrCommand, guid.ToString());

int result;
try
{
var task = taskCompletionSource.Task;
if (await Task.WhenAny(task, Task.Delay(5000)) == task)
{
result = await task;
}
else
{
throw new ArgumentNullException();
}
}
finally
{
HubElectron.ClientResponsesInt.TryRemove(guid, out taskCompletionSource);
}

return result;
}

public static async Task<int> GetSignalrResultInt(string signalrCommand, int parameter1)
{
var taskCompletionSource = new TaskCompletionSource<int>();
var guid = Guid.NewGuid();
HubElectron.ClientResponsesInt.TryAdd(guid, taskCompletionSource);
await Electron.SignalrElectron.Clients.All.SendAsync(signalrCommand, guid.ToString(), parameter1);

int result;
try
{
var task = taskCompletionSource.Task;
if (await Task.WhenAny(task, Task.Delay(5000)) == task)
{
result = await task;
}
else
{
throw new ArgumentNullException();
}
}
finally
{
HubElectron.ClientResponsesInt.TryRemove(guid, out taskCompletionSource);
}

return result;
}

public static async Task<int> GetSignalrResultInt(string signalrCommand, string parameter1)
{
var taskCompletionSource = new TaskCompletionSource<int>();
var guid = Guid.NewGuid();
HubElectron.ClientResponsesInt.TryAdd(guid, taskCompletionSource);
await Electron.SignalrElectron.Clients.All.SendAsync(signalrCommand, guid.ToString(), parameter1);

int result;
try
{
var task = taskCompletionSource.Task;
if (await Task.WhenAny(task, Task.Delay(5000)) == task)
{
result = await task;
}
else
{
throw new ArgumentNullException();
}
}
finally
{
HubElectron.ClientResponsesInt.TryRemove(guid, out taskCompletionSource);
}

return result;
}

public static async Task<int> GetSignalrResultInt(string signalrCommand, JObject parameter1)
{
var taskCompletionSource = new TaskCompletionSource<int>();
var guid = Guid.NewGuid();
HubElectron.ClientResponsesInt.TryAdd(guid, taskCompletionSource);
await Electron.SignalrElectron.Clients.All.SendAsync(signalrCommand, guid.ToString(), parameter1);

int result;
try
{
var task = taskCompletionSource.Task;
if (await Task.WhenAny(task, Task.Delay(5000)) == task)
{
result = await task;
}
else
{
throw new ArgumentNullException();
}
}
finally
{
HubElectron.ClientResponsesInt.TryRemove(guid, out taskCompletionSource);
}

return result;
}

public static async Task<int> GetSignalrResultInt(string signalrCommand, JObject parameter1, string parameter2)
{
var taskCompletionSource = new TaskCompletionSource<int>();
var guid = Guid.NewGuid();
HubElectron.ClientResponsesInt.TryAdd(guid, taskCompletionSource);
await Electron.SignalrElectron.Clients.All.SendAsync(signalrCommand, guid.ToString(), parameter1, parameter2);

int result;
try
{
var task = taskCompletionSource.Task;
if (await Task.WhenAny(task, Task.Delay(5000)) == task)
{
result = await task;
}
else
{
throw new ArgumentNullException();
}
}
finally
{
HubElectron.ClientResponsesInt.TryRemove(guid, out taskCompletionSource);
}

return result;
}

}
}
Loading