Posted to tcl by rmax at Tue Mar 28 22:35:28 GMT 2017view raw

  1. coroutine ratelimit apply {{} {
  2. set limits ""
  3. while 1 {
  4. set args [yieldto return -level 0]
  5. while 1 {
  6. lassign $args key ms body
  7. if {$ms eq ""} {
  8. dict unset limits $key
  9. break
  10. } else {
  11. set squelch [dict exists $limits $key]
  12. if {$squelch} {
  13. after cancel [dict get $limits $key]
  14. }
  15. dict set limits $key [after $ms [list [info coroutine] $key]]
  16. if {$squelch} break
  17. set args [yieldto eval $body]
  18. }
  19. }
  20. }
  21. }}
  22.