
    YHh                     (   d dl mZ d dlZd dlZd dlZd dlZd dlmZ d dlm	Z	 d dl
mZmZmZmZmZ d dlmZ d dlmZ d dlmZ d d	lmZmZ d d
lmZmZ d dlmZmZ ddZ ed      Z  ed      Z!dZ" G d dee!         Z# G d d      Z$ G d d      Z%y)    )annotationsN)	Awaitable)wraps)AnyCallableGenericTypeVarcast)uuid4)ref)	ParamSpec)REACTPY_TESTING_DEFAULT_TIMEOUTREACTPY_WEB_MODULES_DIR)LifeCycleHookcurrent_hook)EventHandlerto_event_handler_functionc                     t         j                  j                         D ]7  } | j                         rt	        j
                  |       n| j                          9 y)z?Clear the directory where ReactPy stores registered web modulesN)r   currentiterdiris_dirshutilrmtreeunlink)paths    O/var/www/html/planif/env/lib/python3.12/site-packages/reactpy/testing/common.pyclear_reactpy_web_modules_dirr      s?    '//779 @#{{}d$++-@    _P_Rg?c                      e Zd ZdZ	 	 	 	 	 	 	 	 ddZej                  edf	 	 	 	 	 	 	 	 	 d	dZej                  ef	 	 	 	 	 	 	 d
dZ	ej                  ef	 	 	 	 	 	 	 d
dZ
y)pollzGWait until the result of an sync or async function meets some conditionc                    t        j                        sdfd}n$t        t        t        t
        t           f         }|| _        || _        || _	        y )Nc                 8   K   t        t         | i |      S wN)r
   r    )argskwargsfunctions     r   corozpoll.__init__.<locals>.coro-   s     B$ 9& 9::s   )r&   _P.argsr'   	_P.kwargsreturnr    )
inspectiscoroutinefunctionr
   r   r   r   r    _func_args_kwargs)selfr(   r&   r'   r)   s    `   r   __init__zpoll.__init__$   sI     **84; Yr]!23X>D

r   zcondition to be truec                Z  K   t        j                          }	 t        j                  |       d{     | j                  | j                  i | j
                   d{   } ||      ryt        j                          |z
  |kD  r d| d| d|}t        j                  |      7 v7 Jw)zECheck that the coroutines result meets a condition within the timeoutNz	Expected z after z seconds - last value was )timeasynciosleepr/   r0   r1   TimeoutError)r2   	conditiontimeoutdelaydescription
started_atresultmsgs           r   untilz
poll.until6   s      YY[
--&&&%4::tzzBT\\BBF ))+
*g5!+ggY>XY_Xbc**3// &Bs"   .B+B'-B+B)A	B+)B+c                T   K   | j                  fd||d       d{   S 7 w)z5Wait until the result is identical to the given valuec                    | u S r%    leftrights    r   <lambda>zpoll.until_is.<locals>.<lambda>P   s     r   zvalue to be identical to Nr@   r2   rF   r:   r;   s    `  r   until_iszpoll.until_isH   s7      ZZ&'y1	
 
 	
 
   (&(c                T   K   | j                  fd||d       d{   S 7 w)z1Wait until the result is equal to the given valuec                    | k(  S r%   rC   rD   s    r   rG   z#poll.until_equals.<locals>.<lambda>^   s     r   zvalue to equal NrH   rI   s    `  r   until_equalszpoll.until_equalsV   s7      ZZ&eY'	
 
 	
 
rK   N)r(   z Callable[_P, Awaitable[_R] | _R]r&   r*   r'   r+   r,   None)
r9   zCallable[[_R], bool]r:   floatr;   rP   r<   strr,   rO   )rF   r    r:   rP   r;   rP   r,   rO   )__name__
__module____qualname____doc__r3   r   r   _DEFAULT_POLL_DELAYr@   rJ   rN   rC   r   r   r"   r"   !   s    Q2  	
 
* 9@@*10'0 0 	0
 0 
0* 9@@*	

 
 	

 

" 9@@*	

 
 	

 

r   r"   c                  .    e Zd ZU dZded<   dddZd	dZy)
HookCatchera  Utility for capturing a LifeCycleHook from a component

    Example:
        .. code-block::

            hooks = HookCatcher(index_by_kwarg="thing")

            @reactpy.component
            @hooks.capture
            def MyComponent(thing):
                ...

            ...  # render the component

            # grab the last render of where MyComponent(thing='something')
            hooks.index["something"]
            # or grab the hook from the component's last render
            hooks.latest

        After the first render of ``MyComponent`` the ``HookCatcher`` will have
        captured the component's ``LifeCycleHook``.
    r   latestNc                     || _         i | _        y r%   )index_by_kwargindex)r2   r[   s     r   r3   zHookCatcher.__init__   s    ,/1
r   c                J    t        |       t              dfd       }|S )zIDecorator for capturing a ``LifeCycleHook`` on each render of a componentc                             }|t        d      t               }|j                  ||j                  ||j                     <   ||_         | i |S )Nz'Hook catcher has been garbage collected)RuntimeErrorr   r[   r\   rY   )r&   r'   r2   hookrender_functionself_refs       r   wrapperz$HookCatcher.capture.<locals>.wrapper   s^    :D|"#LMM>D"".:>

