All checks were successful
Pack and Push NuGet Package / publish (push) Successful in 43s
61 lines
1.8 KiB
C#
61 lines
1.8 KiB
C#
using MessengerApi.Model;
|
|
using portaloggy;
|
|
|
|
namespace MessengerApi
|
|
{
|
|
/// <summary>
|
|
/// This exists so you can mock it.
|
|
/// </summary>
|
|
public interface ISubscriptionClient : IDisposable
|
|
{
|
|
/// <summary>
|
|
/// Subscribes to given message type mask. Thread-safe.
|
|
/// </summary>
|
|
/// <remarks>Expected format of mask: "MY-MESSAGE-TYPE". No wildcards or placeholders, we only compare MessageType.StartsWith using this value.</remarks>
|
|
Subscription Subscribe(string payloadTypeMask);
|
|
|
|
/// <summary>
|
|
/// Unsubscribes from given subscriptions. Thread-safe.
|
|
/// </summary>
|
|
void Unsubscribe(Subscription subscription);
|
|
}
|
|
|
|
/// <summary>
|
|
/// This is where you begin. Instantiate one of these and start subscribing with <see cref="Subscribe(string)"/>.
|
|
/// </summary>
|
|
public class SubscriptionClient : Client, ISubscriptionClient
|
|
{
|
|
private readonly SubscriptionPollingEngine _engine;
|
|
|
|
private bool _isDisposed;
|
|
|
|
public SubscriptionClient(Credentials credentials, HttpClient client = null, ILogger logger = null) : base(credentials, client, logger)
|
|
{
|
|
this._engine = new SubscriptionPollingEngine(this._logger, this);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (this._isDisposed)
|
|
{
|
|
return;
|
|
}
|
|
|
|
this._engine.Dispose();
|
|
this._isDisposed = true;
|
|
}
|
|
|
|
public Subscription Subscribe(string messageTypeMask)
|
|
{
|
|
var sub = new Subscription(messageTypeMask, this);
|
|
this._engine.AddSubscription(sub);
|
|
|
|
return sub;
|
|
}
|
|
|
|
public void Unsubscribe(Subscription subscription)
|
|
{
|
|
this._engine.RemoveSubscription(subscription);
|
|
}
|
|
}
|
|
} |