From ad9cdf0d2a63f55300736a8c550245709f327876 Mon Sep 17 00:00:00 2001 From: Insality Date: Sun, 15 Sep 2024 13:01:44 +0300 Subject: [PATCH 1/3] Move annotations to the project root (exclude from dependency) --- {event/annotations => annotations/event}/event.lua | 0 {event/annotations => annotations/event}/events.lua | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename {event/annotations => annotations/event}/event.lua (100%) rename {event/annotations => annotations/event}/events.lua (100%) 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 From 1bc7e27115bcf65200c0c2daf43605ecd4177e1b Mon Sep 17 00:00:00 2001 From: Insality Date: Sun, 15 Sep 2024 13:13:02 +0300 Subject: [PATCH 2/3] Update annotations --- .vscode/settings.json | 5 ++++- event/event.lua | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 1b0a805..f94480a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -20,5 +20,8 @@ "label", "socket" ], - "Lua.completion.autoRequire": false + "Lua.completion.autoRequire": false, + "Lua.workspace.library": [ + "~/Library/Application Support/Code/User/workspaceStorage/ac07c8aee1c361dc3b09cd3c42fcca08/astronachos.defold" + ] } diff --git a/event/event.lua b/event/event.lua index c4b7f19..96abb88 100644 --- a/event/event.lua +++ b/event/event.lua @@ -1,8 +1,16 @@ 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 +---@field trace fun(logger: event.logger, message: string, data: any|nil) @Log a trace message. +---@field debug fun(logger: event.logger, message: string, data: any|nil) @Log a debug message. +---@field info fun(logger: event.logger, message: string, data: any|nil) @Log an info message. +---@field warn fun(logger: event.logger, message: string, data: any|nil) @Log a warning message. +---@field error fun(logger: event.logger, message: string, data: any|nil) @Log an error message. + ---@class event @Event module ---@overload fun(vararg:any): any|nil Trigger the event. All subscribed callbacks will be called in the order they were subscribed. local M = {} From 65ebe3609853b632dbccc0365461a09b4d350585 Mon Sep 17 00:00:00 2001 From: Insality Date: Thu, 19 Sep 2024 19:13:40 +0300 Subject: [PATCH 3/3] Solve #5 Replace xpcall with pcall due different signtature in HTML5 (didn't pass vargs) Update README, update example --- .vscode/settings.json | 4 ++++ README.md | 4 ++-- event/event.lua | 18 ++++++------------ example/example.gui_script | 3 ++- example/example.script | 1 + example/logic.lua | 8 ++++++++ game.project | 2 +- 7 files changed, 24 insertions(+), 16 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index f94480a..2776a91 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -23,5 +23,9 @@ "Lua.completion.autoRequire": false, "Lua.workspace.library": [ "~/Library/Application Support/Code/User/workspaceStorage/ac07c8aee1c361dc3b09cd3c42fcca08/astronachos.defold" + ], + "Lua.workspace.ignoreDir": [ + ".vscode", + "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/event.lua b/event/event.lua index 96abb88..5e47bfe 100644 --- a/event/event.lua +++ b/event/event.lua @@ -149,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 @@ -187,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 @@ -212,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