]> git.r.bdr.sh - rbdr/custom_event_support.lua/blob - lib/custom_event_support.lua
73686816c2479964f881ed0fa4169d1389bc820c
[rbdr/custom_event_support.lua] / lib / custom_event_support.lua
1 local CustomEventSupport = { static = {} }
2
3 CustomEventSupport.static.event_listeners = nil
4
5 function CustomEventSupport.static:bind(event_type, event_handler)
6 local found, listeners
7
8 if not self.static.event_listeners then
9 self.static.event_listeners = {}
10 end
11
12 if not self.static.event_listeners[event_type] then
13 self.static.event_listeners[event_type] = {}
14 end
15
16 found = false
17
18 listeners = self.static.event_listeners[event_type]
19 for i, v in ipairs(listeners) do
20 if v == event_handler then
21 found = true
22 break
23 end
24 end
25
26 if not found then
27 table.insert(self.static.event_listeners[event_type], event_handler)
28 end
29
30 return self.static
31
32 end
33
34 function CustomEventSupport.static:unbind(event_type, event_handler)
35 local found, listeners, counter
36
37 if not self.static.event_listeners then
38 self.static.event_listeners = {}
39 end
40
41 if not self.static.event_listeners[event_type] then
42 self.static.event_listeners[event_type] = {}
43 end
44
45 found = false
46
47 listeners = self.static.event_listeners[event_type]
48 for i, v in ipairs(listeners) do
49 if v == event_handler then
50 found = true
51 counter = i
52 break
53 end
54 end
55
56 if found then
57 table.remove(self.static.event_listeners[event_type], counter)
58 end
59
60 return self.static
61 end
62
63 function CustomEventSupport.static:dispatch(event_type, data)
64 local event, listeners, instance, i
65
66 if not self.static.event_listeners then
67 self.static.event_listeners = {}
68 end
69
70 if not data then
71 data = {}
72 end
73
74 if not data.target then
75 data.target = self
76 end
77
78 -- Create new CustomEvent
79 event = CustomEvent:new(event_type, data)
80 listeners = self.static.event_listeners[event_type] or {}
81 instance = self.static
82
83 for i, v in ipairs(listeners) do
84 v(instance, event)
85 if event.are_immediate_handlers_prevented == true then
86 break
87 end
88 end
89 end
90
91 CustomEventSupport.event_listeners = nil
92
93 function CustomEventSupport:bind(event_type, event_handler)
94 local found, listeners
95
96 if not self.event_listeners then
97 self.event_listeners = {}
98 end
99
100 if not self.event_listeners[event_type] then
101 self.event_listeners[event_type] = {}
102 end
103
104 found = false
105
106 listeners = self.event_listeners[event_type]
107 for i, v in ipairs(listeners) do
108 if v == event_handler then
109 found = true
110 break
111 end
112 end
113
114 if not found then
115 table.insert(self.event_listeners[event_type], event_handler)
116 end
117
118 return self
119
120 end
121
122 function CustomEventSupport:unbind(event_type, event_handler)
123 local found, listeners, counter
124
125 if not self.event_listeners then
126 self.event_listeners = {}
127 end
128
129 if not self.event_listeners[event_type] then
130 self.event_listeners[event_type] = {}
131 end
132
133 found = false
134
135 listeners = self.event_listeners[event_type]
136 for i, v in ipairs(listeners) do
137 if v == event_handler then
138 found = true
139 counter = i
140 break
141 end
142 end
143
144 if found then
145 table.remove(self.event_listeners[event_type], counter)
146 end
147
148 return self
149 end
150
151 function CustomEventSupport:dispatch(event_type, data)
152 local event, listeners, instance, i
153
154 if not self.event_listeners then
155 self.event_listeners = {}
156 end
157
158 if not data then
159 data = {}
160 end
161
162 if not data.target then
163 data.target = self
164 end
165
166 -- Create new CustomEvent
167 event = CustomEvent:new(event_type, data)
168 listeners = self.event_listeners[event_type] or {}
169 instance = self
170
171 for i, v in ipairs(listeners) do
172 v(instance, event)
173 if event.are_immediate_handlers_prevented == true then
174 break
175 end
176 end
177 end
178
179 return CustomEventSupport