You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
HighGroundRoyaleNetcode/Assets/Scripts/Infrastructure/PubSub/DisposableSubscription.cs

38 lines
997 B
C#

3 weeks ago
using System;
namespace Unity.BossRoom.Infrastructure
{
/// <summary>
/// This class is a handle to an active Message Channel subscription and when disposed it unsubscribes from said channel.
/// </summary>
/// <typeparam name="T"></typeparam>
public class DisposableSubscription<T> : IDisposable
{
Action<T> m_Handler;
bool m_IsDisposed;
IMessageChannel<T> m_MessageChannel;
public DisposableSubscription(IMessageChannel<T> messageChannel, Action<T> handler)
{
m_MessageChannel = messageChannel;
m_Handler = handler;
}
public void Dispose()
{
if (!m_IsDisposed)
{
m_IsDisposed = true;
if (!m_MessageChannel.IsDisposed)
{
m_MessageChannel.Unsubscribe(m_Handler);
}
m_Handler = null;
m_MessageChannel = null;
}
}
}
}