Apply ``updater(prev)`` to produce the next state. If ``updater(prev) is prev`` (identity check, not structural equality), the call is a no-op. Otherwise: 1. The new state is committed to ``self._state``. 2. ``on_change(old, new)`` fires synchronously (if set).
(self, updater: Callable[[T], T])
| 61 | return self._state |
| 62 | |
| 63 | def set_state(self, updater: Callable[[T], T]) -> None: |
| 64 | """Apply ``updater(prev)`` to produce the next state. |
| 65 | |
| 66 | If ``updater(prev) is prev`` (identity check, not structural |
| 67 | equality), the call is a no-op. Otherwise: |
| 68 | |
| 69 | 1. The new state is committed to ``self._state``. |
| 70 | 2. ``on_change(old, new)`` fires synchronously (if set). |
| 71 | 3. All subscribed listeners fire in insertion order. |
| 72 | |
| 73 | Listener and onChange exceptions propagate to the caller; the |
| 74 | state has already been committed at that point. |
| 75 | """ |
| 76 | prev = self._state |
| 77 | nxt = updater(prev) |
| 78 | if nxt is prev: |
| 79 | return |
| 80 | # Commit state BEFORE side effects so an onChange or listener that |
| 81 | # reads back via get_state() sees the new value (matches TS). |
| 82 | self._state = nxt |
| 83 | if self._on_change is not None: |
| 84 | self._on_change(prev, nxt) |
| 85 | # Snapshot before iterating: listeners may unsubscribe themselves |
| 86 | # or trigger another set_state during the callback. |
| 87 | for listener in list(self._listeners): |
| 88 | listener() |
| 89 | |
| 90 | def subscribe(self, listener: Listener) -> Callable[[], None]: |
| 91 | """Register ``listener``. Returns an idempotent unsubscribe.""" |
no outgoing calls
no test coverage detected