refactor(callback): warn if callback function is nil/false

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.
This commit is contained in:
Linden
2024-05-23 09:41:24 +10:00
parent d147e70c77
commit 2909cb5db6
2 changed files with 14 additions and 4 deletions

View File

@@ -68,9 +68,14 @@ end
---@overload fun(event: string, delay: number | false, cb: function, ...) ---@overload fun(event: string, delay: number | false, cb: function, ...)
lib.callback = setmetatable({}, { lib.callback = setmetatable({}, {
__call = function(_, event, delay, cb, ...) __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, ...) return triggerServerCallback(_, event, delay, cb, ...)
end end

View File

@@ -51,9 +51,14 @@ end
---@overload fun(event: string, playerId: number, cb: function, ...) ---@overload fun(event: string, playerId: number, cb: function, ...)
lib.callback = setmetatable({}, { lib.callback = setmetatable({}, {
__call = function(_, event, playerId, cb, ...) __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, ...) return triggerClientCallback(_, event, playerId, cb, ...)
end end