Skip to content

Commit

Permalink
Add some readonly modifiers and applied code style
Browse files Browse the repository at this point in the history
  • Loading branch information
Regenhardt Marlon committed Oct 13, 2019
1 parent 8900ea2 commit d927f2e
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 87 deletions.
61 changes: 26 additions & 35 deletions Examples/Chat/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,45 +32,36 @@ private static async Task Demo()
// Fetch your account's credentials from somewhere.
var auth = new EmailAuthenticationProvider("", "");

// Create an instance of the ActionScheduler. This will
// allow us to execute chat actions like: posting messages,
// kicking users, moving messages, etc.
using (var actionScheduler = new ActionScheduler(auth, roomUrl))
{
// Create an instance of the RoomWatcher class. Here we
// specify (via the type parameter) what WebSocket implementation
// we'd like to use. This class allows you to subscribe to chat events.
using (var roomWatcher = new RoomWatcher<DefaultWebSocket>(auth, roomUrl))
{
// Subscribe to the UserMentioned event.
roomWatcher.AddUserMentionedEventHandler(async m =>
{
await actionScheduler.CreateReplyAsync("hello!", m.MessageId);
// Create an instance of the ActionScheduler. This will
// allow us to execute chat actions like: posting messages,
// kicking users, moving messages, etc.
using var actionScheduler = new ActionScheduler(auth, roomUrl);
// Create an instance of the RoomWatcher class. Here we
// specify (via the type parameter) what WebSocket implementation
// we'd like to use. This class allows you to subscribe to chat events.
using var roomWatcher = new RoomWatcher<DefaultWebSocket>(auth, roomUrl);
// Subscribe to the UserMentioned event.
_ = roomWatcher.AddUserMentionedEventHandler(async m => await actionScheduler.CreateReplyAsync("hello!", m.MessageId));

/// Do stuff ...
});
// Besides being able to subscribe to the default events,
// you can also create (and listen to) your own. Your class must
// implement the ChatEventDataProcessor class, you can also
// optionally implement IChatEventHandler or IChatEventHandler<T>.
var customEventHanlder = new AllData();

// Besides being able to subscribe to the default events,
// you can also create (and listen to) your own. Your class must
// implement the ChatEventDataProcessor class, you can also
// optionally implement IChatEventHandler or IChatEventHandler<T>.
var customEventHanlder = new AllData();
// Add a very basic handler.
customEventHanlder.OnEvent += data => Console.WriteLine(data);

// Add a very basic handler.
customEventHanlder.OnEvent += data => Console.WriteLine(data);
// Add our custom event handler so we can
// begin processing the incoming event data.
roomWatcher.EventRouter.AddProcessor(customEventHanlder);

// Add our custom event handler so we can
// begin processing the incoming event data.
roomWatcher.EventRouter.AddProcessor(customEventHanlder);
// Post a simple message.
var messageId = await actionScheduler.CreateMessageAsync("Hello world.");

// Post a simple message.
var messageId = await actionScheduler.CreateMessageAsync("Hello world.");
while (Console.ReadKey(true).Key != ConsoleKey.Q)
{

while (Console.ReadKey(true).Key != ConsoleKey.Q)
{

}
}
}
}
}
}
}
18 changes: 9 additions & 9 deletions SharpExchange/Api/2.2/ApiRequestScheduler.MasterScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static MasterSheduler()
{
reqs = new Queue<QueuedRequest>();

Task.Run(() => QueueProcessorLoop());
_ = Task.Run(() => QueueProcessorLoop());
}


Expand All @@ -47,7 +47,7 @@ public static void Dispose()
dispose = true;

reqs.Clear();
queueMre.Set();
_ = queueMre.Set();
queueMre.Dispose();
}

Expand All @@ -60,14 +60,14 @@ public static string Schedule(string endpoint)
{
result = x;

mre.Set();
_ = mre.Set();
});

reqs.Enqueue(new QueuedRequest(callback, endpoint));

queueMre.Set();
_ = queueMre.Set();

mre.WaitOne(Timeout);
_ = mre.WaitOne(Timeout);

