Turn a function or coroutine into an event handler Parameters: function: The function or coroutine which handles the event. stop_propagation: Block the event from propagating further up the DOM. prevent_default: Stops the default actio
| 76 | |
| 77 | |
| 78 | class EventHandler: |
| 79 | """Turn a function or coroutine into an event handler |
| 80 | |
| 81 | Parameters: |
| 82 | function: |
| 83 | The function or coroutine which handles the event. |
| 84 | stop_propagation: |
| 85 | Block the event from propagating further up the DOM. |
| 86 | prevent_default: |
| 87 | Stops the default action associate with the event from taking place. |
| 88 | target: |
| 89 | A unique identifier for this event handler (auto-generated by default) |
| 90 | """ |
| 91 | |
| 92 | __slots__ = ( |
| 93 | "__weakref__", |
| 94 | "function", |
| 95 | "prevent_default", |
| 96 | "stop_propagation", |
| 97 | "target", |
| 98 | ) |
| 99 | |
| 100 | def __init__( |
| 101 | self, |
| 102 | function: EventHandlerFunc, |
| 103 | stop_propagation: bool = False, |
| 104 | prevent_default: bool = False, |
| 105 | target: str | None = None, |
| 106 | ) -> None: |
| 107 | self.function = to_event_handler_function(function, positional_args=False) |
| 108 | self.prevent_default = prevent_default |
| 109 | self.stop_propagation = stop_propagation |
| 110 | self.target = target |
| 111 | |
| 112 | def __eq__(self, other: object) -> bool: |
| 113 | undefined = object() |
| 114 | for attr in ( |
| 115 | "function", |
| 116 | "prevent_default", |
| 117 | "stop_propagation", |
| 118 | "target", |
| 119 | ): |
| 120 | if not attr.startswith("_"): |
| 121 | if not getattr(other, attr, undefined) == getattr(self, attr): |
| 122 | return False |
| 123 | return True |
| 124 | |
| 125 | def __repr__(self) -> str: |
| 126 | public_names = [name for name in self.__slots__ if not name.startswith("_")] |
| 127 | items = ", ".join([f"{n}={getattr(self, n)!r}" for n in public_names]) |
| 128 | return f"{type(self).__name__}({items})" |
| 129 | |
| 130 | |
| 131 | def to_event_handler_function( |
no outgoing calls