Wow. This is great.
What a powerful, simple addition to Leo's scriptability.

Would you expect any performance issues?

On Mon, Aug 25, 2014 at 7:55 AM, Edward K. Ream <[email protected]> wrote:
> Rev 060c33a...Leo build: 20140825070951 contains a spectacular collapse in
> complexity in idle-time handling.
>
> Instantly, registering idle-time event handlers can be considered
> deprecated, though I have no plans to remove the horrendous older code.
>
> The new pattern allows multiple, independent (or cooperating) idle-time
> handlers, created at will as needed.
>
> The new code is based on the IdleTime class in qtGui.py.  Rather than access
> this directly, code should use the g.IdleTime proxy::
>
>     def IdleTime(c,handler,delay=500):
>         '''A proxy for the g.app.gui.IdleTime class.'''
>         if g.app and g.app.gui and hasattr(g.app.gui,'idleTimeClass'):
>             return g.app.gui.idleTimeClass(c,handler,delay)
>         else:
>             return None
>
> g.IdleTime ensures that all is well when using gui's that don't have an
> IdleTime class.
>
> Here is how to use the new code::
>
>     def handler(it):
>         '''The IdleTime handler: called at idle time.'''
>         delta_t = it.time-it.starting_time
>         g.trace(it.count,it.c.shortFileName(),'%2.4f' % (delta_t))
>         if it.count >= 5:
>             g.trace('done')
>             it.stop()
>
>     it = g.IdleTime(c,handler,delay=500)
>     if it: it.start()
>
> The code creates an instance of the IdleTime class that calls the given
> handler at idle time, and no more than once every 500 msec.  Here is the
> output::
>
>     handler 1 ekr.leo 0.5100
>     handler 2 ekr.leo 1.0300
>     handler 3 ekr.leo 1.5400
>     handler 4 ekr.leo 2.0500
>     handler 5 ekr.leo 2.5610
>     handler done
>
> Timer instances are completely independent.  For example:
>
>     def handler1(it):
>         '''The IdleTime handler: called at idle time.'''
>         delta_t = it.time-it.starting_time
>         g.trace('%2s %s %2.4f' % (it.count,it.c.shortFileName(),delta_t))
>         if it.count >= 5:
>             g.trace('done')
>             it.stop()
>
>     def handler2(it):
>         '''The IdleTime handler: called at idle time.'''
>         delta_t = it.time-it.starting_time
>         g.trace('%2s %s %2.4f' % (it.count,it.c.shortFileName(),delta_t))
>         if it.count >= 10:
>             g.trace('done')
>             it.stop()
>
>     it1 = g.IdleTime(c,handler1,delay=500)
>     it2 = g.IdleTime(c,handler2,delay=1000)
>     if it1 and it2:
>         it1.start()
>         it2.start()
>
> Here is the output::
>
>     handler1  1 ekr.leo 0.5200
>     handler2  1 ekr.leo 1.0100
>     handler1  2 ekr.leo 1.0300
>     handler1  3 ekr.leo 1.5400
>     handler2  2 ekr.leo 2.0300
>     handler1  4 ekr.leo 2.0600
>     handler1  5 ekr.leo 2.5600
>     handler1 done
>     handler2  3 ekr.leo 3.0400
>     handler2  4 ekr.leo 4.0600
>     handler2  5 ekr.leo 5.0700
>     handler2  6 ekr.leo 6.0800
>     handler2  7 ekr.leo 7.1000
>     handler2  8 ekr.leo 8.1100
>     handler2  9 ekr.leo 9.1300
>     handler2 10 ekr.leo 10.1400
>     handler2 done
>
> That's it.  I plan to use this new code in attempt to do syntax coloring in
> pieces at idle time.  No guarantee that it can work, but I have hopes...
>
> Edward
>
> P.S.  Here is the IdleTime class, as rendered by the flatten-script command.
> This is beautiful code::
>
>     class IdleTime:
>         '''A class that executes a handler at idle time.'''
>         def __init__(self,c,handler,delay=500):
>             '''ctor for IdleTime class.'''
>             # g.trace('(IdleTime)')
>             self.c = c
>             self.count = 0
>                 # The number of times handler has been called.
>             self.delay = delay
>                 # The argument to self.timer.start:
>                 # 0 for idle time, otherwise a delay in msec.
>             self.enabled = False
>                 # True: run the timer continuously.
>             self.handler = handler
>                 # The user-provided idle-time handler.
>             self.last_delay = 0
>                 # The previous value of self.delay.
>             self.starting_time = None
>                 # Time that the timer started.
>             self.time = None
>                 # Time that the handle is called.
>             # Create the timer, but do not fire it.
>             self.timer = QtCore.QTimer()
>             def IdleTime_callback():
>                 self.at_idle_time()
>             self.timer.timeout.connect(IdleTime_callback)
>
>         def at_idle_time(self):
>             '''Call self.handler not more than once every self.delay
> msec.'''
>             trace = False and not g.unitTesting
>             if self.enabled:
>                 # Idle-time processing is enabled.
>                 if self.last_delay == 0:
>                     # At idle time: call the handler.
>                     if trace: g.trace('at idle time')
>                     self.call_handler()
>                     # Now wait at for at least self.delay msec.
>                     self.last_delay = self.delay
>                     self.timer.stop()
>                     self.timer.start(self.delay)
>                 else:
>                     # We have waited at least self.delay msec.
>                     # Now wait for idle time.
>                     if trace: g.trace('waiting for idle time')
>                     self.last_delay = 0
>                     self.timer.stop()
>                     self.timer.start(0)
>             else:
>                 # Idle-time processing is disabled.  Stop the timer.
>                 if trace: g.trace('Stopping timer.')
>                 self.timer.stop()
>
>         def call_handler(self):
>             '''Carefully call the handler.'''
>             try:
>                 self.count += 1
>                 self.time = time.time()
>                 self.handler(self)
>             except Exception:
>                 g.es_exception()
>                 self.stop()
>
>         def start(self):
>             '''Start idle-time processing'''
>             self.enabled = True
>             if self.starting_time is None:
>                 self.starting_time = time.time()
>             # Wait at least self.delay msec, then wait for idle time.
>             self.last_delay = self.delay
>             self.timer.start(self.delay)
>
>         def stop(self):
>             '''Stop idle-time processing.'''
>             self.enabled = False
>
> EKR
>
> --
> You received this message because you are subscribed to the Google Groups
> "leo-editor" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/leo-editor.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.

Reply via email to