scheduler.lua 754 B

12345678910111213141516171819202122232425262728293031
  1. local Rx = require 'rx'
  2. local timerResolution = .25
  3. local function log(message)
  4. print('[' .. string.format('%.2f', Rx.scheduler.currentTime) .. '] ' .. message)
  5. end
  6. -- Demonstrate Rx.Scheduler.Cooperative by running some simultaneous cooperative threads.
  7. Rx.scheduler:schedule(function()
  8. log('this is like a setTimeout')
  9. end, 2)
  10. Rx.scheduler:schedule(function()
  11. local i = 1
  12. while true do
  13. log('this prints i twice per second: ' .. i)
  14. i = i + 1
  15. coroutine.yield(.5)
  16. end
  17. end)
  18. Rx.scheduler:schedule(function()
  19. for i = 1, 3 do
  20. log('this will print for 3 updates after 1 second')
  21. coroutine.yield()
  22. end
  23. end, 1)
  24. -- Simulate 3 virtual seconds.
  25. repeat
  26. Rx.scheduler:update(timerResolution)
  27. until Rx.scheduler.currentTime >= 3