Computercraft thingies
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

program.lua 654B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. os.loadAPI("/lib/util")
  2. function Prog()
  3. local self = {}
  4. util.makeEventListener(self)
  5. local running = true
  6. local timeouts = {}
  7. self.init = function()
  8. self.emit("init")
  9. while running do
  10. evt, a, b, c, d, e = os.pullEvent()
  11. if evt == "timer" and timeouts[a] then
  12. timeouts[a]()
  13. else
  14. self.emit(evt, a, b, c, d, e)
  15. end
  16. end
  17. end
  18. self.exit = function()
  19. self.emit("exit")
  20. running = false
  21. os.startTimer(0)
  22. end
  23. self.setTimeout = function(time, cb)
  24. local id = os.startTimer(time)
  25. timeouts[id] = cb
  26. return id
  27. end
  28. self.cancelTimeout = function(id)
  29. os.cancelTimer(id)
  30. timeouts[id] = nil
  31. end
  32. return self
  33. end