コード例 #1
0
        public void RegisterNotification()
        {
            NotificationName notification          = null;
            Action <INotificationArguments> action = null;

            var service = new Mock <IUserInterfaceService>();
            {
                service.Setup(s => s.RegisterNotification(It.IsAny <NotificationName>(), It.IsAny <Action <INotificationArguments> >()))
                .Callback <NotificationName, Action <INotificationArguments> >(
                    (n, o) =>
                {
                    notification = n;
                    action       = o;
                });
            }

            var notificationNames = new Mock <INotificationNameConstants>();
            {
                notificationNames.Setup(n => n.SystemShuttingDown)
                .Returns(new NotificationName("a"));
            }

            var systemDiagnostics = new SystemDiagnostics((p, s) => { }, null);
            var facade            = new ApplicationFacade(service.Object, notificationNames.Object, systemDiagnostics);

            var name = new NotificationName("bla");
            Action <INotificationArguments> callback = o => { };

            facade.RegisterNotification(name, callback);

            Assert.AreSame(name, notification);
            Assert.AreSame(callback, action);
        }
コード例 #2
0
        public void RegisterNotification()
        {
            NotificationName notification = null;
            Action<INotificationArguments> action = null;

            var service = new Mock<IUserInterfaceService>();
            {
                service.Setup(s => s.RegisterNotification(It.IsAny<NotificationName>(), It.IsAny<Action<INotificationArguments>>()))
                    .Callback<NotificationName, Action<INotificationArguments>>(
                        (n, o) =>
                            {
                                notification = n;
                                action = o;
                            });
            }

            var notificationNames = new Mock<INotificationNameConstants>();
            {
                notificationNames.Setup(n => n.SystemShuttingDown)
                    .Returns(new NotificationName("a"));
            }

            var systemDiagnostics = new SystemDiagnostics((p, s) => { }, null);
            var facade = new ApplicationFacade(service.Object, notificationNames.Object, systemDiagnostics);

            var name = new NotificationName("bla");
            Action<INotificationArguments> callback = o => { };
            facade.RegisterNotification(name, callback);

            Assert.AreSame(name, notification);
            Assert.AreSame(callback, action);
        }
コード例 #3
0
ファイル: View.cs プロジェクト: mengtest/RewriteFrame
        /// <summary>
        /// Remove the observer for a given notifyContext from an observer list for a given Notification name.
        /// </summary>
        /// <param name="notificationName">which observer list to remove from </param>
        /// <param name="notifyContext">remove the observer with this object as its notifyContext</param>
        public virtual void RemoveObserver(NotificationName notificationName, object notifyContext)
        {
            // the observer list for the notification under inspection
            if (observerMap.TryGetValue(notificationName, out IList <IObserver> observers))
            {
                // find the observer for the notifyContext
                for (int i = 0; i < observers.Count; i++)
                {
                    if (observers[i].CompareNotifyContext(notifyContext))
                    {
                        // there can only be one Observer for a given notifyContext
                        // in any given Observer list, so remove it and break
                        observers.RemoveAt(i);
                        break;
                    }
                }

                // Also, when a Notification's Observer list length falls to
                // zero, delete the notification key from the observer map
                if (observers.Count == 0)
                {
                    observerMap.TryRemove(notificationName, out IList <IObserver> _);
                }
            }
        }
コード例 #4
0
 /// <summary>
 /// Remove a previously registered <c>ICommand</c> to <c>INotification</c> mapping.
 /// </summary>
 /// <param name="notificationName">the name of the <c>INotification</c> to remove the <c>ICommand</c> mapping for</param>
 public virtual void RemoveCommand(NotificationName notificationName)
 {
     if (commandMap.TryRemove(notificationName, out Func <ICommand> _))
     {
         view.RemoveObserver(notificationName, this);
     }
 }
コード例 #5
0
 public Notification(Component componentSender, NotificationName notificationName,
                     NotificationArgs extraArgs = null, Component componentReciever = null)
 {
     sender   = componentSender;
     reciever = componentReciever;
     name     = notificationName;
     args     = extraArgs;
 }
コード例 #6
0
 /// <summary>
 /// Register a particular <c>ICommand</c> class as the handler
 /// for a particular <c>INotification</c>.
 /// </summary>
 /// <remarks>
 ///     <para>
 ///         If a <c>ICommand</c> has already been registered to
 ///         handle <c>INotification</c>s with this name, it is no longer
 ///         used, the new <c>Func</c> is used instead.
 ///     </para>
 ///     <para>
 ///         The Observer for the new ICommand is only created if this the
 ///         first time an ICommand has been regisered for this Notification name.
 ///     </para>
 /// </remarks>
 /// <param name="notificationName">the name of the <c>INotification</c></param>
 /// <param name="commandFunc">the <c>Func Delegate</c> of the <c>ICommand</c></param>
 public virtual void RegisterCommand(NotificationName notificationName, Func <ICommand> commandFunc)
 {
     if (commandMap.TryGetValue(notificationName, out Func <ICommand> _) == false)
     {
         view.RegisterObserver(notificationName, new Observer(ExecuteCommand, this));
     }
     commandMap[notificationName] = commandFunc;
 }
