Advertisement
mitchfizz05

Delayed Callback

Dec 12th, 2016
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.51 KB | None | 0 0
  1. --[[
  2.     Lua delayed callback library.
  3.    
  4.     Copyright (c) 2016 Mitchell Nelson <me@mitchfizz05.net>
  5.  
  6.     Permission is hereby granted, free of charge, to any person obtaining a copy
  7.     of this software and associated documentation files (the "Software"), to deal
  8.     in the Software without restriction, including without limitation the rights
  9.     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10.     copies of the Software, and to permit persons to whom the Software is
  11.     furnished to do so, subject to the following conditions:
  12.  
  13.     The above copyright notice and this permission notice shall be included in
  14.     all copies or substantial portions of the Software.
  15.  
  16.     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17.     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18.     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  19.     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20.     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21.     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22.     THE SOFTWARE.
  23. ]]
  24.  
  25. local callbacks = {}
  26.  
  27. local function typeValidate(value, expectedType, argumentNumber, argumentName)
  28.     if type(value) ~= expectedType then
  29.         local a = tostring(argumentNumber)
  30.         if argumentName ~= nil then a = a .. " (" .. argumentName .. ")" end
  31.         error("Argument " .. a .. " expected " .. expectedType .. ", got " .. type(value))
  32.     end
  33. end
  34.  
  35. function new(func, delay)
  36.     typeValidate(func, "function", 1, "func")
  37.     typeValidate(delay, "number", 2, "delay")
  38.  
  39.     if delay > 0 then
  40.         local timer = os.startTimer(delay)
  41.         callbacks[timer] = func
  42.         sleep(0)
  43.         return timer
  44.     else
  45.         func()
  46.     end
  47. end
  48.  
  49. --[[
  50.     Should be called every os.pullEvent.
  51.     Allows library to check if any callbacks need to be executed.
  52. ]]
  53. function pullEvent(e)
  54.     if e[1] == "timer" then
  55.         local toRemove = {}
  56.         -- Check if any of the callback timers match.
  57.         for timer, func in pairs(callbacks) do
  58.             if timer == e[2] then
  59.                 -- Run callback, then queue for removal from callbacks.
  60.                 func()
  61.                 toRemove[#toRemove + 1] = timer
  62.             end
  63.         end
  64.        
  65.         -- Remove any callbacks queued for removal.
  66.         for _, timer in ipairs(toRemove) do
  67.             callbacks[timer] = nil
  68.         end
  69.        
  70.         --[[
  71.             If there are no callbacks left, raise an event.
  72.             This is so if the program wants to, it can exit once all callbacks are finished.
  73.             This probably won't be used often.
  74.         ]]
  75.         os.queueEvent("no_callbacks_left")
  76.     end
  77. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement