Browse code

Don't cancel timers twice.

Returning False from tick will cancel the timer and invalidate `timeout_id`. To
avoid trying to call `source_remove` on a non-existent `timeout_id`, this commit
calls `stop()` from within `tick` (well, `__real_tick`).

Technically, we don't need to call `source_remove` from within `tick` (we could
just set `timeout_id` to `None`) but doing it this way means we have exactly one
code path for ending animations.

Steven Allen authored on 28/04/2016 16:45:11 • Jose Fonseca committed on 28/04/2016 19:19:19
Showing 1 changed files

  • xdot.py index fa81b7d..a0be1b1 100755
... ...
@@ -1322,7 +1322,7 @@ class Animation(object):
1322 1322
         self.timeout_id = None
1323 1323
 
1324 1324
     def start(self):
1325
-        self.timeout_id = GLib.timeout_add(int(self.step * 1000), self.tick)
1325
+        self.timeout_id = GLib.timeout_add(int(self.step * 1000), self.__real_tick)
1326 1326
 
1327 1327
     def stop(self):
1328 1328
         self.dot_widget.animation = NoAnimation(self.dot_widget)
... ...
@@ -1330,8 +1330,18 @@ class Animation(object):
1330 1330
             GLib.source_remove(self.timeout_id)
1331 1331
             self.timeout_id = None
1332 1332
 
1333
+    def __real_tick(self):
1334
+        try:
1335
+            if not self.tick():
1336
+                self.stop()
1337
+                return False
1338
+        except e:
1339
+            self.stop()
1340
+            raise e
1341
+        return True
1342
+
1333 1343
     def tick(self):
1334
-        self.stop()
1344
+        return False
1335 1345
 
1336 1346
 
1337 1347
 class NoAnimation(Animation):