From 7899616988bc546681a67f3287f53b787dccbc5b Mon Sep 17 00:00:00 2001 From: Coder12a <38924418+Coder12a@users.noreply.github.com> Date: Sat, 10 Nov 2018 07:08:56 -0600 Subject: [PATCH] Use extended_api namespace --- README.md | 30 +++++++++++----------- api.txt | 20 +++++++-------- async.lua | 66 ++++++++++++++++++++++++------------------------- description.txt | 2 +- init.lua | 3 +++ 5 files changed, 62 insertions(+), 59 deletions(-) diff --git a/README.md b/README.md index 8e748f6..7de93cb 100644 --- a/README.md +++ b/README.md @@ -1,63 +1,63 @@ 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. Usage Async =========== 1. create a async pool. ```lua -pool = minetest.Async.create_async_pool() +pool = extended_api.Async.create_async_pool() ``` 2. set the priority of the async pool to high. ```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. ```lua -minetest.Async.iterate(pool,1,50,function(i) +extended_api.Async.iterate(pool,1,50,function(i) minetest.log(i) end) ``` 4. run throught each element in a table. ```lua 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) end) ``` 5. async do while loop. ```lua 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) c = c - 1 end) ``` 6. register a async globalstep. this one spams the chat with the word spam. ```lua -minetest.Async.register_globalstep(pool,function(dtime) +extended_api.Async.register_globalstep(pool,function(dtime) minetest.chat_send_all("spam") end) ``` 7. chain task runs a group of functions from a table. ```lua -minetest.Async.chain_task(pool,{ +extended_api.Async.chain_task(pool,{ function(args) - args.count = 1 - minetest.log(args.count) - return args + args.count = 1 + minetest.log(args.count) + return args end, function(args) - args.count = args.count + 1 - minetest.log(args.count) - return args + args.count = args.count + 1 + minetest.log(args.count) + return args end}) ``` 8. adds a single function to the task queue. This is a sort of waiting list. ```lua -minetest.Async.queue_task(pool,function() +extended_api.Async.queue_task(pool,function() minetest.log("Hello World!") end) ``` diff --git a/api.txt b/api.txt index 9594b8c..dc0d1cd 100644 --- a/api.txt +++ b/api.txt @@ -5,35 +5,35 @@ on_construct_node_near_by(pos,other_pos,name) on_destruct_node_near_by(pos,other_pos,name) -- async -- 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. -- resting is the delay between resuming threads. -- 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. -- from is the starting value for 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. -- 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. -- 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. -- 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. -- 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. -- 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. -- 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. -- tasks is a table of functions that ran in order. --- callback() function is ran when the chain_task ends. callback can be nil. -minetest.Async.chain_task(pool,tasks,callback) +-- callback(arg) function is ran when the chain_task ends. callback can be nil. +extended_api.Async.chain_task(pool,tasks,callback) -- Async task queue. -- 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. -minetest.Async.queue_task(pool,func,callback) \ No newline at end of file +-- callback(arg) function is ran when the queue_task ends. callback can be nil. +extended_api.Async.queue_task(pool,func,callback) \ No newline at end of file diff --git a/async.lua b/async.lua index b6ca34d..e551860 100644 --- a/async.lua +++ b/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"} return pool end -function minetest.Async.create_worker(pool,func) +function extended_api.Async.create_worker(pool,func) local thread = coroutine.create(func) table.insert(pool.threads, thread) end -function minetest.Async.create_globalstep_worker(pool,func) +function extended_api.Async.create_globalstep_worker(pool,func) local thread = coroutine.create(func) table.insert(pool.globalstep_threads, thread) end -function minetest.Async.run_worker(pool,index) +function extended_api.Async.run_worker(pool,index) local thread = pool.threads[index] if thread == nil or coroutine.status(thread) == "dead" then table.remove(pool.threads, index) - minetest.after(0,minetest.Async.schedule_worker,pool) + minetest.after(0,extended_api.Async.schedule_worker,pool) return false else coroutine.resume(thread) - minetest.after(0,minetest.Async.schedule_worker,pool) + minetest.after(0,extended_api.Async.schedule_worker,pool) return true 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] if thread == nil or coroutine.status(thread) == "dead" then 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 else coroutine.resume(thread) - minetest.after(0,minetest.Async.schedule_globalstep_worker,pool) + minetest.after(0,extended_api.Async.schedule_globalstep_worker,pool) return true end end -function minetest.Async.schedule_worker(pool) +function extended_api.Async.schedule_worker(pool) pool.state = "running" 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 end pool.state = "suspended" return false 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 - minetest.after(0,minetest.Async.run_globalstep_worker,pool,index) + minetest.after(0,extended_api.Async.run_globalstep_worker,pool,index) return true end return false end -function minetest.Async.priority(pool,resting,maxtime) +function extended_api.Async.priority(pool,resting,maxtime) pool.resting = resting pool.maxtime = maxtime end -function minetest.Async.iterate(pool,from,to,func,callback) - minetest.Async.create_worker(pool,function() +function extended_api.Async.iterate(pool,from,to,func,callback) + extended_api.Async.create_worker(pool,function() local last_time = minetest.get_us_time() * 1000 local maxtime = pool.maxtime for i = from, to do @@ -82,11 +82,11 @@ function minetest.Async.iterate(pool,from,to,func,callback) callback() end end) - minetest.Async.schedule_worker(pool) + extended_api.Async.schedule_worker(pool) end -function minetest.Async.foreach(pool,array, func, callback) - minetest.Async.create_worker(pool,function() +function extended_api.Async.foreach(pool,array, func, callback) + extended_api.Async.create_worker(pool,function() local last_time = minetest.get_us_time() * 1000 local maxtime = pool.maxtime for k,v in ipairs(array) do @@ -103,11 +103,11 @@ function minetest.Async.foreach(pool,array, func, callback) callback() end end) - minetest.Async.schedule_worker(pool) + extended_api.Async.schedule_worker(pool) end -function minetest.Async.do_while(pool,condition_func, func, callback) - minetest.Async.create_worker(pool,function() +function extended_api.Async.do_while(pool,condition_func, func, callback) + extended_api.Async.create_worker(pool,function() local last_time = minetest.get_us_time() * 1000 local maxtime = pool.maxtime while(condition_func()) do @@ -124,11 +124,11 @@ function minetest.Async.do_while(pool,condition_func, func, callback) callback() end end) - minetest.Async.schedule_worker(pool) + extended_api.Async.schedule_worker(pool) end -function minetest.Async.register_globalstep(pool,func) - minetest.Async.create_globalstep_worker(pool,function() +function extended_api.Async.register_globalstep(pool,func) + extended_api.Async.create_globalstep_worker(pool,function() local last_time = minetest.get_us_time() * 1000 local dtime = last_time while(true) do @@ -144,11 +144,11 @@ function minetest.Async.register_globalstep(pool,func) end end end) - minetest.Async.schedule_globalstep_worker(pool) + extended_api.Async.schedule_globalstep_worker(pool) end -function minetest.Async.chain_task(pool,tasks,callback) - minetest.Async.create_worker(pool,function() +function extended_api.Async.chain_task(pool,tasks,callback) + extended_api.Async.create_worker(pool,function() local pass_arg = {} local last_time = minetest.get_us_time() * 1000 local maxtime = pool.maxtime @@ -166,14 +166,14 @@ function minetest.Async.chain_task(pool,tasks,callback) callback(pass_arg) end end) - minetest.Async.schedule_worker(pool) + extended_api.Async.schedule_worker(pool) 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}) if pool.queue_threads > 0 then pool.queue_threads = pool.queue_threads - 1 - minetest.Async.create_worker(pool,function() + extended_api.Async.create_worker(pool,function() local pass_arg = {} local last_time = minetest.get_us_time() * 1000 local maxtime = pool.maxtime @@ -199,6 +199,6 @@ function minetest.Async.queue_task(pool,func,callback) end end end) - minetest.Async.schedule_worker(pool) + extended_api.Async.schedule_worker(pool) end end \ No newline at end of file diff --git a/description.txt b/description.txt index 0c22f63..9d8de11 100644 --- a/description.txt +++ b/description.txt @@ -1 +1 @@ -This mod adds more functions and features to the minetest api. \ No newline at end of file +This mod is a library that adds more functions and features. \ No newline at end of file diff --git a/init.lua b/init.lua index 77e6457..0b310d9 100644 --- a/init.lua +++ b/init.lua @@ -1,3 +1,6 @@ modpath = minetest.get_modpath("extended_api") + +extended_api = {} + dofile(string.format("%s/node_funcs.lua",modpath)) dofile(string.format("%s/async.lua",modpath)) \ No newline at end of file