Skip to content

GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.

feat: successfully invoke appropriate command handler through Bus

Larse3280e3d5de4
diff --git a/LarsWM.Core/Common/Services/Bus.cs b/LarsWM.Core/Common/Services/Bus.cs
index b1d95c36..f07d9aa0 100644
--- a/LarsWM.Core/Common/Services/Bus.cs
+++ b/LarsWM.Core/Common/Services/Bus.cs
@@ -1,6 +1,7 @@
-using LarsWM.Core.Common.Models;
+using LarsWM.Core.Common.Models;
using System;
using System.Collections.Generic;
+using System.Linq;
using System.Text;
namespace LarsWM.Core.Common.Services
@@ -16,16 +17,29 @@ namespace LarsWM.Core.Common.Services
/// <summary>
/// Sends command to appropriate command handler.
/// </summary>
- public void Invoke<T>(T command) where T : ICommand<object>
+ public void Invoke<T>(T command) where T : Command
{
- // try-catch here? perhaps commands should always return CommandResponse (has State - failure,success, and Data - any return value from command)?
- throw new NotImplementedException();
+ var commandType = typeof(T);
+
+ // TODO: add centralised error handling here?
+ // TODO: create CommandResponse interface with State (failure, success) and Data (any return value from command)?
+ foreach (var handler in RegisteredCommandHandlers)
+ {
+ var handlerInterface = handler.GetInterfaces()[0];
+ var handlerCommandType = handlerInterface.GetGenericArguments()[0];
+
+ if (handlerCommandType == commandType)
+ {
+ ICommandHandler<T> handlerInstance = Program.ServiceProvider.GetService(handler) as ICommandHandler<T>;
+ handlerInstance.Handle(command);
+ }
+ }
}
/// <summary>
/// Sends event to appropriate event handler.
/// </summary>
- public void RaiseEvent<T>(T @event) where T : IEvent
+ public void RaiseEvent<T>(T @event) where T : Event
{
throw new NotImplementedException();
}
diff --git a/LarsWM.Core/Common/Services/IBus.cs b/LarsWM.Core/Common/Services/IBus.cs
index dfdff9c5..5b7cd5f6 100644
--- a/LarsWM.Core/Common/Services/IBus.cs
+++ b/LarsWM.Core/Common/Services/IBus.cs
@@ -7,8 +7,8 @@ namespace LarsWM.Core.Common.Services
{
public interface IBus
{
- void Invoke<T>(T command) where T : ICommand<object>;
- void RaiseEvent<T>(T @event) where T : IEvent;
+ void Invoke<T>(T command) where T : Command;
+ void RaiseEvent<T>(T @event) where T : Event;
void RegisterCommandHandler<T>();
void RegisterEventHandler<T>();
}
diff --git a/LarsWM.Core/Program.cs b/LarsWM.Core/Program.cs
index 9507aed7..8ac1e1e0 100644
--- a/LarsWM.Core/Program.cs
+++ b/LarsWM.Core/Program.cs
@@ -11,7 +11,7 @@ namespace LarsWM.Core
{
class Program
{
- public static ServiceCollection ServiceCollection { get; private set; } = new ServiceCollection();
+ public static IServiceProvider ServiceProvider { get; private set; }
/// <summary>
/// The main entry point for the application.
@@ -21,25 +21,28 @@ namespace LarsWM.Core
{
Debug.WriteLine("Application started");
- ConfigureServices();
+ var serviceCollection = CreateServiceCollection();
- var serviceProvider = ServiceCollection.BuildServiceProvider();
-
- var bus = serviceProvider.GetRequiredService<IBus>();
+ ServiceProvider = serviceCollection.BuildServiceProvider();
+ var bus = ServiceProvider.GetRequiredService<IBus>();
bus.RegisterCommandHandler<AddMonitorHandler>();
bus.RegisterEventHandler<MonitorAddedHandler>();
- var startup = serviceProvider.GetRequiredService<Startup>();
+ var startup = ServiceProvider.GetRequiredService<Startup>();
startup.Init();
}
- private static void ConfigureServices()
+ private static ServiceCollection CreateServiceCollection()
{
- ServiceCollection.AddSingleton<IBus, Bus>();
- ServiceCollection.AddSingleton<AddMonitorHandler>();
- ServiceCollection.AddSingleton<MonitorAddedHandler>();
- ServiceCollection.AddSingleton<Startup>();
+ var serviceCollection = new ServiceCollection();
+
+ serviceCollection.AddSingleton<IBus, Bus>();
+ serviceCollection.AddSingleton<AddMonitorHandler>();
+ serviceCollection.AddSingleton<MonitorAddedHandler>();
+ serviceCollection.AddSingleton<Startup>();
+
+ return serviceCollection;
}
}
}