Soundcheck.Check.Refactor.StreamCandidate (soundcheck v0.1.0)

Copy Markdown View Source

Basics

This check is disabled by default.

Learn how to enable it via .credo.exs.

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

Explanation

Flags list assigns that handlers grow one item at a time and the template renders with :for. The LiveView keeps the whole list in memory and resends every item whenever it changes; a stream keeps no items on the server and sends only the new one.

Before:

def handle_info({:new_message, message}, socket) do
  {:noreply, update(socket, :messages, &(&1 ++ [message]))}
end

<div :for={message <- @messages}>{message.body}</div>

After:

def handle_info({:new_message, message}, socket) do
  {:noreply, stream_insert(socket, :messages, message)}
end

<div id="messages" phx-update="stream">
  <div :for={{id, message} <- @streams.messages} id={id}>{message.body}</div>
</div>

A write counts as growing the list when update/3 gets a function that appends or prepends to its argument (&(&1 ++ [x]), &[x | &1]), or assign/3 gets an append or prepend to socket.assigns.key.

Confidence: low. Tier: syntactic (works without compiling).

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

Unused.

This parameter defaults to false.

General Parameters

Like with all checks, general params can be applied.

Parameters can be configured via the .credo.exs config file.