Skip to content

Commit

Permalink
implement Disposable (ported from #115)
Browse files Browse the repository at this point in the history
  • Loading branch information
swiftyspiffy committed Dec 24, 2023
1 parent cc34a1f commit 4da01ff
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 4da01ff

Please sign in to comment.