2020-01-31 00:55:50 +00:00
|
|
|
--[[
|
|
|
|
simple kernel for async tasks running in the background
|
2020-02-01 08:39:51 +00:00
|
|
|
|
2020-03-16 09:17:48 +00:00
|
|
|
can "stall" a task by yielding the string "stall"
|
|
|
|
this will suspend the coroutine until the rest of
|
|
|
|
the queue has been processed or stalled
|
|
|
|
and can early-out update_for_time
|
|
|
|
|
2020-02-01 08:39:51 +00:00
|
|
|
todo:
|
|
|
|
multiple types of callbacks
|
|
|
|
finish, error, step
|
|
|
|
getting a reference to the task for manipulation
|
|
|
|
attaching multiple callbacks
|
|
|
|
cancelling
|
2020-01-31 00:55:50 +00:00
|
|
|
]]
|
|
|
|
|
2020-04-07 03:49:10 +00:00
|
|
|
local path = (...):gsub("async", "")
|
|
|
|
local class = require(path .. "class")
|
|
|
|
|
|
|
|
local async = class()
|
2020-01-31 00:55:50 +00:00
|
|
|
|
|
|
|
function async:new()
|
2020-04-07 03:49:10 +00:00
|
|
|
return self:init({
|
2020-01-31 00:55:50 +00:00
|
|
|
tasks = {},
|
2020-03-16 09:17:48 +00:00
|
|
|
tasks_stalled = {},
|
2020-04-07 03:49:10 +00:00
|
|
|
})
|
2020-01-31 00:55:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
--add a task to the kernel
|
2020-04-28 02:08:53 +00:00
|
|
|
function async:call(f, args, callback, error_callback)
|
2020-05-19 12:11:38 +00:00
|
|
|
self:add(coroutine.create(f), args, callback, error_callback)
|
2020-04-28 02:08:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
--add an already-existing coroutine to the kernel
|
|
|
|
function async:add(co, args, callback, error_callback)
|
|
|
|
table.insert(self.tasks, {
|
|
|
|
co,
|
2020-05-19 12:11:38 +00:00
|
|
|
args or {},
|
2020-04-28 02:08:53 +00:00
|
|
|
callback or false,
|
|
|
|
error_callback or false,
|
2020-01-31 00:55:50 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
--update some task in the kernel
|
|
|
|
function async:update()
|
|
|
|
--grab task definition
|
|
|
|
local td = table.remove(self.tasks, 1)
|
|
|
|
if not td then
|
2020-03-16 09:17:48 +00:00
|
|
|
--have we got stalled tasks to re-try?
|
|
|
|
if #self.tasks_stalled > 0 then
|
|
|
|
--swap queues rather than churning elements
|
|
|
|
self.tasks_stalled, self.tasks = self.tasks, self.tasks_stalled
|
|
|
|
td = table.remove(self.tasks, 1)
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
2020-01-31 00:55:50 +00:00
|
|
|
end
|
|
|
|
--run a step
|
2020-04-28 02:08:53 +00:00
|
|
|
--(using unpack because coroutine is also nyi and it's core to this async model)
|
|
|
|
local co, args, cb, error_cb = unpack(td)
|
|
|
|
--(8 temps rather than table churn capturing varargs)
|
2020-05-19 12:11:38 +00:00
|
|
|
local success, a, b, c, d, e, f, g, h = coroutine.resume(co, unpack(args))
|
2020-01-31 00:55:50 +00:00
|
|
|
--error?
|
|
|
|
if not success then
|
|
|
|
if error_cb then
|
|
|
|
error_cb(a)
|
|
|
|
else
|
|
|
|
error("failure in async task: "..a)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
--check done
|
2020-02-01 10:38:00 +00:00
|
|
|
if coroutine.status(co) == "dead" then
|
2020-01-31 00:55:50 +00:00
|
|
|
--done? run callback with result
|
2020-03-16 09:17:48 +00:00
|
|
|
if cb then
|
|
|
|
cb(a, b, c, d, e, f, g, h)
|
|
|
|
end
|
2020-01-31 00:55:50 +00:00
|
|
|
else
|
2020-03-16 09:17:48 +00:00
|
|
|
--if not completed, re-add to the appropriate queue
|
|
|
|
if a == "stall" then
|
|
|
|
--add to stalled queue as signalled stall
|
|
|
|
table.insert(self.tasks_stalled, td)
|
|
|
|
else
|
|
|
|
table.insert(self.tasks, td)
|
|
|
|
end
|
2020-01-31 00:55:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
--update tasks for some amount of time
|
2020-03-16 09:17:48 +00:00
|
|
|
function async:update_for_time(t, early_out_stalls)
|
2020-01-31 00:55:50 +00:00
|
|
|
local now = love.timer.getTime()
|
|
|
|
while love.timer.getTime() - now < t do
|
|
|
|
if not self:update() then
|
|
|
|
break
|
|
|
|
end
|
2020-03-16 09:17:48 +00:00
|
|
|
--all stalled?
|
|
|
|
if early_out_stalls and #self.tasks == 0 then
|
|
|
|
break
|
|
|
|
end
|
2020-01-31 00:55:50 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-16 09:17:48 +00:00
|
|
|
--add a function to run after a certain delay (in seconds)
|
|
|
|
function async:add_timeout(f, delay)
|
|
|
|
local trigger_time = love.timer.getTime() + delay
|
|
|
|
self:call(function()
|
|
|
|
while love.timer.getTime() < trigger_time do
|
|
|
|
coroutine.yield("stall")
|
|
|
|
end
|
|
|
|
f()
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
--add a function to run repeatedly every delay (in seconds)
|
|
|
|
--note: not super useful currently unless you plan to destroy the async object
|
|
|
|
-- as there's no way to remove tasks :)
|
|
|
|
function async:add_interval(f, delay)
|
|
|
|
local trigger_time = love.timer.getTime() + delay
|
|
|
|
self:call(function()
|
|
|
|
while true do
|
|
|
|
while love.timer.getTime() < trigger_time do
|
|
|
|
coroutine.yield("stall")
|
|
|
|
end
|
|
|
|
f()
|
|
|
|
trigger_time = trigger_time + delay
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2020-01-31 00:55:50 +00:00
|
|
|
return async
|