Skip to content

Commit

Permalink
Merge pull request #6 from Insality/develop
Browse files Browse the repository at this point in the history
Fix xpcall issue in 9 release
  • Loading branch information
Insality authored Sep 19, 2024
2 parents c2514ca + 242af9c commit a6f81ec
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
"Lua.completion.autoRequire": false,
"Lua.workspace.ignoreDir": [
".vscode",
"event/annotations"
"annotations"
]
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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])
Expand Down
File renamed without changes.
File renamed without changes.
19 changes: 7 additions & 12 deletions event/event.lua
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion example/example.gui_script
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions example/example.script
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions example/logic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion game.project
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bootstrap]
main_collection = /test/test.collectionc
main_collection = /example/example.collectionc

[script]
shared_state = 1
Expand Down

0 comments on commit a6f81ec

Please sign in to comment.