6$"5"567DK"D3F33r   )r&   r   r'   r   r,   r   )r   r   )r2   ra   rc   rb   s    ` @r   capturezHookCatcher.capture   s,     t9				4 
 		4 r   r%   )r[   z
str | None)ra   Callable[..., Any]r,   re   )rR   rS   rT   rU   __annotations__r3   rd   rC   r   r   rX   rX   e   s    . 2r   rX   c                  4    e Zd ZdZddZ	 	 d	 	 	 	 	 	 	 ddZy)StaticEventHandlera  Utility for capturing the target of one event handler

    Example:
        .. code-block::

            static_handler = StaticEventHandler()

            @reactpy.component
            def MyComponent():
                state, set_state = reactpy.hooks.use_state(0)
                handler = static_handler.use(lambda event: set_state(state + 1))
                return reactpy.html.button({"onClick": handler}, "Click me!")

            # gives the target ID for onClick where from the last render of MyComponent
            static_handlers.target

        If you need to capture event handlers from different instances of a component
        the you should create multiple ``StaticEventHandler`` instances.

        .. code-block::

            static_handlers_by_key = {
                "first": StaticEventHandler(),
                "second": StaticEventHandler(),
            }

            @reactpy.component
            def Parent():
                return reactpy.html.div(Child(key="first"), Child(key="second"))

            @reactpy.component
            def Child(key):
                state, set_state = reactpy.hooks.use_state(0)
                handler = static_handlers_by_key[key].use(lambda event: set_state(state + 1))
                return reactpy.html.button({"onClick": handler}, "Click me!")

            # grab the individual targets for each instance above
            first_target = static_handlers_by_key["first"].target
            second_target = static_handlers_by_key["second"].target
    c                6    t               j                  | _        y r%   )r   hextarget)r2   s    r   r3   zStaticEventHandler.__init__   s    gkkr   c                D    t        t        |      ||| j                        S r%   )r   r   rk   )r2   r(   stop_propagationprevent_defaults       r   usezStaticEventHandler.use   s&     %h/KK	
 	
r   Nr,   rO   )FF)r(   re   rm   boolrn   rq   r,   r   )rR   rS   rT   rU   r3   ro   rC   r   r   rh   rh      s@    'R" "' %	
$
 
 	

 

r   rh   rp   )&
__future__r   r6   r-   r   r5   collections.abcr   	functoolsr   typingr   r   r   r	   r
   uuidr   weakrefr   typing_extensionsr   reactpy.configr   r   reactpy.core._life_cycle_hookr   r   reactpy.core.eventsr   r   r   r   r    rV   r"   rX   rh   rC   r   r   <module>r|      s|    "     %  8 8   ' S E G@ t_T]  A
72; A
H3 3l8
 8
r   