Skip to content

Commit

Permalink
refactor(callback): warn if callback function is nil/false
Browse files Browse the repository at this point in the history
Between the headache of people misusing callbacks
and people complaining about third-party resources
that misused them, I would rather just shut them up.

I'll just deprecate this crap at some point anyway.
  • Loading branch information
thelindat committed May 22, 2024
1 parent d147e70 commit 2909cb5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
9 changes: 7 additions & 2 deletions imports/callback/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,14 @@ end
---@overload fun(event: string, delay: number | false, cb: function, ...)
lib.callback = setmetatable({}, {
__call = function(_, event, delay, cb, ...)
local cbType = type(cb)
if not cb then
warn(("callback event '%s' does not have a function to callback to and will instead await\nuse lib.callback.await or a regular event to remove this warning")
:format(event))
else
local cbType = type(cb)

assert(cbType == 'function', ("expected argument 3 to have type 'function' (received %s)"):format(cbType))
assert(cbType == 'function', ("expected argument 3 to have type 'function' (received %s)"):format(cbType))
end

return triggerServerCallback(_, event, delay, cb, ...)
end
Expand Down
9 changes: 7 additions & 2 deletions imports/callback/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,14 @@ end
---@overload fun(event: string, playerId: number, cb: function, ...)
lib.callback = setmetatable({}, {
__call = function(_, event, playerId, cb, ...)
local cbType = type(cb)
if not cb then
warn(("callback event '%s' does not have a function to callback to and will instead await\nuse lib.callback.await or a regular event to remove this warning")
:format(event))
else
local cbType = type(cb)

assert(cbType == 'function', ("expected argument 3 to have type 'function' (received %s)"):format(cbType))
assert(cbType == 'function', ("expected argument 3 to have type 'function' (received %s)"):format(cbType))
end

return triggerClientCallback(_, event, playerId, cb, ...)
end
Expand Down

0 comments on commit 2909cb5

Please sign in to comment.