//--------------------------------------------------------------------- /// <summary> /// Add the recipientObj - Action to the msgCode. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="who"></param> /// <param name="actionT"></param> /// <returns></returns> public bool AddRecipientObjAction <T>(object who, Action <T> actionT) { RecipientObjAction registInstAction = new RecipientObjAction(who, typeof(T), actionT); _listRecipientObjAction.Add(registInstAction); return(true); }
//--------------------------------------------------------------------- /// <summary> /// Register a msg Code with a generic action. /// Link/Attach a msg code to an action. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="messengerName"></param> /// <param name="handler"></param> /// <returns></returns> public bool Register <T>(string msgCode, object recipient, Action <T> actionT) { _lastErrCode = MessengerErrCode.None; // the syntax code must be ok if (!IsMsgCodeSyntaxOk(msgCode)) { _lastErrCode = MessengerErrCode.MsgCodeSyntaxWrong; return(false); } if (recipient == null) { _lastErrCode = MessengerErrCode.RecipientObjIsNull; return(false); } if (actionT == null) { _lastErrCode = MessengerErrCode.ActionTIsNull; return(false); } // if the code is not already used, create an entry in the dict MsgCodeListAction msgCodeListAction; if (_dictMsgCodeListAction.ContainsKey(msgCode)) { // msg code already exists, get it msgCodeListAction = _dictMsgCodeListAction[msgCode]; } else { // msg code not exists so create it msgCodeListAction = new MsgCodeListAction(msgCode); _dictMsgCodeListAction.TryAdd(msgCode, msgCodeListAction); } // check that the object is already registered with the code msg RecipientObjAction regInstAction = msgCodeListAction.FindByRecipientObj(recipient); if (regInstAction != null) { _lastErrCode = MessengerErrCode.RecipientAlreadyRegisteredOnCodeMsg; return(false); } // add the registered inst-Action to the msgCode msgCodeListAction.AddRecipientObjAction(recipient, actionT); // check the registering msgCodeListAction.FindByRecipientObj(recipient); if (regInstAction != null) { _lastErrCode = MessengerErrCode.RegisterMsgCodeRecipientActionFailed; return(false); } return(true); }
//--------------------------------------------------------------------- /// <summary> /// remove the recipient obj attached to the msgCode (only one instance). /// </summary> /// <param name="recipientObjAction"></param> /// <returns></returns> public bool RemoveRecipientObjAction(RecipientObjAction recipientObjAction) { if (recipientObjAction == null) { return(false); } if (!_listRecipientObjAction.Contains(recipientObjAction)) { return(false); } return(_listRecipientObjAction.Remove(recipientObjAction)); //return _listRecipientObjAction.TryTake(out recipientObjAction); }
//--------------------------------------------------------------------- /// <summary> /// Unregistered the msgcode - recipient object, (no matter of the registered and the type). /// </summary> /// <param name="msgCode"></param> /// <param name="recipient"></param> /// <returns></returns> public bool UnRegister(string msgCode, object recipient) { _lastErrCode = MessengerErrCode.None; // the syntax code must be ok if (!IsMsgCodeSyntaxOk(msgCode)) { _lastErrCode = MessengerErrCode.MsgCodeSyntaxWrong; return(false); } if (recipient == null) { _lastErrCode = MessengerErrCode.RecipientObjIsNull; return(false); } // the code not exists MsgCodeListAction msgCodeListAction; if (!_dictMsgCodeListAction.ContainsKey(msgCode)) { _lastErrCode = MessengerErrCode.MsgCodeNotFound; return(false); } // msg code already exists, get it msgCodeListAction = _dictMsgCodeListAction[msgCode]; // the recipient is attached to the msg code? RecipientObjAction recipientObjAction = msgCodeListAction.FindByRecipientObj(recipient); if (recipientObjAction == null) { _lastErrCode = MessengerErrCode.RecipientNotRegisteredOnCodeMsg; return(false); } // remove the recipient obj attached to the msgCode (only one instance) msgCodeListAction.RemoveRecipientObjAction(recipientObjAction); // remove the MsgCodeListAction is the object is empty (a MsgCode with no recipient) if (msgCodeListAction.Count == 0) { RemoveMsgCodeListAction(msgCodeListAction); } return(true); }