|
|
@ -7,15 +7,21 @@ local table = require("table") |
|
|
|
|
|
|
|
local async = {} |
|
|
|
|
|
|
|
-- TODO FINISH THIS |
|
|
|
-- An async wrapper for event.pullFiltered |
|
|
|
function async.pullFiltered(...) |
|
|
|
-- Check if we're in a coroutine or not |
|
|
|
_, status = coroutine.running() |
|
|
|
|
|
|
|
local start = computer.uptime() |
|
|
|
local args = {...} |
|
|
|
|
|
|
|
local blocking = nil |
|
|
|
-- Check if we're in a coroutine or not |
|
|
|
if type(args[#args]) == "boolean" and args[#args] then |
|
|
|
blocking = true |
|
|
|
else |
|
|
|
blocking = false |
|
|
|
end |
|
|
|
|
|
|
|
-------------------- |
|
|
|
-- Break on interrupts |
|
|
|
function f(name, ...) |
|
|
|
-- if args[1] happens to be "interrupted" already the second won't trigger |
|
|
@ -23,11 +29,11 @@ function async.pullFiltered(...) |
|
|
|
return true |
|
|
|
end |
|
|
|
end |
|
|
|
-------------------- |
|
|
|
|
|
|
|
if type(args[1]) == "string" then |
|
|
|
-- Check if not in a coroutine |
|
|
|
if not status then return event.pullFiltered(f, ...) end |
|
|
|
print("Testing") |
|
|
|
if blocking then return event.pullFiltered(f, ...) end |
|
|
|
|
|
|
|
repeat |
|
|
|
coroutine.yield() |
|
|
@ -38,8 +44,10 @@ function async.pullFiltered(...) |
|
|
|
if type(args[1]) == "number" then |
|
|
|
timeout = args[1] |
|
|
|
args = ll.slice({...}, 2) |
|
|
|
end |
|
|
|
-- For speed purposes check up here even though it's more repeated code |
|
|
|
elseif type(args[1]) == "function" then |
|
|
|
if type(args[1]) == "function" then |
|
|
|
-------------------- |
|
|
|
-- Wrap other function so this will handle interrupts |
|
|
|
function g(name, ...) |
|
|
|
local a = ll.slice(args, 2) |
|
|
@ -50,10 +58,11 @@ function async.pullFiltered(...) |
|
|
|
-- Otherwise run other function |
|
|
|
return args[1](name, ...) |
|
|
|
end |
|
|
|
-------------------- |
|
|
|
|
|
|
|
if timeout ~= nil then |
|
|
|
-- Check if not in a coroutine |
|
|
|
if not status then |
|
|
|
if blocking then |
|
|
|
return event.pullFiltered(timeout, g, table.unpack(args)) |
|
|
|
end |
|
|
|
|
|
|
@ -63,7 +72,7 @@ function async.pullFiltered(...) |
|
|
|
until #tmp > 0 or computer.uptime() - start >= timeout |
|
|
|
else |
|
|
|
-- Check if not in a coroutine |
|
|
|
if not status then |
|
|
|
if blocking then |
|
|
|
return event.pullFiltered(g, ...) |
|
|
|
end |
|
|
|
|
|
|
@ -73,9 +82,10 @@ function async.pullFiltered(...) |
|
|
|
until #tmp > 0 |
|
|
|
end |
|
|
|
else |
|
|
|
print(timeout, blocking) |
|
|
|
if timeout ~= nil then |
|
|
|
-- Check if not in a coroutine |
|
|
|
if not status then |
|
|
|
if blocking then |
|
|
|
return event.pullFiltered(timeout, f, table.unpack(args)) |
|
|
|
end |
|
|
|
|
|
|
@ -85,7 +95,7 @@ function async.pullFiltered(...) |
|
|
|
until #tmp > 0 or computer.uptime() - start >= timeout |
|
|
|
else |
|
|
|
-- Check if not in a coroutine |
|
|
|
if not status then |
|
|
|
if blocking then |
|
|
|
return event.pullFiltered(f, table.unpack(args)) |
|
|
|
end |
|
|
|
|
|
|
@ -96,6 +106,9 @@ function async.pullFiltered(...) |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
if tmp == nil then |
|
|
|
return nil |
|
|
|
end |
|
|
|
return table.unpack(tmp) |
|
|
|
end |
|
|
|
|
|
|
@ -103,10 +116,9 @@ end |
|
|
|
async.pull = async.pullFiltered |
|
|
|
|
|
|
|
-- Sleep in a coroutine friendly way |
|
|
|
function async.sleep(s) |
|
|
|
local _, status = coroutine.running() |
|
|
|
function async.sleep(s, blocking) |
|
|
|
-- If not a coroutine just regular sleep |
|
|
|
if status then |
|
|
|
if blocking then |
|
|
|
os.sleep(s) |
|
|
|
return |
|
|
|
end |
|
|
@ -136,7 +148,7 @@ local loop = { |
|
|
|
function async.bundle(f, ...) |
|
|
|
return { |
|
|
|
coro = coroutine.create(f), |
|
|
|
args = {...}, |
|
|
|
args = {...} or {}, |
|
|
|
results = {} |
|
|
|
} |
|
|
|
end |
|
|
@ -156,19 +168,20 @@ function async.roundRobin(loop, ignoreError) |
|
|
|
local i = 1 |
|
|
|
repeat |
|
|
|
local b = loop.live[i] |
|
|
|
print(b) |
|
|
|
local r = {coroutine.resume(b.coro, table.unpack(b.args))} |
|
|
|
local status, results = r[1], ll.slice(r, 2) |
|
|
|
local blocking, results = r[1], ll.slice(r, 2) |
|
|
|
-- Fail and exit if a function is bad |
|
|
|
if not status and not ignoreError then |
|
|
|
if blocking and not ignoreError then |
|
|
|
io.stderr:write(results[1]) |
|
|
|
return |
|
|
|
-- Don't exit but print the bad message |
|
|
|
elseif not status then |
|
|
|
elseif blocking then |
|
|
|
print(results[1]) |
|
|
|
end |
|
|
|
|
|
|
|
-- Stop running dead coroutines and add the results |
|
|
|
if coroutine.status(b.coro) == "dead" then |
|
|
|
if coroutine.blocking(b.coro) == "dead" then |
|
|
|
b.results = results |
|
|
|
table.insert(loop.dead, #loop.dead+1, table.remove(loop.live, i)) |
|
|
|
else |
|
|
|