gpui-cc Course handbook
CHAPTER 04 / STAGE 12COURSE

Keep the model across rebuilds

Keep durable app data separate from frame descriptions.

state-update
On this page

Learn: an element describes this frame, not the app’s history

If a release changes the count but the next fresh element resets it to zero, state was stored in the wrong place. Put durable data in the app/session. A describe call reads that data and makes fresh elements. Its return value may disappear every frame without losing the model.

Run it: build two descriptions from two owners

Terminal
./build/examples/core-lab rebuild-description
C++ · core-lab.cpp
void rebuild_description() {
    struct Rating { unsigned value = 2; };
    const auto describe = [](const Rating& model) { return std::string(model.value, '*'); };
    Rating first, second;
    std::cout << describe(first) << '\n';
    ++first.value;
    std::cout << describe(first) << '\n';
    std::cout << describe(second) << '\n';
}

Expect **, ***, **. The two Rating objects do not share state. Try it: call describe(first) twice after mutation; neither call consumes or changes the model.

Build it: update, then describe again

Your challenge: Give each app instance a persistent model and a method/function to handle an action. Rerender from that model. For this stage, use a visual bar or a color change to show state; text is not required yet.

Choose a C++ ownership structure you can explain (a session-owned value is plenty). Use an action instead of a native event as the model’s input. Return whether applying the action changed state so the runtime can distinguish a real update from a no-op. Store stable IDs and action values in hit records. Do not introduce global state or a GPUI-style entity map before you need it.

Check it

Start two independent models at zero. Deliver two valid actions to one and none to the other. Rebuild both descriptions repeatedly and after resize; the first still shows two steps and the second zero. Verify a rejected action returns “unchanged.” Observe either the generated scene bar width or raster output, not just a model member.

Terminal
python3 course/check_core.py state-update

Hints and troubleshooting

Hint: the log counts up but the UI resets

Trace which object the action mutates and which object the describe function reads. Check for copies or a model constructed inside render. GPUI-CE separates durable entity state from frame-local elements; our small session may do so with one value.