add start of mediator
This commit is contained in:
3
MareSynchronos/Mediator/IMessage.cs
Normal file
3
MareSynchronos/Mediator/IMessage.cs
Normal file
@@ -0,0 +1,3 @@
|
||||
namespace MareSynchronos.Mediator;
|
||||
|
||||
public interface IMessage { }
|
||||
51
MareSynchronos/Mediator/MareMediator.cs
Normal file
51
MareSynchronos/Mediator/MareMediator.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using MareSynchronos.Utils;
|
||||
|
||||
namespace MareSynchronos.Mediator;
|
||||
|
||||
public class MareMediator : IDisposable
|
||||
{
|
||||
private record MediatorSubscriber(object Subscriber, Action<IMessage> Action);
|
||||
|
||||
private readonly Dictionary<Type, HashSet<MediatorSubscriber>> _subscriberDict = new();
|
||||
|
||||
public void Subscribe<T>(object subscriber, Action<IMessage> action) where T : IMessage
|
||||
{
|
||||
_subscriberDict.TryAdd(typeof(T), new HashSet<MediatorSubscriber>());
|
||||
|
||||
if (!_subscriberDict[typeof(T)].Add(new(subscriber, action)))
|
||||
{
|
||||
throw new InvalidOperationException("Already subscribed");
|
||||
}
|
||||
}
|
||||
|
||||
public void Unsubscribe<T>(object subscriber) where T : IMessage
|
||||
{
|
||||
if (_subscriberDict.TryGetValue(typeof(T), out var subscribers))
|
||||
{
|
||||
subscribers.RemoveWhere(p => p.Subscriber == subscriber);
|
||||
}
|
||||
}
|
||||
|
||||
public void Publish(IMessage message)
|
||||
{
|
||||
if (_subscriberDict.TryGetValue(message.GetType(), out var subscribers))
|
||||
{
|
||||
foreach (var subscriber in subscribers)
|
||||
{
|
||||
try
|
||||
{
|
||||
subscriber.Action.Invoke(message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Error("Error executing " + subscriber.Action.Method, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_subscriberDict.Clear();
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ using Dalamud.Game.Gui;
|
||||
using MareSynchronos.Export;
|
||||
using Dalamud.Data;
|
||||
using MareSynchronos.MareConfiguration;
|
||||
using MareSynchronos.Mediator;
|
||||
|
||||
namespace MareSynchronos;
|
||||
|
||||
@@ -46,6 +47,7 @@ public sealed class Plugin : IDalamudPlugin
|
||||
private readonly ServerConfigurationManager _serverConfigurationManager;
|
||||
private readonly GposeUi _gposeUi;
|
||||
private readonly ConfigurationService _configurationService;
|
||||
private readonly MareMediator _mediator;
|
||||
|
||||
|
||||
public Plugin(DalamudPluginInterface pluginInterface, CommandManager commandManager, DataManager gameData,
|
||||
@@ -56,6 +58,7 @@ public sealed class Plugin : IDalamudPlugin
|
||||
_pluginInterface.UiBuilder.DisableGposeUiHide = true;
|
||||
_commandManager = commandManager;
|
||||
_configurationService = new(_pluginInterface);
|
||||
_mediator = new();
|
||||
|
||||
_localization = new Dalamud.Localization("MareSynchronos.Localization.", "", useEmbedded: true);
|
||||
_localization.SetupWithLangCode("en");
|
||||
@@ -138,6 +141,9 @@ public sealed class Plugin : IDalamudPlugin
|
||||
_transientResourceManager?.Dispose();
|
||||
_dalamudUtil.Dispose();
|
||||
_configurationService?.Dispose();
|
||||
|
||||
_mediator.Dispose();
|
||||
|
||||
Logger.Debug("Shut down");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user