return result;
}
Expand All @@ -82,8 +82,8 @@ private static void QueueProcessorLoop()
{
if (reqs.Count == 0)
{
queueMre.Reset();
queueMre.WaitOne();
_ = queueMre.Reset();
_ = queueMre.WaitOne();

if (dispose)
{
Expand All @@ -94,8 +94,8 @@ private static void QueueProcessorLoop()
var req = reqs.Dequeue();
var json = HttpRequest.GetAsync(req.Url).Result;

req.Callback.InvokeAsync(json);
mre.WaitOne(waitTime);
_ = req.Callback.InvokeAsync(json);
_ = mre.WaitOne(waitTime);
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions SharpExchange/Api/2.2/ApiRequestScheduler.MethodSheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public MethodSheduler()
{
reqs = new Queue<QueuedRequest>();

Task.Run(() => QueueProcessorLoop());
_ = Task.Run(() => QueueProcessorLoop());
}

~MethodSheduler()
Expand All @@ -53,7 +53,7 @@ public void Dispose()
dispose = true;

reqs.Clear();
queueMre.Set();
_ = queueMre.Set();
queueMre.Dispose();

GC.SuppressFinalize(this);
Expand All @@ -75,14 +75,14 @@ public async Task<Result<T>> ScheduleAsync<T>(string fullUrl)
//TODO: Log this somewhere.
}

mre.Set();
_ = mre.Set();
});

reqs.Enqueue(new QueuedRequest(callback, fullUrl));

queueMre.Set();
_ = queueMre.Set();

await Task.Run(() => mre.WaitOne(Timeout));
_ = await Task.Run(() => mre.WaitOne(Timeout));

