What if they click twice?
See two runs race, then say which one should win.
One click, one Command, one answer. That was the last lesson. People don't wait for answers before clicking again.
Predict
Read Search.ts. Each button runs the Search Command, and catalog says how long each pretend lookup takes: cats 1800 milliseconds, dogs 400.
Press Search cats and then Search dogs straight after, in your head. Two seconds later, which results are on screen?
Check your answer
The cats. Try it and read the log.
Both runs started. Dogs answered first and was shown. Then cats answered, late, and CompletedSearch replaced the dogs with it. The screen now says "You asked for dogs" above a list of cats. Every line did what it was told. Nobody said what should happen when two runs overlap, so both ran.
Try it
Before changing anything: when the second search starts, what should become of the first?
Say it. Add a mode to the Command's options and press Run:
{ args: { term: Term }, mode: "switch" },Search cats, then dogs. Before you look: what will the log say happened to the cats?
Now change
"switch"to"queue"and do it again. Before you run: which results show first, and how long until the dogs appear?Which mode would you choose for a save button, where the second click has newer text than the first? Put
"switch"back when you have decided.
About steps 2, 3 and 4
With switch, the log shows Search interrupted (switch) right where the second click lands. The first run was stopped, so its answer never arrives and there is nothing stale to show.
With queue, the second run waits for the first. Cats show after 1.8 seconds, then dogs 0.4 seconds later. Nothing is lost and nothing overlaps.
A search wants switch: only the latest question matters. A save wants queue: every write should land, in order. Left out, the mode is concurrent, which is what you started with.
What you found
When two runs of one Command overlap there are three things you could mean. Let both run, replace the old one, or make the new one wait. None is right everywhere, so it is a decision, and you make it once, where the Command is defined.
Look at what you didn't write. No flag for "a search is in flight", no check that an answer is still wanted, no cancelling by hand. This is lesson 6: whoever runs it can stop it. The runtime runs your Commands, so it can interrupt one or hold one back. And it is lesson 3: the body of Search never changed. How it runs was decided from outside.
A Command is one piece of work with one answer. A clock, a socket or a keyboard isn't like that. It keeps reporting until someone tells it to stop, and someone always forgets. How do you stop listening?
Overlap is a decision
Say once whether a new run joins, replaces or waits for the last one. The runtime enforces it by interrupting or queueing.