Jump to content


Mitchfizz05's Content

There have been 2 items by Mitchfizz05 (Search limited from 29-March 23)


By content type

See this member's

Sort by                Order  

#262690 Delayed Callback

Posted by Mitchfizz05 on 13 December 2016 - 05:02 PM in APIs and Utilities

View Postjustync7, on 13 December 2016 - 01:22 AM, said:

Huh, Seems like you were a bit late to the party.
Good job, nonetheless.

Ah damn it! xD

Eh well. I suppose this will still serve its purpose in my own project.



#262665 Delayed Callback

Posted by Mitchfizz05 on 12 December 2016 - 11:06 PM in APIs and Utilities

Hello!

Out of the blue, I'm posting a small library I've made, which will be used for a larger project I'm working on.

It's a library that lets you specify a callback, and queue it for execution after a set amount of time without blocking the rest of the code from executing.

The general gist:
callback.new(function ()
print("Hello world!")
end, 5)

This would print "Hello world!" after 5 seconds.

When there are no more callbacks waiting to be executed, an event called "no_events_left" will be raised. This might be useful if you want your program to exit after all callbacks are finished, for example.

Setup

This library does require your program to have a main pullEvent loop, as it needs to be able to see all events happening to know when to execute the callback. How to do this depends on how your pullEvent loop is setup.

If you're loop is something like this: local e, param1, param2 = os.pullEvent() , or something similar, put this on the line after os.pullEvent()...
callback.pullEvent({e, param1})

If you're loop is something like this: local e = {os.pullEvent()} , or similar, put this after...
callback.pullEvent(e)

Obviously, substitute variable names where necessary.

Also, you'll want to load the API with os.loadAPI("callback").

If you're confused, feel free to check the demo (under downloads).

Download

pastebin get rsVLRw8t callback
pastebin get Gz1TFRas demo

The library is licensed under the MIT license (you don't need to ask me for permission to use, just give credit).

Thanks!