The loopLesson 11 of 23

Who decides what a click means?

Let the view say what happened and one function decide what it means.

In the last lesson you wrote the list of messages by hand. In a real app somebody's clicks write it. So something has to turn a click into a message, and the only thing that knows about buttons is the view.

Predict

Find the button in Counter.ts:

h.button.on("click", CounterMessage.ClickedIncrement())

ClickedIncrement() is called while the view is being built, before anyone has clicked anything. So does the count go up every time the screen is drawn?

Check your answer

No. ClickedIncrement() doesn't increment. It builds a small value, { _tag: "ClickedIncrement" }, which is a description of something that can happen. The view hands that value to the runtime, and the runtime sends it to update if and when the click comes.

You can see it in the log on the right: there is init, and no message until you press the button.

Try it

  1. Press + a few times and watch the log. Where have you seen a list like this before?

  2. The counter can only go up. Add ClickedDecrement: {} to CounterMessage, and stop there. What is missing, and who should complain about it?

  3. Handle it in update with (count) => count - 1, add a button, and run. Then do the same for ClickedReset, which sets a value directly: $.evolve({ count: 0 }).

  4. Suppose the button sent SetCount({ value: model.count + 1 }) instead, with the view working out the new number. After ten clicks, what would the log tell you? And where would you make the change if the count must never pass 10?

About steps 2 and 4

In step 2, update is missing a case. It is a record with one handler per message, so in your own editor TypeScript reports the gap until you fill it. The sandbox here strips types and runs what you wrote, which is why nothing stopped you.

In step 4 the log would say SetCount 1, SetCount 2, and so on: what the code decided, with no trace of what the person did. And the rule about 10 would have to go into every view that sends SetCount. With ClickedIncrement, the log says what happened and the rule goes in one place, update.

What you found

The log is the session from the last lesson, written for you one click at a time, and the Model on screen is still its fold.

The view's part is small on purpose. It can't change the Model and it doesn't decide anything. It names what happened, in the past tense and without opinions: ClickedIncrement, not Increment, and never SetCount. Every decision about what that means is made by update, which is the one function you'd have to read to know how this component behaves.

ClickedIncrement carries no information beyond its name. Most things a person does carry more: what they typed, which row they picked. What does a message need to know?

11
Principle 11

The view reports, update decides

A message says what happened, never what to do. Every decision about state lives in one pure function.

ResultStarting

No messages yet

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