Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/ElectronNET.API/API/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ public void Exit(int exitCode = 0)

public void DisposeSocket()
{
BridgeConnector.Socket.DisposeSocket();
BridgeConnector.Socket.Dispose();
}

/// <summary>
Expand Down
46 changes: 42 additions & 4 deletions src/ElectronNET.API/Bridge/SocketIOFacade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ namespace ElectronNET.API;
using SocketIO.Serializer.SystemTextJson;
using SocketIO = SocketIOClient.SocketIO;

internal class SocketIoFacade
internal class SocketIoFacade : IDisposable
{
private readonly SocketIO _socket;
private readonly object _lockObj = new object();
private bool _isDisposed;

public SocketIoFacade(string uri)
{
Expand All @@ -27,6 +28,8 @@ public SocketIoFacade(string uri)

public void Connect()
{
this.CheckDisposed();

_socket.OnError += (sender, e) => { Console.WriteLine($"BridgeConnector Error: {sender} {e}"); };

_socket.OnConnected += (_, _) =>
Expand All @@ -46,6 +49,8 @@ public void Connect()

public void On(string eventName, Action action)
{
this.CheckDisposed();

lock (_lockObj)
{
_socket.On(eventName, _ => { Task.Run(action); });
Expand All @@ -54,6 +59,8 @@ public void On(string eventName, Action action)

public void On<T>(string eventName, Action<T> action)
{
this.CheckDisposed();

lock (_lockObj)
{
_socket.On(eventName, response =>
Expand All @@ -66,6 +73,8 @@ public void On<T>(string eventName, Action<T> action)

public void Once(string eventName, Action action)
{
this.CheckDisposed();

lock (_lockObj)
{
_socket.On(eventName, _ =>
Expand All @@ -78,6 +87,8 @@ public void Once(string eventName, Action action)

public void Once<T>(string eventName, Action<T> action)
{
this.CheckDisposed();

lock (_lockObj)
{
_socket.On(eventName, (socketIoResponse) =>
Expand All @@ -90,6 +101,11 @@ public void Once<T>(string eventName, Action<T> action)

public void Off(string eventName)
{
if (_isDisposed)
{
return;
}

lock (_lockObj)
{
_socket.Off(eventName);
Expand All @@ -98,11 +114,33 @@ public void Off(string eventName)

public async Task Emit(string eventName, params object[] args)
{
await _socket.EmitAsync(eventName, args).ConfigureAwait(false);
if (!_isDisposed)
{
await _socket.EmitAsync(eventName, args).ConfigureAwait(false);
}
}

/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_isDisposed = true;
_socket.Dispose();
}
}

public void DisposeSocket()
private void CheckDisposed()
{
_socket.Dispose();
if (this._isDisposed)
{
throw new ObjectDisposedException(nameof(SocketIoFacade));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected override Task StartCore()

protected override Task StopCore()
{
this.socket.DisposeSocket();
this.socket.Dispose();
return Task.CompletedTask;
}

Expand Down
4 changes: 3 additions & 1 deletion src/ElectronNET.IntegrationTests/Tests/BrowserWindowTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,11 @@ public async Task BoundsChanged_event_fires_with_updated_bounds()
try
{
window = await Electron.WindowManager.CreateWindowAsync(
new BrowserWindowOptions { Show = false, Width = 300, Height = 200 },
new BrowserWindowOptions { Show = true, Width = 300, Height = 200 },
"about:blank");

await Task.Delay(5.seconds());

var tcs = new TaskCompletionSource<Rectangle>(TaskCreationOptions.RunContinuationsAsynchronously);
window.OnBoundsChanged += bounds => tcs.TrySetResult(bounds);

Expand Down
6 changes: 4 additions & 2 deletions src/ElectronNET.IntegrationTests/Tests/WebContentsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,15 @@ public async Task GetSetUserAgent_check()

try
{
await Task.Delay(1.seconds());

window = await Electron.WindowManager.CreateWindowAsync(new BrowserWindowOptions { Show = true }, "about:blank");

await Task.Delay(3.seconds());
await Task.Delay(5.seconds());

window.WebContents.SetUserAgent("MyUserAgent/1.0");

await Task.Delay(1.seconds());
await Task.Delay(2.seconds());

var ok = await window.WebContents.GetUserAgentAsync();
ok.Should().Be("MyUserAgent/1.0");
Expand Down