async/README.md

74 lines
1.8 KiB
Markdown
Raw Permalink Normal View History

2019-05-13 21:22:21 +02:00
async
===========
2019-05-13 21:22:21 +02:00
async mod is a library pack.
It adds two new node events and contains async functions.
Usage Async
===========
1. create a async instance.
```lua
2019-05-13 21:22:21 +02:00
async = async.Async()
```
2019-07-22 03:03:09 +02:00
2. max time for thread before yielding (maxtime is in milliseconds).
```lua
2019-07-22 03:03:09 +02:00
async.maxtime = 200
```
2019-07-22 03:03:09 +02:00
3. queue_threads is the amount of active threads when you run function queue_task.
```lua
async.queue_threads = 8
```
4. iterate from 1 to 50 and log the value i.
```lua
async.iterate(1, 50, function(i)
minetest.log(i)
2019-07-22 03:03:09 +02:00
end, function() minetest.log("Callback") end)
```
2019-07-22 03:03:09 +02:00
5. run throught each element in a table.
```lua
local array = {"start", "text2", "text3", "text4", "text5", "end"}
2019-10-15 18:19:31 +02:00
async.foreach(pairs(array), function(k, v)
minetest.log(v)
2019-07-22 03:03:09 +02:00
end, function() minetest.log("Callback") end)
```
2019-07-22 03:03:09 +02:00
6. async do while loop.
```lua
local c = 50
2019-07-22 03:03:09 +02:00
async.do_while(function() return c > 0 end, function()
minetest.log(c)
c = c - 1
2019-07-22 03:03:09 +02:00
end, function() minetest.log("Callback") end)
```
2019-07-22 03:03:09 +02:00
7. register a async globalstep. this one spams the chat with the word spam.
```lua
async.register_globalstep(function(dtime)
minetest.chat_send_all("spam")
end)
```
2019-07-22 03:03:09 +02:00
8. chain task runs a group of functions from a table.
```lua
async.chain_task({
function(args)
2018-11-10 14:08:56 +01:00
args.count = 1
minetest.log(args.count)
return args
end,
function(args)
2018-11-10 14:08:56 +01:00
args.count = args.count + 1
minetest.log(args.count)
return args
2019-07-22 03:03:09 +02:00
end}, function(args) minetest.log(args.count) end)
```
2019-07-22 03:03:09 +02:00
9. adds a single function to the task queue. This is a sort of waiting list.
```lua
async.queue_task(function()
2018-11-09 23:04:48 +01:00
minetest.log("Hello World!")
2019-07-22 03:03:09 +02:00
end, function(args) minetest.log("callback") end)
```
2019-07-22 03:03:09 +02:00
10. Same as queue_task but the task does not go into a queue.
```lua
async.single_task(function()
minetest.log("Hello World!")
2019-07-22 03:03:09 +02:00
end, function(args) minetest.log("callback") end)
```