Skip to content

Instantly share code, notes, and snippets.

@nowelium
Created April 14, 2013 11:49
Show Gist options
  • Save nowelium/5382432 to your computer and use it in GitHub Desktop.
Save nowelium/5382432 to your computer and use it in GitHub Desktop.
Obsolute Timer
doFile("Timer.io")
a := block(
"every 1 sec" println
Date now println
b := block(
"[b]sub block" println
)
b @@call
System sleep(3)
)
c := block(
"every 5 src" println
Date now println
d := block(
"[d]sub block" println
)
d @@call
)
t := Timer clone
t scheduleWithFixedDelay(a, 1)
t scheduleWithFixedDelay(c, 5)
t scheduleWithFixedDelay(block("stop" println; t stop), 30)
t start
t join
doFile("Timer.io")
a := block(
"every 1 sec" println
Date now println
b := block(
"[b]sub block" println
)
b @@call
System sleep(3)
)
c := block(
"every 5 src" println
Date now println
d := block(
"[d]sub block" println
)
d @@call
)
t := Timer clone
t scheduleWithFixedRate(a, 1)
t scheduleWithFixedRate(c, 5)
t scheduleWithFixedRate(block("stop" println; t stop), 30)
t start
t join
Timer := Object clone do (
ScheduleUnit := Object clone do(
newSlot("command", nil)
newSlot("period", nil)
init := method(
self initial := Date now asNumber
self current := Date now asNumber
)
execute := method(
self current = Date now asNumber
diff := (self current - self initial)
if(period < diff) then (
command @@call
)
)
)
ScheduleFixRateUnit := ScheduleUnit clone do (
execute := method(
self current = Date now asNumber
diff := (self current - self initial)
if(period < diff) then (
self initial = self current
command @@call
)
)
)
ScheduleFixDelayUnit := ScheduleUnit clone do (
execute := method(
self current = Date now asNumber
diff := (self current - self initial)
if(period < diff) then (
command call
self initial = Date now asNumber
)
)
)
schedule := method(command, period,
addUnit(ScheduleUnit clone, command, period)
)
scheduleWithFixedRate := method(command, period,
addUnit(ScheduleFixRateUnit clone, command, period)
)
scheduleWithFixedDelay := method(command, period,
addUnit(ScheduleFixDelayUnit clone, command, period)
)
addUnit := method(unit, command, period,
unit setCommand(command)
unit setPeriod(period)
self units append(unit)
)
start := method(
block(loop(
units foreach(unit,
unit @@execute
)
yield
System sleep(0.01)
)) @@call
self running = true
)
join := method(
while(Scheduler yieldingCoros size > 0,
if(self running not) then(
break
)
yield
)
)
stop := method(
self running = false
)
init := method(
self units := List clone
self running := false
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment