Exemplo n.º 1
0
    public static void Notify(string key, object data)
    {
        if (key == null)
        {
            throw new ArgumentNullException(nameof(key), "KEY argument should have value");
        }

        var subscription = SubscribersWithData.Where(s => s.Item2 == key).ToList();

        if (KeepActionValue)
        {
            if (subscription.Count == 0)
            {
                KeepActionData.Add(new Tuple <string, object>(key, data));
            }
        }

        var actions = KeepActionData.Where(k => k.Item1 == key).ToArray();

        if (subscription.Count > 0)
        {
            subscription.ToList().ForEach((Tuple <object, string, Action <object> > action) => {
                if (actions.Length > 0)
                {
                    var datas = actions.Select(o => o.Item2).ToList();
                    if (datas.Count > 0)
                    {
                        action.Item3.Invoke(datas);
                    }
                }
                action.Item3.Invoke(data);
            });
        }
        KeepActionData.Remove(actions.FirstOrDefault());
    }
Exemplo n.º 2
0
        /// <summary>
        /// Subscribe an action with object for string key.
        /// </summary>
        /// <param name="key">String key for subscribe action name.</param>
        /// <param name="action">Action to call with object</param>
        public static void Subscribe(string key, Action <object> action)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key), "KEY argument should have value");
            }

            SubscribersWithData?.Add(new Tuple <string, Action <object> >(key, action));
        }
Exemplo n.º 3
0
    public static void Subscribe(object receiver, string key, Action <object> action)
    {
        if (key == null)
        {
            throw new ArgumentNullException(nameof(key), "KEY argument should have value");
        }

        var listOfSubs = Subscribers?.Where(k => k.Item1 == receiver && k.Item2 == key).FirstOrDefault();

        if (listOfSubs != null)
        {
            throw new Exception("reciever already subscribed");
        }

        SubscribersWithData?.Add(new Tuple <object, string, Action <object> >(receiver, key, action));
    }
Exemplo n.º 4
0
        /// <summary>
        /// Notify action for your key with a data.
        /// </summary>
        /// <param name="key">String key for notify action</param>
        /// <param name="data">Send data to subscribe on this key</param>
        /// <returns>Task of notify</returns>
        public static Task Notify(string key, object data)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key), "KEY argument should have value");
            }

            return(Task.Run(delegate
            {
                var subscription = SubscribersWithData.Where(s => s.Item1 == key).ToList();
                if (KeepActionValue)
                {
                    if (subscription.Count == 0)
                    {
                        KeepActionData.Add(new Tuple <string, object>(key, data));
                    }
                }

                var actions = KeepActionData.Where(k => k.Item1 == key).ToArray();
                if (subscription.Count > 0)
                {
                    subscription.AsParallel().ForAll((Tuple <string, Action <object> > action) =>
                    {
                        if (actions.Length > 0)
                        {
                            var datas = actions.Select(o => o.Item2).ToList();
                            if (datas.Count > 0)
                            {
                                if (EnableLogs)
                                {
                                    Debug.WriteLine(
                                        $"Your ('{key}') action have value before you subscribed on ('{key}') key");
                                }
                                action.Item2.Invoke(datas);
                            }
                        }
                        if (EnableLogs)
                        {
                            Debug.WriteLine($"Your ('{key}') action was run completely ...");
                        }
                        action.Item2.Invoke(data);
                    });
                }
                KeepActionData.Remove(actions.FirstOrDefault());
            }));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Unsubscribe notification key in subscription list
        /// </summary>
        /// <param name="key">String key for unsubscribe action name.</param>
        public static void Unsubscribe(string key)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key), "KEY argument should have value");
            }

            var listOfSubs = Subscribers?.Where(k => k.Item1 == key).FirstOrDefault();

            if (listOfSubs != null)
            {
                Subscribers?.Remove(listOfSubs);
            }

            var listOfSubsWithOutActionObject = SubscribersWithData?.Where(k => k.Item1 == key).SingleOrDefault();

            if (listOfSubsWithOutActionObject != null)
            {
                SubscribersWithData?.Remove(listOfSubsWithOutActionObject);
            }
        }
Exemplo n.º 6
0
 /// <summary>
 /// Unsubscribe to all notification keys.
 /// </summary>
 public static void UnsubscribeAll()
 {
     Subscribers?.Clear();
     SubscribersWithData?.Clear();
 }