Skip to content

Commit 0483923

Browse files
committed
added global state management docs
1 parent 3f01b49 commit 0483923

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

tsunami/prompts/system.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,50 @@ Most applications won't need these specialty hooks, but they're available for ad
280280

281281
This ensures hooks are called in the same order every render, which is essential for Tsunami's state management.
282282

283+
## Global State Management
284+
285+
Tsunami provides three types of global atoms for sharing state across components and with external systems:
286+
287+
### Atom Types
288+
289+
**UseSharedAtom** - Basic shared state between components:
290+
291+
```go
292+
// Shared between components, not shared externally
293+
// Triggers re-renders when updated
294+
isLoading, setIsLoading, _ := vdom.UseSharedAtom[bool](ctx, "isLoading")
295+
```
296+
297+
**UseConfig** - Configuration that external systems can read/write:
298+
299+
```go
300+
// External tools can GET/POST to /api/config to read/modify these
301+
// Triggers re-renders when updated (internally or externally)
302+
theme, setTheme, _ := vdom.UseConfig[string](ctx, "theme")
303+
apiKey, _, _ := vdom.UseConfig[string](ctx, "apiKey")
304+
```
305+
306+
**UseData** - Application data that external systems can read:
307+
308+
```go
309+
// External tools can GET /api/data to inspect app state
310+
// Triggers re-renders when updated
311+
userStats, setUserStats, _ := vdom.UseData[UserStats](ctx, "currentUser")
312+
apiResult, setLastPoll, setLastPollFn := vdom.UseData[APIResult](ctx, "lastPoll")
313+
```
314+
315+
All atom types work exactly like UseState - they return the current value, a setter function, and a functional setter. The key difference is their scope and external API accessibility.
316+
317+
### External API Integration
318+
319+
The UseConfig and UseData atoms automatically create REST endpoints:
320+
321+
- `GET /api/config` - Returns all config atom values
322+
- `POST /api/config` - Updates (merges) config atom values
323+
- `GET /api/data` - Returns all data atom values
324+
325+
This makes Tsunami applications naturally suitable for integration with external tools, monitoring systems, and AI agents that need to inspect or configure the application.
326+
283327
## Style Handling
284328

285329
Tsunami applications use Tailwind v4 CSS by default for styling (className prop). You can also define inline styles using a map[string]any in the props:

0 commit comments

Comments
 (0)