update is pure. So how does anything happen?
Return a description of work from update and get its result back as a message.
Rolling a die takes time and the answer is random. update is a pure function of a Model and a message, so it can do neither. This component has a Roll button anyway.
Predict
Read Dice.ts. Near the top, RollDie is defined: a name, its arguments, and a body that waits, picks a number and ends with a message. Below it, find what update does with ClickedRoll.
Press Roll in your head. What changes on screen?
Check your answer
Only the count underneath: "Asked for 1 rolls". The die stays blank.
RollDie is a description of rolling a die, and nobody runs it. It is c from lesson 1 again. Defining it did nothing, and update never mentions it. Press the button and check the log: one ClickedRoll, and no number, ever.
Try it
Have
updatehand the description over. Change theClickedRollhandler to:ClickedRoll: ($) => $.evolve({ rolls: (rolls) => rolls + 1 }).run(RollDie.make({ sides: 6 }))Before you run: is the die rolled inside
update? How long doesupdatetake to return?Press Run, then Roll, and read the four rows the log gains, top to bottom. Which of them are messages, and what sent the second one?
Change
sidesto 20 and roll again. Where did 20 travel?Press Roll three times quickly. The component can tell you a roll is in flight without a flag in the Model: use
h.isRunning(RollDie)to disable the button and label it "Rolling…" while one is.
About steps 1 and 2
No, and no time at all. RollDie.make({ sides: 6 }) builds a value. update returns two things, the next Model and that value, and it is done. It is still pure: same Model and message in, same Model and Command out.
The log shows the rest. ClickedRoll says run RollDie. Then the runtime starts it. 700 milliseconds later CompletedRollDie arrives, sent by the Command's result and not by a click. Then the run ends.
What you found
A side effect leaves update as a value and comes back as a fact. The value is a Command, and it is "describe, then run" from lesson 1 with the runtime doing the running. Its body is an ordinary Effect, written with Effect.gen as in lesson 2, so everything from the first eight lessons applies inside it: failures in its type, services it asks for, a runtime that can stop it.
The result arrives the only way anything gets into a component, as a message. So the log holds the whole story, including the part that happened outside. A Command that can fail reports two facts, Succeeded… and Failed…, and update handles both.
The die takes 700 milliseconds, and you pressed Roll three times in step 4. Three rolls were in flight at once. Was that what you wanted? What should happen if they click twice?
Effects leave as values and come back as facts
update returns a Command, which is a description of work. The runtime runs it and reports the result as one more message.