return result;
}
Expand All @@ -97,8 +97,8 @@ private void QueueProcessorLoop()
{
if (reqs.Count == 0)
{
queueMre.Reset();
queueMre.WaitOne();
_ = queueMre.Reset();
_ = queueMre.WaitOne();

if (dispose)
{
Expand All @@ -109,7 +109,7 @@ private void QueueProcessorLoop()
var req = reqs.Dequeue();
var json = MasterSheduler.Schedule(req.Url);

req.Callback.InvokeAsync(json);
_ = req.Callback.InvokeAsync(json);

var backoff = 0;

Expand All @@ -124,7 +124,7 @@ private void QueueProcessorLoop()

if (backoff != 0)
{
mre.WaitOne(backoff * 1000);
_ = mre.WaitOne(backoff * 1000);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions SharpExchange/Api/2.2/ApiRequestScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ private static string GetEndpointId(string url)
{
if (string.IsNullOrEmpty(s) || (s.Any(char.IsDigit) && s != "2.2")) continue;

id.Append(s);
id.Append('/');
_ = id.Append(s);
_ = id.Append('/');
}

return id.ToString();
Expand Down
16 changes: 8 additions & 8 deletions SharpExchange/Chat/Actions/ActionScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace SharpExchange.Chat.Actions
{
public class ActionScheduler : IDisposable
{
private IAuthenticationProvider auth;
private readonly IAuthenticationProvider auth;
private readonly ManualResetEvent queueMre;
private readonly Queue<ChatAction> actionQueue;
private bool dispose;
Expand All @@ -36,7 +36,7 @@ public ActionScheduler(IAuthenticationProvider authProvider, string roomUrl)
Host = host.GetChatHost();
RoomId = id;

Task.Run(new Action(ProcessQueue));
_ = Task.Run(new Action(ProcessQueue));
}

public ActionScheduler(IAuthenticationProvider authProvider, string host, int roomId)
Expand All @@ -56,7 +56,7 @@ public ActionScheduler(IAuthenticationProvider authProvider, string host, int ro
Host = host.GetChatHost();
RoomId = roomId;

Task.Run(new Action(ProcessQueue));
_ = Task.Run(new Action(ProcessQueue));
}

~ActionScheduler()
Expand All @@ -71,7 +71,7 @@ public void Dispose()
if (dispose) return;
dispose = true;

queueMre.Set();
_ = queueMre.Set();
queueMre.Dispose();
actionQueue.Clear();

Expand All @@ -93,14 +93,14 @@ public async Task<T> ScheduleActionAsync<T>(ChatAction act, TimeSpan timeout)
act.CallBack = new Action<object>(x =>
{
data = x;
wait.Set();
_ = wait.Set();
});

actionQueue.Enqueue(act);

queueMre.Set();
_ = queueMre.Set();

await Task.Run(() => wait.WaitOne(timeout));
_ = await Task.Run(() => wait.WaitOne(timeout));

return (T)data;
}
Expand All @@ -113,7 +113,7 @@ private void ProcessQueue()
{
if (actionQueue.Count == 0)
{
queueMre.Reset();
_ = queueMre.Reset();
}

if (queueMre.WaitOne() && dispose)
Expand Down
4 changes: 2 additions & 2 deletions SharpExchange/Chat/Events/EventRouter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace SharpExchange.Chat.Events
{
public sealed class EventRouter : IDisposable
{
private List<ChatEventDataProcessor> processors;
private readonly List<ChatEventDataProcessor> processors;
private bool dispose;

public int RoomId { get; private set; }
Expand Down Expand Up @@ -99,7 +99,7 @@ private void HandleNewMessage(string json)
continue;
}

Task.Run(() => processor.ProcessEventData(ev));
_ = Task.Run(() => processor.ProcessEventData(ev));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion SharpExchange/Chat/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ private MessageStates GetStates(IHtmlDocument dom)

for (var i = 0; i < childCount; i++)
{
content.RemoveChild(content.Children[0]);
_ = content.RemoveChild(content.Children[0]);
}

var isPuregd = content.TextContent.Trim() == "(older data no longer available)";
Expand Down
2 changes: 1 addition & 1 deletion SharpExchange/Chat/Room.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ private User[] GetUsers(IHtmlCollection<IElement> userElements)
?.Value
.Split()[0];

int.TryParse(msgCountStr ?? "0", out var msgCount);
_ = int.TryParse(msgCountStr ?? "0", out var msgCount);

users[i] = new User
{
Expand Down
2 changes: 1 addition & 1 deletion SharpExchange/Chat/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ private Room[] GetRooms(IHtmlCollection<IElement> roomElements)
?.Value
.Split()[0];

int.TryParse(msgCountStr ?? "0", out var msgCount);
_ = int.TryParse(msgCountStr ?? "0", out var msgCount);

rooms[i] = new Room
{
Expand Down
10 changes: 5 additions & 5 deletions SharpExchange/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@ public static string ToQueryString(this Dictionary<string, string> d)
{
var encoded = WebUtility.UrlEncode(kv.Value);

builder.Append(kv.Key);
builder.Append('=');
builder.Append(encoded);
builder.Append('&');
_ = builder.Append(kv.Key);
_ = builder.Append('=');
_ = builder.Append(encoded);
_ = builder.Append('&');
}

builder.Remove(builder.Length - 1, 1);
_ = builder.Remove(builder.Length - 1, 1);

return builder.ToString();
}
Expand Down
4 changes: 2 additions & 2 deletions SharpExchange/Net/CookieManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace SharpExchange.Net
{
public class CookieManager
{
private Dictionary<string, Cookie> cookies;
private readonly Dictionary<string, Cookie> cookies;

public Cookie this[string name] => cookies[name];

Expand Down Expand Up @@ -59,7 +59,7 @@ public void Add(Cookie cookie)
{
if (cookie.Expired)
{
cookies.Remove(cookie.Name);
_ = cookies.Remove(cookie.Name);
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions SharpExchange/Net/HttpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ public async Task<RestResponse> SendAsync()

foreach (var c in valIdCookies)
{
request.AddCookie(c.Name, c.Value);
_ = request.AddCookie(c.Name, c.Value);
}
}

if (Data != null)
{
foreach (var k in Data.Keys)
{
request.AddParameter(k, Data[k]);
_ = request.AddParameter(k, Data[k]);
}
}

Expand Down
Loading

0 comments on commit d927f2e

Please sign in to comment.