Fix scheduler examples;
This commit is contained in:
parent
799cabc8f2
commit
01b87a5577
@ -1,4 +1,5 @@
|
||||
local Rx = require 'rx'
|
||||
local scheduler = Rx.CooperativeScheduler.create()
|
||||
|
||||
-- Cheer someone on using functional reactive programming
|
||||
|
||||
@ -8,12 +9,12 @@ local observable = Rx.Observable.fromCoroutine(function()
|
||||
end
|
||||
|
||||
return 'who do we appreciate'
|
||||
end)
|
||||
end, scheduler)
|
||||
|
||||
observable
|
||||
:map(function(value) return value .. '!' end)
|
||||
:subscribe(print)
|
||||
|
||||
repeat
|
||||
Rx.scheduler:update()
|
||||
until Rx.scheduler:isEmpty()
|
||||
scheduler:update()
|
||||
until scheduler:isEmpty()
|
||||
|
@ -1,15 +1,16 @@
|
||||
local Rx = require 'rx'
|
||||
local scheduler = Rx.CooperativeScheduler.create()
|
||||
local timerResolution = .25
|
||||
local function log(message)
|
||||
print('[' .. string.format('%.2f', Rx.scheduler.currentTime) .. '] ' .. message)
|
||||
print('[' .. string.format('%.2f', scheduler.currentTime) .. '] ' .. message)
|
||||
end
|
||||
|
||||
-- Demonstrate Rx.Scheduler.Cooperative by running some simultaneous cooperative threads.
|
||||
Rx.scheduler:schedule(function()
|
||||
scheduler:schedule(function()
|
||||
log('this is like a setTimeout')
|
||||
end, 2)
|
||||
|
||||
Rx.scheduler:schedule(function()
|
||||
scheduler:schedule(function()
|
||||
local i = 1
|
||||
while true do
|
||||
log('this prints i twice per second: ' .. i)
|
||||
@ -18,7 +19,7 @@ Rx.scheduler:schedule(function()
|
||||
end
|
||||
end)
|
||||
|
||||
Rx.scheduler:schedule(function()
|
||||
scheduler:schedule(function()
|
||||
for i = 1, 3 do
|
||||
log('this will print for 3 updates after 1 second')
|
||||
coroutine.yield()
|
||||
@ -27,5 +28,5 @@ end, 1)
|
||||
|
||||
-- Simulate 3 virtual seconds.
|
||||
repeat
|
||||
Rx.scheduler:update(timerResolution)
|
||||
until Rx.scheduler.currentTime >= 3
|
||||
scheduler:update(timerResolution)
|
||||
until scheduler.currentTime >= 3
|
||||
|
Loading…
x
Reference in New Issue
Block a user