An event that describes any state change in the app. Attributes: name: The event name. router_data: The routing data where event occurred. payload: The event payload.
| 76 | frozen=True, |
| 77 | ) |
| 78 | class Event: |
| 79 | """An event that describes any state change in the app. |
| 80 | |
| 81 | Attributes: |
| 82 | name: The event name. |
| 83 | router_data: The routing data where event occurred. |
| 84 | payload: The event payload. |
| 85 | """ |
| 86 | |
| 87 | name: str |
| 88 | |
| 89 | router_data: dict[str, Any] = dataclasses.field(default_factory=dict) |
| 90 | |
| 91 | payload: dict[str, Any] = dataclasses.field(default_factory=dict) |
| 92 | |
| 93 | @property |
| 94 | def state_cls(self) -> "type[BaseState]": |
| 95 | """The state class for the event.""" |
| 96 | from reflex_base.registry import RegistrationContext |
| 97 | |
| 98 | substate_name = self.name.rpartition(".")[0] |
| 99 | return RegistrationContext.get().base_states[substate_name] |
| 100 | |
| 101 | @classmethod |
| 102 | def from_event_type( |
| 103 | cls, |
| 104 | events: "IndividualEventType | list[IndividualEventType] | None", |
| 105 | *, |
| 106 | router_data: dict[str, Any] | None = None, |
| 107 | ) -> "list[Event]": |
| 108 | """Create a list of Events from event-like objects. |
| 109 | |
| 110 | Args: |
| 111 | events: The event-like objects to create Events from. |
| 112 | router_data: The routing data for the events. |
| 113 | |
| 114 | Returns: |
| 115 | A list of Events created from the event-like objects. |
| 116 | """ |
| 117 | # If the event handler returns nothing, return an empty list. |
| 118 | if events is None: |
| 119 | return [] |
| 120 | |
| 121 | # If the handler returns a single event, wrap it in a list. |
| 122 | if not isinstance(events, list): |
| 123 | events = [events] |
| 124 | |
| 125 | # Fix the events created by the handler. |
| 126 | out = [] |
| 127 | for e in events: |
| 128 | if callable(e) and getattr(e, "__name__", "") == "<lambda>": |
| 129 | # A lambda was returned, assume the user wants to call it with no args. |
| 130 | e = e() |
| 131 | if isinstance(e, Event): |
| 132 | # If the event is already an event, append it to the list. |
| 133 | if router_data is not None and e.router_data != router_data: |
| 134 | out.append( |
| 135 | dataclasses.replace(e, router_data=e.router_data | router_data) |
no outgoing calls