Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --this file is an API for dynamically adding coroutines to the running set
- local routines = {}
- function add(f,...)
- if (type(f) ~= "function") then error("Expected function",2) return end
- local coro = coroutine.create(f)
- routines[coro] = {args={...},started=false,filter=nil}
- return
- end
- function begin(f,...)
- if (type(f) ~= "function") then error("Expected function",2) return end
- add(f,...)
- local event = {}
- while true do
- local mfd = {}
- for coro,t in pairs(routines) do
- if (coro ~= nil) then
- if (t.started == false) then
- t.started = true
- local _,param = coroutine.resume(coro,unpack(t.args))
- t.filter = param
- else
- if ((t.filter == nil) or (t.filter == event[1]) or (event[1] == "terminate")) then
- local _,param = coroutine.resume(coro,unpack(event))
- t.filter = param
- end
- end
- if (coroutine.status(coro) == "dead") then table.insert(mfd,coro) end
- end
- end
- for _,coro in pairs(mfd) do routines[coro] = nil end
- event = {os.pullEventRaw()}
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement