o
    �õ±jx  ã                   @  sÌ   U d Z ddlmZ ddlmZ ddlmZmZmZ ddl	m
Z
 ddlmZmZmZ ddlmZmZ dd	lmZ d
dgZedeedf d�Ze
ddd
„ƒZdZded< edƒZG dd„ dƒZefddd„ZdS ) z&
Implementation for async generators.
é    )Úannotations)Úget_running_loop)ÚAsyncGeneratorÚCallableÚIterable)Úasynccontextmanager)ÚEmptyÚFullÚQueue)ÚAnyÚTypeVaré   )Úrun_in_executor_with_contextÚaclosingÚgenerator_to_async_generatorÚ_T_GeneratorN)ÚboundÚthingÚreturnú"AsyncGenerator[_T_Generator, None]c                 C s.   �z| V  W |   ¡ I dH  dS |   ¡ I dH  w )z1Similar to `contextlib.aclosing`, in Python 3.10.N)Úaclose)r   © r   ú›/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/prompt_toolkit/eventloop/async_generator.pyr      s   €"iè  ÚintÚDEFAULT_BUFFER_SIZEÚ_Tc                   @  s   e Zd ZdS )Ú_DoneN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   2   s    r   Úget_iterableúCallable[[], Iterable[_T]]Úbuffer_sizeúAsyncGenerator[_T, None]c                  s    �d‰t |d�‰tƒ }d	‡ ‡‡fdd„}t|ƒ}z/	 zˆ ¡ }W n ty3   | dˆj¡I dH }Y nw t|tƒr:n|V  qW d‰|I dH  dS d‰|I dH  w )
a]  
    Turn a generator or iterable into an async generator.

    This works by running the generator in a background thread.

    :param get_iterable: Function that returns a generator or iterable when
        called.
    :param buffer_size: Size of the queue between the async consumer and the
        synchronous generator that produces items.
    F)Úmaxsizer   ÚNonec                    s,  zvˆ ƒ D ]V} ˆr% W 	 zˆj tƒ dd� W dS  ty$   ˆr"Y dS Y q
w 	 z	ˆj | dd� W n) tyX   ˆrVY  W 	 zˆj tƒ dd� W dS  tyU   ˆrSY dS Y q;w Y q%w 	 qW 	 zˆj tƒ dd� W dS  tyv   ˆrtY dS Y q\w 	 zˆj tƒ dd� W 	 w  ty•   ˆr“Y    Y dS Y qww )z~
        Consume the generator in background thread.
        When items are received, they'll be pushed to the queue.
        Tr   )ÚtimeoutN)Úputr   r	   )Úitem©r    ÚqÚquittingr   r   ÚrunnerI   sf   
ûýòûýøýòûýýøýz,generator_to_async_generator.<locals>.runnerTN)r   r%   )	r
   r   r   Ú
get_nowaitr   Úrun_in_executorÚgetÚ
isinstancer   )r    r"   Úloopr,   Úrunner_fr(   r   r)   r   r   6   s,   €
"ÿ
øü)r   r   r   r   )r    r!   r"   r   r   r#   )Ú__doc__Ú
__future__r   Úasyncior   Úcollections.abcr   r   r   Ú
contextlibr   Úqueuer   r	   r
   Útypingr   r   Úutilsr   Ú__all__r   r   r   Ú__annotations__r   r   r   r   r   r   r   Ú<module>   s&    þþ