using System;
using System.Collections.Generic;
using System.Linq;
namespace IOTContainer.MvvmBase
{
///
/// 消息基础类
///
public class MessageBase
{
#region 静态函数
private static MessageBase _defult;
///
/// 默认消息体
///
public static MessageBase Defult
{
get
{
if (_defult == null)
_defult = new MessageBase();
return _defult;
}
}
#endregion
#region 构造函数
///
/// 初始化插件消息器
///
public MessageBase()
{
this._messages = new List();
}
#endregion
#region 字段属性
///
/// 消息列表
///
private List _messages;
#endregion
#region 对外方法
///
/// 注册消息
///
/// 接收者
/// 消息令牌
/// 消息操作事件
public void Register(object recipient, string token, MessageHandler handler)
{
if (recipient == null)
throw new Exception("Recipient Is Null!");
if (string.IsNullOrWhiteSpace(token))
throw new Exception("Toke Is Null Or WhiteSpace!");
if (handler == null)
throw new Exception("Handler Is Null!");
this._messages.Add(new MessageItem(recipient, token, handler));
}
///
/// 广播消息
///
/// 发送者
/// 消息令牌
/// 消息信息
public void Send(object sender, string token, MessageArgs args)
{
if (string.IsNullOrWhiteSpace(token))
throw new Exception("Toke Is Null Or WhiteSpace!");
var messages = this._messages.Where(x => x.Token == token);
foreach (var item in messages)
{
item.Do(sender, args);
}
}
///
/// 发送消息
///
/// 发送者
/// 接收者
/// 消息令牌
/// 消息信息
public void Send(object sender, object recipient, string token, MessageArgs args)
{
if (recipient == null)
throw new Exception("Recipient Is Null!");
if (string.IsNullOrWhiteSpace(token))
throw new Exception("Toke Is Null Or WhiteSpace!");
var messages = this._messages.Where(x => x.Recipient == recipient && x.Token == token);
foreach (var item in messages)
{
item.Do(sender, args);
}
}
///
/// 取消注册,移除所有包含recipient的消息
///
/// 接收者
public void Unregister(object recipient)
{
if (recipient == null)
throw new Exception("Recipient Is Null!");
this._messages.RemoveAll(x => x.Recipient == recipient);
}
///
/// 取消注册,移除所有包含recipient,token的消息
///
/// 接收者
/// 消息令牌
public void Unregister(object recipient, string token)
{
if (recipient == null)
throw new Exception("Recipient Is Null!");
if (string.IsNullOrWhiteSpace(token))
throw new Exception("Toke Is Null Or WhiteSpace!");
this._messages.RemoveAll(x => x.Recipient == recipient && x.Token == token);
}
#endregion
#region 消息子项
///
/// 消息子项
///
private class MessageItem : IDisposable
{
public MessageItem(object recipient, string token, MessageHandler handler)
{
this.Recipient = recipient;
this.Token = token;
this._handler = handler;
}
///
/// 注册者
///
public object Recipient { get; private set; }
///
/// 令牌
///
public string Token { get; private set; }
///
/// 操作事件
///
private event MessageHandler _handler;
///
/// 执行发送消息事件
///
/// 发送者
/// 消息
public void Do(object sender, MessageArgs args)
{
this._handler?.Invoke(sender, args);
}
///
/// 销毁程序
///
public void Dispose()
{
this.Recipient = null;
this.Token = null;
this._handler = null;
}
}
#endregion
}
#region 消息委托
///
/// 消息传递委托定义
///
/// 发送者
/// 消息
public delegate void MessageHandler(object sender, MessageArgs e);
///
/// 消息回调委托定义
///
/// 发送者
/// 消息
public delegate void MessageCallBack(object sender, object message);
#endregion
#region 消息体定义
///
/// 消息体
///
public sealed class MessageArgs
{
public static readonly MessageArgs Empty = new MessageArgs();
///
/// 无参构造函数
///
public MessageArgs()
{
this.Message = null;
}
///
/// 构造函数
///
/// 消息
public MessageArgs(object message)
{
this.Message = message;
this._callback = null;
}
///
/// 构造函数
///
/// 消息
/// 回调函数
public MessageArgs(object message, MessageCallBack callback)
{
this.Message = message;
this._callback = callback;
}
///
/// 消息
///
public object Message { get; }
///
/// 操作事件
///
private event MessageCallBack _callback;
///
/// 执行回调事件
///
/// 发送者
/// 消息
public void Callback(object sender, object message)
{
this._callback?.Invoke(sender, message);
}
}
#endregion
}