
    H>qj$                        d dl Z d dlmZmZ d dlmZmZ ddlmZ ddl	m
Z
 ddlmZ  G d	 d
e      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Zy)    N)defaultdictdeque)DequeOptional   )Op   )EventNO_VALUEc                   N    e Zd ZdZd ZddZddZddZddZddZ	ddZ
dd	Zy
)Fork c                 .    t         j                  |        y N)list__init__selfs    S/opt/rentech/trading_bot/.venv/lib/python3.12/site-packages/eventkit/ops/combine.pyr   zFork.__init__   s    d    c                 D     |j                   |   | j                          |S r   )_set_sourcesclear)r   joiners     r   joinz	Fork.join   s     T"

r   c                 4    | j                  t                     S r   )r   Concatr   s    r   concatzFork.concat       yy""r   c                 4    | j                  t                     S r   )r   Merger   s    r   mergez
Fork.merge       yy!!r   c                 4    | j                  t                     S r   )r   Switchr   s    r   switchzFork.switch   r    r   c                 4    | j                  t                     S r   )r   Zipr   s    r   zipzFork.zip   s    yyr   c                 4    | j                  t                     S r   )r   	Ziplatestr   s    r   	ziplatestzFork.ziplatest!   s    yy%%r   c                 4    | j                  t                     S r   )r   Chainr   s    r   chainz
Fork.chain$   r$   r   N)r   JoinOp)returnr   )r2   r"   )r2   r&   )r2   r)   )r2   r,   )r2   r/   )__name__
__module____qualname__	__slots__r   r   r   r#   r'   r*   r-   r0   r   r   r   r   r   
   s/    I
#"# &"r   r   c                   ,    e Zd ZU dZdZee   ed<   d Zy)r1   z_
    Base class for join operators that combine the emits
    from multiple source events.
    )_sourcesr8   c                     t         r   NotImplementedErrorr   sourcess     r   r   zJoinOp._set_sources2   s    !!r   N)	r3   r4   r5   __doc__r6   r   r
   __annotations__r   r   r   r   r1   r1   (   s    
 IEl"r   r1   c                   V    e Zd ZU dZdZee   ed<   defdZd Z	d Z
defd	Zd
 Zd Zy)AddableJoinOpz
    Base class for join operators where new sources, produced by a
    parent higher-order event, can be added dynamically.
    )_parentrB   r=   c                 x    t         j                  |        t               | _        d | _         | j
                  |  y r   )r1   r   r   r8   rB   r   r<   s     r   r   zAddableJoinOp.__init__@   s.    7#r   c                 ^    |D ](  }t        j                  |      }| j                  |       * y r   )r
   create
add_source)r   r=   sources      r   r   zAddableJoinOp._set_sourcesF   s+     	$F\\&)FOOF#	$r   c                     t         r   r:   r   rG   s     r   rF   zAddableJoinOp.add_sourceK   s    !!r   parentc                 j    || _         |j                  r |xj                  | j                  z  c_        y y r   )rB   
done_event_on_parent_doner   rJ   s     r   
set_parentzAddableJoinOp.set_parentO   s.    !5!55 r   c                     | j                  |       | j                  j                  |       | j                  s| j                  | j	                          y y y r   )_disconnect_fromr8   removerB   set_donerI   s     r   on_source_donezAddableJoinOp.on_source_doneT   sB    f%V$}}!5MMO "6}r   c                 j    || j                   z  }d | _        | j                  s| j                          y y r   )rM   rB   r8   rS   rN   s     r   rM   zAddableJoinOp._on_parent_doneZ   s.    $&&&}}MMO r   N)r3   r4   r5   r>   r6   r   r
   r?   r   r   rF   rO   rT   rM   r   r   r   rA   rA   6   sD    
 Ie_$ $$
