The Million-Item List

The core principles of our project—the instant sync engine, the server() function, and the global app object—feel solid. For the first time, we've moved from foundational invention to simply _using_ our own tools to build. The goal was to see what it felt like to create something more complex than a simple button.


We decided to build the canonical example: a todo application with filtering. It has three views: "All," "Active," and "Completed."


Our first attempt was naive, but it worked. The problem wasn't that it didn't work. The problem surfaced when our architect, in their typical fashion, asked a deeply uncomfortable question:


"This is fine for ten todos. What happens when there are a million?"


The question hung in the air. We all knew the answer. To render the "Active" list, our component code was running a .filter() over the _entire_ app.state.todos array every single time it rendered. With a million items, that's a brute-force, O(n) operation. It was a clumsy, inefficient way of deriving data, a betrayal of our entire philosophy of surgical precision.


We needed a way to create a pre-filtered, indexed subset of our data that would update itself in real-time. We didn't need reactive components; our system already had that. We needed reactive _queries_.


This led to the creation of app.view(). It's a way to create a living, self-maintaining index on top of your state. You tell it which data to watch and the rule for inclusion, and it creates a new array that is always perfectly, and instantly, up-to-date.


The magic is how it updates. When a single todo in the main app.state.todos array changes, the system doesn't re-filter the entire million-item list. It takes that one changed item and checks it against the view's predicate. If the item now belongs in the view, it's added. If it no longer belongs, it's removed. The update work is O(1), not O(n).


Here's the critical detail: the view's array itself is read-only. You can't manually push or pop items from it, as its contents are managed by the system. However, the elements _inside_ the array are not copies. They are the exact same database objects that exist in app.state.todos. They are direct, mutable references, meaning app.views.active_todos[0] === app.state.todos[42] would be true if they point to the same item.


This means you can mutate an object you get from a view, and that change is a direct mutation of your state.


Here is the new, correct way to build our filtered list:


Now, when a user clicks an active todo, we mutate its completed property directly. The engine sees this change, automatically removes the object from the active_todos view, adds it to the completed_todos view, and sends the precise DOM instructions to the client to reflect this change.


The developer simply declares the desired index. The system handles the complexity of keeping it in sync efficiently. State is the foundation, but Views are what make that foundation scalable, allowing you to build complex applications that remain instantaneous, whether they're handling ten items or a million.