|
| 1 | +# Signals |
| 2 | + |
| 3 | +`Unicorn` emits [Django signals](https://docs.djangoproject.com/en/stable/topics/signals/) |
| 4 | +for each component lifecycle event. You can connect receivers to observe component |
| 5 | +activity without monkey-patching internal methods — useful for debug toolbars, logging, |
| 6 | +analytics, or custom auditing. |
| 7 | + |
| 8 | +## Connecting to a signal |
| 9 | + |
| 10 | +```python |
| 11 | +from django.dispatch import receiver |
| 12 | +from django_unicorn.signals import component_rendered |
| 13 | + |
| 14 | +@receiver(component_rendered) |
| 15 | +def on_render(sender, component, html, **kwargs): |
| 16 | + print(f"{component.component_name} rendered {len(html)} bytes") |
| 17 | +``` |
| 18 | + |
| 19 | +`sender` is always the component **class** (not an instance). Every signal also |
| 20 | +passes the live `component` instance as a keyword argument, plus any event-specific |
| 21 | +kwargs documented below. |
| 22 | + |
| 23 | +## Available signals |
| 24 | + |
| 25 | +All signals are importable from `django_unicorn.signals`. |
| 26 | + |
| 27 | +--- |
| 28 | + |
| 29 | +### `component_mounted` |
| 30 | + |
| 31 | +Sent when a component is first created (mirrors the {meth}`mount` hook). |
| 32 | + |
| 33 | +| kwarg | type | description | |
| 34 | +|-------|------|-------------| |
| 35 | +| `component` | `UnicornView` | The component instance | |
| 36 | + |
| 37 | +--- |
| 38 | + |
| 39 | +### `component_hydrated` |
| 40 | + |
| 41 | +Sent when a component's data is hydrated (mirrors the {meth}`hydrate` hook). |
| 42 | + |
| 43 | +| kwarg | type | description | |
| 44 | +|-------|------|-------------| |
| 45 | +| `component` | `UnicornView` | The component instance | |
| 46 | + |
| 47 | +--- |
| 48 | + |
| 49 | +### `component_completed` |
| 50 | + |
| 51 | +Sent after all component actions have been executed (mirrors the {meth}`complete` hook). |
| 52 | + |
| 53 | +| kwarg | type | description | |
| 54 | +|-------|------|-------------| |
| 55 | +| `component` | `UnicornView` | The component instance | |
| 56 | + |
| 57 | +--- |
| 58 | + |
| 59 | +### `component_rendered` |
| 60 | + |
| 61 | +Sent after a component is rendered during an AJAX request (mirrors the {meth}`rendered` |
| 62 | +hook). Not fired for the initial server-side page render via the template tag. |
| 63 | + |
| 64 | +| kwarg | type | description | |
| 65 | +|-------|------|-------------| |
| 66 | +| `component` | `UnicornView` | The component instance | |
| 67 | +| `html` | `str` | The rendered HTML string | |
| 68 | + |
| 69 | +--- |
| 70 | + |
| 71 | +### `component_parent_rendered` |
| 72 | + |
| 73 | +Sent after a child component's parent is rendered (mirrors the {meth}`parent_rendered` |
| 74 | +hook). |
| 75 | + |
| 76 | +| kwarg | type | description | |
| 77 | +|-------|------|-------------| |
| 78 | +| `component` | `UnicornView` | The **child** component instance | |
| 79 | +| `html` | `str` | The rendered parent HTML string | |
| 80 | + |
| 81 | +--- |
| 82 | + |
| 83 | +### `component_property_updating` |
| 84 | + |
| 85 | +Sent before a component property is updated (mirrors the {meth}`updating` hook). |
| 86 | + |
| 87 | +| kwarg | type | description | |
| 88 | +|-------|------|-------------| |
| 89 | +| `component` | `UnicornView` | The component instance | |
| 90 | +| `name` | `str` | Property name being updated | |
| 91 | +| `value` | `Any` | The incoming new value | |
| 92 | + |
| 93 | +--- |
| 94 | + |
| 95 | +### `component_property_updated` |
| 96 | + |
| 97 | +Sent after a component property is updated (mirrors the {meth}`updated` hook). |
| 98 | + |
| 99 | +| kwarg | type | description | |
| 100 | +|-------|------|-------------| |
| 101 | +| `component` | `UnicornView` | The component instance | |
| 102 | +| `name` | `str` | Property name that was updated | |
| 103 | +| `value` | `Any` | The new value | |
| 104 | + |
| 105 | +--- |
| 106 | + |
| 107 | +### `component_property_resolved` |
| 108 | + |
| 109 | +Sent after a component property value is resolved (mirrors the {meth}`resolved` hook). |
| 110 | +Unlike `component_property_updating` / `component_property_updated`, this signal fires |
| 111 | +**only once** per sync cycle. |
| 112 | + |
| 113 | +| kwarg | type | description | |
| 114 | +|-------|------|-------------| |
| 115 | +| `component` | `UnicornView` | The component instance | |
| 116 | +| `name` | `str` | Property name that was resolved | |
| 117 | +| `value` | `Any` | The resolved value | |
| 118 | + |
| 119 | +--- |
| 120 | + |
| 121 | +### `component_method_calling` |
| 122 | + |
| 123 | +Sent before a component method is called (mirrors the {meth}`calling` hook). |
| 124 | + |
| 125 | +| kwarg | type | description | |
| 126 | +|-------|------|-------------| |
| 127 | +| `component` | `UnicornView` | The component instance | |
| 128 | +| `name` | `str` | Method name about to be called | |
| 129 | +| `args` | `tuple` | Positional arguments | |
| 130 | + |
| 131 | +--- |
| 132 | + |
| 133 | +### `component_method_called` |
| 134 | + |
| 135 | +Sent after a component method is invoked — on both **success and failure**. |
| 136 | +This signal includes the return value and exception info, making it more complete |
| 137 | +than the `called()` hook. |
| 138 | + |
| 139 | +| kwarg | type | description | |
| 140 | +|-------|------|-------------| |
| 141 | +| `component` | `UnicornView` | The component instance | |
| 142 | +| `method_name` | `str` | Method name that was called | |
| 143 | +| `args` | `tuple` | Positional arguments | |
| 144 | +| `kwargs` | `dict` | Keyword arguments | |
| 145 | +| `result` | `Any` | Return value of the method, or `None` on failure | |
| 146 | +| `success` | `bool` | `True` if the method completed without raising an exception | |
| 147 | +| `error` | `Exception \| None` | The exception raised, or `None` on success | |
| 148 | + |
| 149 | +```python |
| 150 | +from django.dispatch import receiver |
| 151 | +from django_unicorn.signals import component_method_called |
| 152 | + |
| 153 | +@receiver(component_method_called) |
| 154 | +def log_method(sender, component, method_name, result, success, error, **kwargs): |
| 155 | + if success: |
| 156 | + print(f"[unicorn] {component.component_name}.{method_name}() → {result!r}") |
| 157 | + else: |
| 158 | + print(f"[unicorn] {component.component_name}.{method_name}() raised {error!r}") |
| 159 | +``` |
| 160 | + |
| 161 | +--- |
| 162 | + |
| 163 | +### `component_pre_parsed` |
| 164 | + |
| 165 | +Sent before the incoming request data is parsed and applied to the component |
| 166 | +(mirrors the {meth}`pre_parse` hook). |
| 167 | + |
| 168 | +| kwarg | type | description | |
| 169 | +|-------|------|-------------| |
| 170 | +| `component` | `UnicornView` | The component instance | |
| 171 | + |
| 172 | +--- |
| 173 | + |
| 174 | +### `component_post_parsed` |
| 175 | + |
| 176 | +Sent after the incoming request data is parsed and applied to the component |
| 177 | +(mirrors the {meth}`post_parse` hook). |
| 178 | + |
| 179 | +| kwarg | type | description | |
| 180 | +|-------|------|-------------| |
| 181 | +| `component` | `UnicornView` | The component instance | |
| 182 | + |
| 183 | +--- |
| 184 | + |
| 185 | +## Overriding hooks vs. connecting signals |
| 186 | + |
| 187 | +The existing lifecycle hook methods (`mount`, `hydrate`, `rendered`, etc.) and signals |
| 188 | +serve different purposes: |
| 189 | + |
| 190 | +- **Hook methods** — override in your component subclass to run logic *inside* the |
| 191 | + component (e.g. `def mount(self): self.items = Items.objects.all()`). |
| 192 | +- **Signals** — connect a receiver anywhere in your project to observe *any* component |
| 193 | + without modifying component code. |
| 194 | + |
| 195 | +```{note} |
| 196 | +If you override a hook method in your component class and do **not** call |
| 197 | +``super()``, the corresponding signal will **not** fire because the default |
| 198 | +implementation (which sends the signal) is bypassed. |
| 199 | +``` |
0 commit comments