Operating system for OpenComputers
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.

44 lines
1.2KB

  1. local event = {}
  2. function event.pull(t,...) -- number -- -- return an event, optionally with timeout *t* and filter *...*.
  3. local tA = {...}
  4. if type(t) == "string" then
  5. table.insert(tA,1,t)
  6. t = 0
  7. end
  8. if not t or t <= 0 then
  9. t = math.huge
  10. end
  11. local tE = computer.uptime()+t
  12. repeat
  13. tEv = {coroutine.yield()}
  14. local ret = true
  15. for i = 1, #tA do
  16. if type(tEv[i]) == "string" and type(tA[i]) == "string" then
  17. if not (tEv[i] or ""):match(tA[i]) then
  18. ret = false
  19. end
  20. else
  21. ret = tEv[i] == tA[i]
  22. end
  23. end
  24. if ret then return table.unpack(tEv) end
  25. until computer.uptime() > tE
  26. return nil
  27. end
  28. function event.listen(e,f) -- string function -- -- run function *f* for every occurance of event *e*
  29. os.spawn(function() while true do
  30. local tEv = {coroutine.yield()}
  31. if tEv[1] == e then
  32. f(table.unpack(tEv))
  33. end
  34. if not os.taskInfo(os.taskInfo().parent) or (tEv[1] == "unlisten" and tEv[2] == e and tEv[3] == tostring(f)) then break end
  35. end end,string.format("[%d] %s listener",os.pid(),e))
  36. end
  37. function event.ignore(e,f) -- string function -- -- stop function *f* running for every occurance of event *e*
  38. computer.pushSignal("unlisten",e,tostring(f))
  39. end
  40. return event