o
    _ci                     @   s~   d Z ddlZddlmZmZmZmZmZ ddlm	Z	 ddl
mZmZ ddlmZ er0ddlmZ eeZG dd	 d	e	ZdS )
zWorker module.    N)AnyCallableDictListTYPE_CHECKING)EventEmitter)ExecutionContextJSHandle)
debugError)
CDPSessionc                
       s   e Zd ZdZdddedeeee gdf deegdf ddf
 fd	d
Z	de
ddfddZedefddZde
fddZdededefddZdededefddZ  ZS )Workera  The Worker class represents a WebWorker.

    The events `workercreated` and `workerdestroyed` are emitted on the page
    object to signal the worker lifecycle.

    .. code::

        page.on('workercreated', lambda worker: print('Worker created:', worker.url))
    clientr   urlconsoleAPICalledNexceptionThrownreturnc              
      s   t     _|_ j_j _dtdtfdddtdd f fdd}j	d| z	j
d	i  W n tyR } ztt| W Y d }~nd }~ww dtdd ffd
d}j	d| j	dfdd d S )NremoteObjectr   c                 S   s   d S N r   r   r   ;/usr/local/lib/python3.10/dist-packages/pyppeteer/worker.pyjsHandleFactory)   s   z(Worker.__init__.<locals>.jsHandleFactoryeventc                    s6   dt dtf fddt| d    d S )Nr   r   c                    s   t  | S r   )r	   r   )r   executionContextr   r   r   /   s   zKWorker.__init__.<locals>.onExecutionContentCreated.<locals>.jsHandleFactorycontext)r   r	   r   _executionContextCallback)r   )r   r   self)r   r   onExecutionContentCreated,   s
   
z2Worker.__init__.<locals>.onExecutionContentCreatedzRuntime.executionContextCreatedzRuntime.enablec                    s6   g }|  dg D ]	}|| q | d | d S )Nargstype)getappend)r   r   arg)r   r   r   r   onConsoleAPICalled?   s   z+Worker.__init__.<locals>.onConsoleAPICalledzRuntime.consoleAPICalledzRuntime.exceptionThrownc                    s    | d S )NZexceptionDetailsr   )	exception)r   r   r   <lambda>H   s    z!Worker.__init__.<locals>.<lambda>)super__init__Z_client_url_loopcreate_future_executionContextPromiser   r	   onsend	Exceptionr
   logger)r   r   r   r   r   r   er#   	__class__)r   r   r   r   r   r   r'      s,   


zWorker.__init__valuec                 C   s   | j | d S r   )r+   
set_result)r   r3   r   r   r   r   K   s   z Worker._executionContextCallbackc                 C   s   | j S )zReturn URL.)r(   r   r   r   r   r   N   s   z
Worker.urlc                    s   | j I dH S )zReturn ExecutionContext.N)r+   r5   r   r   r   r   S   s   zWorker.executionContextpageFunctionr   c                    "   | j I dH j|g|R  I dH S )zEvaluate ``pageFunction`` with ``args``.

        Shortcut for ``(await worker.executionContext).evaluate(pageFunction, *args)``.
        N)r+   evaluater   r6   r   r   r   r   r8   W      zWorker.evaluatec                    r7   )zEvaluate ``pageFunction`` with ``args`` and return :class:`~pyppeteer.execution_context.JSHandle`.

        Shortcut for ``(await worker.executionContext).evaluateHandle(pageFunction, *args)``.
        N)r+   evaluateHandler9   r   r   r   r;   _   r:   zWorker.evaluateHandle)__name__
__module____qualname____doc__strr   r   r	   r   r'   r   r   propertyr   r   r   r8   r;   __classcell__r   r   r1   r   r      s    

,r   )r?   loggingtypingr   r   r   r   r   Zpyeer   Zpyppeteer.execution_contextr   r	   Zpyppeteer.helperr
   Zpyppeteer.connectionr   	getLoggerr<   r/   r   r   r   r   r   <module>   s   
