scheduler.lua 787 B

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