Use extended_api namespace
This commit is contained in:
parent
fe4db5b70c
commit
7899616988
30
README.md
30
README.md
@ -1,63 +1,63 @@
|
|||||||
extended_api
|
extended_api
|
||||||
===========
|
===========
|
||||||
|
|
||||||
extended_api is a minetest mod that extends the current minetest api.
|
extended_api mod is a library pack.
|
||||||
It adds two new node events and contains async functions.
|
It adds two new node events and contains async functions.
|
||||||
|
|
||||||
Usage Async
|
Usage Async
|
||||||
===========
|
===========
|
||||||
1. create a async pool.
|
1. create a async pool.
|
||||||
```lua
|
```lua
|
||||||
pool = minetest.Async.create_async_pool()
|
pool = extended_api.Async.create_async_pool()
|
||||||
```
|
```
|
||||||
2. set the priority of the async pool to high.
|
2. set the priority of the async pool to high.
|
||||||
```lua
|
```lua
|
||||||
minetest.Async.priority(pool,50,500)
|
extended_api.Async.priority(pool,50,500)
|
||||||
```
|
```
|
||||||
3. iterate from 1 to 50 and log the value i.
|
3. iterate from 1 to 50 and log the value i.
|
||||||
```lua
|
```lua
|
||||||
minetest.Async.iterate(pool,1,50,function(i)
|
extended_api.Async.iterate(pool,1,50,function(i)
|
||||||
minetest.log(i)
|
minetest.log(i)
|
||||||
end)
|
end)
|
||||||
```
|
```
|
||||||
4. run throught each element in a table.
|
4. run throught each element in a table.
|
||||||
```lua
|
```lua
|
||||||
local array = {"start","text2","text3","text4","text5","end"}
|
local array = {"start","text2","text3","text4","text5","end"}
|
||||||
minetest.Async.foreach(pool,array, function(k,v)
|
extended_api.Async.foreach(pool,array, function(k,v)
|
||||||
minetest.log(v)
|
minetest.log(v)
|
||||||
end)
|
end)
|
||||||
```
|
```
|
||||||
5. async do while loop.
|
5. async do while loop.
|
||||||
```lua
|
```lua
|
||||||
local c = 50
|
local c = 50
|
||||||
minetest.Async.do_while(pool,function() return c>0 end, function()
|
extended_api.Async.do_while(pool,function() return c>0 end, function()
|
||||||
minetest.log(c)
|
minetest.log(c)
|
||||||
c = c - 1
|
c = c - 1
|
||||||
end)
|
end)
|
||||||
```
|
```
|
||||||
6. register a async globalstep. this one spams the chat with the word spam.
|
6. register a async globalstep. this one spams the chat with the word spam.
|
||||||
```lua
|
```lua
|
||||||
minetest.Async.register_globalstep(pool,function(dtime)
|
extended_api.Async.register_globalstep(pool,function(dtime)
|
||||||
minetest.chat_send_all("spam")
|
minetest.chat_send_all("spam")
|
||||||
end)
|
end)
|
||||||
```
|
```
|
||||||
7. chain task runs a group of functions from a table.
|
7. chain task runs a group of functions from a table.
|
||||||
```lua
|
```lua
|
||||||
minetest.Async.chain_task(pool,{
|
extended_api.Async.chain_task(pool,{
|
||||||
function(args)
|
function(args)
|
||||||
args.count = 1
|
args.count = 1
|
||||||
minetest.log(args.count)
|
minetest.log(args.count)
|
||||||
return args
|
return args
|
||||||
end,
|
end,
|
||||||
function(args)
|
function(args)
|
||||||
args.count = args.count + 1
|
args.count = args.count + 1
|
||||||
minetest.log(args.count)
|
minetest.log(args.count)
|
||||||
return args
|
return args
|
||||||
end})
|
end})
|
||||||
```
|
```
|
||||||
8. adds a single function to the task queue. This is a sort of waiting list.
|
8. adds a single function to the task queue. This is a sort of waiting list.
|
||||||
```lua
|
```lua
|
||||||
minetest.Async.queue_task(pool,function()
|
extended_api.Async.queue_task(pool,function()
|
||||||
minetest.log("Hello World!")
|
minetest.log("Hello World!")
|
||||||
end)
|
end)
|
||||||
```
|
```
|
||||||
|
20
api.txt
20
api.txt
@ -5,35 +5,35 @@ on_construct_node_near_by(pos,other_pos,name)
|
|||||||
on_destruct_node_near_by(pos,other_pos,name)
|
on_destruct_node_near_by(pos,other_pos,name)
|
||||||
-- async
|
-- async
|
||||||
-- Create's a async pool.
|
-- Create's a async pool.
|
||||||
minetest.Async.create_async_pool() --- return pool
|
extended_api.Async.create_async_pool() --- return pool
|
||||||
-- Set the priority of the given async pool.
|
-- Set the priority of the given async pool.
|
||||||
-- resting is the delay between resuming threads.
|
-- resting is the delay between resuming threads.
|
||||||
-- maxtime is how long a thread will work before yield.
|
-- maxtime is how long a thread will work before yield.
|
||||||
minetest.Async.priority(pool,resting,maxtime)
|
extended_api.Async.priority(pool,resting,maxtime)
|
||||||
-- Async for loop.
|
-- Async for loop.
|
||||||
-- from is the starting value for the loop.
|
-- from is the starting value for the loop.
|
||||||
-- to is the ending value to will break the loop.
|
-- to is the ending value to will break the loop.
|
||||||
-- func(i) is the function that is ran in the for loop. func can break the loop if it returns false.
|
-- func(i) is the function that is ran in the for loop. func can break the loop if it returns false.
|
||||||
-- callback() function is ran when the for loop ends. callback can be nil.
|
-- callback() function is ran when the for loop ends. callback can be nil.
|
||||||
minetest.Async.iterate(pool,from,to,func,callback)
|
extended_api.Async.iterate(pool,from,to,func,callback)
|
||||||
-- Async foreach loop.
|
-- Async foreach loop.
|
||||||
-- array is the list to loop throught.
|
-- array is the list to loop throught.
|
||||||
-- func(k,v) is the function that is ran in the foreach loop. func can break the loop if it returns false.
|
-- func(k,v) is the function that is ran in the foreach loop. func can break the loop if it returns false.
|
||||||
-- callback() function is ran when the foreach loop ends. callback can be nil.
|
-- callback() function is ran when the foreach loop ends. callback can be nil.
|
||||||
minetest.Async.foreach(pool,array, func, callback)
|
extended_api.Async.foreach(pool,array, func, callback)
|
||||||
-- Async do while loop.
|
-- Async do while loop.
|
||||||
-- condition_func() returns the condition.
|
-- condition_func() returns the condition.
|
||||||
-- func() is the function that is ran in the do while loop. func can break the loop if it returns false.
|
-- func() is the function that is ran in the do while loop. func can break the loop if it returns false.
|
||||||
-- callback() function is ran when the do while loop ends. callback can be nil.
|
-- callback() function is ran when the do while loop ends. callback can be nil.
|
||||||
minetest.Async.do_while(pool,condition_func, func, callback)
|
extended_api.Async.do_while(pool,condition_func, func, callback)
|
||||||
-- Async globalstep loop. this can never be shutdown like in the minetest version. happens every 0.05 seconds.
|
-- Async globalstep loop. this can never be shutdown like in the minetest version. happens every 0.05 seconds.
|
||||||
-- func(dtime) is the function that is ran in the globalstep.
|
-- func(dtime) is the function that is ran in the globalstep.
|
||||||
minetest.Async.register_globalstep(pool,func)
|
extended_api.Async.register_globalstep(pool,func)
|
||||||
-- Async chain task.
|
-- Async chain task.
|
||||||
-- tasks is a table of functions that ran in order.
|
-- tasks is a table of functions that ran in order.
|
||||||
-- callback() function is ran when the chain_task ends. callback can be nil.
|
-- callback(arg) function is ran when the chain_task ends. callback can be nil.
|
||||||
minetest.Async.chain_task(pool,tasks,callback)
|
extended_api.Async.chain_task(pool,tasks,callback)
|
||||||
-- Async task queue.
|
-- Async task queue.
|
||||||
-- func is a single function that gets added to the queue list.
|
-- func is a single function that gets added to the queue list.
|
||||||
-- callback() function is ran when the queue_task ends. callback can be nil.
|
-- callback(arg) function is ran when the queue_task ends. callback can be nil.
|
||||||
minetest.Async.queue_task(pool,func,callback)
|
extended_api.Async.queue_task(pool,func,callback)
|
66
async.lua
66
async.lua
@ -1,71 +1,71 @@
|
|||||||
minetest.Async = {}
|
extended_api.Async = {}
|
||||||
|
|
||||||
function minetest.Async.create_async_pool()
|
function extended_api.Async.create_async_pool()
|
||||||
local pool = {threads = {},globalstep_threads = {},task_queue = {},resting = 200,maxtime = 200,queue_threads = 8,state = "suspended"}
|
local pool = {threads = {},globalstep_threads = {},task_queue = {},resting = 200,maxtime = 200,queue_threads = 8,state = "suspended"}
|
||||||
return pool
|
return pool
|
||||||
end
|
end
|
||||||
|
|
||||||
function minetest.Async.create_worker(pool,func)
|
function extended_api.Async.create_worker(pool,func)
|
||||||
local thread = coroutine.create(func)
|
local thread = coroutine.create(func)
|
||||||
table.insert(pool.threads, thread)
|
table.insert(pool.threads, thread)
|
||||||
end
|
end
|
||||||
|
|
||||||
function minetest.Async.create_globalstep_worker(pool,func)
|
function extended_api.Async.create_globalstep_worker(pool,func)
|
||||||
local thread = coroutine.create(func)
|
local thread = coroutine.create(func)
|
||||||
table.insert(pool.globalstep_threads, thread)
|
table.insert(pool.globalstep_threads, thread)
|
||||||
end
|
end
|
||||||
|
|
||||||
function minetest.Async.run_worker(pool,index)
|
function extended_api.Async.run_worker(pool,index)
|
||||||
local thread = pool.threads[index]
|
local thread = pool.threads[index]
|
||||||
if thread == nil or coroutine.status(thread) == "dead" then
|
if thread == nil or coroutine.status(thread) == "dead" then
|
||||||
table.remove(pool.threads, index)
|
table.remove(pool.threads, index)
|
||||||
minetest.after(0,minetest.Async.schedule_worker,pool)
|
minetest.after(0,extended_api.Async.schedule_worker,pool)
|
||||||
return false
|
return false
|
||||||
else
|
else
|
||||||
coroutine.resume(thread)
|
coroutine.resume(thread)
|
||||||
minetest.after(0,minetest.Async.schedule_worker,pool)
|
minetest.after(0,extended_api.Async.schedule_worker,pool)
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function minetest.Async.run_globalstep_worker(pool,index)
|
function extended_api.Async.run_globalstep_worker(pool,index)
|
||||||
local thread = pool.globalstep_threads[index]
|
local thread = pool.globalstep_threads[index]
|
||||||
if thread == nil or coroutine.status(thread) == "dead" then
|
if thread == nil or coroutine.status(thread) == "dead" then
|
||||||
table.remove(pool.globalstep_threads, index)
|
table.remove(pool.globalstep_threads, index)
|
||||||
minetest.after(0,minetest.Async.schedule_globalstep_worker,pool)
|
minetest.after(0,extended_api.Async.schedule_globalstep_worker,pool)
|
||||||
return false
|
return false
|
||||||
else
|
else
|
||||||
coroutine.resume(thread)
|
coroutine.resume(thread)
|
||||||
minetest.after(0,minetest.Async.schedule_globalstep_worker,pool)
|
minetest.after(0,extended_api.Async.schedule_globalstep_worker,pool)
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function minetest.Async.schedule_worker(pool)
|
function extended_api.Async.schedule_worker(pool)
|
||||||
pool.state = "running"
|
pool.state = "running"
|
||||||
for index,value in ipairs(pool.threads) do
|
for index,value in ipairs(pool.threads) do
|
||||||
minetest.after(pool.resting / 1000,minetest.Async.run_worker,pool,index)
|
minetest.after(pool.resting / 1000,extended_api.Async.run_worker,pool,index)
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
pool.state = "suspended"
|
pool.state = "suspended"
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
function minetest.Async.schedule_globalstep_worker(pool)
|
function extended_api.Async.schedule_globalstep_worker(pool)
|
||||||
for index,value in ipairs(pool.globalstep_threads) do
|
for index,value in ipairs(pool.globalstep_threads) do
|
||||||
minetest.after(0,minetest.Async.run_globalstep_worker,pool,index)
|
minetest.after(0,extended_api.Async.run_globalstep_worker,pool,index)
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
function minetest.Async.priority(pool,resting,maxtime)
|
function extended_api.Async.priority(pool,resting,maxtime)
|
||||||
pool.resting = resting
|
pool.resting = resting
|
||||||
pool.maxtime = maxtime
|
pool.maxtime = maxtime
|
||||||
end
|
end
|
||||||
|
|
||||||
function minetest.Async.iterate(pool,from,to,func,callback)
|
function extended_api.Async.iterate(pool,from,to,func,callback)
|
||||||
minetest.Async.create_worker(pool,function()
|
extended_api.Async.create_worker(pool,function()
|
||||||
local last_time = minetest.get_us_time() * 1000
|
local last_time = minetest.get_us_time() * 1000
|
||||||
local maxtime = pool.maxtime
|
local maxtime = pool.maxtime
|
||||||
for i = from, to do
|
for i = from, to do
|
||||||
@ -82,11 +82,11 @@ function minetest.Async.iterate(pool,from,to,func,callback)
|
|||||||
callback()
|
callback()
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
minetest.Async.schedule_worker(pool)
|
extended_api.Async.schedule_worker(pool)
|
||||||
end
|
end
|
||||||
|
|
||||||
function minetest.Async.foreach(pool,array, func, callback)
|
function extended_api.Async.foreach(pool,array, func, callback)
|
||||||
minetest.Async.create_worker(pool,function()
|
extended_api.Async.create_worker(pool,function()
|
||||||
local last_time = minetest.get_us_time() * 1000
|
local last_time = minetest.get_us_time() * 1000
|
||||||
local maxtime = pool.maxtime
|
local maxtime = pool.maxtime
|
||||||
for k,v in ipairs(array) do
|
for k,v in ipairs(array) do
|
||||||
@ -103,11 +103,11 @@ function minetest.Async.foreach(pool,array, func, callback)
|
|||||||
callback()
|
callback()
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
minetest.Async.schedule_worker(pool)
|
extended_api.Async.schedule_worker(pool)
|
||||||
end
|
end
|
||||||
|
|
||||||
function minetest.Async.do_while(pool,condition_func, func, callback)
|
function extended_api.Async.do_while(pool,condition_func, func, callback)
|
||||||
minetest.Async.create_worker(pool,function()
|
extended_api.Async.create_worker(pool,function()
|
||||||
local last_time = minetest.get_us_time() * 1000
|
local last_time = minetest.get_us_time() * 1000
|
||||||
local maxtime = pool.maxtime
|
local maxtime = pool.maxtime
|
||||||
while(condition_func()) do
|
while(condition_func()) do
|
||||||
@ -124,11 +124,11 @@ function minetest.Async.do_while(pool,condition_func, func, callback)
|
|||||||
callback()
|
callback()
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
minetest.Async.schedule_worker(pool)
|
extended_api.Async.schedule_worker(pool)
|
||||||
end
|
end
|
||||||
|
|
||||||
function minetest.Async.register_globalstep(pool,func)
|
function extended_api.Async.register_globalstep(pool,func)
|
||||||
minetest.Async.create_globalstep_worker(pool,function()
|
extended_api.Async.create_globalstep_worker(pool,function()
|
||||||
local last_time = minetest.get_us_time() * 1000
|
local last_time = minetest.get_us_time() * 1000
|
||||||
local dtime = last_time
|
local dtime = last_time
|
||||||
while(true) do
|
while(true) do
|
||||||
@ -144,11 +144,11 @@ function minetest.Async.register_globalstep(pool,func)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
minetest.Async.schedule_globalstep_worker(pool)
|
extended_api.Async.schedule_globalstep_worker(pool)
|
||||||
end
|
end
|
||||||
|
|
||||||
function minetest.Async.chain_task(pool,tasks,callback)
|
function extended_api.Async.chain_task(pool,tasks,callback)
|
||||||
minetest.Async.create_worker(pool,function()
|
extended_api.Async.create_worker(pool,function()
|
||||||
local pass_arg = {}
|
local pass_arg = {}
|
||||||
local last_time = minetest.get_us_time() * 1000
|
local last_time = minetest.get_us_time() * 1000
|
||||||
local maxtime = pool.maxtime
|
local maxtime = pool.maxtime
|
||||||
@ -166,14 +166,14 @@ function minetest.Async.chain_task(pool,tasks,callback)
|
|||||||
callback(pass_arg)
|
callback(pass_arg)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
minetest.Async.schedule_worker(pool)
|
extended_api.Async.schedule_worker(pool)
|
||||||
end
|
end
|
||||||
|
|
||||||
function minetest.Async.queue_task(pool,func,callback)
|
function extended_api.Async.queue_task(pool,func,callback)
|
||||||
table.insert(pool.task_queue,{func = func,callback = callback})
|
table.insert(pool.task_queue,{func = func,callback = callback})
|
||||||
if pool.queue_threads > 0 then
|
if pool.queue_threads > 0 then
|
||||||
pool.queue_threads = pool.queue_threads - 1
|
pool.queue_threads = pool.queue_threads - 1
|
||||||
minetest.Async.create_worker(pool,function()
|
extended_api.Async.create_worker(pool,function()
|
||||||
local pass_arg = {}
|
local pass_arg = {}
|
||||||
local last_time = minetest.get_us_time() * 1000
|
local last_time = minetest.get_us_time() * 1000
|
||||||
local maxtime = pool.maxtime
|
local maxtime = pool.maxtime
|
||||||
@ -199,6 +199,6 @@ function minetest.Async.queue_task(pool,func,callback)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
minetest.Async.schedule_worker(pool)
|
extended_api.Async.schedule_worker(pool)
|
||||||
end
|
end
|
||||||
end
|
end
|
@ -1 +1 @@
|
|||||||
This mod adds more functions and features to the minetest api.
|
This mod is a library that adds more functions and features.
|
3
init.lua
3
init.lua
@ -1,3 +1,6 @@
|
|||||||
modpath = minetest.get_modpath("extended_api")
|
modpath = minetest.get_modpath("extended_api")
|
||||||
|
|
||||||
|
extended_api = {}
|
||||||
|
|
||||||
dofile(string.format("%s/node_funcs.lua",modpath))
|
dofile(string.format("%s/node_funcs.lua",modpath))
|
||||||
dofile(string.format("%s/async.lua",modpath))
|
dofile(string.format("%s/async.lua",modpath))
|
Loading…
Reference in New Issue
Block a user