o
    Nie*                     @   s   d dl mZ zd dlmZ W n ey   d dl mZ Y nw d dlmZ d dlmZ ddl	m
Z
mZmZ 	 dd	d
ZeddZG dd deZe add Zdd Zdd Zdd ZdS )    )
namedtuple)Iterable)partial)local   )Promiseasync_instanceget_default_schedulerF)	AnyListSizedCallableOptionalTupleUnionIteratorHashablec                    s*   t d   fddtdt D S )Nr   c                 3   s     | ]}||   V  qd S N ).0i
chunk_sizeiterable_objr   F/home/ubuntu/.local/lib/python3.10/site-packages/promise/dataloader.py	<genexpr>   s
    
zget_chunks.<locals>.<genexpr>r   )maxrangelen)r   r   r   r   r   
get_chunks   s   
r   Loaderzkey,resolve,rejectc                   @   sb   e Zd ZdZdZdZ							dddZdddZdd Zd	d
 Z	dd Z
dd Zdd ZdS )
DataLoaderTNc                 C   sx   |d ur|| _ t| j std||d ur|| _|d ur!|| _|d ur(|| _|p-dd | _|p2i | _g | _	|| _
d S )NznDataLoader must be have a batch_load_fn which accepts List<key> and returns Promise<List<value>>, but got: {}.c                 S   s   | S r   r   )xr   r   r   <lambda>K   s    z%DataLoader.__init__.<locals>.<lambda>)batch_load_fncallable	TypeErrorformatbatchmax_batch_sizecacheget_cache_key_promise_cache_queue
_scheduler)selfr$   r(   r)   r*   r+   	cache_map	schedulerr   r   r   __init__+   s"   


zDataLoader.__init__c                 C   s^   |du rt d|| |}| jr| j|}|r|S tt| j|}| jr-|| j|< |S )z[
        Loads a key, returning a `Promise` for the value represented by that key.
        NzCThe loader.load() function must be called with a value,but got: {}.)	r&   r'   r+   r*   r,   getr   r   do_resolve_reject)r/   key	cache_keycached_promisepromiser   r   r   loadP   s   

zDataLoader.loadc                 C   sP   | j t|||d t| j dkr&| jr ttt| | j d S t|  d S d S )N)r5   resolverejectr   )	r-   appendr    r   r(   enqueue_post_promise_jobr   dispatch_queuer.   )r/   r5   r:   r;   r   r   r   r4   o   s   zDataLoader.do_resolve_rejectc                    s0   t |tstd|t fdd|D S )a.  
        Loads multiple keys, promising an array of values

        >>> a, b = await my_loader.load_many([ 'a', 'b' ])

        This is equivalent to the more verbose:

        >>> a, b = await Promise.all([
        >>>    my_loader.load('a'),
        >>>    my_loader.load('b')
        >>> ])
        zJThe loader.loadMany() function must be called with Array<key> but got: {}.c                    s   g | ]}  |qS r   )r9   )r   r5   r/   r   r   
<listcomp>   s    z(DataLoader.load_many.<locals>.<listcomp>)
isinstancer   r&   r'   r   all)r/   keysr   r?   r   	load_many~   s   
zDataLoader.load_manyc                 C   s   |  |}| j|d | S )zu
        Clears the value at `key` from the cache, if it exists. Returns itself for
        method chaining.
        N)r+   r,   pop)r/   r5   r6   r   r   r   clear   s   
zDataLoader.clearc                 C   s   | j   | S )z
        Clears the entire cache. To be used when some event results in unknown
        invalidations across this particular `DataLoader`. Returns itself for
        method chaining.
        )r,   rF   r?   r   r   r   	clear_all   s   
zDataLoader.clear_allc                 C   sB   |  |}|| jvrt|trt|}nt|}|| j|< | S )z
        Adds the provied key and value to the cache. If the key already exists, no
        change is made. Returns itself for method chaining.
        )r+   r,   rA   	Exceptionr   r;   r:   )r/   r5   valuer6   r8   r   r   r   prime   s   




zDataLoader.prime)NNNNNNNr   )__name__
__module____qualname__r(   r)   r*   r2   r9   r4   rD   rF   rG   rJ   r   r   r   r   r!   %   s$    

%

r!   c                    s>   t tdstd t_st  fdd}tj| d S )Nresolved_promisec                    s   t   d S r   )r   invoke)vfnr1   r   r   on_promise_resolve   s   z4enqueue_post_promise_job.<locals>.on_promise_resolve)hasattrr*   r   r:   rN   r	   then)rR   r1   rS   r   rQ   r   r=      s   
r=   c                 C   sR   | j }g | _ | j}|r"|t|k r"t||}|D ]}t| | qdS t| | dS )zh
    Given the current state of a Loader instance, perform a batch load
    from its current queue.
    N)r-   r)   r   r   dispatch_queue_batch)loaderqueuer)   chunkschunkr   r   r   r>      s   
r>   c              
      s   dd D  z|   }W n ty' } zt| | W Y d }~d S d }~ww |r/t|ts<t| td| d S  fdd}||t	t|  d S )Nc                 S   s   g | ]}|j qS r   )r5   )r   lr   r   r   r@     s    z(dispatch_queue_batch.<locals>.<listcomp>zDataLoader must be constructed with a function which accepts Array<key> and returns Promise<Array<value>>, but the function did not return a Promise: {}.c                    sp   t | tstd| t| t krtd | t| D ]\}}t |tr0|| q!|| q!d S )NzDataLoader must be constructed with a function which accepts Array<key> and returns Promise<Array<value>>, but the function did not return a Promise of an Array: {}.zDataLoader must be constructed with a function which accepts Array<key> and returns Promise<Array<value>>, but the function did not return a Promise of an Array of the same length as the Array of keys.

Keys:
{}

Values:
{})	rA   r   r&   r'   r   ziprH   r;   r:   )valuesr[   rI   rC   rX   r   r   batch_promise_resolved  s   

z4dispatch_queue_batch.<locals>.batch_promise_resolved)
r$   rH   failed_dispatchrA   r   r&   r'   rU   catchr   )rW   rX   batch_promiseer_   r   r^   r   rV      s,   
 
rV   c                 C   s$   |D ]}|  |j || qdS )z
    Do not cache individual loads if the entire batch dispatch fails,
    but still reject each request so they do not hang.
    N)rF   r5   r;   )rW   rX   errorr[   r   r   r   r`   >  s   r`   N)r   )collectionsr   collections.abcr   ImportError	functoolsr   	threadingr   r8   r   r   r	   typingr
   r   r   r   r   r   r   r   r   r   r    r!   r*   r=   r>   rV   r`   r   r   r   r   <module>   s&    

	 3@