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 areJSON.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
| Source | Which namespace |
|---|---|
data-state-init / -bind / -set / -toggle | The element's nearest render object |
data in a Linker response | The nearest render object with state:write around the triggering element |
Linker actions state.set / state.clear | The scope argument |
| Your own JS | state.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):
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| Method | Notifies subscribers | Notes |
|---|---|---|
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 one | The scoped version clears only its own keys |
subscribe(key, fn) | — | '*' receives everything; returns off() |
Value rules
| Case | Behavior |
|---|---|
Objects, arrays, strings, numbers, booleans, null | Normal |
undefined | Same as delete |
Functions, Map, Set, circular references | Warns and doesn't write |
Date | Reads back as an ISO string |
s.get('o').x = 1 | Does 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
| When | How |
|---|---|
| After a successful save | scope.clear(), or the old input overwrites the server's new values |
| Switching to another record | scope.clear(), or use a different object name |
| Logging out | Nothing to do; pagehide clears it on full-page navigation |
From Linker:
LinkerResponse::ok().then(Action::state_clear("customerForm"))Watch every change
document.addEventListener('cg:state', e => console.log('state', e.detail.key, '→', e.detail.value));