Website Summary Files Commits Branches @commit @diff @stats
path: root / src / Web / PUSG.Web / Events / EventService.cs
diff options
context:
space:
mode:
Commit1489a24d74985ef9e51fd9976fc580857062011c (patch)
Author
 ivaylo.zhelev <ivaylo.zhelev@solarwinds.com>
2021-02-28 14:22:18 +0200
Committer
 ivaylo.zhelev <ivaylo.zhelev@solarwinds.com>
2021-02-28 14:22:18 +0200
Filesd98fea7203c6dfabad2c692b1c9848e94fd69e76 /src/Web/PUSG.Web/Events/EventService.cs
pusg-api-main.tar
pusg-api-main.tar.gz
pusg-api-main.zip
Parent5bd0777227894df6204cd8e52d19f019f296e910 (diff)
Added Event Service HEAD main
* Major Refactoring to Services (moved to correct namespace) * Fixed bug on Disconnect for HomeHub * Fixed bug with multiple notifications of end game (introduced new bug to fix later)
1 files changed, 41 insertions, 0 deletions
-rw-r--r--src/Web/PUSG.Web/Events/EventService.cs41
diff --git a/src/Web/PUSG.Web/Events/EventService.cs b/src/Web/PUSG.Web/Events/EventService.cs
new file mode 100644
index 0000000..8ab1b68
--- /dev/null
+++ b/src/Web/PUSG.Web/Events/EventService.cs
@@ -0,0 +1,41 @@
+using PUSG.Web.Events.Api;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace PUSG.Web.Events
+{
+ public class EventService : IEventService
+ {
+ private List<IEventHandler> eventHandlers;
+
+ public EventService()
+ {
+ this.eventHandlers = new List<IEventHandler>();
+ }
+
+ private void DisposeUnavailable()
+ {
+ this.eventHandlers.RemoveAll(eventHandler => !eventHandler.IsAvailable());
+ }
+
+ public IEnumerable<IEventHandler> EventHandlers => this.eventHandlers;
+
+ public async Task Publish(string eventName, params object[] args)
+ {
+ List<IEventHandler> availableHandlers = this.EventHandlers.Where(eventHandler => eventHandler.IsAvailable()).ToList();
+
+ foreach (var eventHandler in availableHandlers)
+ {
+ await eventHandler.HandleEvent(eventName, args);
+ }
+
+ this.DisposeUnavailable();
+ }
+
+ public void Subscribe(IEventHandler eventHandler)
+ {
+ this.eventHandlers.Add(eventHandler);
+ }
+ }
+}