Skip to content

Commit

Permalink
Merge pull request #125 from TwitchLib/implement_disposable
Browse files Browse the repository at this point in the history
Implement disposable (ported from #115)
  • Loading branch information
Syzuna authored Dec 25, 2023
2 parents cc34a1f + 4da01ff commit 90193f4
Showing 1 changed file with 36 additions and 1 deletion.
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);
}
}
}

0 comments on commit 90193f4

Please sign in to comment.