PageRenderTime 34ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/library/DeferredCommand.py

http://pyjamas.googlecode.com/
Python | 39 lines | 27 code | 12 blank | 0 comment | 5 complexity | 2acd2ff87acdf26c94d971496fb88d67 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. from Timer import Timer
  2. deferredCommands = []
  3. timerIsActive = False
  4. class DeferredCommand:
  5. def add(self, cmd):
  6. global deferredCommands
  7. deferredCommands.append(cmd)
  8. self.maybeSetDeferredCommandTimer()
  9. def flushDeferredCommands(self):
  10. global deferredCommands
  11. for i in range(len(deferredCommands)):
  12. current = deferredCommands[0]
  13. del deferredCommands[0]
  14. if current == None:
  15. return
  16. else:
  17. current.execute()
  18. def maybeSetDeferredCommandTimer(self):
  19. global timerIsActive, deferredCommands
  20. if (not timerIsActive) and (not len(deferredCommands)==0):
  21. Timer(1, self)
  22. timerIsActive = True
  23. def onTimer(self):
  24. global timerIsActive
  25. self.flushDeferredCommands()
  26. timerIsActive = False
  27. self.maybeSetDeferredCommandTimer()