By Chason Arthur
Shared foundation / validation slice
One small table. A durable contract.
72 synthetic service locations. Change sorting, filters, length, page, column visibility and order, then reload. Preferences remain browser-local, expire after 30 days, and never include live records or credentials.
Code walkthrough
Follow the implementation in code.
Use these examples alongside the steps on this page. Read the explanation before copying a snippet; source excerpts depend on the surrounding implementation, and illustrative examples are labeled.
Create the synthetic table through its shared wrapper
In a standalone page with an empty #fixture-table host, let the wrapper own that host. A stable table ID and column keys give saved preferences a predictable scope. This abbreviated example uses the same synthetic rows as the table above.
import { dealers } from "/lab/shared/data.mjs";
import { createTable } from "/lab/shared/table.mjs";
const table = await createTable({
element: document.querySelector("#fixture-table"),
tableId: "example-foundation-dealers",
columns: [
{ key: "id", title: "ID" },
{ key: "name", title: "Service location" }
],
data: dealers
});
Validate a view before restoring it
The shared sanitizer accepts a bounded table-state shape and checks the column names. An invalid or stale shape returns null instead of being applied to the table.
const { sanitizeTableState } = await import("/lab/shared/table.mjs");
const names = ["id", "name", "region", "tickets"];
const candidate = window.fixtureTable.store.get().state;
const validState = sanitizeTableState(candidate, names);
console.log(validState);
Keep the saved value bounded and time limited
The preference store writes a versioned envelope with a 30-day expiry. This source excerpt shows the envelope; the store handles serialization, validation and denied-storage fallback around it.
{
schemaVersion: STORAGE_VERSION,
savedAt: now(),
expiresAt: now() + PREFERENCE_TTL,
data: current
}
Exercise sorting and length through the live fixture
Use the browser console on this page after the enhanced table has loaded. Reload and read the values again to check persistence. The fixture contains only synthetic records.
const table = window.fixtureTable;
if (!table.api) throw new Error("Enhanced table unavailable");
table.api.column("name:name").order("desc").draw();
table.api.page.len(25).draw();
console.log(table.api.order(), table.api.page.len());
Check the in-memory fallback without browser storage
Inject a storage object that refuses access. A distinct test scope prevents this check from replacing the table’s real preferences. The store should retain a valid value for the page lifetime.
const { createStore } = await import("/lab/shared/storage.mjs");
const denied = {
getItem() { throw new Error("Storage denied"); },
setItem() { throw new Error("Storage denied"); }
};
const testStore = createStore(
"example-fallback", { compact: false },
value => typeof value.compact === "boolean",
{ storage: denied }
);
testStore.set({ compact: true });
console.assert(testStore.status() === "memory");
console.assert(testStore.get().compact === true);