The loopLesson 13 of 23

How do you change one thing in a list?

Return a new list instead of editing the old one, and tell the rows apart.

A payload can say which one, so a list needs one kind of message for all its rows, with the row's id inside. The harder question is what update does when it arrives.

Predict

Read SubmittedTodo in update. It doesn't push onto the list. It builds a new one: [...todos, { id, title, done: false }].

Type a title and press Add in your head. Then open the log row just before the new SubmittedTodo. Does its Model list two todos or three?

Check your answer

Two. Try it.

Each row holds the Model as it was after that message. The add built a new list and left the old one alone. Had update pushed onto the same array, every earlier state would have quietly grown a third todo.

Try it

  1. Add a fact that carries a row's id: ToggledTodo: { id: Schema.Number }.

  2. Handle it by returning a new list in which one row is new. Import UiStruct from "@effect/ui", then:

    ToggledTodo: ($, { id }) =>
      $.evolve({
        todos: (todos) =>
          todos.map((todo) =>
            todo.id === id ? UiStruct.evolve(todo, { done: (done) => !done }) : todo
          )
      })

    Before you run: of the todo objects in the new list, how many are new?

  3. Give each row a checkbox, inside the li: h.input.attr("type", "checkbox").checked(todo.done).on("change", TodosMessage.ToggledTodo({ id: todo.id })). Run, tick one, and read what the log says changed.

  4. Add ClickedRemove: { id: Schema.Number }, handle it with todos.filter((todo) => todo.id !== id), and give each row a button that sends it. Remove the first row. How does the runtime know the row that is left is the second one, and not the first with new text?

About steps 2 and 4

One. map returns a new array, UiStruct.evolve a new object for the row that changed, and every other row is the same object as before. Nothing was edited.

In step 4 the answer is .key(String(todo.id)). A key tells the runtime which row is which from one drawing to the next, so removing a row removes that row's elements and leaves the rest alone.

What you found

update never edits the Model. It returns a new one. For a list that means [...todos, item] to add, map to change one and filter to remove one, and for one row UiStruct.evolve, which is $.evolve for a struct inside the Model.

Lesson 10 depended on this without saying so. You could see any earlier state by stopping the fold early. That only holds if a later message can't reach back and change an earlier state. Replacing instead of editing keeps every row of the log true.

A todo is done or not, and a boolean says exactly that. Most things have more than two states. Describe those with booleans as well, and can the Model say something untrue?

13
Principle 13

Replace, never edit

update returns a new Model and leaves the old one alone. That is why every earlier state is still there to look at.

ResultStarting

No messages yet

Use the example above. Each message that reaches update shows here, with what it changed.