Jump to content




Delayed Callback

api lua utility

2 replies to this topic

#1 Mitchfizz05

  • Members
  • 125 posts
  • LocationAdelaide, Australia

Posted 12 December 2016 - 11:06 PM

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!

Edited by Mitchfizz05, 12 December 2016 - 11:31 PM.


#2 Justy

  • Members
  • 210 posts
  • LocationCLGD

Posted 13 December 2016 - 01:22 AM

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

#3 Mitchfizz05

  • Members
  • 125 posts
  • LocationAdelaide, Australia

Posted 13 December 2016 - 05:02 PM

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users