Add header.
[kai/samba.git] / source4 / lib / appweb / ejs-2.0 / ejs / lib / timer.js
1 /*
2  *      @file   timer.js
3  *      @brief  Timer class
4  *      @copy   Copyright (c) Mbedthis Software LLC, 2005-2006. All Rights Reserved.
5  *
6  *      Usage:
7  *              timer = new System.Timer("name", period);
8  *              timer.onTick = function(arg) {
9  *                      //      Anything here
10  *              }
11  *              timer.start();
12  *      or
13  *
14  *              timer = new System.Timer("name", period, obj, method);
15  *              timer.start();
16  */
17
18 /******************************************************************************/
19
20 class System.Timer
21 {
22         var             id;
23
24         /* MOB -- really need accessor on period. If user updates period, 
25                 then due must be updated. */
26         var             period;
27         var             due;
28         var             runOnce;                                        // Run timer just once
29         var             method;                                         // Callback method
30         var             obj;                                            // Callback object 
31
32         function Timer(id, period, obj, method)
33         {
34                 this.id = id;
35                 this.period = period;
36                 due = time() + period;
37
38                 if (arguments.length >= 3) {
39                         this.obj = obj;
40                 } else {
41                         this.obj = this;
42                 }
43                 if (arguments.length >= 4) {
44                         this.method = method;
45                 } else {
46                         this.method = "onTick";
47                 }
48         }
49         
50         /* MOB this should be deprecated */
51         function reschedule(period)
52         {
53                 /* MOB -- should update the timer service somehow */
54                 this.period = period;
55         }
56
57         function run(now)
58         {
59                 if (obj[method] == undefined) {
60                         trace("Timer cant find timer method " + method);
61                         due = now + this.period;
62                         return;
63                 }
64
65                 /*
66                  *      Run the timer
67                  */
68                 try {
69                         obj[method](this);
70                 }
71                 catch (error) {
72                         trace("Timer exception: " + error);
73                 }
74
75                 if (runOnce) {
76                         timerService.removeTimer(this);
77
78                 } else {
79                         due = now + this.period;
80                 }
81         }
82
83         function start()
84         {
85                 if (obj[method] == undefined) {
86                         throw new Error("Callback method is undefined");
87                 } else {
88                         timerService.addTimer(this);
89                 }
90         }
91
92         function stop()
93         {
94                 timerService.removeTimer(this);
95         }
96
97 }
98
99 /* MOB -- should not need this */
100 Timer = System.Timer;
101
102
103 /* 
104  *      Timer service
105  */
106 class System.TimerService
107 {
108         var             timers;
109         var             nextDue;
110
111         function TimerService() 
112         {
113                 timers = new Object();
114                 nextDue = 0;
115                 global.timerService = this;
116         }
117
118         function addTimer(timer)
119         {
120                 timers[timer.id] = timer;
121         }
122
123         function removeTimer(timer)
124         {
125                 try {
126                         delete timers[timer.id];
127                 }
128                 catch {}
129         }
130
131         function getIdleTime()
132         {
133                 return nextDue - time();
134         }
135
136         function runTimers()
137         {
138                 var             now = time();
139
140                 nextDue = 2147483647;           /* MOB -- MATH.MAX_INT; */
141
142                 for each (var timer in timers)
143                 {
144                         if (timer.due < now) {
145                                 timer.run(now);
146                         }
147                 }
148                 for each (var timer in timers)
149                 {
150                         if (timer.due < nextDue) {
151                                 nextDue = timer.due;
152                         }
153                 }
154                 // println("runTimers leaving with " + (nextDue - now));
155                 return nextDue - time();
156         }
157 }
158 TimerService = System.TimerService;