|
|
@ -7,6 +7,101 @@ 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 = {...} |
|
|
|
|
|
|
|
-- Break on interrupts |
|
|
|
function f(name, ...) |
|
|
|
-- if args[1] happens to be "interrupted" already the second won't trigger |
|
|
|
if name == args[1] or name == "interrupted" then |
|
|
|
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") |
|
|
|
|
|
|
|
repeat |
|
|
|
coroutine.yield() |
|
|
|
tmp = {event.pullFiltered(.01, f, ...)} |
|
|
|
until #tmp > 0 |
|
|
|
else |
|
|
|
local timeout = nil |
|
|
|
if type(args[1]) == "number" then |
|
|
|
timeout = args[1] |
|
|
|
args = ll.slice({...}, 2) |
|
|
|
-- For speed purposes check up here even though it's more repeated code |
|
|
|
elseif type(args[1]) == "function" then |
|
|
|
-- Wrap other function so this will handle interrupts |
|
|
|
function g(name, ...) |
|
|
|
local a = ll.slice(args, 2) |
|
|
|
-- Handle interrupt |
|
|
|
if name == "interrupted" then |
|
|
|
return true |
|
|
|
end |
|
|
|
-- Otherwise run other function |
|
|
|
return args[1](name, ...) |
|
|
|
end |
|
|
|
|
|
|
|
if timeout ~= nil then |
|
|
|
-- Check if not in a coroutine |
|
|
|
if not status then |
|
|
|
return event.pullFiltered(timeout, g, table.unpack(args)) |
|
|
|
end |
|
|
|
|
|
|
|
repeat |
|
|
|
coroutine.yield() |
|
|
|
tmp = {event.pullFiltered(.01, g, table.unpack(args))} |
|
|
|
until #tmp > 0 or computer.uptime() - start >= timeout |
|
|
|
else |
|
|
|
-- Check if not in a coroutine |
|
|
|
if not status then |
|
|
|
return event.pullFiltered(g, ...) |
|
|
|
end |
|
|
|
|
|
|
|
repeat |
|
|
|
coroutine.yield() |
|
|
|
tmp = {event.pullFiltered(.01, g, table.unpack(args))} |
|
|
|
until #tmp > 0 |
|
|
|
end |
|
|
|
else |
|
|
|
if timeout ~= nil then |
|
|
|
-- Check if not in a coroutine |
|
|
|
if not status then |
|
|
|
return event.pullFiltered(timeout, f, table.unpack(args)) |
|
|
|
end |
|
|
|
|
|
|
|
repeat |
|
|
|
coroutine.yield() |
|
|
|
tmp = {event.pullFiltered(.01, f, table.unpack(args))} |
|
|
|
until #tmp > 0 or computer.uptime() - start >= timeout |
|
|
|
else |
|
|
|
-- Check if not in a coroutine |
|
|
|
if not status then |
|
|
|
return event.pullFiltered(f, table.unpack(args)) |
|
|
|
end |
|
|
|
|
|
|
|
repeat |
|
|
|
coroutine.yield() |
|
|
|
tmp = {event.pullFiltered(.01, f, table.unpack(args))} |
|
|
|
until #tmp > 0 |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
return table.unpack(tmp) |
|
|
|
end |
|
|
|
|
|
|
|
-- An alias for async.pullFiltered which does all of the logic for both functions |
|
|
|
async.pull = async.pullFiltered |
|
|
|
|
|
|
|
-- Sleep in a coroutine friendly way |
|
|
|
function async.sleep(s) |
|
|
|
local _, status = coroutine.running() |
|
|
@ -67,11 +162,11 @@ function async.roundRobin(loop, ignoreError) |
|
|
|
if not status and not ignoreError then |
|
|
|
io.stderr:write(results[1]) |
|
|
|
return |
|
|
|
-- Don't exit but print the bad message |
|
|
|
-- Don't exit but print the bad message |
|
|
|
elseif not status then |
|
|
|
print(results[1]) |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
-- Stop running dead coroutines and add the results |
|
|
|
if coroutine.status(b.coro) == "dead" then |
|
|
|
b.results = results |
|
|
@ -85,4 +180,4 @@ end |
|
|
|
|
|
|
|
return async |
|
|
|
|
|
|
|
-- wget -f http://mc.bashed.rocks:13699/lib/async.lua async.lua |
|
|
|
-- wget -f http://mc.bashed.rocks:13699/lib/async.lua /usr/lib/async.lua |