refactor: async - better readme + performance

This commit is contained in:
Mycroft
2022-07-16 15:38:23 +01:00
parent 213c2c4573
commit 05b043fc67
4 changed files with 13 additions and 52 deletions
-9
View File
@@ -1,9 +0,0 @@
root = true
[*]
indent_size = 4
indent_style = tab
end_of_line = crlf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
+8 -35
View File
@@ -1,42 +1,15 @@
<<<<<<< HEAD
# esx-legacy
Consolidated esx-legacy files into one repo for easy download since we will not support it
=======
# async
Async utilities for FXServer
<h1 align='center'>[ESX] Async</a></h1><p align='center'><b><a href='https://discord.esx-framework.org/'>Discord</a> - <a href='https://esx-framework.org/'>Website</a> - <a href='https://docs.esx-framework.org/legacy/installation'>Documentation</a></b></h5>
## Installation
Set it as a dependency in you fxmanifest.lua
```
server_script '@async/async.lua'
```
A very Simple resource that allows resources to run tasks asynchronously.
## Usage
```
local tasks = {}
## Legal
for i = 1, 100, 1 do
local task = function(cb)
SetTimeout(1000, function()
local result = math.random(1, 50000)
async - asynchronous tasks.
cb(result)
end)
end
Copyright (C) 2015-2022 Jérémie N'gadi
table.insert(tasks, task)
end
This program Is free software: you can redistribute it And/Or modify it under the terms Of the GNU General Public License As published by the Free Software Foundation, either version 3 Of the License, Or (at your option) any later version.
Async.parallel(tasks, function(results)
print(json.encode(results))
end)
This program Is distributed In the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty Of MERCHANTABILITY Or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License For more details.
Async.parallelLimit(tasks, 2, function(results)
print(json.encode(results))
end)
Async.series(tasks, function(results)
print(json.encode(results))
end)
```
>>>>>>> async/master
You should have received a copy Of the GNU General Public License along with this program. If Not, see <http://www.gnu.org/licenses/>.
+3 -7
View File
@@ -1,7 +1,3 @@
if Citizen and Citizen.CreateThread then
CreateThread = Citizen.CreateThread
end
Async = {}
function Async.parallel(tasks, cb)
@@ -16,7 +12,7 @@ function Async.parallel(tasks, cb)
for i = 1, #tasks, 1 do
CreateThread(function()
tasks[i](function(result)
table.insert(results, result)
results[#results+ 1] = result
remaining = remaining - 1;
@@ -39,7 +35,7 @@ function Async.parallelLimit(tasks, limit, cb)
local queue, results = {}, {}
for i=1, #tasks, 1 do
table.insert(queue, tasks[i])
queue[queue + 1] = tasks[i]
end
local function processQueue()
@@ -53,7 +49,7 @@ function Async.parallelLimit(tasks, limit, cb)
running = running + 1
task(function(result)
table.insert(results, result)
results[#results+ 1] = result
remaining = remaining - 1;
running = running - 1
+2 -1
View File
@@ -1,6 +1,7 @@
fx_version 'cerulean'
game 'gta5'
description 'Async'
author 'ESX-Framework'
description 'asynchronous Tasks'
shared_script 'async.lua'