xonsh/docs/advanced_events.rst

43 lines
1.7 KiB
ReStructuredText
Raw Permalink Normal View History

2016-08-27 22:25:29 -04:00
.. _events:
********************
Advanced Events
********************
If you haven't, go read the `events tutorial <tutorial_events.html>`_ first. This documents the messy
2016-08-27 23:10:24 -04:00
details of the event system.
2016-08-27 22:25:29 -04:00
2016-08-27 23:10:24 -04:00
You may also find the `events API reference <api/events.html>`_ useful.
Why Unordered?
==============
Yes, handler call order is not guaranteed. Please don't file bugs about this.
2016-08-27 23:10:24 -04:00
2020-10-03 23:02:39 -04:00
This was chosen because the order of handler registration is dependent on load order, which is
2016-08-27 23:10:24 -04:00
stable in a release but not something generally reasoned about. In addition, xontribs mean that we
2016-08-27 23:27:52 -04:00
don't know what handlers could be registered. So even an "ordered" event system would be unable to
make guarantees about ordering because of the larger system.
2016-08-27 23:10:24 -04:00
2020-10-03 23:02:39 -04:00
Because of this, the event system is not ordered; this is a form of abstraction. Order-dependent
2016-08-27 23:27:52 -04:00
semantics are not encouraged by the built-in methods.
2016-08-27 23:10:24 -04:00
So how do I handle results?
===========================
2022-07-01 21:17:01 +05:30
``Event.fire()`` returns a list of the returns from the handlers. You should merge this list in an
2016-08-27 23:10:24 -04:00
appropriate way.
2016-08-27 23:27:52 -04:00
What are Species?
=================
In xonsh, events come in species. Each one may look like an event and quack like an event, but they
behave differently.
This was done because load hooks look like events and quack like events, but they have different
semantics. See `LoadEvents <api/events.html#xonsh.events.LoadEvent>`_ for details.
2022-07-01 21:17:01 +05:30
In order to turn an event from the default ``Event``, you must transmogrify it, using
2016-08-27 23:27:52 -04:00
``events.transmogrify()``. The class the event is turned in to must be a subclass of ``AbstractEvent``.
(Under the hood, transmogrify creates a new instance and copies the handlers and docstring from the
old instance to the new one.)