Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local loop, event = nil, {} --pre-declare the loop and event
- function unblock( func )
- --create the coroutine
- --start with the function parameters
- --unblock( sleep, 1 ) will work because of this
- --pre-declare filter (starts without one)
- local tEventData, co, filter = {}
- return function( ... ) --return a function you can call as many times as you like
- if not co or coroutine.status( co ) == "dead" then --We need a fresh copy of our function
- co = coroutine.create( func )
- tEventData = {...}
- filter = nil
- end
- tEventData = event or {} --get the current event from the statemachine
- if not filter or tEventData[ 1 ] == filter then --if there isn't a filter or the filter matches
- local tVar = {coroutine.resume( co, unpack( tEventData ) )} --we resume the coroutine
- if tVar[ 1 ] and coroutine.status( co ) ~= "dead" then --if the coroutine didn't error and isn't done
- --we have a filter (or lack thereof)
- filter = tVar[ 2 ]
- elseif tVar[ 1 ] then --if the coroutine didn't error but is done
- --we have a returned value, and the execution has finished
- return unpack( tVar )
- elseif coroutine.status( co ) == "dead" then --if the coroutine is dead, but wasn't ok
- --we have an error
- error( tVar[ 2 ], 0 )
- end
- end
- end
- end
- function setLoop( func ) --sets the loop function to call
- if type( func ) ~= "function" then --if you sent me a value I didn't want
- error( "Expected function, got " .. type( func ), 2 ) --I'm going to tell you
- end
- loop = func
- end
- function run() --this is the only blocking call of this API, literally to run the program
- while true do
- if loop() then
- break
- end
- event = {os.pullEventRaw()} --this handles event handling for you
- end
- end
- function pullEvent() --my own pullEvent thing.
- return unpack( event )
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement