Effects through the loopLesson 18 of 23

Where does loaded data live?

Declare the data a component shows and keep loading out of the Model.

A profile page shows a user that lives on a server. That data isn't something the person did, and it takes most of a second to arrive. Where does it go?

Predict

Read Profile.ts. The Model has one field, id, and GetUser describes a lookup that nothing uses yet. Suppose you loaded the user with what you know so far: a Command, and some new fields in the Model. Which fields would you add, and what could they say that isn't true?

Check your answer

Probably loading: boolean, error: string | null and user: User | null. That is the bag of flags from lesson 14. It can say loading: true beside last time's user, or hold an error and a user at once.

You would also have to run the Command when the page first draws and on every change of id, and decide what happens when two lookups overlap.

Try it

  1. Press User 2 and read the log. One row, PickedUser. Before you go on: at which moments would a fetch have to start, and what do those moments have in common?

  2. Add this to Html.component({ ... }), after message, and run. You wrote no Command and no message. What do you expect in the log?

queries: {
  user: { query: GetUser, args: ({ model }) => ({ id: model.id }) }
}
  1. Draw the result. Import AsyncResult from "effect/reactivity" and replace the placeholder paragraph. How many states can this value be in, and can two be true at once?

AsyncResult.match(h.queries.user, {
  onInitial: () => h.p.class("muted").children("Loading…"),
  onFailure: () =>
    h.p.class("error").children(`There is no user ${model.id}.`),
  onSuccess: ({ value: user }) => [
    h.strong.children(user.name),
    h.span.class("pill").children(user.role)
  ]
})
  1. Pick users 2 and 3, then go back to 1. Before you press it: how long will user 1 take this time? Then try user 4.

What you found

The queries entry is a description of the data this component shows, as a function of the Model. You have written that shape twice: view describes the screen, and subscriptions describes what to listen to. A Query describes what to load, and the runtime does the rest. It fetches at those args, fetches again when they change, drops a lookup nobody waits for, and keeps one result per args, which is why user 1 came back at once.

The result sits beside the Model, not in it. It is one of three states, so it can't contradict itself. The log shows the split: PickedUser is a message, because a person did it, and the user rows after it are the runtime reporting what arrived. The Model still holds only id.

Every lesson so far had one component on the screen. An app has many. What happens when there are two?

18
Principle 18

Say what you need, not how to get it

A component declares the data it shows. Fetching, caching and loading states belong to the runtime, and the Model holds only what the user did.

ResultStarting

No messages yet

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