summaryrefslogtreecommitdiff
path: root/WorldlineKeepers/Assets/Scripts/Tools/Notification/NotificationCenter.cs
blob: 4e52656778e1a5a3959fe00ecc5c7658fcb50824 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System;
using System.Collections.Generic;
using UnityEngine;

namespace WK
{

    public sealed class NotificationCenter : Singleton<NotificationCenter> 
    {
        public delegate void NotificatonHandler(params object[] args);

        private Dictionary<string/*eventName*/, Dictionary<object/*publisher*/, List<NotificatonHandler>>> m_EventListeners = new Dictionary<string, Dictionary<object, List<NotificatonHandler>>>();

        /// <summary>
        /// 当前在调用中的回调
        /// </summary>
        private HashSet<List<NotificatonHandler>> m_CurInvokingCallbacks = new HashSet<List<NotificatonHandler>>();

        public void AddObserver(string notificationName, NotificatonHandler handler)
        {
            AddObserver(null, notificationName, handler);
        }

        public void AddObserver(object sender, string notificationName, NotificatonHandler handler)
        {
            if (handler == null)
            {
                Debug.LogError("Can't add a null event handler for notification, " + notificationName);
                return;
            }
            if (string.IsNullOrEmpty(notificationName))
            {
                Debug.LogError("Can't observe an unnamed notification");
                return;
            }
            if (!m_EventListeners.ContainsKey(notificationName))
            {
                m_EventListeners.Add(notificationName, new Dictionary<object, List<NotificatonHandler>>());
            }
            Dictionary<object, List<NotificatonHandler>> dictionary = m_EventListeners[notificationName];
            object key = ((sender != null) ? sender : this);
            if (!dictionary.ContainsKey(key))
            {
                dictionary.Add(key, new List<NotificatonHandler>());
            }
            List<NotificatonHandler> list = dictionary[key];
            if (m_CurInvokingCallbacks.Contains(list))
            {
                list = (dictionary[key] = new List<NotificatonHandler>(list));
            }
            list.Add(handler);
        }

        public void RemoveObserver(string notificationName, NotificatonHandler handler)
        {
            RemoveObserver(null, notificationName, handler);
        }

        public void RemoveObserver(object sender, string notificationName, NotificatonHandler handler)
        {
            if (handler == null)
            {
                Debug.LogError("Can't remove a null event handler for notification, " + notificationName);
            }
            else if (string.IsNullOrEmpty(notificationName))
            {
                Debug.LogError("A notification name is required to stop observation");
            }
            else
            {
                if (!m_EventListeners.ContainsKey(notificationName))
                {
                    return;
                }
                Dictionary<object, List<NotificatonHandler>> dictionary = m_EventListeners[notificationName];
                object key = ((sender != null) ? sender : this);
                if (!dictionary.ContainsKey(key))
                {
                    return;
                }
                List<NotificatonHandler> list = dictionary[key];
                int num = list.IndexOf(handler);
                if (num != -1)
                {
                    if (m_CurInvokingCallbacks.Contains(list))
                    {
                        list = (dictionary[key] = new List<NotificatonHandler>(list));
                    }
                    list.RemoveAt(num);
                }
            }
        }

        public void Clean()
        {
            string[] array = new string[m_EventListeners.Keys.Count];
            m_EventListeners.Keys.CopyTo(array, 0);
            for (int num = array.Length - 1; num >= 0; num--)
            {
                string key = array[num];
                Dictionary<object, List<NotificatonHandler>> dictionary = m_EventListeners[key];
                object[] array2 = new object[dictionary.Keys.Count];
                dictionary.Keys.CopyTo(array2, 0);
                for (int num2 = array2.Length - 1; num2 >= 0; num2--)
                {
                    object key2 = array2[num2];
                    if (dictionary[key2].Count == 0)
                    {
                        dictionary.Remove(key2);
                    }
                }
                if (dictionary.Count == 0)
                {
                    m_EventListeners.Remove(key);
                }
            }
        }

        public void PostNotification(string notificationName)
        {
            PostNotification(notificationName, null);
        }

        public void PostNotification(object sender, string notificationName)
        {
            PostNotification(sender, notificationName, null);
        }

        public void PostNotification(object sender, string notificationName, params object[] p)
        {
            if (string.IsNullOrEmpty(notificationName))
            {
                Debug.LogError("A notification name is required");
            }
            else
            {
                if (!m_EventListeners.ContainsKey(notificationName))
                {
                    return;
                }
                Dictionary<object, List<NotificatonHandler>> dictionary = m_EventListeners[notificationName];
                if (sender != null && dictionary.ContainsKey(sender))
                {
                    List<NotificatonHandler> list = dictionary[sender];
                    m_CurInvokingCallbacks.Add(list);
                    for (int i = 0; i < list.Count; i++)
                    {
                        list[i](p);
                    }
                    m_CurInvokingCallbacks.Remove(list);
                }
                if (dictionary.ContainsKey(this))
                {
                    List<NotificatonHandler> list2 = dictionary[this];
                    m_CurInvokingCallbacks.Add(list2);
                    for (int j = 0; j < list2.Count; j++)
                    {
                        list2[j](p);
                    }
                    m_CurInvokingCallbacks.Remove(list2);
                }
            }
        }

    }

}