49 lines
1.4 KiB
C#
49 lines
1.4 KiB
C#
using DaSaSo.ViewModel.Enums;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace DaSaSo.ViewModel
|
|
{
|
|
class Mediator
|
|
{
|
|
private static IDictionary<EMediator, List<Action<object>>> pl_dict = new Dictionary<EMediator, List<Action<object>>>();
|
|
public static void Subscribe(EMediator token, Action<object> callback)
|
|
{
|
|
if (!pl_dict.ContainsKey(token))
|
|
{
|
|
var list = new List<Action<object>>();
|
|
list.Add(callback);
|
|
pl_dict.Add(token, list);
|
|
}
|
|
else
|
|
{
|
|
bool found = false;
|
|
foreach (var item in pl_dict[token])
|
|
{
|
|
if (item.Method.ToString() == callback.Method.ToString())
|
|
found = true;
|
|
}
|
|
if (!found)
|
|
pl_dict[token].Add(callback);
|
|
}
|
|
}
|
|
public static void Unsubscribe(EMediator token, Action<object> callback)
|
|
{
|
|
if (pl_dict.ContainsKey(token))
|
|
pl_dict[token].Remove(callback);
|
|
}
|
|
|
|
public static void Notify(EMediator token, object? args = null)
|
|
{
|
|
if (pl_dict.ContainsKey(token))
|
|
{
|
|
foreach (var callback in pl_dict[token])
|
|
callback(args);
|
|
}
|
|
}
|
|
}
|
|
}
|