# Saving and resuming

> The flow owns its state. One snapshot goes in, one callback comes out.

Framework: Lit (`@stepperize/lit`), Stepperize v9.
Source: https://stepperize.com/docs/v9/lit/guides/persistence

The flow always holds its own state. You do not pass the current step or the
data on every render and wait for callbacks to apply them; you give the flow a
starting point, and it tells you about every change.

That exchange uses one object, the **snapshot**: everything a flow needs to
continue later.

```ts
type Snapshot = {
  step: string; // the current step
  history: string[]; // the path taken, which prev() walks back
  data: object; // the data of every step, by id
  complete: string[]; // the steps next() has validated, in that order
};
```

It is plain JSON when your data is. Each binding exports it typed for your
steps, as `Snapshot<typeof checkout.steps>`.

On this page, `stepper` is the controller's `value`: `this.stepper.value`.

## Save and resume

```ts
stepper = checkout.controller(this, {
  snapshot: saved,
  onChange: (snapshot) => save(snapshot),
});
```

- `snapshot` is where the flow starts. Pass any part of it, for example only
  `{ step: "payment" }` or only `{ data }`.
- `onChange` is called with the whole snapshot after the step, the data or
  the completion changes.

`snapshot` is read **once**, when the flow is created. Passing a different
object later does nothing; that is what [`restore`](#loading-a-draft-later) is
for.

## What onChange reports

| Change | `onChange` |
| --- | --- |
| An accepted `next`, `prev` or `goTo` | Yes |
| A `next()` the schema rejected | Yes: the data that was passed is kept |
| `setData` with a new value | Yes |
| `setData` with the value the step already has | No: nothing changed |
| `validate()` | No |
| `reset`, `restore` | Yes |
| `isPending` or `isFinished` changing | No |
| A move the guard refused | No |

## In the browser, without a mismatch

A server cannot read `localStorage`, so a flow that starts from it would
render differently on the server and in the browser. Start from what both
know, and apply the saved snapshot after mounting.

```ts title="checkout-steps.ts"
import { html, LitElement } from "lit";
import { checkout } from "./checkout";

export class CheckoutSteps extends LitElement {
  stepper = checkout.controller(this, {
    onChange: (snapshot) => localStorage.setItem("checkout", JSON.stringify(snapshot)),
  });

  // After the first update, which is the one that hydrates the server HTML.
  firstUpdated() {
    const saved = localStorage.getItem("checkout");
    if (saved) this.stepper.value.restore(JSON.parse(saved));
  }

  render() {
    return html`<h2>${this.stepper.value.current.title}</h2>`;
  }
}
customElements.define("checkout-steps", CheckoutSteps);
```

This only matters with [server rendering](https://stepperize.com/docs/v9/lit/guides/ssr). An
element that is only rendered in the browser can read the storage directly,
as below.

In an app that only renders in the browser, reading it directly is fine:

```ts
stepper = checkout.controller(this, {
  snapshot: JSON.parse(localStorage.getItem("checkout") ?? "{}"),
  onChange: (snapshot) => localStorage.setItem("checkout", JSON.stringify(snapshot)),
});
```

## Saving to a server

`onChange` is also called by `setData`. If you write drafts on every
keystroke, debounce the request.

A draft loaded on the server is the best starting point: the first render
already shows the right step.

```ts title="checkout-steps.ts"
import { html, LitElement } from "lit";
import type { Snapshot } from "@stepperize/lit";
import { checkout } from "./checkout";

type CheckoutSnapshot = Snapshot<typeof checkout.steps>;

/** `<checkout-steps draft='{"step":"payment",...}'>`: the server writes the draft as JSON. */
export class CheckoutSteps extends LitElement {
  static properties = { draft: { type: Object } };
  declare draft?: CheckoutSnapshot;
  #timer?: ReturnType<typeof setTimeout>;

  stepper = checkout.controller(this, () => ({
    snapshot: this.draft,
    onChange: (snapshot) => {
      clearTimeout(this.#timer);
      this.#timer = setTimeout(() => api.saveDraft(snapshot), 500);
    },
  }));

  render() {
    return html`<h2>${this.stepper.value.current.title}</h2>`;
  }
}
customElements.define("checkout-steps", CheckoutSteps);
```

A property with `type: Object` is parsed from its attribute as JSON, and the
attribute is in the HTML on the server and in the browser alike. The flow is
created no later than the first update, after the attribute has been read.

## Loading a draft later

When the draft arrives after the first render, apply it with `restore`. It
replaces the state without validation or guards, and cancels a move that is
running.

```ts
async firstUpdated() {
  const draft = await api.loadDraft();
  if (draft) this.stepper.value.restore(draft);
}
```

`restore` replaces the parts you pass and keeps the others:

| Call | Effect |
| --- | --- |
| `restore(snapshot)` | Replace everything. |
| `restore({ data })` | Replace the data of every step; stay on the current step. |
| `restore({ step: "review" })` | Go to a step without validation or guards. |
| `restore({ complete: [] })` | Clear the completion. |

Each part is replaced as a whole: `restore({ data: { shipping } })` leaves the
other steps without data. To change one step, use `setData`.

## Old snapshots

Flows change. When a saved snapshot names a step that no longer exists, it is
dropped from `step`, `history` and `complete`, and the flow starts on the
first step if `step` was the one that disappeared. A snapshot from an older
version of your flow never throws.

The data of each step is not checked when it is loaded. It is validated like
any other data, the next time `next()` runs on that step.

A snapshot holds what was typed, not the schemas' output, so a step loaded as
complete has no entry in `stepper.output` yet. The `next()` that finishes the
flow validates those steps once more before calling `onFinish`. If the saved
data no longer passes, because the schema changed since, the flow does not
finish: `next()` resolves with `reason: "invalid"`, the step stops being
complete and `stepper.issues(id)` says why. To find out earlier, call
`stepper.validate(id)` after loading the draft.

`restore` keeps the output of every complete step whose data it did not
replace, so `restore({ step: "review" })` loses nothing.

## Starting over

`reset()` goes back to the snapshot the flow started with, and cancels a move
that is still running. Remember to delete your saved copy: `reset()` reports
the fresh snapshot through `onChange` like any other change.

```ts
#startOver() {
  this.stepper.value.reset();
  localStorage.removeItem("checkout");
}
```

## Reading the snapshot

`stepper.snapshot()` returns the current one at any time, for example to send
it with a support request, or to read the completed steps.

## What not to save

A snapshot contains everything the steps stored. Remove what should not be
written to storage before saving it.

```ts
onChange: (snapshot) => {
  const { payment: _payment, ...data } = snapshot.data;
  localStorage.setItem("checkout", JSON.stringify({ ...snapshot, data }));
},
```

Next: [the step in the URL](https://stepperize.com/docs/v9/lit/guides/url).
