DurableStash.Session (durable_stash v0.1.0)

Copy Markdown View Source

The durable process behind one browser session.

One DurableStash.Session exists per browser session; every LiveView of that session reads and writes through it. State is partitioned per view module, and the single-process actor model gives a total order over writes — per-key last-write-wins with no timestamps needed.

State shape (string keys — it round-trips through JSON object storage):

%{
  "views" => %{
    "Elixir.My.View" => %{"vsn" => 1, "data" => %{"key" => value}}
  },
  "last_seen_at" => unix_millis | nil
}

Each view slice carries the vsn its writer declared. A write with a different vsn replaces the slice wholesale instead of merging — the adapter only does that with a full, migrated key set, so a version bump can never mix old- and new-shape keys.

Every accepted write returns :sync, so durability is guaranteed before a deploy can take the node down. Writes are rare UI state; the immediate object-store PUT is the point, not a cost.

Summary

Functions

Removes keys from the view's data map. Unknown keys are ignored.

Returns {:ok, %{"vsn" => vsn, "data" => data}} or :not_found when the view has never stashed.

Returns the view's data map, %{} when the view has never stashed.

Callback implementation for DurableServer.init/1.

Merges changes (string-keyed map) into the view's slice. Per-key LWW.

Drops the view's slice.

Functions

after_terminate(terminate_return, info)

Callback implementation for DurableServer.after_terminate/2.

code_change(old_vsn, state, new_vsn)

Callback implementation for DurableServer.code_change/3.

drop(server, view, keys)

Removes keys from the view's data map. Unknown keys are ignored.

fetch_view(server, view)

Returns {:ok, %{"vsn" => vsn, "data" => data}} or :not_found when the view has never stashed.

get_view(server, view)

Returns the view's data map, %{} when the view has never stashed.

handle_cast(request, state)

Callback implementation for DurableServer.handle_cast/2.

handle_continue(continue, state)

Callback implementation for DurableServer.handle_continue/2.

handle_info(msg, state)

Callback implementation for DurableServer.handle_info/2.

init(state)

Callback implementation for DurableServer.init/1.

merge(server, view, changes, vsn \\ 1)

Merges changes (string-keyed map) into the view's slice. Per-key LWW.

When the stored slice carries a different vsn, the slice is replaced with changes wholesale (see the moduledoc).

reset_view(server, view)

Drops the view's slice.

terminate(reason, state)

Callback implementation for DurableServer.terminate/2.