Improved error handling of events

This commit is contained in:
Timo Smit 2019-01-17 15:31:23 +01:00
parent 6d3197413a
commit cd701f187b

View file

@ -15,9 +15,6 @@
-- You should have received a copy of the GNU General Public License -- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>. -- along with this program. If not, see <http://www.gnu.org/licenses/>.
local constants = wolfa_requireModule("util.constants")
local util = wolfa_requireModule("util.util")
local events = {} local events = {}
local data = {} local data = {}
@ -28,51 +25,59 @@ end
function events.add(name) function events.add(name)
if events.get(name) then if events.get(name) then
error("event is already added: "..name) error("event is already added: "..name, 2)
end end
data[name] = {} data[name] = {}
end end
function events.ishandled(name, func) function events.getHandlers(name)
if not events.get(name) then if not events.get(name) then
error("event not added: "..name) error("event not added: "..name, 2)
end end
return events.get(name)
end
function events.isHandled(name, func)
if not events.get(name) then
error("event not added: "..name, 2)
end
local handlers = events.get(name) local handlers = events.get(name)
for i = 0, #handlers do for i = 0, #handlers do
if handlers[i] == func then if handlers[i] == func then
return true return true
end end
end end
return false return false
end end
function events.handle(name, func) function events.handle(name, func)
if not events.get(name) then if not events.get(name) then
error("event not added: "..name) error("event not added: "..name, 2)
end end
if events.ishandled(name, func) then if events.isHandled(name, func) then
error("event "..name.." is already handled by this function") error("event "..name.." is already handled by this function", 2)
end end
table.insert(data[name], func) table.insert(data[name], func)
end end
function events.unhandle(name, func) function events.unhandle(name, func)
if not events.get(name) then if not events.get(name) then
error("event not added: "..name) error("event not added: "..name, 2)
end end
if not events.ishandled(name, func) then if not events.isHandled(name, func) then
error("event "..name.." is not handled by this function") error("event "..name.." is not handled by this function", 2)
end end
local handlers = events.get(name) local handlers = events.get(name)
for i = 0, #handlers do for i = 0, #handlers do
if handlers[i] == func then if handlers[i] == func then
table.remove(handlers, i) table.remove(handlers, i)
@ -82,21 +87,21 @@ end
function events.trigger(name, ...) function events.trigger(name, ...)
local handlers = events.get(name) local handlers = events.get(name)
if not handlers then if not handlers then
error("event not added: "..name) error("event not added: "..name, 2)
end end
local returnValue local returnValue
for _, handler in pairs(handlers) do for _, handler in pairs(handlers) do
local handlerReturn = handler(...) local handlerReturn = handler(...)
if not returnValue and returnValue ~= 0 and handlerReturn ~= nil then if not returnValue and returnValue ~= 0 and handlerReturn ~= nil then
returnValue = handlerReturn returnValue = handlerReturn
end end
end end
return returnValue return returnValue
end end