Operating system for OpenComputers
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

43 lignes
1.1KB

  1. local event = {}
  2. function event.pull(t,...) -- 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 not (tEv[i] or ""):match(tA[i]) then
  17. ret = false
  18. end
  19. end
  20. if ret then return table.unpack(tEv) end
  21. until computer.uptime() > tE
  22. return nil
  23. end
  24. function event.listen(e,f) -- run function *f* for every occurance of event *e*
  25. local op = os.getenv("parent")
  26. os.setenv("parent",cPid)
  27. os.spawn(function() while true do
  28. local tEv = {coroutine.yield()}
  29. if tEv[1] == e then
  30. f(table.unpack(tEv))
  31. end
  32. if not tTasks[os.getenv("parent")] or (tEv[1] == "unlisten" and tEv[2] == e and tEv[3] == tostring(f)) then break end
  33. end end,string.format("[%d] %s listener",cPid,e))
  34. os.setenv("parent",op)
  35. end
  36. function event.ignore(e,f) -- stop function *f* running for every occurance of event *e*
  37. computer.pushSignal("unlisten",e,tostring(f))
  38. end
  39. return event