Skip to content

Commit

Permalink
Replace events reflection (#237)
Browse files Browse the repository at this point in the history
* Removed unused reflection events, properly replaced throttling error events, fixed typo in throttled message

* Updated EventInvocationExtensions with file scope namespace
  • Loading branch information
Bukk94 authored Jul 30, 2023
1 parent 46811c9 commit 761e2da
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 729 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,5 @@ $RECYCLE.BIN/

# Windows shortcuts
*.lnk

.idea/*
657 changes: 0 additions & 657 deletions TwitchLib.Client/Extensions/EventInvocationExt.cs

This file was deleted.

17 changes: 17 additions & 0 deletions TwitchLib.Client/Extensions/EventInvocationExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using TwitchLib.Communication.Events;

namespace TwitchLib.Client.Extensions;

/// <summary>
/// Extends logic for handling events.
/// </summary>
public static class EventInvocationExtensions
{
/// <summary>
/// Invokes the event handler when it is not null. Returns a completed task otherwise.
/// </summary>
internal static Task TryInvoke<TEventArgs>(this AsyncEventHandler<TEventArgs> eventHandler, object sender, TEventArgs eventArgs)
{
return eventHandler?.Invoke(sender, eventArgs) ?? Task.CompletedTask;
}
}
39 changes: 0 additions & 39 deletions TwitchLib.Client/Internal/EventHelper.cs

This file was deleted.

34 changes: 16 additions & 18 deletions TwitchLib.Client/Throttling/ThrottlingService.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using Microsoft.Extensions.Logging;
using TwitchLib.Client.Events;
using TwitchLib.Client.Extensions;
using TwitchLib.Client.Internal;
using TwitchLib.Client.Models;
using TwitchLib.Client.Models.Interfaces;
using TwitchLib.Communication.Events;
Expand All @@ -24,6 +20,7 @@ internal class ThrottlingService
private Task _sendTask;

internal event AsyncEventHandler<OnMessageThrottledArgs> OnThrottled;
internal event AsyncEventHandler<OnErrorEventArgs> OnError;

internal ThrottlingService(
IClient client,
Expand All @@ -35,14 +32,14 @@ internal ThrottlingService(
_sendOptions = messageSendOptions ?? new SendOptions();
_throttler = new Throttler(_sendOptions);

_client.OnConnected -= StartThrottler;
_client.OnConnected += StartThrottler;
_client.OnConnected -= StartThrottlerAsync;
_client.OnConnected += StartThrottlerAsync;

_client.OnReconnected -= StartThrottler;
_client.OnReconnected += StartThrottler;
_client.OnReconnected -= StartThrottlerAsync;
_client.OnReconnected += StartThrottlerAsync;

_client.OnDisconnected -= StopThrottler;
_client.OnDisconnected += StopThrottler;
_client.OnDisconnected -= StopThrottlerAsync;
_client.OnDisconnected += StopThrottlerAsync;
}

/// <summary>
Expand All @@ -69,7 +66,7 @@ internal bool Enqueue(OutboundChatMessage message)
return true;
}

private Task StartThrottler(object sender, OnConnectedEventArgs args)
private Task StartThrottlerAsync(object sender, OnConnectedEventArgs args)
{
// Cancel old token first
_tokenSource.Cancel();
Expand All @@ -78,7 +75,7 @@ private Task StartThrottler(object sender, OnConnectedEventArgs args)
return Task.CompletedTask;
}

private Task StopThrottler(object sender, OnDisconnectedEventArgs args)
private Task StopThrottlerAsync(object sender, OnDisconnectedEventArgs args)
{
_tokenSource.Cancel();
return Task.CompletedTask;
Expand Down Expand Up @@ -118,7 +115,7 @@ private async Task TrySendAsync()
// cause Throttle raises the corresponding Event with the needed information.
if (_throttler.ShouldThrottle())
{
await ThrottleMessage(message.Item2);
await ThrottleMessageAsync(message.Item2);
return;
}

Expand All @@ -136,21 +133,22 @@ private async Task TrySendAsync()
catch (Exception ex)
{
_logger?.LogException(ex.Message, ex);
await _client.RaiseEvent(nameof(_client.OnError), new OnErrorEventArgs(ex));

await OnError.TryInvoke(this, new OnErrorEventArgs(ex));
}
}

private async Task ThrottleMessage(OutboundChatMessage itemNotSent)
private Task ThrottleMessageAsync(OutboundChatMessage itemNotSent)
{
const string msg = "Message Throttle Occured. Too Many Messages within the period specified in WebsocketClientOptions.";
const string msg = "Message Throttle Occured. Too Many Messages within the period specified in ISendOptions.";

var args = new OnMessageThrottledArgs(
msg,
itemNotSent,
_sendOptions.ThrottlingPeriod,
_sendOptions.SendsAllowedInPeriod);

if (OnThrottled != null) await OnThrottled?.Invoke(null, args);
return OnThrottled.TryInvoke(null, args);
}
}
}
26 changes: 11 additions & 15 deletions TwitchLib.Client/TwitchClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ private void InitializeClient()

_throttling = new ThrottlingService(_client, _sendOptions, _logger);
_throttling.OnThrottled += OnThrottled;
_throttling.OnError += ThrottlerOnError;

_client.OnConnected += _client_OnConnectedAsync;
_client.OnMessage += _client_OnMessage;
Expand All @@ -560,21 +561,6 @@ private void InitializeClient()
}
#endregion

/// <summary>
/// Raises the event.
/// </summary>
/// <param name="eventName">Name of the event.</param>
/// <param name="args">The arguments.</param>
internal async Task RaiseEvent(string eventName, object args = null)
{
await EventHelper.RaiseEvent(this, eventName, args);
}

private Task OnThrottled(object sender, OnMessageThrottledArgs e)
{
return RaiseEvent(nameof(OnMessageThrottled), e);
}

/// <summary>
/// Sends a RAW IRC message.
/// </summary>
Expand Down Expand Up @@ -908,6 +894,16 @@ public Task OnReadLineTestAsync(string rawIrc)
}

#region Client Events
private Task OnThrottled(object sender, OnMessageThrottledArgs e)
{
return OnMessageThrottled.TryInvoke(sender, e);
}

private Task ThrottlerOnError(object sender, OnErrorEventArgs e)
{
return OnError.TryInvoke(sender, e);
}

/// <summary>
/// Handles the OnFatality event of the _client control.
/// </summary>
Expand Down

0 comments on commit 761e2da

Please sign in to comment.