import { createEffect, createSignal, For, Show } from "solid-js";
import { createStore } from "solid-js";
import preview from "../../../.storybook/preview.js";
import { captureStoreUpdates, trackDeep, trackStore } from "@solid-primitives/deep";
import readme from "../README.md?raw";
import {
Button,
Container,
Section,
Separator,
StatRow,
TextField,
colors,
font,
radii,
} from "../../../.storybook/ui/index.js";
const meta = preview.meta({
title: "Reactivity/Deep",
tags: ["autodocs"],
parameters: {
layout: "centered",
docs: {
description: {
component: readme,
},
},
},
});
export default meta;
export const TrackDeepMultiple = meta.story({
name: "One effect, two stores",
parameters: {
docs: {
description: {
story:
"`trackDeep` accepts any wrappable structure — not just a store proxy but any plain object or array containing store nodes. Here two independent stores (`user` and `prefs`) are bundled into a single plain object and subscribed with one `createEffect`. Any mutation to either store fires the effect. `trackStore` cannot do this — it requires a store proxy at the root.",
},
},
},
render: () => {
const [user, setUser] = createStore({ name: "Alice", email: "alice@example.com" });
const [prefs, setPrefs] = createStore({ theme: "light" as "light" | "dark", notify: true });
const [saveCount, setSaveCount] = createSignal(0);
const [lastSave, setLastSave] = createSignal("—");
let ready = false;
createEffect(
() => trackDeep({ user, prefs }),
() => {
if (!ready) {
ready = true;
return;
}
setSaveCount(c => c + 1);
setLastSave(new Date().toLocaleTimeString());
},
);
return (
setUser(s => {
s.name = v;
})
}
/>
setUser(s => {
s.email = v;
})
}
/>
);
},
});
export const TrackStoreWholeTree = meta.story({
name: "Sync on any nested change",
parameters: {
docs: {
description: {
story:
"`trackStore` subscribes to every node in a store tree using Solid's internal `$TRACK` structural subscriptions, and memoizes a per-node accessor in a `WeakMap` so each node is only ever subscribed once regardless of how many effects share the same store. Edit the note, adjust a quantity, or add and remove items — any change at any depth fires the effect once.",
},
},
},
render: () => {
const [cart, setCart] = createStore({
items: [
{ name: "Widget", qty: 1 },
{ name: "Gadget", qty: 2 },
] as { name: string; qty: number }[],
note: "",
});
const [fireCount, setFireCount] = createSignal(0);
let nextId = 2;
let ready = false;
createEffect(
() => trackStore(cart),
() => {
if (!ready) {
ready = true;
return;
}
setFireCount(c => c + 1);
},
);
return (
{(item, i) => (
{item.name}
{item.qty}
)}
setCart(s => {
s.note = v;
})
}
placeholder="Special instructions…"
/>
);
},
});
export const StoreDeltaStory = meta.story({
name: "Which path changed?",
parameters: {
docs: {
description: {
story:
"`captureStoreUpdates(store)` returns a `getDelta()` function that, on each call, reports the `{ path, value }` pairs describing exactly which store nodes changed since the previous call. Wrap it in `createEffect` for a reactive delta stream — ideal for undo/redo stacks, selective sync, or change debugging. Toggle a task or rename one to see the path and the affected node.",
},
},
},
render: () => {
const [board, setBoard] = createStore({
tasks: [
{ id: 1, title: "Design", done: false },
{ id: 2, title: "Build", done: false },
{ id: 3, title: "Ship", done: false },
],
});
const getDelta = captureStoreUpdates(board);
type DeltaEntry = { path: string; value: string };
const [deltaLog, setDeltaLog] = createSignal([]);
let ready = false;
createEffect(
() => getDelta(),
updates => {
if (!ready) {
ready = true;
return;
}
if (updates.length === 0) return;
setDeltaLog(prev =>
[
updates.map(u => ({
path: `[${u.path.map(String).join(", ")}]`,
value: JSON.stringify(u.value, null, 2),
})),
...prev,
].slice(0, 4),
);
},
);
return (
{(task, i) => (