
    kj                     L    d dl Z d dlZd dlmZmZ d dlmZ  G d d          ZdS )    N)AnyCallable)	BaseTimerc                      e Zd ZdefdZdd e            ddeegef         dede	d	e
ed
f         deeef         de	fdZdd e            ddeegef         dede	d	e
ed
f         deeef         de	fdZddd e            ddeegef         dede	ded	e
ed
f         deeef         de	fdZdeegef         d	e
ed
f         deeef         de
eef         de	deg df         fdZddZdde	deddfdZde	ddfdZdS ) 	Schedulertimerc                     t          t                    s"t          d                                        t	                      | _        d| _        t          j        fdt          j
                  | _        dS )zCreate a Scheduler object to handle events.

        Args:
            timer (BaseTimer): timer to use, subclass of BaseTimer.

        Raises:
            ValueError: timer is not a valid argument.
        z;timer, {}, has to be a instance of BaseTimer or a callable!r   c                       j         S N)time)r   s   Z/home/agentuser/manim-venv/lib/python3.11/site-packages/moderngl_window/utils/scheduler.py<lambda>z$Scheduler.__init__.<locals>.<lambda>   s	    %*     N)
isinstancer   
ValueErrorformatdict_events	_event_idsched	schedulerr   sleep
_scheduler)selfr   s    `r   __init__zScheduler.__init__	   sr     %++ 	MTTUZ[[   04vv/*<*<*<*<djIIr       )priority	argumentskwargsactiondelayr   r   .r    returnc                    | j                             |||||          }|| j        | j        <   | xj        dz  c_        | j        dz
  S )aR  Schedule a function for execution after a delay.

        Args:
            action (callable):
                function to be called.
            delay (float):
                delay in seconds.
            priority (int, optional):
                priority for this event, lower is more important. Defaults to 1.
            arguments (tuple, optional):
                arguments for the action. Defaults to ().
            kwargs (dict, optional):
                keyword arguments for the action. Defaults to dict().

        Returns:
            int: event id that can be canceled.
        r   )r   enterr   r   )r   r!   r"   r   r   r    events          r   run_oncezScheduler.run_once   sL    4 %%eXvy&QQ',T^$!~!!r   r   c                    | j                             |||||          }|| j        | j        <   | xj        dz  c_        | j        dz
  S )aw  Schedule a function to be executed at a certain time.

        Args:
            action (callable):
                function to be called.
            time (float):
                epoch time at which the function should be called.
            priority (int, optional):
                priority for this event, lower is more important. Defaults to 1.
            arguments (tuple, optional):
                arguments for the action. Defaults to ().
            kwargs (dict, optional):
                keyword arguments for the action. Defaults to dict().

        Returns:
            int: event id that can be canceled.
        r   )r   enterabsr   r   )r   r!   r   r   r   r    r&   s          r   run_atzScheduler.run_at;   sL    4 ((xFSS',T^$!~!!r   g        )r   initial_delayr   r    r+   c                    |                      |||||f| j                  }| j                            |||          }|| j        | j        <   | xj        dz  c_        | j        dz
  S )a  Schedule a recurring function to be called every `delay` seconds after a initial delay.

        Args:
            action (callable):
                function to be called.
            delay (float):
                delay in seconds.
            priority (int, optional):
                priority for this event, lower is more important. Defaults to 1.
            initial_delay (float, optional):
                initial delay in seconds before executing for the first time.
            Defaults to 0. arguments (tuple, optional):
                arguments for the action. Defaults to ().
            kwargs (dict, optional):
                keyword arguments for the action. Defaults to dict().

        Returns:
            int: event id that can be canceled.
        r   )_recurring_event_factoryr   r   r%   r   )	r   r!   r"   r   r+   r   r    recurring_eventr&   s	            r   	run_everyzScheduler.run_everyZ   sq    : 77Ivx'8$.
 
 %%mXOO',T^$!~!!r   functionscheduling_infoidNc                 ,     d fdS )a  Factory for creating recurring events that will reschedule themselves.

        Args:
            function (callable): function to be called.
            arguments (tuple): arguments for the function.
            kwargs (dict): keyword arguments for the function.
            scheduling_info (tuple): tuple of information for scheduling the task.
            id (int): event id this event should be assigned to.
        r#   Nc                  T     i   j         j        g R  } | j        <   d S r   )r   r%   r   )r&   _fr   r0   r2   r    r1   r   s    r   r5   z.Scheduler._recurring_event_factory.<locals>._f   sI    Hi*6***)DO)???B???E$DLr   r#   Nr   )r   r0   r   r    r1   r2   r5   s   ``````@r   r-   z"Scheduler._recurring_event_factory   sL    $	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%
 	r   c                 <    | j                             d           dS )zBRun the scheduler without blocking and execute any expired events.F)blockingN)r   run)r   s    r   executezScheduler.execute   s!    U+++++r   r   event_idc                 ~    |dk    r|                      |           dS |                     | j         |d|f           dS )zCancel a previously scheduled event.

        Args:
            event_id (int): event to be canceled
            delay (float, optional): delay before canceling the event. Defaults to 0.
        r   )r   r   N)_cancelr'   )r   r;   r"   s      r   cancelzScheduler.cancel   sI     A::LL"""""MM$,h[MQQQQQr   c                     || j         vr"t          d                    |                    | j                             |          }| j                            |           d S )Nz)Recurring event with id {} does not exist)r   r   r   popr   r>   )r   r;   r&   s      r   r=   zScheduler._cancel   s^    4<''HOOPXYYZZZ  **u%%%%%r   r6   )r   )__name__
__module____qualname__r   r   r   r   r   floatinttupler'   r*   r/   r-   r:   r>   r=   r   r   r   r   r      s       Ji J J J J0 %'!%" " "#$" "
 " c?" S#X" 
" " " "H %'!%" " "#$" "
 " c?" S#X" 
" " " "H "%'!%#" #" #"#$#" #"
 #" #" c?#" S#X#" 
#" #" #" #"JC5#:& c? S#X	
 sCx  
"d(	   2, , , ,
R 
Rs 
R5 
R 
R 
R 
R 
R& & & & & & & &r   r   )r   r   typingr   r   moderngl_window.timers.baser   r   r   r   r   <module>rI      s}                      1 1 1 1 1 1d& d& d& d& d& d& d& d& d& d&r   