a
    ÁQ\a  ã                   @   s€   d Z ddlmZmZ ddlmZmZmZmZm	Z	 ddl
mZ dgZedƒZG dd	„ d	ƒZeg ee f eed
f dœdd„Zd
S )z&
Implementation for async generators.
é    )ÚQueueÚget_event_loop)ÚAsyncGeneratorÚCallableÚIterableÚTypeVarÚUnioné   )Úrun_in_executor_with_contextÚgenerator_to_async_generatorÚ_Tc                   @   s   e Zd ZdS )Ú_DoneN)Ú__name__Ú
__module__Ú__qualname__© r   r   úGlib/python3.9/site-packages/prompt_toolkit/eventloop/async_generator.pyr      s   r   N)Úget_iterableÚreturnc                   s„   d‰t ƒ ‰ tƒ ‰tƒ ‰ddœ‡ ‡‡‡‡fdd„}t|ƒ}z6ˆ ¡ I dH }t|t ƒrVq^q:|V  q:W d‰|I dH  nd‰|I dH  0 dS )zÝ
    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.
    FN)r   c               
      sJ   z4ˆƒ D ]} ˆr q$ˆ  ˆj| ¡ qW ˆ  ˆjˆ ¡ nˆ  ˆjˆ ¡ 0 dS )z~
        Consume the generator in background thread.
        When items are received, they'll be pushed to the queue.
        N)Zcall_soon_threadsafeZ
put_nowait)Úitem©Z_doner   ZloopÚqZquittingr   r   Úrunner%   s    
z,generator_to_async_generator.<locals>.runnerT)r   r   r   r
   ÚgetÚ
isinstance)r   r   Zrunner_fr   r   r   r   r      s    

ù)Ú__doc__Zasyncior   r   Útypingr   r   r   r   r   Zutilsr
   Ú__all__r   r   r   r   r   r   r   Ú<module>   s   ÿ
þ