Permalink
Browse files

Restored the interface to the callable passed to the notify keyword

argument in the Timer constructor: it takes one argument, the timer
that fired it.
  • Loading branch information...
1 parent 41078c9 commit 727548a9542b75099d61a6e1e0e0758d2a0d0b8b @dpopowich dpopowich committed Aug 27, 2010
Showing with 46 additions and 26 deletions.
  1. +3 −4 examples/timerdemo/timerdemo.py
  2. +43 −22 library/pyjamas/Timer.py
@@ -82,9 +82,8 @@ def onClick(self, button):
self.button.stop = True
self.timer.scheduleRepeating(1000)
self.button.setText(self.stop_txt)
- self.updateclock()
- def updateclock(self):
+ def updateclock(self, timer):
# the callable attached to the timer with notify
dt = datetime.now().replace(microsecond=0)
@@ -211,7 +210,7 @@ def initialize(self):
self.slider.setValue(self.value)
self.slider.setControlPos(self.value)
- def onTimer(self):
+ def onTimer(self, timer):
# when the timer fires we randomize the color and (maybe)
# reschedule ourselves.
@@ -234,7 +233,7 @@ def onControlValueChanged(self, sender, old, new):
else:
# no it's being reset
self.__label.setText(str(new) + ' sec')
- self.onTimer()
+ self.onTimer(self.timer)
def randomcolor(self):
@@ -58,10 +58,13 @@ def onWindowClosing(self):
def __init__(self, delayMillis=0, notify=None):
'''Called with no arguments, create a timer that will call its
- run() method when it is scheduled and fired. This is GWT's
- interface and behaviour. There are two enhancements to
- pyjamas' implementation when specified with special keyword
- arguments:
+ run() method when it is scheduled and fired. This usage
+ requires subclassing to implement the run() method. This is
+ GWT's interface and behaviour.
+
+ There are two enhancements to pyjamas' implementation when
+ specified with special keyword arguments, one of which
+ obviates the need for subclassing.
timer = Timer(delayMillis=ms)
@@ -73,31 +76,49 @@ def __init__(self, delayMillis=0, notify=None):
and:
timer = Timer(notify=object_or_func)
-
+
is the same as:
timer = Timer()
run = getattr(object_or_func, 'onTimer', object_or_func)
if not callable(run): raise ValueError, msg
- timer.run = run
i.e., the value passed to notify is checked to see if it has
- an onTimer attribute; if so, it is used as run(), if not the
- object itself is used as run()
+ an onTimer attribute; if so, it is used as the callable, if
+ not, the object itself is used as the callable.
+
+ NOTE: when notify is specified, the function or method will be
+ called with one argument: the instance of the timer. So, this
+ would be proper usage:
+
+ def timer_cb(timer):
+ ...
+
+ timer = Timer(notify=timer_cb)
+
+ or:
+
+ class myclass:
+
+ def __init__(self):
+ ...
+ self.timer = Timer(notify=self)
- NOTE: there are no positional arguments!
+ def onTimer(self, timer):
+ ...
'''
# initialize a few house keeping vars
- self.tid = None
+ self.__tid = None
+ self.__onTimer = lambda: self.run()
Window.addWindowCloseListener(Timer.__WindowCloseListener())
# check notify
if notify is not None:
run = getattr(notify, 'onTimer', notify)
if not callable(run):
raise ValueError, 'Programming error: notify must be callable'
- self.run = run
+ self.__onTimer = lambda: run(self)
# schedule?
if delayMillis != 0:
@@ -106,15 +127,15 @@ def __init__(self, delayMillis=0, notify=None):
def cancel(self):
'Cancel the timer.'
- if self.tid is None:
+ if self.__tid is None:
return
- if self.is_repeating:
- self.__clearInterval(self.tid)
+ if self.__is_repeating:
+ self.__clearInterval(self.__tid)
else:
- self.__clearTimeout(self.tid)
+ self.__clearTimeout(self.__tid)
- self.tid = None
+ self.__tid = None
Timer.__timers.discard(self)
def run(self):
@@ -132,8 +153,8 @@ def schedule(self, delayMillis):
raise ValueError, 'delay must be positive'
self.cancel()
- self.is_repeating = False
- self.tid = self.__setTimeout(delayMillis)
+ self.__is_repeating = False
+ self.__tid = self.__setTimeout(delayMillis)
Timer.__timers.add(self)
@@ -146,16 +167,16 @@ def scheduleRepeating(self, periodMillis):
raise ValueError, 'period must be positive'
self.cancel()
- self.is_repeating = True
- self.tid = self.__setInterval(periodMillis)
+ self.__is_repeating = True
+ self.__tid = self.__setInterval(periodMillis)
Timer.__timers.add(self)
# fire the timer
def __fire(self):
# if not repeating, remove it from the list of active timers
- if not self.is_repeating:
+ if not self.__is_repeating:
Timer.__timers.discard(self)
- self.run()
+ self.__onTimer()
######################################################################
# Platforms need to implement the following four methods

0 comments on commit 727548a

Please sign in to comment.