Effects through the loopLesson 17 of 23

How do you stop listening?

Say while what a Stream should be open, and never write the cleanup.

A stopwatch needs a clock that reports ten times a second. That isn't a Command, which does one piece of work and gives one answer. It is the Stream from lesson 7: values that keep coming until someone closes it.

Predict

Read Stopwatch.ts. There is a running flag, a Ticked message and an update that knows what a tick means.

Press Start in your head. The button will say Stop. Does the time move?

Check your answer

No. Try it: running becomes true and the time stays at 0.0s.

update knows what Ticked means, but nothing ever sends it. There is no clock yet.

Try it

  1. Add one more function to the component, next to view. Import Subscription from "@effect/ui" and Stream from "effect":

    subscriptions: ({ model }) => [
      model.running &&
      Subscription.make(
        "tick",
        Stream.tick(interval).pipe(
          Stream.drop(1),
          Stream.map(() => StopwatchMessage.Ticked())
        )
      )
    ],

    Stream.tick gives its first value at once, so drop(1) skips it. Before you run: nothing here starts a clock, and nothing stops one. So what will stop the ticking when you press Stop?

  2. Press Run, then Start, wait, and Stop. Read the log. What are the rows around the Ticked ones?

  3. Add a second condition, so the entry reads model.running && model.elapsed < 3000 && .... Start it again from zero. You wrote nothing that stops a timer. What does the log show at three seconds? Take the condition out again afterwards.

  4. Suppose this stopwatch were removed from the page while running. What would have to be cleaned up, and who would do it?

About steps 1 and 4

The Model stops it. After every update, the runtime calls subscriptions with the new Model and compares the answer with the Streams it has open. While running is true the list holds tick, so the runtime opens it: the log says tick started. When running turns false the entry is false, the list is empty, and the runtime closes the Stream: tick stopped.

In step 4 the Stream would have to be closed, and the runtime closes it. A component that is gone lists nothing.

What you found

You never said when to start listening or when to stop. You said while what: tick should be open while the Model says running. Starting and stopping are the runtime's work, so there is no cleanup to forget. The rule in step 3 took one condition.

subscriptions has the same shape as view. Both take the Model and return a description: one of what should be on screen, the other of what should be listened to. The runtime makes each of them true and keeps it true. Each value the Stream produces arrives as a message and goes through update like a click would.

A screen and its listeners are now both functions of the Model. One thing a component shows is neither: data that lives on a server. Where does loaded data live?

17
Principle 17

Listening is a function of state

You don't start and stop a timer or a socket. You say while what it should be open, and the runtime opens and closes it as the Model changes.

ResultStarting

No messages yet

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