コード例 #7
0
        /// <summary>
        /// Registers the notification.
        /// </summary>
        /// <param name="name">The name of the notification.</param>
        /// <param name="callback">The callback method that is called when the notification is activated.</param>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if <paramref name="name"/> is <see langword="null" />.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if <paramref name="callback"/> is <see langword="null" />.
        /// </exception>
        public void RegisterNotification(NotificationName name, Action <INotificationArguments> callback)
        {
            if (m_IsApplicationShuttingDown)
            {
                return;
            }

            m_Service.RegisterNotification(name, callback);
        }
コード例 #8
0
ファイル: View.cs プロジェクト: mengtest/RewriteFrame
 /// <summary>
 ///     Register an <c>IObserver</c> to be notified
 ///     of <c>INotifications</c> with a given name.
 /// </summary>
 /// <param name="notificationName">the name of the <c>INotifications</c> to notify this <c>IObserver</c> of</param>
 /// <param name="observer">the <c>IObserver</c> to register</param>
 public virtual void RegisterObserver(NotificationName notificationName, IObserver observer)
 {
     if (observerMap.TryGetValue(notificationName, out IList <IObserver> observers))
     {
         observers.Add(observer);
     }
     else
     {
         observerMap.TryAdd(notificationName, new List <IObserver> {
             observer
         });
     }
 }
コード例 #9
0
        /// <summary>
        /// Registers the notification.
        /// </summary>
        /// <param name="name">The name of the notification.</param>
        /// <param name="callback">The callback method that is called when the notification is activated.</param>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if <paramref name="name"/> is <see langword="null" />.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if <paramref name="callback"/> is <see langword="null" />.
        /// </exception>
        public void RegisterNotification(NotificationName name, Action <INotificationArguments> callback)
        {
            {
                Enforce.Argument(() => name);
                Enforce.Argument(() => callback);
            }

            if (!m_Notifications.ContainsKey(name))
            {
                m_Notifications.Add(name, new List <Action <INotificationArguments> >());
            }

            var list = m_Notifications[name];

            list.Add(callback);
        }
コード例 #10
0
        public void AddMessageHandler(Action <Notification> action, NotificationName notificationName)
        {
            if (notificationName == NotificationName.NOT_SET)
            {
                Debug.WriteLine("Null name specified for notification in AddObserver.");
                return;
            }
            var notificationHandler = new NotificationHandler(action);

            if (!notifications.ContainsKey(notificationName))
            {
                notifications.Add(notificationName, new List <NotificationHandler>());
            }

            var notifyList = notifications[notificationName];

            notifyList.Add(notificationHandler);
        }
コード例 #11
0
        public void RemoveObserver(Component component, NotificationName name)
        {
            List <Observer> notifyList;

            notifications.TryGetValue(name, out notifyList);

            if (notifyList == null || notifyList.Count == 0)
            {
                notifications.Remove(name);
                return;
            }

            var observer = notifyList.Find(n => n.component = component);

            if (observer != null)
            {
                notifyList.Remove(observer);
            }
        }
        /// <summary>
        /// 发送通知
        /// </summary>
        /// <param name="notificationName">通知名称</param>
        /// <param name="data">通知数据</param>
        /// <param name="tenantId">租户</param>
        /// <param name="notificationSeverity">级别</param>
        /// <returns></returns>
        public virtual async Task DispatchAsync(NotificationName notificationName, NotificationData data, Guid?tenantId = null,
                                                NotificationSeverity notificationSeverity = NotificationSeverity.Info)
        {
            // 获取自定义的通知
            var defineNotification = _notificationDefinitionManager.Get(notificationName.CateGory);

            //// 没有定义的通知,应该也要能发布、订阅,
            //// 比如订单之类的,是以订单编号为通知名称,这是动态的,没法自定义
            //if(defineNotification == null)
            //{
            //    defineNotification = new NotificationDefinition(notificationName.CateGory);
            //}

            var notificationInfo = new NotificationInfo
            {
                CateGory             = notificationName.CateGory,
                Name                 = notificationName.Name,
                CreationTime         = DateTime.Now,
                NotificationSeverity = notificationSeverity,
                Lifetime             = defineNotification.NotificationLifetime,
                NotificationType     = defineNotification.NotificationType,
                TenantId             = tenantId,
                Data                 = data
            };

            var providers = Enumerable
                            .Reverse(_notificationPublishProviderManager.Providers);

            if (defineNotification.Providers.Any())
            {
                providers = providers.Where(p => defineNotification.Providers.Contains(p.Name));
            }

            await PublishFromProvidersAsync(providers, notificationInfo);

            if (notificationInfo.Lifetime == NotificationLifetime.OnlyOne)
            {
                // 一次性通知在发送完成后就取消用户订阅
                await _notificationStore.DeleteAllUserSubscriptionAsync(notificationInfo.TenantId,
                                                                        notificationInfo.Name);
            }
        }
