-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem.ts
More file actions
162 lines (148 loc) Β· 4.28 KB
/
system.ts
File metadata and controls
162 lines (148 loc) Β· 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* System Configuration - Zustand Integration
*
* This example shows how to manage Zustand stores as Braided resources.
* The stores live in the system, providing a centralized place for all state.
*/
import { defineResource } from "braided";
import { create } from "zustand";
const slowResource = defineResource({
start: () => {
return new Promise((resolve) => {
setTimeout(() => {
resolve("Slow resource");
}, 300); // 300ms to simulate a slow resource
// this gives time for the suspense boundary to render
// obviously, you should not do this in production
});
},
halt: () => {
console.log("Slow resource halted");
},
});
/**
* Counter Store Resource
*
* A Zustand store managed as a Braided resource.
* The store persists across React remounts.
*/
export const counterStoreResource = defineResource({
start: () => {
console.log("π’ Counter store starting...");
// Create a Zustand store
const useCounterStore = create<{
count: number;
increment: () => void;
decrement: () => void;
reset: () => void;
}>((set) => ({
count: 0,
increment: () => set((state) => ({ count: state.count + 1 })),
decrement: () => set((state) => ({ count: state.count - 1 })),
reset: () => set({ count: 0 }),
}));
return { useCounterStore };
},
halt: (store) => {
const state = store.useCounterStore.getState();
console.log(`π’ Counter store halting (final count: ${state.count})`);
},
});
/**
* Todo Store Resource
*
* Another Zustand store for managing todos.
* Demonstrates multiple stores in one system.
*/
export const todoStoreResource = defineResource({
start: () => {
console.log("π Todo store starting...");
type Todo = {
id: string;
text: string;
completed: boolean;
};
const useTodoStore = create<{
todos: Todo[];
addTodo: (text: string) => void;
toggleTodo: (id: string) => void;
removeTodo: (id: string) => void;
clearCompleted: () => void;
}>((set) => ({
todos: [],
addTodo: (text) =>
set((state) => ({
todos: [
...state.todos,
{ id: Date.now().toString(), text, completed: false },
],
})),
toggleTodo: (id) =>
set((state) => ({
todos: state.todos.map((todo) =>
todo.id === id ? { ...todo, completed: !todo.completed } : todo
),
})),
removeTodo: (id) =>
set((state) => ({
todos: state.todos.filter((todo) => todo.id !== id),
})),
clearCompleted: () =>
set((state) => ({
todos: state.todos.filter((todo) => !todo.completed),
})),
}));
return { useTodoStore };
},
halt: (store) => {
const state = store.useTodoStore.getState();
console.log(`π Todo store halting (${state.todos.length} todos)`);
},
});
/**
* Logger Resource - Depends on stores
*
* Demonstrates a resource that observes Zustand stores.
* Can log actions or react to state changes.
*/
export const loggerResource = defineResource({
dependencies: ["counterStore", "todoStore"],
start: ({ counterStore, todoStore }) => {
console.log("π Logger starting...");
// Subscribe to counter changes
const unsubCounter = counterStore.useCounterStore.subscribe((state) => {
console.log(`π Counter changed: ${state.count}`);
});
// Subscribe to todo changes
const unsubTodo = todoStore.useTodoStore.subscribe((state) => {
console.log(`π Todos changed: ${state.todos.length} total`);
});
return {
logCurrentState: () => {
const counterState = counterStore.useCounterStore.getState();
const todoState = todoStore.useTodoStore.getState();
console.log("π Current State:", {
count: counterState.count,
todos: todoState.todos.length,
});
},
cleanup: () => {
unsubCounter();
unsubTodo();
},
};
},
halt: (logger) => {
logger.cleanup();
console.log("π Logger halting");
},
});
/**
* System Configuration
*/
export const systemConfig = {
counterStore: counterStoreResource,
todoStore: todoStoreResource,
logger: loggerResource,
// slow: slowResource, // uncomment this to see the suspense boundary
};