You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
4.2 KiB
C#

1 month ago
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;
//using DQ.Construction.NewLook.Utility;
using WorkData;
using WorkData.Entity;
#region 文件说明
/*-----------------------------------------------------
* c 2024 jdfcd
* CLR 4.0.30319.42000
* DQ.Construction.NewLook.DataManager
*
* pnpe
* pnpe@qq.com
* 2024/6/19 12:01:26
*/
#endregion 文件说明
namespace WellWorkDataUI
{
/// <summary>
/// The k e p data manager.
/// </summary>
public static class KEPDataManager
{
// 使用字典来存储不同类型的事件
private static Dictionary<Type, Dictionary<string, Action>> subscriptions = new Dictionary<Type, Dictionary<string, Action>>();
/// <summary>
/// 基础
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="name"></param>
/// <param name="callback"></param>
/// <param name="loadCallBack"></param>
private static void Subscribe<T>(string name, Action callback, bool loadCallBack = false)
where T : class
{
string pageUniqueValue = name;
var type = typeof(T);
if (!subscriptions.ContainsKey(type))
{
subscriptions[type] = new Dictionary<string, Action>();
}
if (!subscriptions[type].ContainsKey(pageUniqueValue))
{
subscriptions[type].Add(pageUniqueValue, callback);
}
else
{
subscriptions[type][pageUniqueValue] = callback;
}
if (loadCallBack)
{
NotifyChange<T>();
}
}
public static void Subscribe<T>(this Form form, Action callback, bool loadCallBack = false)
where T : class
{
Subscribe<T>(form.Name, callback, loadCallBack);
}
/// <summary>
/// 订阅方法
/// </summary>
/// <param name="customUserControl">The page unique value.</param>
/// <param name="callback">The callback.</param>
/// <param name="loadCallBack">If true, load call back.</param>
//public static void Subscribe<T>(this CustomUserControl customUserControl, Action callback, bool loadCallBack = false)
// where T : class
//{
// Subscribe<T>(customUserControl.Name, callback, loadCallBack);
//}
///// <summary>
///// 订阅方法
///// </summary>
///// <param name="customUserControl">The page unique value.</param>
//public static void UnSubscribe<T>(this CustomUserControl customUserControl)
// where T : class
//{
// string pageUniqueValue = customUserControl.Name;
// var type = typeof(T);
// if (!subscriptions.ContainsKey(type))
// {
// subscriptions[type] = new Dictionary<string, Action>();
// }
// if (subscriptions[type].ContainsKey(pageUniqueValue))
// {
// subscriptions[type].Remove(pageUniqueValue);
// }
//}
// 触发事件的方法
/// <summary>
/// Ons the data changed.
/// </summary>
/// <param name="t">The t.</param>
public static void NotifyChange<T>()
where T : class
{
Task.Run(() =>
{
var type = typeof(T);
if (subscriptions.ContainsKey(type))
{
foreach (var subscription in subscriptions[type].Values)
{
try
{
subscription?.Invoke();
}
catch (Exception ex)
{
//Log
Reporter.WriteMsg($"数据管理推送数据:" + ex.Message);
}
}
}
if (type == typeof(WellDeflection))
{
//清理井斜相关缓存
CalcDataHelp.Reset();
}
});
}
}
}