Minor improvements

This commit is contained in:
Cameron
2024-03-24 08:07:44 -05:00
parent e9bffa4dea
commit 81651a0e59
4 changed files with 17 additions and 24 deletions

View File

@@ -85,9 +85,6 @@ namespace TwitchIrcClient.IRC.Messages
public string Id => TryGetTag("id"); public string Id => TryGetTag("id");
public UserNoticeType? UserNoticeType => Enum.TryParse(TryGetTag("msg-id"), out UserNoticeType type) public UserNoticeType? UserNoticeType => Enum.TryParse(TryGetTag("msg-id"), out UserNoticeType type)
? type : null; ? type : null;
/// <summary>
///
/// </summary>
public string Login => TryGetTag("login"); public string Login => TryGetTag("login");
/// <summary> /// <summary>
/// Whether the user is a moderator in this channel /// Whether the user is a moderator in this channel
@@ -263,21 +260,15 @@ namespace TwitchIrcClient.IRC.Messages
public UserType UserType public UserType UserType
{ get { get
{ {
if (!MessageTags.TryGetValue("user-type", out string? value)) var value = TryGetTag("user-type");
return UserType.None; return value.ToUpper() switch
switch (value.ToUpper())
{ {
case "ADMIN": "ADMIN" => UserType.Admin,
return UserType.Admin; "GLOBAL_MOD" => UserType.GlobalMod,
case "GLOBAL_MOD": "STAFF" => UserType.Staff,
return UserType.GlobalMod; "" => UserType.Normal,
case "STAFF": _ => UserType.Normal,
return UserType.Staff; };
case "":
return UserType.Normal;
default:
throw new InvalidDataException();
}
} }
} }
/// <summary> /// <summary>

View File

@@ -88,7 +88,9 @@ namespace TwitchIrcClient.IRC
{ {
lock (Semaphore) lock (Semaphore)
{ {
Semaphore.Release(MessageLimit - Semaphore.CurrentCount); var count = MessageLimit - Semaphore.CurrentCount;
if (count > 0)
Semaphore.Release(count);
} }
} }
catch (SemaphoreFullException) { } catch (SemaphoreFullException) { }
@@ -96,17 +98,17 @@ namespace TwitchIrcClient.IRC
} }
#region RateLimiter Dispose #region RateLimiter Dispose
private bool disposedValue; //https://stackoverflow.com/questions/8927878/what-is-the-correct-way-of-adding-thread-safety-to-an-idisposable-object
private int _disposedCount;
protected virtual void Dispose(bool disposing) protected virtual void Dispose(bool disposing)
{ {
if (!disposedValue) if (Interlocked.Increment(ref _disposedCount) == 1)
{ {
if (disposing) if (disposing)
{ {
Semaphore?.Dispose(); Semaphore?.Dispose();
Timer?.Dispose(); Timer?.Dispose();
} }
disposedValue = true;
} }
} }

View File

@@ -52,7 +52,7 @@ async Task<IrcConnection> CreateConnection(string channel)
Console.Write("Channel: "); Console.Write("Channel: ");
var channelName = Console.ReadLine(); var channelName = Console.ReadLine();
ArgumentNullException.ThrowIfNull(channelName, nameof(Channel)); ArgumentNullException.ThrowIfNull(channelName, nameof(Channel));
var connection = CreateConnection(channelName); var connection = await CreateConnection(channelName);
while (true) while (true)
{ {
//all the work happens in other threads //all the work happens in other threads