From 2909cb5db62567f13453fa0247572212634c1fb7 Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Thu, 23 May 2024 09:41:24 +1000 Subject: [PATCH] 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. --- imports/callback/client.lua | 9 +++++++-- imports/callback/server.lua | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/imports/callback/client.lua b/imports/callback/client.lua index 6535bda2..0e61f851 100644 --- a/imports/callback/client.lua +++ b/imports/callback/client.lua @@ -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 diff --git a/imports/callback/server.lua b/imports/callback/server.lua index 65eef3e8..5309a4ae 100644 --- a/imports/callback/server.lua +++ b/imports/callback/server.lua @@ -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