mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-19 07:56:02 +01:00
feat(callback): add timeout to callback events
Should help when people await broken or unregistered events. Defaults to 60 seconds, adjusted with convar ox:callbackTimeout.
This commit is contained in:
@@ -1,9 +1,12 @@
|
|||||||
local events = {}
|
local pendingCallbacks = {}
|
||||||
local timers = {}
|
local timers = {}
|
||||||
local cbEvent = ('__ox_cb_%s')
|
local cbEvent = '__ox_cb_%s'
|
||||||
|
local callbackTimeout = GetConvarInt('ox:callbackTimeout', 60000)
|
||||||
|
|
||||||
RegisterNetEvent(cbEvent:format(cache.resource), function(key, ...)
|
RegisterNetEvent(cbEvent:format(cache.resource), function(key, ...)
|
||||||
local cb = events[key]
|
local cb = pendingCallbacks[key]
|
||||||
|
pendingCallbacks[key] = nil
|
||||||
|
|
||||||
return cb and cb(...)
|
return cb and cb(...)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
@@ -36,16 +39,15 @@ local function triggerServerCallback(_, event, delay, cb, ...)
|
|||||||
|
|
||||||
repeat
|
repeat
|
||||||
key = ('%s:%s'):format(event, math.random(0, 100000))
|
key = ('%s:%s'):format(event, math.random(0, 100000))
|
||||||
until not events[key]
|
until not pendingCallbacks[key]
|
||||||
|
|
||||||
TriggerServerEvent(cbEvent:format(event), cache.resource, key, ...)
|
TriggerServerEvent(cbEvent:format(event), cache.resource, key, ...)
|
||||||
|
|
||||||
---@type promise | false
|
---@type promise | false
|
||||||
local promise = not cb and promise.new()
|
local promise = not cb and promise.new()
|
||||||
|
|
||||||
events[key] = function(response, ...)
|
pendingCallbacks[key] = function(response, ...)
|
||||||
response = { response, ... }
|
response = { response, ... }
|
||||||
events[key] = nil
|
|
||||||
|
|
||||||
if promise then
|
if promise then
|
||||||
return promise:resolve(response)
|
return promise:resolve(response)
|
||||||
@@ -57,6 +59,8 @@ local function triggerServerCallback(_, event, delay, cb, ...)
|
|||||||
end
|
end
|
||||||
|
|
||||||
if promise then
|
if promise then
|
||||||
|
SetTimeout(callbackTimeout, function() promise:reject(("callback event '%s' timed out"):format(key)) end)
|
||||||
|
|
||||||
return table.unpack(Citizen.Await(promise))
|
return table.unpack(Citizen.Await(promise))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -76,7 +80,8 @@ end
|
|||||||
local function callbackResponse(success, result, ...)
|
local function callbackResponse(success, result, ...)
|
||||||
if not success then
|
if not success then
|
||||||
if result then
|
if result then
|
||||||
return print(('^1SCRIPT ERROR: %s^0\n%s'):format(result , Citizen.InvokeNative(`FORMAT_STACK_TRACE` & 0xFFFFFFFF, nil, 0, Citizen.ResultAsString()) or ''))
|
return print(('^1SCRIPT ERROR: %s^0\n%s'):format(result,
|
||||||
|
Citizen.InvokeNative(`FORMAT_STACK_TRACE` & 0xFFFFFFFF, nil, 0, Citizen.ResultAsString()) or ''))
|
||||||
end
|
end
|
||||||
|
|
||||||
return false
|
return false
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
local events = {}
|
local pendingCallbacks = {}
|
||||||
local cbEvent = ('__ox_cb_%s')
|
local cbEvent = '__ox_cb_%s'
|
||||||
|
local callbackTimeout = GetConvarInt('ox:callbackTimeout', 60000)
|
||||||
|
|
||||||
RegisterNetEvent(cbEvent:format(cache.resource), function(key, ...)
|
RegisterNetEvent(cbEvent:format(cache.resource), function(key, ...)
|
||||||
local cb = events[key]
|
local cb = pendingCallbacks[key]
|
||||||
|
pendingCallbacks[key] = nil
|
||||||
|
|
||||||
return cb and cb(...)
|
return cb and cb(...)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
@@ -17,16 +20,15 @@ local function triggerClientCallback(_, event, playerId, cb, ...)
|
|||||||
|
|
||||||
repeat
|
repeat
|
||||||
key = ('%s:%s:%s'):format(event, math.random(0, 100000), playerId)
|
key = ('%s:%s:%s'):format(event, math.random(0, 100000), playerId)
|
||||||
until not events[key]
|
until not pendingCallbacks[key]
|
||||||
|
|
||||||
TriggerClientEvent(cbEvent:format(event), playerId, cache.resource, key, ...)
|
TriggerClientEvent(cbEvent:format(event), playerId, cache.resource, key, ...)
|
||||||
|
|
||||||
---@type promise | false
|
---@type promise | false
|
||||||
local promise = not cb and promise.new()
|
local promise = not cb and promise.new()
|
||||||
|
|
||||||
events[key] = function(response, ...)
|
pendingCallbacks[key] = function(response, ...)
|
||||||
response = { response, ... }
|
response = { response, ... }
|
||||||
events[key] = nil
|
|
||||||
|
|
||||||
if promise then
|
if promise then
|
||||||
return promise:resolve(response)
|
return promise:resolve(response)
|
||||||
@@ -38,6 +40,8 @@ local function triggerClientCallback(_, event, playerId, cb, ...)
|
|||||||
end
|
end
|
||||||
|
|
||||||
if promise then
|
if promise then
|
||||||
|
SetTimeout(callbackTimeout, function() promise:reject(("callback event '%s' timed out"):format(key)) end)
|
||||||
|
|
||||||
return table.unpack(Citizen.Await(promise))
|
return table.unpack(Citizen.Await(promise))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
import { cache } from '../cache';
|
import { cache } from '../cache';
|
||||||
|
|
||||||
const activeEvents: Record<string, (...args: any[]) => void> = {};
|
const pendingCallbacks: Record<string, (...args: any[]) => void> = {};
|
||||||
|
const callbackTimeout = GetConvarInt('ox:callbackTimeout', 60000);
|
||||||
|
|
||||||
onNet(`__ox_cb_${cache.resource}`, (key: string, ...args: any) => {
|
onNet(`__ox_cb_${cache.resource}`, (key: string, ...args: any) => {
|
||||||
const resolve = activeEvents[key];
|
const resolve = pendingCallbacks[key];
|
||||||
|
delete pendingCallbacks[key];
|
||||||
|
|
||||||
return resolve && resolve(...args);
|
return resolve && resolve(...args);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -32,12 +35,14 @@ export function triggerServerCallback<T = unknown>(
|
|||||||
|
|
||||||
do {
|
do {
|
||||||
key = `${eventName}:${Math.floor(Math.random() * (100000 + 1))}`;
|
key = `${eventName}:${Math.floor(Math.random() * (100000 + 1))}`;
|
||||||
} while (activeEvents[key]);
|
} while (pendingCallbacks[key]);
|
||||||
|
|
||||||
emitNet(`__ox_cb_${eventName}`, cache.resource, key, ...args);
|
emitNet(`__ox_cb_${eventName}`, cache.resource, key, ...args);
|
||||||
|
|
||||||
return new Promise<T>((resolve) => {
|
return new Promise<T>((resolve, reject) => {
|
||||||
activeEvents[key] = resolve;
|
pendingCallbacks[key] = resolve;
|
||||||
|
|
||||||
|
setTimeout(reject, callbackTimeout, `callback event '${key}' timed out`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
import { cache } from '../cache';
|
import { cache } from '../cache';
|
||||||
|
|
||||||
const activeEvents: Record<string, (...args: any[]) => void> = {};
|
const pendingCallbacks: Record<string, (...args: any[]) => void> = {};
|
||||||
|
const callbackTimeout = GetConvarInt('ox:callbackTimeout', 60000);
|
||||||
|
|
||||||
onNet(`__ox_cb_${cache.resource}`, (key: string, ...args: any) => {
|
onNet(`__ox_cb_${cache.resource}`, (key: string, ...args: any) => {
|
||||||
const resolve = activeEvents[key];
|
const resolve = pendingCallbacks[key];
|
||||||
|
delete pendingCallbacks[key];
|
||||||
|
|
||||||
return resolve && resolve(...args);
|
return resolve && resolve(...args);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -16,12 +19,14 @@ export function triggerClientCallback<T = unknown>(
|
|||||||
|
|
||||||
do {
|
do {
|
||||||
key = `${eventName}:${Math.floor(Math.random() * (100000 + 1))}:${playerId}`;
|
key = `${eventName}:${Math.floor(Math.random() * (100000 + 1))}:${playerId}`;
|
||||||
} while (activeEvents[key]);
|
} while (pendingCallbacks[key]);
|
||||||
|
|
||||||
emitNet(`__ox_cb_${eventName}`, playerId, cache.resource, key, ...args);
|
emitNet(`__ox_cb_${eventName}`, playerId, cache.resource, key, ...args);
|
||||||
|
|
||||||
return new Promise<T>((resolve) => {
|
return new Promise<T>((resolve, reject) => {
|
||||||
activeEvents[key] = resolve;
|
pendingCallbacks[key] = resolve;
|
||||||
|
|
||||||
|
setTimeout(reject, callbackTimeout, `callback event '${key}' timed out`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user