Thank you for sharing this well-described and useful library!
I tested and read the code of Pigeon and found several points:
- If we use the “hook” callback for instant feedback, we should be careful. Since this hook will be called directly from the place where “send” is triggered, it means if we call
pigeon.send
ingo
place and catch it ingui
, it will result in a context error:
You can only access gui.* functions and values from a gui script instance (.gui_script file)
It can be tricky, but we can check the equality of the context like this:
local function send_event(target, event_name)
local current_url = msg.url()
current_url.fragment = nil
local target_url = msg.url(target)
target_url.fragment = nil
if current_url == target_url then
-- One context, we can call functions directly
else
-- Different context, we should use msg.post()
end
end
At least we can show the warning or handle it in some other way.
-
Note that if we set both the “hook” and “url” parameters, only the “hook” parameter will be used.
-
Hook callback functions receive two arguments: the message_id:hash and message:table. It might be useful to pass the first argument parameter in the
subscribe
method. For example:
local function close_function(self)
print("We got correct self!", self)
end
function init(self)
pigeon.subscribe("test", close_function, self)
end
This allows you to avoid creating another closure function. However, it seems not very useful because hooks work only in one context (i.e., one script only and required lua files in this script).
-
The tests from the repository show one failed test:
DEBUG:SCRIPT: Pigeon tests end -------- [ PASSED: 20 FAILED: 1 ]
-
[minor] The example cannot be run on Defold 1.4.2.