|
|
None | __init__ (Self self) |
| |
|
Mapping[str, Any] | __getstate__ (Self self) |
| |
|
None | __setstate__ (Self self, Mapping[str, Any] state) |
| |
|
Callable[[Handler], Handler] | on (Self self, str event) |
| |
|
Handler | on (Self self, str event, Handler f) |
| |
| Union[Handler, Callable[[Handler], Handler]] | on (Self self, str event, Optional[Handler] f=None) |
| |
| Callable[[Handler], Handler] | listens_to (Self self, str event) |
| |
| Handler | add_listener (Self self, str event, Handler f) |
| |
| Set[str] | event_names (Self self) |
| |
| bool | emit (Self self, str event, *Any args, **Any kwargs) |
| |
| Callable | once (Self self, str event, Optional[Callable] f=None) |
| |
| None | remove_listener (Self self, str event, Callable f) |
| |
| None | remove_all_listeners (Self self, Optional[str] event=None) |
| |
| List[Callable] | listeners (Self self, str event) |
| |
|
|
| _add_event_handler (Self self, str event, Callable k, Callable v) |
| |
|
None | _emit_run (Self self, Callable f, Tuple[Any,...] args, Dict[str, Any] kwargs) |
| |
|
None | _emit_handle_potential_error (Self self, str event, Any error) |
| |
|
bool | _call_handlers (Self self, str event, Tuple[Any,...] args, Dict[str, Any] kwargs) |
| |
| None | _remove_listener (Self self, str event, Callable f) |
| |
The base event emitter class. All other event emitters inherit from
this class.
Most events are registered with an emitter via the `on` and `once`
methods, and fired with the `emit` method. However, pyee event emitters
have two *special* events:
- `new_listener`: Fires whenever a new listener is created. Listeners for
this event do not fire upon their own creation.
- `error`: When emitted raises an Exception by default, behavior can be
overridden by attaching callback to the event.
For example:
```py
@ee.on('error')
def on_error(message):
logging.err(message)
ee.emit('error', Exception('something blew up'))
```
All callbacks are handled in a synchronous, blocking manner. As in node.js,
raised exceptions are not automatically handled for you---you must catch
your own exceptions, and treat them accordingly.
| Union[Handler, Callable[[Handler], Handler]] pyee.base.EventEmitter.on |
( |
Self |
self, |
|
|
str |
event, |
|
|
Optional[Handler] |
f = None |
|
) |
| |
Registers the function `f` to the event name `event`, if provided.
If `f` isn't provided, this method calls `EventEmitter#listens_to`, and
otherwise calls `EventEmitter#add_listener`. In other words, you may either
use it as a decorator:
```py
@ee.on('data')
def data_handler(data):
print(data)
```
Or directly:
```py
ee.on('data', data_handler)
```
In both the decorated and undecorated forms, the event handler is
returned. The upshot of this is that you can call decorated handlers
directly, as well as use them in remove_listener calls.
Note that this method's return type is a union type. If you are using
mypy or pyright, you will probably want to use either
`EventEmitter#listens_to` or `EventEmitter#add_listener`.