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

Implement disposable (ported from #115) #125

Merged
merged 1 commit into from
Dec 25, 2023
Merged
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
37 changes: 36 additions & 1 deletion TwitchLib.PubSub/TwitchPubSub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace TwitchLib.PubSub
/// Implements the <see cref="ITwitchPubSub" />
/// </summary>
/// <seealso cref="ITwitchPubSub" />
public class TwitchPubSub : ITwitchPubSub
public class TwitchPubSub : ITwitchPubSub, IDisposable
{
private const string PingPayload = "{ \"type\": \"PING\" }";

Expand Down Expand Up @@ -73,6 +73,8 @@ public class TwitchPubSub : ITwitchPubSub

private readonly Dictionary<string, string> _topicToChannelId = new Dictionary<string, string>();

private bool _disposed = false;

#region Events
/// <inheritdoc />
/// <summary>
Expand Down Expand Up @@ -1075,5 +1077,38 @@ public void TestMessageParser(string testJsonString)
{
ParseMessageAsync(testJsonString).GetAwaiter().GetResult();
}

/// <summary>
/// Implement IDisposable.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual async void Dispose(bool disposing)
{
if (_disposed)
return;

if (disposing)
{
await _socket.CloseAsync();
_socket.Dispose();
_previousRequestsSemaphore.Dispose();
_pingTimer.Dispose();
_pongTimer.Dispose();
}

_previousRequests.Clear();
_topicList.Clear();
_disposed = true;
}

~TwitchPubSub()
{
Dispose(false);
}
}
}
Loading