"6 6
r   rA   c                       e Zd ZdZd Zy)r"   r   c                 \    | j                   j                  |       | j                  |       y r   )r8   append_connect_fromrI   s     r   rF   zMerge.add_sourced   s"    V$6"r   N)r3   r4   r5   r6   rF   r   r   r   r"   r"   a   s    I#r   r"   c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)r&   )
_source2cb_active_sourcec                 h    t         j                  |        i | _        d | _         | j                  |  y r   )rA   r   r[   r\   r   r<   s     r   r   zSwitch.__init__l   s/    t$"7#r   c                    | j                   j                  |       | j                  j                  |      }|sMt	        j
                  | j                  |      }|| j                  |<   |j                  || j                         y y Ndone)	r8   rX   r[   get	functoolspartialon_source_sconnectrT   r   rG   cbs      r   rF   zSwitch.add_sourcer   sl    V$__  (""4#3#3V<B&(DOOF#NN2D$7$7N8 r   c                     || j                   v r@| j                   j                  |       | j                  j                  |d       }|r||z  }y y y r   )r8   rR   r[   poprg   s      r   _remove_sourcezSwitch._remove_sourcez   sL    T]]"MM  ($$VT2B"  #r   c                     || j                   ur"| j                  | j                          || _          | j                  |  y r   )r\   rk   emit)r   rG   argss      r   re   zSwitch.on_source_s   s:    ,,, 3 34"(D		4r   c                     | j                  |       | j                  s%| j                  d | _        | j	                          y y y r   )rk   r8   rB   r\   rS   rI   s     r   rT   zSwitch.on_source_done   s8    F#}}!5"&DMMO "6}r   N)	r3   r4   r5   r6   r   rF   rk   re   rT   r   r   r   r&   r&   i   s     0I$9r   r&   c                   (    e Zd ZdZd Zd Zd Zd Zy)r   )r[   c                 Z    t         j                  |        i | _         | j                  |  y r   )rA   r   r[   r   r<   s     r   r   zConcat.__init__   s'    t$7#r   c                 ,   || j                   v ry | j                   j                  |       | j                  j                  |      }|sMt	        j
                  | j                  |      }|| j                  |<   |j                  || j                         y y r_   )	r8   rX   r[   rb   rc   rd   _on_source_srf   rT   rg   s      r   rF   zConcat.add_source   s{    T]]"V$__  (""4#4#4f=B&(DOOF#NN2D$7$7N8 r   c                 B   | j                   r| j                   d   |urs| j                   j                         }| j                  j                  |d       }|r|j	                  || j
                         | j                   r| j                   d   |urs | j                  |  y )Nr   r`   )r8   popleftr[   rj   
disconnectrT   rm   )r   rG   rn   srh   s        r   rs   zConcat._on_source_s   s    mma 0 >%%'A$$Q-BRd&9&9:	 mma 0 >
 			4r   c                 8   | j                   j                  |      }|j                  || j                         || j                  v r*| j                  j                  |       || j                  v r*| j                  s| j                  | j                          y y y r_   )r[   rj   rv   rT   r8   rR   rB   rS   rg   s      r   rT   zConcat.on_source_done   s{    __  ("4#6#67%MM  ( %}}!5MMO "6}r   N)r3   r4   r5   r6   r   rF   rs   rT   r   r   r   r   r      s    I$
9r   r   c                   "    e Zd ZdZd Zd Zd Zy)r/   )_qq_source2cbsc                     t         j                  |        t               | _        t	        t
              | _         | j                  |  y r   )rA   r   r   rz   r   r   r{   r   r<   s     r   r   zChain.__init__   s6    t$7&t,7#r   c                    | j                   s| j                  |       nMfd}t               | j                  j	                         ||z  }| j
                  |   j	                  |       | j                   j	                  |       y )Nc                  (    j                  |        y r   )rX   )rn   qs    r   rh   zChain.add_source.<locals>.cb   s    r   )r8   rY   r   rz   rX   r{   )r   rG   rh   r   s      @r   rF   zChain.add_source   sh    }}v&AHHOOAbLFV$++B/V$r   c                 .   || j                   d   ury | j                  |       | j                   j                          | j                   r| j                   d   }| j                  j                         }|D ]  } | j                  |   | j
                  j                  |g       D ]  }||z  }	 |j                         r| j                   j                          | j                  |       y | j                   s| j                  | j                          y y y Nr   )r8   rQ   ru   rz   rm   r{   rj   ra   rY   rB   rS   )r   rG   r   rn   rh   s        r   rT   zChain.on_source_done   s    q))f%mm]]1%F  "A !		4 !&&**626 "{{}%%'v&}}!5MMO "6}r   N)r3   r4   r5   r6   r   rF   rT   r   r   r   r/   r/      s    &I$
%r   r/   c                   (    e Zd ZdZd Zd Zd Zd Zy)r)   )_resultsr{   
_num_readyc                     t         j                  |        d| _        t        t              | _        |r | j                  |  y y r   )r1   r   r   r   r   r{   r   r<   s     r   r   zZip.__init__   s:    &t,Dw' r   c                    t        d |D              | _        t        d | j                  D              r| j                          y | j                  D cg c]  }t                c}| _        t        | j                        D ]j  \  }}t        j                  | j                  |      }|j                  || j                  | j                         | j                  |   j                  |       l y c c}w )Nc              3   F   K   | ]  }t        j                  |        y wr   )r
   rE   .0rw   s     r   	<genexpr>z#Zip._set_sources.<locals>.<genexpr>   s     ?!ell1o?s   !c              3   <   K   | ]  }|j                           y wr   r`   r   s     r   r   z#Zip._set_sources.<locals>.<genexpr>   s     /Aqvvx/   )r   r8   anyrS   r   	enumeraterc   rd   _on_source_irf   on_source_errorrT   r{   rX   )r   r=   _irG   rh   s         r   r   zZip._set_sources   s    ?w??///MMO*.--8Q8"4==1 	0IAv""4#4#4a8BNN2t33T5H5HIV$++B/	0 9s   C/c                    | j                   |   }|s8| xj                  dz  c_        | j                  t        | j                         k(  }nd}|j                  t        |      dk(  r|d   n	|r|nt               |rMt        d | j                   D              }t        d | j                   D              | _         | j                  |  y y )Nr   Fr   c              3   <   K   | ]  }|j                           y wr   )ru   r   r   s     r   r   z#Zip._on_source_i.<locals>.<genexpr>   s     ;		;r   c              3   2   K   | ]  }t        |        y wr   )boolr   s     r   r   z#Zip._on_source_i.<locals>.<genexpr>   s     !Aa$q'!As   )r   r   lenrX   r   tuplesumrm   )r   r   rn   r   readytups         r   r   zZip._on_source_i   s    MM!OOq OOOs4=='99EE	CINa(K;T]];;C!!A4==!AADODIIsO r   c                 "   | j                   j                  |       | j                   sh| j                  j                         D ]3  \  }}|D ])  }|j	                  || j
                  | j                         + 5 d | _        | j                          y y r   r8   rR   r{   itemsrv   r   rT   rS   r   rG   cbsrh   s       r   rT   zZip.on_source_done       V$}}#//557 G GB%%D00$2E2EGGG  $DMMO r   Nr3   r4   r5   r6   r   r   r   rT   r   r   r   r)   r)      s    9I(	0r   r)   c                   .    e Zd ZdZdddZd Zd Zd Zy)	r,   )_values
