# `Soundcheck.Check.Warning.AssignSetOnlyWhenConnected`
[🔗](https://github.com/leonqadirie/soundcheck/blob/v0.1.0/lib/soundcheck/check/warning/assign_set_only_when_connected.ex#L1)

## Basics

> #### This check is disabled by default. {: .neutral}
>
> [Learn how to enable it](`e:credo:config_file.html#checks`) via `.credo.exs`.

This check has a base priority of `high` and works with any version of Elixir.

## Explanation

Flags template reads of assigns that `mount/3` sets only when
`connected?(socket)` is true. LiveView renders every page twice: first
over HTTP, disconnected, then over the websocket. The disconnected
render raises `KeyError` on the missing assign, so the page never
loads.

Before:

    def mount(_params, _session, socket) do
      if connected?(socket),
        do: {:ok, assign(socket, :stats, Stats.load())},
        else: {:ok, socket}
    end

    def render(assigns), do: ~H"<p>{@stats.total}</p>"

After:

    def mount(_params, _session, socket) do
      stats = if connected?(socket), do: Stats.load(), else: nil
      {:ok, assign(socket, :stats, stats)}
    end

The check interprets `mount/3` once per context, following
`if connected?(socket)` and its negations. Keys that `handle_params/3`
or an `on_mount` hook assigns are never reported, because both run on
the disconnected render too. When the disconnected path passes the
socket to a function soundcheck does not model, the check makes no
claim. When `handle_params/3` does, or an `on_mount` hook cannot be
read, findings are unverifiable.

Confidence: high. Tier: semantic (run `mix compile` before `mix credo`).

## Check-Specific Parameters

Use the following parameters to configure this check:

### `:excluded_paths`

  Paths to skip (atoms, binaries as path segments, or regexes).

*This parameter defaults to* `[~r/\/test\//, "test"]`.

### `:strict`

  Report unverifiable findings too.

*This parameter defaults to* `false`.

## General Parameters

Like with all checks, [general params](`e:credo:check_params.html`) can be applied.

Parameters can be configured via the [`.credo.exs` config file](`e:credo:config_file.html`).

---

*Consult [api-reference.md](api-reference.md) for complete listing*
