Skip to content

How CascadeUI Thinks

Two pictures carry most of CascadeUI: the path one click takes from your callback to the edit on screen, and the shape of the state the store keeps. This page draws both, then maps views, sessions, and navigation to the pages that cover them.

The Data Flow

Animation: Kael's counter in server 22 and Kael's badge in server 33 both show 2. Kael clicks +1. The callback's dispatch_scoped() sends a SCOPED_UPDATE action to the store. LoggingMiddleware logs it and passes it on. The reducer works on a copy of the state, and the store keeps it, with user:100 now at 3. Both views list SCOPED_UPDATE and their selectors return 3, was 2, so each redraws: the counter's edit answers the click, and the badge edits its own message.

This is what happens when Kael clicks +1 on the counter from Adding Shared State:

  1. The click arrives. The library starts the timer that acknowledges the click if the callback is slow, checks that Kael may click (Who Can Click), and takes the view's turn, so clicks on it run one at a time.
  2. The callback dispatches. dispatch_scoped() builds a SCOPED_UPDATE action and hands it to the store.
  3. Middleware sees it first, each one in the order it was installed, and passes it on (Middleware).
  4. The reducer works on a copy of the state and returns the new state, and the store keeps it. Reducers run one at a time (Store, Actions, and Reducers).
  5. The views that listen are told. A view that lists the action redraws: on_state_changed() calls build_ui(), then refresh(). A view with a selector redraws only when what its selector returns has changed (Subscribers and Slots).
  6. The edits go out. The clicked view's refresh answers the click and edits its message in one request. Every other view that redraws does so on a task of its own and edits its own message, so a slow one never holds up the click (Responding to Clicks).

When the callback returns, the library records a click on a StatefulButton or StatefulSelect that leaves the view open as a COMPONENT_INTERACTION action, which takes the same path.

A callback can also change the view's own attributes and call refresh(), as Your First Panel does. That redraws the panel that was clicked and no other: only an action reaches the rest.

The State Tree

store.state is one dict with six keys. Your data lives under application, and the library keeps the other five, and reading them helps when you debug.

Diagram: store.state holds six keys. views, sessions, components, and modals are written by the library as views are sent, clicked, and closed, and are empty after a restart. persistent_views is written by the library as persistent panels are sent and closed, and is read back after a restart when persistence is set up. application holds your reducers' slots and the scoped slot dispatch_scoped() writes, and the slots you save come back after a restart.

Key One entry for What an entry holds After a restart
views each live view, by its id id, type, user_id, guild_id, session_id, message_id, channel_id, props, created_at, updated_at, plus undo_stack and redo_stack once a view with undo records a step empty
sessions each session, by its id id, user_id, guild_id, members (the ids of its views), shared_data, created_at, updated_at empty
components each clicked control, by its custom_id id, view_id, interactions (the last 50), last_interaction empty
modals each view whose modal was submitted, by the view's id submissions (the last 50), last_submission empty
persistent_views each persistent panel, by its persistence_key persistence_key, class_name, message_id, channel_id, guild_id, user_id, registered_at read back, with persistence set up
application each slot, by its name your reducers' slots, and scoped, which dispatch_scoped() writes the slots you save come back

Closing a view removes its row, its components and modals entries, and its session once no view is left in it. The six keys stay, even when one is left empty. A modals entry is written when a Modal built with view_id= is submitted, as a form's text fields are. Saving a slot across a restart is set up per slot (Persistence).

A screen's way back is not in the state. The navigation stack lives on the view object, and push() and pop() hand it from screen to screen (Navigation).

Views, Sessions, and Navigation

What it is How long it lasts More
A view The object behind one panel, on one message Until it closes: exit(), its timeout, its message deleted, or a newer panel replacing it Lifecycle
A session One per panel: the view and every screen pushed from it belong to it and share its shared_data Until its last screen closes Session Data
Navigation push() shows another screen on the same message, pop() builds the one before again, and replace() swaps the panel for a new message, with no way back As long as the panel: each screen hands the stack to the next Navigation

Next

The Five Pillars sorts every view setting by the question it answers: who can click, how many can be open, how long a view lives, who shares it, and where it goes next. State covers the store page by page.