Skullet.Memory (Skullet v0.1.0)

Copy Markdown View Source

Remembers the data a skeleton last showed, so its next skeleton is drawn from that instead of from a hand-written sample.

The upstream JavaScript library remembers shapes in localStorage; a Phoenix app can do better and remember the data, then render it: the skeleton is measured from the real component and is exact at any viewport size, on any device.

Opt in per skeleton:

<Skullet.skeleton :let={user} id="profile" async={@user} remember={@current_user.id}>
  …
</Skullet.skeleton>

remember is a scope — usually the user's id — and the skeleton's id completes the key. While loading, a remembered value is used as the sample; once loaded, the real value replaces it.

The store is this node's memory only: it is not shared between nodes, it expires after config :skullet, :memory_ttl_ms (30 minutes by default), and it must be cleared when somebody signs out — forget/1 with their scope, from wherever that happens.

Summary

Types

The skeleton's id attribute, which completes the key with the scope.

How far back get/3 looks; :ttl_ms may be negative, which expires everything.

The scope a skeleton remembers under: usually the current user's id, but any term works — a tenant, an account, :global.

The data the skeleton last showed: whatever your component was passed.

Functions

Returns a specification to start this module under a supervisor.

Forgets everything, for every scope.

Forgets everything remembered under scope, as on sign-out.

The value remembered for scope and id, or nil.

Remembers value for scope and id.

Types

key()

@type key() :: String.t()

The skeleton's id attribute, which completes the key with the scope.

option()

@type option() :: {:ttl_ms, integer()}

How far back get/3 looks; :ttl_ms may be negative, which expires everything.

scope()

@type scope() :: term()

The scope a skeleton remembers under: usually the current user's id, but any term works — a tenant, an account, :global.

value()

@type value() :: term()

The data the skeleton last showed: whatever your component was passed.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

clear()

@spec clear() :: :ok

Forgets everything, for every scope.

forget(scope)

@spec forget(scope()) :: :ok

Forgets everything remembered under scope, as on sign-out.

get(scope, id, opts \\ [])

@spec get(scope(), key(), [option()]) :: value() | nil

The value remembered for scope and id, or nil.

Options

  • :ttl_ms — how old a value may be; defaults to config :skullet, :memory_ttl_ms

put(scope, id, value)

@spec put(scope(), key(), value()) :: :ok

Remembers value for scope and id.

start_link(opts)

@spec start_link(keyword()) :: GenServer.on_start()