Changeset 35943 for lang/io

Show
Ignore:
Timestamp:
11/18/09 03:14:53 (4 years ago)
Author:
nowelium
Message:

Io Timer

Location:
lang/io/Timer
Files:
2 added
1 modified

Legend:

Unmodified
Added
Removed
  • lang/io/Timer/Timer.io

    r35936 r35943  
    11Timer := Object clone do ( 
    2     ScheduleFixRateUnit := Object clone do ( 
     2    ScheduleUnit := Object clone do( 
    33        newSlot("command", nil) 
    44        newSlot("period", nil) 
    5         run := method( 
    6             ini := Date now asNumber 
    7             current := Date now asNumber 
    8             loop ( 
    9                 current = Date now asNumber 
    10                 diff := (current - ini) 
    11                 if(period < diff) then ( 
    12                     ini = current 
    13                     command @@call 
    14                     yield 
    15                 ) 
    16                 System sleep(0.01) 
     5        init := method( 
     6            self initial := Date now asNumber 
     7            self current := Date now asNumber 
     8        ) 
     9        execute := method( 
     10            self current = Date now asNumber 
     11            diff := (self current - self initial) 
     12            if(period < diff) then ( 
     13                command @@call 
     14            ) 
     15        ) 
     16    ) 
     17    ScheduleFixRateUnit := ScheduleUnit clone do ( 
     18        execute := method( 
     19            self current = Date now asNumber 
     20            diff := (self current - self initial) 
     21            if(period < diff) then ( 
     22                self initial = self current 
     23                command @@call 
     24            ) 
     25        ) 
     26    ) 
     27    ScheduleFixDelayUnit := ScheduleUnit clone do ( 
     28        execute := method( 
     29            self current = Date now asNumber 
     30            diff := (self current - self initial) 
     31            if(period < diff) then ( 
     32                command call 
     33                self initial = Date now asNumber 
    1734            ) 
    1835        ) 
    1936    ) 
    2037    schedule := method(command, period, 
    21         unit := ScheduleFixRateUnit clone 
     38        addUnit(ScheduleUnit clone, command, period) 
     39    ) 
     40    scheduleWithFixedRate := method(command, period, 
     41        addUnit(ScheduleFixRateUnit clone, command, period) 
     42    ) 
     43    scheduleWithFixedDelay := method(command, period, 
     44        addUnit(ScheduleFixDelayUnit clone, command, period) 
     45    ) 
     46    addUnit := method(unit, command, period, 
    2247        unit setCommand(command) 
    2348        unit setPeriod(period) 
    2449        self units append(unit) 
    2550    ) 
    26     run := method( 
    27         self units foreach(u, 
    28             u @@run 
     51    start := method( 
     52        block(loop( 
     53            units foreach(unit, 
     54                unit @@execute 
     55            ) 
     56            yield 
     57            System sleep(0.01) 
     58        )) @@call 
     59        self running = true 
     60    ) 
     61    join := method( 
     62        while(Scheduler yieldingCoros size > 0, 
     63            if(self running not) then( 
     64                break 
     65            ) 
     66                yield 
    2967        ) 
     68    ) 
     69    stop := method( 
     70        self running = false 
    3071    ) 
    3172    init := method( 
    3273        self units := List clone 
     74        self running := false 
    3375    ) 
    3476)