コード例 #13
0
        public void RemoveMessageHandler(Action <Notification> action, NotificationName name)
        {
            List <NotificationHandler> notifyList;

            notifications.TryGetValue(name, out notifyList);

            if (notifyList == null || notifyList.Count == 0)
            {
                notifications.Remove(name);
                return;
            }

            var handler = notifyList.Find(n => n.action == action);

            if (handler != null)
            {
                handler.Invalid = true;
                notifyList.Remove(handler);
            }
        }
        /// <summary>
        /// 发送通知事件
        /// </summary>
        /// <param name="notificationName"></param>
        /// <param name="data"></param>
        /// <param name="tenantId"></param>
        /// <param name="notificationSeverity"></param>
        /// <returns></returns>
        public virtual async Task DispatchEventAsync(NotificationName notificationName, NotificationData data, Guid?tenantId = null,
                                                     NotificationSeverity notificationSeverity = NotificationSeverity.Info)
        {
            // 获取自定义的通知
            var defineNotification = _notificationDefinitionManager.Get(notificationName.CateGory);

            var notificationEventData = new NotificationEventData
            {
                CateGory             = notificationName.CateGory,
                Name                 = notificationName.Name,
                CreationTime         = DateTime.Now,
                NotificationSeverity = notificationSeverity,
                Lifetime             = defineNotification.NotificationLifetime,
                NotificationType     = defineNotification.NotificationType,
                TenantId             = tenantId,
                Data                 = data
            };
            // 发布分布式通知事件,让消息中心统一处理
            await DistributedEventBus.PublishAsync(notificationEventData);
        }
コード例 #15
0
        public void AddObserver(Component component, Action <Notification> action, NotificationName notificationName)
        {
            if (notificationName == NotificationName.NOT_SET)
            {
                Debug.LogWarning("Null name specified for notification in AddObserver.");
                return;
            }
            var observer = new Observer(component, action);

            if (!notifications.ContainsKey(notificationName))
            {
                notifications.Add(notificationName, new List <Observer>());
            }

            var notifyList = notifications[notificationName];

            if (notifyList != null)
            {
                notifyList.Add(observer);
            }
        }
コード例 #16
0
 public void PostNotification(Component sender, NotificationName notificationName, NotificationArgs args,
                              Component reciever)
 {
     PostNotification(new Notification(sender, notificationName, args, reciever));
 }
コード例 #17
0
 public void PostNotification(Component sender, NotificationName notificationName, NotificationArgs args)
 {
     PostNotification(new Notification(sender, notificationName, args));
 }
コード例 #18
0
 public void PostNotification(NotificationName notificationName, NotificationArgs args)
 {
     PostNotification(null, notificationName, args, null);
 }
コード例 #19
0
 public void PostNotification(Component sender, NotificationName notificationName)
 {
     PostNotification(sender, notificationName, null, null);
 }
コード例 #20
0
 public void PostNotification(Component sender, NotificationName notificationName, Component reciever)
 {
     PostNotification(sender, notificationName, null, reciever);
 }
コード例 #21
0
ファイル: Facade.cs プロジェクト: mengtest/RewriteFrame
 /// <summary>
 /// Register an <c>ICommand</c> with the <c>Controller</c> by Notification name.
 /// </summary>
 /// <param name="notificationName">the name of the <c>INotification</c> to associate the <c>ICommand</c> with</param>
 /// <param name="commandFunc">a reference to the Class of the <c>ICommand</c></param>
 public virtual void RegisterCommand(NotificationName notificationName, Func <ICommand> commandFunc)
 {
     controller.RegisterCommand(notificationName, commandFunc);
 }
コード例 #22
0
ファイル: Facade.cs プロジェクト: mengtest/RewriteFrame
 /// <summary>
 /// Remove a previously registered <c>ICommand</c> to <c>INotification</c> mapping from the Controller.
 /// </summary>
 /// <param name="notificationName">the name of the <c>INotification</c> to remove the <c>ICommand</c> mapping for</param>
 public virtual void RemoveCommand(NotificationName notificationName)
 {
     controller.RemoveCommand(notificationName);
 }
コード例 #23
0
 public void PostNotification(NotificationName name, NotificationArgs args = null)
 {
     PostNotification(new Notification(name, args));
 }
コード例 #24
0
ファイル: Facade.cs プロジェクト: mengtest/RewriteFrame
 /// <summary>
 /// Check if a Command is registered for a given Notification
 /// </summary>
 /// <param name="notificationName"></param>
 /// <returns>whether a Command is currently registered for the given <c>notificationName</c>.</returns>
 public virtual bool HasCommand(NotificationName notificationName)
 {
     return(controller.HasCommand(notificationName));
 }
コード例 #25
0
ファイル: Facade.cs プロジェクト: mengtest/RewriteFrame
 /// <summary>
 /// Create and send an <c>INotification</c>.
 /// </summary>
 /// <remarks>
 ///     <para>
 ///         Keeps us from having to construct new notification
 ///         instances in our implementation code.
 ///     </para>
 /// </remarks>
 /// <param name="notificationName">the name of the notiification to send</param>
 /// <param name="body">the body of the notification (optional)</param>
 /// <param name="type">type the type of the notification (optional)</param>
 public virtual void SendNotification(NotificationName notificationName, object body = null)
 {
     NotifyObservers(new Notification(notificationName, body));
 }
コード例 #26
0
 public Notification(NotificationName _name, NotificationArgs _args = null)
 {
     name = _name;
     args = _args;
 }
コード例 #27
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MissingNotificationActionException"/> class.
 /// </summary>
 /// <param name="name">The <c>DnsName</c> which was a duplicate.</param>
 public MissingNotificationActionException(NotificationName name)
     : this(string.Format(CultureInfo.InvariantCulture, Resources.Exceptions_Messages_MissingNotificationAction_WithName, name))
 {
 }
コード例 #28
0
 /// <summary>
 /// Check if a Command is registered for a given Notification
 /// </summary>
 /// <param name="notificationName"></param>
 /// <returns>whether a Command is currently registered for the given <c>notificationName</c>.</returns>
 public virtual bool HasCommand(NotificationName notificationName)
 {
     return(commandMap.ContainsKey(notificationName));
 }
コード例 #29
0
        /// <summary>
        /// Registers the notification.
        /// </summary>
        /// <param name="name">The name of the notification.</param>
        /// <param name="callback">The callback method that is called when the notification is activated.</param>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if <paramref name="name"/> is <see langword="null" />.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if <paramref name="callback"/> is <see langword="null" />.
        /// </exception>
        public void RegisterNotification(NotificationName name, Action<INotificationArguments> callback)
        {
            {
                Enforce.Argument(() => name);
                Enforce.Argument(() => callback);
            }

            if (!m_Notifications.ContainsKey(name))
            {
                m_Notifications.Add(name, new List<Action<INotificationArguments>>());
            }

            var list = m_Notifications[name];
            list.Add(callback);
        }
コード例 #30
0
        /// <summary>
        /// Registers the notification.
        /// </summary>
        /// <param name="name">The name of the notification.</param>
        /// <param name="callback">The callback method that is called when the notification is activated.</param>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if <paramref name="name"/> is <see langword="null" />.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if <paramref name="callback"/> is <see langword="null" />.
        /// </exception>
        public void RegisterNotification(NotificationName name, Action<INotificationArguments> callback)
        {
            if (m_IsApplicationShuttingDown)
            {
                return;
            }

            m_Service.RegisterNotification(name, callback);
        }
コード例 #31
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="name">name of the <c>Notification</c> instance. (required)</param>
 /// <param name="body">the <c>Notification</c> body. (optional)</param>
 /// <param name="type">the type of the <c>Notification</c> (optional)</param>
 public Notification(NotificationName name, object body = null)
 {
     Name = name;
     Body = body;
 }
コード例 #32
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MissingNotificationActionException"/> class.
 /// </summary>
 /// <param name="name">The <c>DnsName</c> which was a duplicate.</param>
 public MissingNotificationActionException(NotificationName name)
     : this(string.Format(CultureInfo.InvariantCulture, Resources.Exceptions_Messages_MissingNotificationAction_WithName, name))
 {
 }
コード例 #33
0
ファイル: Notifier.cs プロジェクト: mengtest/RewriteFrame
 /// <summary>
 /// Create and send an <c>INotification</c>.
 /// </summary>
 /// <remarks>
 ///     <para>
 ///         Keeps us from having to construct new INotification
 ///         instances in our implementation code.
 ///     </para>
 /// </remarks>
 /// <param name="notificationName">the name of the notiification to send</param>
 /// <param name="body">the body of the notification (optional)</param>
 /// <param name="type">the type of the notification (optional)</param>
 public virtual void SendNotification(NotificationName notificationName, object body = null)
 {
     Facade.SendNotification(notificationName, body);
 }