Fix scheduler examples;

This commit is contained in:
bjorn 2015-10-25 11:51:35 -07:00
parent 799cabc8f2
commit 01b87a5577
2 changed files with 11 additions and 9 deletions

View File

@ -1,4 +1,5 @@
local Rx = require 'rx' local Rx = require 'rx'
local scheduler = Rx.CooperativeScheduler.create()
-- Cheer someone on using functional reactive programming -- Cheer someone on using functional reactive programming
@ -8,12 +9,12 @@ local observable = Rx.Observable.fromCoroutine(function()
end end
return 'who do we appreciate' return 'who do we appreciate'
end) end, scheduler)
observable observable
:map(function(value) return value .. '!' end) :map(function(value) return value .. '!' end)
:subscribe(print) :subscribe(print)
repeat repeat
Rx.scheduler:update() scheduler:update()
until Rx.scheduler:isEmpty() until scheduler:isEmpty()

View File

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