/library/DeferredCommand.py
Python | 39 lines | 27 code | 12 blank | 0 comment | 5 complexity | 2acd2ff87acdf26c94d971496fb88d67 MD5 | raw file
1from Timer import Timer 2 3deferredCommands = [] 4timerIsActive = False 5 6class DeferredCommand: 7 def add(self, cmd): 8 global deferredCommands 9 10 deferredCommands.append(cmd) 11 self.maybeSetDeferredCommandTimer() 12 13 def flushDeferredCommands(self): 14 global deferredCommands 15 16 for i in range(len(deferredCommands)): 17 current = deferredCommands[0] 18 del deferredCommands[0] 19 20 if current == None: 21 return 22 else: 23 current.execute() 24 25 def maybeSetDeferredCommandTimer(self): 26 global timerIsActive, deferredCommands 27 28 if (not timerIsActive) and (not len(deferredCommands)==0): 29 Timer(1, self) 30 timerIsActive = True 31 32 def onTimer(self): 33 global timerIsActive 34 35 self.flushDeferredCommands() 36 timerIsActive = False 37 self.maybeSetDeferredCommandTimer() 38 39