mirror of
https://github.com/esx-framework/esx_core.git
synced 2026-08-29 23:01:30 +00:00
Add files
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
if Citizen and Citizen.CreateThread then
|
||||
CreateThread = Citizen.CreateThread
|
||||
end
|
||||
|
||||
Async = {}
|
||||
|
||||
function Async.parallel(tasks, cb)
|
||||
|
||||
local remaining = #tasks
|
||||
local results = {}
|
||||
|
||||
for i=1, #tasks, 1 do
|
||||
|
||||
CreateThread(function()
|
||||
|
||||
tasks[i](function(result)
|
||||
|
||||
table.insert(results, result)
|
||||
|
||||
remaining = remaining - 1;
|
||||
|
||||
if remaining == 0 then
|
||||
cb(results)
|
||||
end
|
||||
|
||||
end)
|
||||
|
||||
end)
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
function Async.parallelLimit(tasks, limit, cb)
|
||||
|
||||
local remaining = #tasks
|
||||
local running = 0
|
||||
local queue = {}
|
||||
local results = {}
|
||||
|
||||
for i=1, #tasks, 1 do
|
||||
table.insert(queue, tasks[i])
|
||||
end
|
||||
|
||||
local function processQueue()
|
||||
|
||||
if #queue == 0 then
|
||||
return
|
||||
end
|
||||
|
||||
while running < limit and #queue > 0 do
|
||||
|
||||
local task = table.remove(queue, 1)
|
||||
|
||||
running = running + 1
|
||||
|
||||
task(function(result)
|
||||
|
||||
table.insert(results, result)
|
||||
|
||||
remaining = remaining - 1;
|
||||
running = running - 1
|
||||
|
||||
if remaining == 0 then
|
||||
cb(results)
|
||||
end
|
||||
|
||||
end)
|
||||
|
||||
end
|
||||
|
||||
CreateThread(processQueue)
|
||||
|
||||
end
|
||||
|
||||
processQueue()
|
||||
|
||||
end
|
||||
|
||||
function Async.series(tasks, cb)
|
||||
Async.parallelLimit(tasks, 1, cb)
|
||||
end
|
||||
Reference in New Issue
Block a user