Quantcast
Channel: ESOUI - AddOns by votan
Viewing all articles
Browse latest Browse all 722

LibAsync (1.7)

$
0
0
Description
Read this article of the Wiki.

API
local async = LibStub("LibAsync")
local task = async:Create(name)

The signature of FuncOfTask is:
local function(task)
end

-- Get the current context, if you are within a FuncOfTask or nil.
function async:GetCurrent()

-- Create an interruptible task context.
function async:Create(name)

-- Resume the execution context.
task:Resume()
-- Suspend the execution context and allow to resume anytime later.
function task:Suspend()

-- Interupt and fully stop the execution context. Can be called from outside to stop everything.
function task:Cancel()

-- Run the given FuncOfTask in your task context execution.
function task:Call(funcOfTask)

-- Continue your task context execution with the given FuncOfTask after the previous as finished.
function task:Then(funcOfTask)

-- Start an interruptible for-loop.
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.
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.

-- Suspend the execution of your task context for the given delay in milliseconds and then call the given FuncOfTask to continue.
function task:Delay(delay, funcOfTask)

-- Stop the delay created by Delay
function task:StopTimer()

-- Set a FuncOfTask as a final handler. Called even if something went wrong in your context.
function task:Finally(funcOfTask)

-- Set a FuncOfTask as an error handler. Called if something went wrong in your context.
function task:OnError(funcOfTask)


Example 1
local async = LibStub("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
local async = LibStub("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)
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/bug/sell/ignore, etc.) => Spread you code over time.

Viewing all articles
Browse latest Browse all 722

Trending Articles