1 local CustomEventSupport = { static = {} }
3 CustomEventSupport.static.event_listeners = nil
5 function CustomEventSupport.static:bind(event_type, event_handler)
8 if not self.static.event_listeners then
9 self.static.event_listeners = {}
12 if not self.static.event_listeners[event_type] then
13 self.static.event_listeners[event_type] = {}
18 listeners = self.static.event_listeners[event_type]
19 for i, v in ipairs(listeners) do
20 if v == event_handler then
27 table.insert(self.static.event_listeners[event_type], event_handler)
34 function CustomEventSupport.static:unbind(event_type, event_handler)
35 local found, listeners, counter
37 if not self.static.event_listeners then
38 self.static.event_listeners = {}
41 if not self.static.event_listeners[event_type] then
42 self.static.event_listeners[event_type] = {}
47 listeners = self.static.event_listeners[event_type]
48 for i, v in ipairs(listeners) do
49 if v == event_handler then
57 table.remove(self.static.event_listeners[event_type], counter)
63 function CustomEventSupport.static:dispatch(event_type, data)
64 local event, listeners, instance, i
66 if not self.static.event_listeners then
67 self.static.event_listeners = {}
74 if not data.target then
78 -- Create new CustomEvent
79 event = CustomEvent:new(event_type, data)
80 listeners = self.static.event_listeners[event_type] or {}
81 instance = self.static
83 for i, v in ipairs(listeners) do
85 if event.are_immediate_handlers_prevented == true then
91 CustomEventSupport.event_listeners = nil
93 function CustomEventSupport:bind(event_type, event_handler)
94 local found, listeners
96 if not self.event_listeners then
97 self.event_listeners = {}
100 if not self.event_listeners[event_type] then
101 self.event_listeners[event_type] = {}
106 listeners = self.event_listeners[event_type]
107 for i, v in ipairs(listeners) do
108 if v == event_handler then
115 table.insert(self.event_listeners[event_type], event_handler)
122 function CustomEventSupport:unbind(event_type, event_handler)
123 local found, listeners, counter
125 if not self.event_listeners then
126 self.event_listeners = {}
129 if not self.event_listeners[event_type] then
130 self.event_listeners[event_type] = {}
135 listeners = self.event_listeners[event_type]
136 for i, v in ipairs(listeners) do
137 if v == event_handler then
145 table.remove(self.event_listeners[event_type], counter)
151 function CustomEventSupport:dispatch(event_type, data)
152 local event, listeners, instance, i
154 if not self.event_listeners then
155 self.event_listeners = {}
162 if not data.target then
166 -- Create new CustomEvent
167 event = CustomEvent:new(event_type, data)
168 listeners = self.event_listeners[event_type] or {}
171 for i, v in ipairs(listeners) do
173 if event.are_immediate_handlers_prevented == true then
179 return CustomEventSupport