Nex.Store (nex_core v0.4.3)

Copy Markdown

Page-scoped state management for Nex applications.

Provides simple key-value storage isolated by page view, similar to React/Vue state - refresh the page and state is gone.

Usage

# In a Page module
def mount(_params) do
  %{title: "Todos", todos: Nex.Store.get(:todos, [])}
end

def create_todo(%{"text" => text}) do
  todo = %{id: unique_id(), text: text, completed: false}
  Nex.Store.update(:todos, [], &[todo | &1])
  # ...
end

State is tied to a _page_id that is generated on first page render. HTMX requests carry this _page_id to maintain state within the same page view. Refreshing the page generates a new _page_id, effectively clearing state.

TTL & Cleanup

Each page's state has a TTL (default 1 hour). After TTL expires, state is automatically cleaned up to prevent memory leaks.

Summary

Functions

Returns a specification to start this module under a supervisor.

Delete all state for a page

Clear page_id from process dictionary (called by Nex.Handler after request)

Delete value from page store

Generate a new page ID

Get value from page store

Get current page ID from process dictionary

Put value into page store

Set current page ID in process dictionary (called by Nex.Handler)

Update value in page store

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

clear_page(page_id)

Delete all state for a page

clear_process_dictionary()

Clear page_id from process dictionary (called by Nex.Handler after request)

delete(key)

Delete value from page store

generate_page_id()

Generate a new page ID

get(key, default \\ nil)

Get value from page store

get_page_id()

Get current page ID from process dictionary

put(key, value)

Put value into page store

set_page_id(page_id)

Set current page ID in process dictionary (called by Nex.Handler)

start_link(opts \\ [])

update(key, default, fun)

Update value in page store