Description
Read
this article of the Wiki.
API
local async = LibAsync
local task = async:Create(name)
The signature of FuncOfTask is:
Code:
local function(task)
end
-- Get the current context, if you are within a FuncOfTask or nil.
Code:
function async:GetCurrent()
-- Create an interruptible task context.
Code:
function async:Create(name)
-- Resume the execution context.
-- Suspend the execution context and allow to resume anytime later.
Code:
function task:Suspend()
-- Interupt and fully stop the execution context. Can be called from outside to stop everything.
Code:
function task:Cancel()
-- Run the given FuncOfTask in your task context execution.
Code:
function task:Call(funcOfTask)
-- Continue your task context execution with the given FuncOfTask after the previous as finished.
Code:
function task:Then(funcOfTask)
-- Start an interruptible for-loop.
Code:
function task:For(from, to, step)
function task:For(pairs(tbl))
function task:For(ipairs(tbl))
-- Execute the async-for with the given step-function. The parameters of the step-function are those you would use in your for body.
Code:
function task:Do(func)
The signature if func is
function(index) or
function(key, value)
If you return async.BREAK within func, it will break the loop.
-- Start an interruptible while-loop.
Code:
function task:While(func):Do(doFunc)
As long as func returns true, doFunc and func will be called again.
-- Start an once per frame wait loop.
Code:
function task:WaitUntil(func)
Until func returns true, the function will be called again in the next frame. Keep the check condition simple. For example, checking a flag variable set by an event.
-- Suspend the execution of your task context for the given delay in milliseconds and then call the given FuncOfTask to continue.
Code:
function task:Delay(delay, funcOfTask)
Delay will suspend the exceution immediately, no matter there in the chain it is used. For delaying at chain position use:
Code:
function task:ThenDelay(delay, funcOfTask)
-- Stop the delay created by Delay
Code:
function task:StopTimer()
-- Set a FuncOfTask as a final handler. Called even if something went wrong in your context.
Code:
function task:Finally(funcOfTask)
-- Set a FuncOfTask as an error handler. Called if something went wrong in your context.
Code:
function task:OnError(funcOfTask)
Example 1
Lua Code:
local async = LibAsync
local task = async:Create("example1")
local i=1
local function Hello() d("Hello") end
local function World() d("World") end
task:Call(function(task)
d(i)
task:Call(Hello):Then(World)
i = i + 1
return i<1000
end):Then(function() d("end") end)
Example 2
Lua Code:
local async = LibAsync
local task = async:Create("example2")
local start = GetGameTimeMilliseconds()
task:For (1,1000):Do(function(index) d(index) end):Then(
function()
task:For (pairs({"a", "b"})):Do(function(key, value) d(key,value)
task:For (ipairs({"c", "d"})):Do(function(key, value) d(key,value) end)
end)
end):For(1001,1010):Do(function(index) d(index) end):Then(function(task)
df("%ims", GetGameTimeMilliseconds() - start)
end)
Example 3
Lua Code:
local async = LibAsync
local i = 0
async:While(function() return i<100 end):Do(function()
d(i)
i = i+1
end)
Example 4
Lua Code:
local async = LibAsync
local time = GetGameTimeSeconds()+10
async:WaitUntil(function()
return GetGameTimeSeconds()>=time
end):Then(function()
d("end")
end)
Example 5: Nested calls.
Lua Code:
local async = LibAsync
async:Call(function(task)
d("a")
task:Call(function(task)
d("b")
task:Call(function(task)
d("c")
end)
end)
end):Then(function(task)
d("d")
task:Call(function(task)
d("e")
task:Call(function(task)
d("f")
end)
end)
end)
Output:
a
b
c
d
e
f
Remarks- Yes, using LibAsync increases the total duration of the process for a better framerate.
- Lua code can cause hiccups and (micro-)freezes, but has nothing to do the lag.
- Lua can cause crash to bug report, if too much memory is allocated within one frame. (e.g. string operations) => Spread you code over time.
- Lua can cause kick to login, if too much server request are done within a time range. (e.g. map pings/buy/sell/ignore, etc.) => Spread you code over time.