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,15 +25,23 @@ 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
return events.get(name)
end
function events.isHandled(name, func)
if not events.get(name) then
error("event not added: "..name, 2)
end end
local handlers = events.get(name) local handlers = events.get(name)
@ -52,11 +57,11 @@ 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)
@ -64,11 +69,11 @@ 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)
@ -84,7 +89,7 @@ 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