Skip to content

Client state ​

state.js is a memory-only key-value store. It's the reason the whole "no diffing" design works. This page is identical to the PHP version's: it's the same JavaScript.

Storage model ​

  • A single Map<string, string>; values are JSON.stringify-ed strings.
  • Memory only: it never touches sessionStorage / localStorage.
  • The real key is <object name>:<key>; keys prefixed with @ are global (@theme → theme).

How long it lives ​

Page load → empty
  set() / a binding takes the HTML default → has values
  A fragment re-renders → still there
  Page-mode navigation (the document isn't replaced) → still there
  clear() (after a successful save) → empty
  Reload / close the tab / full-page navigation → gone
  Back / forward (a bfcache restore counts as a new load) → gone
  • Other tabs can't see it.
  • A reload always resets it. That's deliberate: it isn't a draft-saving mechanism.

Who writes to it ​

SourceWhich namespace
data-state-init / -bind / -set / -toggleThe element's nearest render object
data in a Linker responseThe nearest render object with state:write around the triggering element
Linker actions state.set / state.clearThe scope argument
Your own JSstate.scope(name)

On the Rust side, LinkerResponse::ok().data("count", 3) makes the browser set("count", 3); Action::state_set("wizard", "step", 2) is state.scope("wizard").set("step", 2).

JS API ​

Only code inside the bundle imports it. Templates use declarative bindings. To write your own JS, put it in resources/js/site.js (or a file it imports):

js
import state from './cg/state.js';

const s = state.scope('customerPanel');

s.set('keyword', 'acme');
s.get('keyword', '');            // a deep copy
s.merge('filters', { sort: 'id' });
s.update('tags', (t = []) => t.concat('vip'));
s.remove('keyword');
s.clear();                       // clears only this namespace

const off = s.subscribe('keyword', (value, key, previous) => { /* ... */ });
off();                           // clean up subscriptions you wrote yourself
MethodNotifies subscribersNotes
get(key, fallback)—Returns a deep copy; changing it doesn't affect the store
set(key, value)✅undefined is the same as remove; unserializable values return false
merge(key, patch)✅Shallow merge
update(key, fn)✅Writes back what fn(current) returns
remove(key)✅Notifies only if there was a value
clear()✅ one by oneThe scoped version clears only its own keys
subscribe(key, fn)—'*' receives everything; returns off()

Value rules ​

CaseBehavior
Objects, arrays, strings, numbers, booleans, nullNormal
undefinedSame as delete
Functions, Map, Set, circular referencesWarns and doesn't write
DateReads back as an ISO string
s.get('o').x = 1Does not reach the store; use merge

The Rust side serializes data with serde, so what arrives is always valid JSON; Option::None becomes null.

No equality check

Writing the same value still notifies and still redraws. A subscriber that writes back to the same key loops forever.

What can go in ​

Yes: tabs, expand/collapse, filters, sorting, page numbers, scroll position, unsaved input, which wizard step you're on (for display).

No: permissions, amounts, cart contents, anything the server must trust, passwords, File objects.

When to clear it yourself ​

WhenHow
After a successful savescope.clear(), or the old input overwrites the server's new values
Switching to another recordscope.clear(), or use a different object name
Logging outNothing to do; pagehide clears it on full-page navigation

From Linker:

rust
LinkerResponse::ok().then(Action::state_clear("customerForm"))

Watch every change ​

js
document.addEventListener('cg:state', e => console.log('state', e.detail.key, '→', e.detail.value));