1 | using System;
|
---|
2 |
|
---|
3 | using SharpVectors.Dom.Events;
|
---|
4 |
|
---|
5 | namespace SharpVectors.Dom.Events
|
---|
6 | {
|
---|
7 | /// <summary>
|
---|
8 | /// Summary description for EventListenerMap.
|
---|
9 | /// </summary>
|
---|
10 | public class EventListenerMap
|
---|
11 | {
|
---|
12 | #region Private Fields
|
---|
13 |
|
---|
14 | private const int GrowthBuffer = 8;
|
---|
15 | private const int GrowthFactor = 2;
|
---|
16 |
|
---|
17 | private EventListenerMapEntry[] entries;
|
---|
18 | private int count;
|
---|
19 | private bool locked;
|
---|
20 |
|
---|
21 | #endregion
|
---|
22 |
|
---|
23 | #region Private Helpers
|
---|
24 |
|
---|
25 | private EventListenerMapEntry[] GrowBy(
|
---|
26 | int growth)
|
---|
27 | {
|
---|
28 | if (entries == null)
|
---|
29 | {
|
---|
30 | entries = new EventListenerMapEntry[
|
---|
31 | growth * GrowthFactor + GrowthBuffer];
|
---|
32 |
|
---|
33 | this.count = 0;
|
---|
34 | this.locked = false;
|
---|
35 |
|
---|
36 | return entries;
|
---|
37 | }
|
---|
38 |
|
---|
39 | int newCount = count + growth;
|
---|
40 |
|
---|
41 | if (newCount > entries.Length)
|
---|
42 | {
|
---|
43 | newCount = newCount * GrowthFactor + GrowthBuffer;
|
---|
44 |
|
---|
45 | EventListenerMapEntry[] newEntries =
|
---|
46 | new EventListenerMapEntry[newCount];
|
---|
47 |
|
---|
48 | Array.Copy(entries, 0, newEntries, 0, entries.Length);
|
---|
49 |
|
---|
50 | entries = newEntries;
|
---|
51 | }
|
---|
52 |
|
---|
53 | return entries;
|
---|
54 | }
|
---|
55 |
|
---|
56 | #endregion
|
---|
57 |
|
---|
58 | #region Public Methods
|
---|
59 |
|
---|
60 | public void AddEventListener(
|
---|
61 | string namespaceUri,
|
---|
62 | string eventType,
|
---|
63 | object eventGroup,
|
---|
64 | EventListener listener)
|
---|
65 | {
|
---|
66 | EventListenerMapEntry[] entries = GrowBy(1);
|
---|
67 |
|
---|
68 | for (int i = 0; i < count; i++)
|
---|
69 | {
|
---|
70 | if (namespaceUri != entries[i].NamespaceUri)
|
---|
71 | {
|
---|
72 | continue;
|
---|
73 | }
|
---|
74 |
|
---|
75 | if (eventType != entries[i].Type)
|
---|
76 | {
|
---|
77 | continue;
|
---|
78 | }
|
---|
79 |
|
---|
80 | if (listener == entries[i].Listener)
|
---|
81 | {
|
---|
82 | return;
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | entries[count] = new EventListenerMapEntry(
|
---|
87 | namespaceUri, eventType, eventGroup, listener, locked);
|
---|
88 |
|
---|
89 | count++;
|
---|
90 | }
|
---|
91 |
|
---|
92 | public void RemoveEventListener(
|
---|
93 | string namespaceUri,
|
---|
94 | string eventType,
|
---|
95 | EventListener listener)
|
---|
96 | {
|
---|
97 | if (entries == null)
|
---|
98 | {
|
---|
99 | return;
|
---|
100 | }
|
---|
101 |
|
---|
102 | for (int i = 0; i < count; i++)
|
---|
103 | {
|
---|
104 | if (namespaceUri != entries[i].NamespaceUri)
|
---|
105 | {
|
---|
106 | continue;
|
---|
107 | }
|
---|
108 |
|
---|
109 | if (eventType != entries[i].Type)
|
---|
110 | {
|
---|
111 | continue;
|
---|
112 | }
|
---|
113 |
|
---|
114 | if (listener == entries[i].Listener)
|
---|
115 | {
|
---|
116 | count--;
|
---|
117 | entries[i] = entries[count];
|
---|
118 | entries[count] = new EventListenerMapEntry();
|
---|
119 |
|
---|
120 | return;
|
---|
121 | }
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | public void FireEvent(
|
---|
126 | IEvent @event)
|
---|
127 | {
|
---|
128 | string namespaceUri = @event.NamespaceUri;
|
---|
129 | string eventType = @event.Type;
|
---|
130 | for (int i = 0; i < count; i++)
|
---|
131 | {
|
---|
132 | // Check if the entry was added during this phase
|
---|
133 | if (entries[i].Locked)
|
---|
134 | continue;
|
---|
135 |
|
---|
136 | string entryNamespaceUri = entries[i].NamespaceUri;
|
---|
137 | string entryEventType = entries[i].Type;
|
---|
138 |
|
---|
139 | if (entryNamespaceUri != null && namespaceUri != null)
|
---|
140 | {
|
---|
141 | if (entryNamespaceUri != namespaceUri)
|
---|
142 | {
|
---|
143 | continue;
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | if (entryEventType != eventType)
|
---|
148 | {
|
---|
149 | continue;
|
---|
150 | }
|
---|
151 |
|
---|
152 | entries[i].Listener(@event);
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | public bool HasEventListenerNs(
|
---|
157 | string namespaceUri,
|
---|
158 | string eventType)
|
---|
159 | {
|
---|
160 | if (entries == null)
|
---|
161 | {
|
---|
162 | return false;
|
---|
163 | }
|
---|
164 |
|
---|
165 | for (int i = 0; i < count; i++)
|
---|
166 | {
|
---|
167 | if (namespaceUri != entries[i].NamespaceUri)
|
---|
168 | {
|
---|
169 | continue;
|
---|
170 | }
|
---|
171 |
|
---|
172 | if (eventType != entries[i].Type)
|
---|
173 | {
|
---|
174 | continue;
|
---|
175 | }
|
---|
176 |
|
---|
177 | return true;
|
---|
178 | }
|
---|
179 |
|
---|
180 | return false;
|
---|
181 | }
|
---|
182 |
|
---|
183 | public void Lock()
|
---|
184 | {
|
---|
185 | locked = true;
|
---|
186 | }
|
---|
187 |
|
---|
188 | public void Unlock()
|
---|
189 | {
|
---|
190 | // Unlock the map
|
---|
191 | locked = false;
|
---|
192 |
|
---|
193 | // Unlock pending entries
|
---|
194 | for (int i = 0; i < count; i++)
|
---|
195 | {
|
---|
196 | entries[i].Locked = false;
|
---|
197 | }
|
---|
198 | }
|
---|
199 | #endregion
|
---|
200 | }
|
---|
201 | }
|
---|