-
-
Notifications
You must be signed in to change notification settings - Fork 439
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from overextended/dev
- Loading branch information
Showing
11 changed files
with
232 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
108 changes: 54 additions & 54 deletions
108
imports/callbacks/client.lua → imports/callback/client.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,54 @@ | ||
local ServerCallbacks = {} | ||
|
||
---@param event string | ||
---@param delay number prevent the event from being called for the given time | ||
local function callbackTimer(event, delay) | ||
if type(delay) == 'number' then | ||
local time = GetGameTimer() | ||
if (ServerCallbacks[event] or 0) > time then | ||
return false | ||
end | ||
ServerCallbacks[event] = time + delay | ||
end | ||
return true | ||
end | ||
|
||
local function startCallback(resource, event, ...) | ||
local id = math.random(0, 100000) | ||
event = ('__cb_%s:%s'):format(resource, event) | ||
TriggerServerEvent(event, id, ...) | ||
return event..id | ||
end | ||
|
||
local ServerCallback = table.create(0, 2) | ||
|
||
---@param resource string | ||
---@param event string | ||
---@param delay number prevent the event from being called for the given time | ||
--- Sends an event to the server and halts the current thread until a response is returned. | ||
ServerCallback.Await = function(resource, event, delay, ...) | ||
if callbackTimer(event, delay) then | ||
local promise = promise.new() | ||
event = RegisterNetEvent(startCallback(resource, event, ...), function(response) | ||
promise:resolve(response) | ||
RemoveEventHandler(event) | ||
end) | ||
return table.unpack(Citizen.Await(promise)) | ||
end | ||
end | ||
|
||
---@param resource string | ||
---@param event string | ||
---@param delay number | ||
---@param cb function | ||
--- Sends an event to the server and triggers a callback function once the response is returned. | ||
ServerCallback.Async = function(resource, event, delay, cb, ...) | ||
if callbackTimer(event, delay) then | ||
event = RegisterNetEvent(startCallback(resource, event, ...), function(response) | ||
cb(table.unpack(response)) | ||
RemoveEventHandler(event) | ||
end) | ||
end | ||
end | ||
|
||
return ServerCallback | ||
local ServerCallbacks = {} | ||
|
||
---@param event string | ||
---@param delay number prevent the event from being called for the given time | ||
local function callbackTimer(event, delay) | ||
if type(delay) == 'number' then | ||
local time = GetGameTimer() | ||
if (ServerCallbacks[event] or 0) > time then | ||
return false | ||
end | ||
ServerCallbacks[event] = time + delay | ||
end | ||
return true | ||
end | ||
|
||
local function triggerCallback(event, ...) | ||
local id = math.random(0, 100000) | ||
event = ('__cb_%s'):format(event) | ||
TriggerServerEvent(event, id, ...) | ||
return event..id | ||
end | ||
|
||
---Sends an event to the server and triggers a callback function once the response is returned. | ||
---``` | ||
---callback(event: string, delay: number, function(...) | ||
--- print(...) | ||
---end) | ||
---``` | ||
local callback = {} | ||
|
||
---@param event string | ||
---@param delay number prevent the event from being called for the given time | ||
--- Sends an event to the server and halts the current thread until a response is returned. | ||
function callback.await(event, delay, ...) | ||
if callbackTimer(event, delay) then | ||
local promise = promise.new() | ||
event = RegisterNetEvent(triggerCallback(event, ...), function(response) | ||
promise:resolve(response) | ||
RemoveEventHandler(event) | ||
end) | ||
return table.unpack(Citizen.Await(promise)) | ||
end | ||
end | ||
|
||
return setmetatable(callback, { | ||
__call = function(event, delay, cb, ...) | ||
if callbackTimer(event, delay) then | ||
event = RegisterNetEvent(triggerCallback(event, ...), function(response) | ||
cb(table.unpack(response)) | ||
RemoveEventHandler(event) | ||
end) | ||
end | ||
end | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
local callback = {} | ||
|
||
---@param name string | ||
---@param callback function | ||
--- Registers an event handler and callback function to respond to client requests. | ||
function callback.register(name, cb) | ||
name = ('__cb_%s'):format(name) | ||
|
||
RegisterServerEvent(name, function(id, ...) | ||
TriggerClientEvent(name..id, source, {cb(source, ...)}) | ||
end) | ||
end | ||
|
||
return callback |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.