_is_primedr{   T)rd   c                    t         j                  |        || _        t        t              | _        |r | j                  |  y y r   )r1   r   r   r   r   r{   r   )r   rd   r=   s      r   r   zZiplatest.__init__  s:    !&t,Dw' r   c                    |D cg c]  }t        j                  |       }}t        d |D              | _        | j                  s| j	                          y |D cg c]  }|j                          c}| _        t        | j                        D ]j  \  }}t        j                  | j                  |      }|j                  || j                  | j                         | j                  |   j                  |       l y c c}w c c}w )Nc              3   B   K   | ]  }|j                         r|  y wr   r`   r   s     r   r   z)Ziplatest._set_sources.<locals>.<genexpr>  s     AAaAs   )r
   rE   r   r8   rS   valuer   r   rc   rd   r   rf   r   rT   r{   rX   )r   r=   rw   r   rG   rh   s         r   r   zZiplatest._set_sources  s    ,34q5<<?44AAA}}MMO+23a	3"4==1 	0IAv""4#4#4a8BNN2t33T5H5HIV$++B/	0 5
 4s   C=Dc                     t        |      dk(  r|d   n	|r|nt        | j                  |<   | j                  s"t	        d | j                  D               | _        | j                  r | j
                  | j                    y y )Nr   r   c              3   ,   K   | ]  }|t         u   y wr   r   )r   rs     r   r   z)Ziplatest._on_source_i.<locals>.<genexpr>"  s     %Ja8m%Js   )r   r   r   r   r   rm   )r   r   rn   s      r   r   zZiplatest._on_source_i  sd    4yA~DG44X 	Q"%%JT\\%J"JJDO??DIIt||$ r   c                 "   | j                   j                  |       | j                   sh| j                  j                         D ]3  \  }}|D ])  }|j	                  || j
                  | j                         + 5 d | _        | j                          y y r   r   r   s       r   rT   zZiplatest.on_source_done&  r   r   Nr   r   r   r   r,   r,     s    8I)- (
0%r   r,   )rc   collectionsr   r   typingr   r   opr   eventr
   utilr   r   r   r1   rA   r"   r&   r   r/   r)   r,   r   r   r   <module>r      s     * "   "4 "<"R "(F (V#M #"] "J ]  F'M 'T*& *Z& &r   