Xj ddlmZddlmZddlmZmZmZmZm Z m Z ddl m Z eGddZ GddZeZd ed eegeffd Zd ed ed efdZe de Zded efdZy)) dataclass)wraps)AnyCallableIteratorListTypeTypeVar) EventEmitterc"eZdZUeed<eed<y)HandlereventmethodN)__name__ __module__ __qualname__str__annotations__r\/mnt/ssd/data/Dropbox/adrian/vault-secondbrain/venv/lib/python3.12/site-packages/pyee/cls.pyr r s J rr c4eZdZddZddZdeefdZdZy)HandlersreturnNcg|_yN _handlersselfs r__init__zHandlers.__init__s (*rc:|jj|yr)rappend)r handlers rr#zHandlers.appends g&rc,t|jSr)iterrrs r__iter__zHandlers.__iter__sDNN##rcg|_yrrrs rresetzHandlers.resets r)rN) rrrr!r#rr r'r)rrrrrs"+'$(7+$rrrrc,dtdtffd }|S)zr Register an event handler on an evented class. See the `evented` class decorator for a full example. rrcHtjt||S)N)rr)rr#r )rrs r decoratorzon..decorator%suV<= r)r)rr,s` ronr-s (x rr rc@tdtffd }|S)Nrcg|i|Srr)argskwargsrr s rboundz_bind..bound-sd,T,V,,r)rr)r rr2s`` r_bindr3,s' 6]-#-- LrCls)r2clsc tttj|jt |jdt dt dt ddffd }||_|S)aQ Configure an evented class. Evented classes are classes which use an EventEmitter to call instance methods during runtime. To achieve this without this helper, you would instantiate an `EventEmitter` in the `__init__` method and then call `event_emitter.on` for every method on `self`. This decorator and the `on` function help make things look a little nicer by defining the event handler on the method in the class and then adding the `__init__` hook in a wrapper: ```py from pyee.cls import evented, on @evented class Evented: @on("event") def event_handler(self, *args, **kwargs): print(self, args, kwargs) evented_obj = Evented() evented_obj.event_emitter.emit( "event", "hello world", numbers=[1, 2, 3] ) ``` The `__init__` wrapper will create a `self.event_emitter: EventEmitter` automatically but you can also define your own event_emitter inside your class's unwrapped `__init__` method. For example, to use this decorator with a `TwistedEventEmitter`:: ```py @evented class Evented: def __init__(self): self.event_emitter = TwistedEventEmitter() @on("event") async def event_handler(self, *args, **kwargs): await self.some_async_action(*args, **kwargs) ``` r r0r1rNc|g|i|t|dst|_D]<}|jj|jt ||j >y)N event_emitter)hasattrr r8r-rr3r)r r0r1hhandlersog_inits rinitzevented..initis_&t&v&t_-!-D  BA    ! !!''5qxx+@ A Br)listrr)r!rr)r5r=r;r<s @@reventedr?7smZ#9oH OO G 3<<B3BsBcBdBBCL JrN) dataclassesr functoolsrtypingrrrrr r pyeer r rrrr-r3r4r?rrrrDs!??     J  c hz834 SSe4 ===r