diff --git a/.vscode/settings.json b/.vscode/settings.json index 06c583a..14777b4 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -23,6 +23,6 @@ "Lua.completion.autoRequire": false, "Lua.workspace.ignoreDir": [ ".vscode", - "event/annotations" + "annotations" ] } diff --git a/README.md b/README.md index 699773d..a4ddd9a 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ Memory allocation tracking is turned off in release builds, regardless of the `g ### Quick API Reference ```lua --- Event Module +local event = require("event.event") event.create(callback, [callback_context]) event:subscribe(callback, [callback_context]) event:unsubscribe(callback, [callback_context]) @@ -78,7 +78,7 @@ event:clear() event.set_logger(logger) event.set_memory_threshold(threshold) --- Global Events Module +local events = require("event.events") events.subscribe(name, callback, [callback_context]) events.unsubscribe(name, callback, [callback_context]) events.is_subscribed(name, callback, [callback_context]) diff --git a/event/annotations/event.lua b/annotations/event/event.lua similarity index 100% rename from event/annotations/event.lua rename to annotations/event/event.lua diff --git a/event/annotations/events.lua b/annotations/event/events.lua similarity index 100% rename from event/annotations/events.lua rename to annotations/event/events.lua diff --git a/event/event.lua b/event/event.lua index 54d63e0..5e47bfe 100644 --- a/event/event.lua +++ b/event/event.lua @@ -1,6 +1,7 @@ local IS_DEBUG = sys.get_engine_info().is_debug local MEMORY_THRESHOLD_WARNING = IS_DEBUG and sys.get_config_int("event.memory_threshold_warning", 0) or 0 +---Contains each item[1] - callback, item[2] - callback_context, item[3] - script_context ---@class event.callback_data: table ---@class event.logger @@ -148,14 +149,6 @@ function M:is_subscribed(callback, callback_context) end ----Error handler for event callbacks. ----@param error_message string Error message ----@return string Error message with stack trace -local function event_error_handler(error_message) - return debug.traceback(error_message, 2) -end - - ---Trigger the event and call all subscribed callbacks. Returns the result of the last callback. If no callbacks are subscribed, nothing will happen. ---@vararg any Any number of parameters to be passed to the subscribed callbacks. ---@return any result Result of the last triggered callback @@ -186,9 +179,9 @@ function M:trigger(...) -- Call callback local ok, result_or_error if event_callback_context then - ok, result_or_error = xpcall(event_callback, event_error_handler, event_callback_context, ...) + ok, result_or_error = pcall(event_callback, event_callback_context, ...) else - ok, result_or_error = xpcall(event_callback, event_error_handler, ...) + ok, result_or_error = pcall(event_callback, ...) end -- Check memory allocation @@ -211,8 +204,10 @@ function M:trigger(...) -- Handle errors if not ok then - M.logger:error(result_or_error) - break -- TODO: Is it necessary to stop the event if one of the callbacks failed? + local caller_info = debug.getinfo(2) + local place = caller_info.short_src .. ":" .. caller_info.currentline + M.logger:error("Error in trigger event", place) + M.logger:error(debug.traceback(result_or_error, 2)) end result = result_or_error diff --git a/example/example.gui_script b/example/example.gui_script index 2d6b931..c885e31 100644 --- a/example/example.gui_script +++ b/example/example.gui_script @@ -6,11 +6,12 @@ local logic = require("example.logic") local function setup_logger() - event.set_logger(log.get_logger("event")) + event.set_logger(log.get_logger("event") --[[@as event.logger]]) end local function on_click(self, counter) + print("on click", self, counter) print("Button clicked! Counter: " .. counter) end diff --git a/example/example.script b/example/example.script index 8af39f7..137ce39 100644 --- a/example/example.script +++ b/example/example.script @@ -6,6 +6,7 @@ local function on_change_data(self, counter) local pos = go.get_position().y go.animate(".", "position.y", go.PLAYBACK_ONCE_PINGPONG, pos + 50, go.EASING_OUTSINE, 0.3) + logic.call_error:trigger() end function init(self) diff --git a/example/logic.lua b/example/logic.lua index 61afebb..54bb29c 100644 --- a/example/logic.lua +++ b/example/logic.lua @@ -2,8 +2,16 @@ local event = require("event.event") local events = require("event.events") local M = {} + +local function print_error(message) + error(message) +end + M.counter = 0 M.on_change_data = event.create() +M.call_error = event.create(function() + print_error("from event") +end) function M.set_counter(self, counter) M.counter = counter diff --git a/game.project b/game.project index 8d6283d..7cee23c 100644 --- a/game.project +++ b/game.project @@ -1,5 +1,5 @@ [bootstrap] -main_collection = /test/test.collectionc +main_collection = /example/example.collectionc [script] shared_state = 1