memorio 4.6.6 → 4.6.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,141 +0,0 @@
1
- /**
2
- * Memorio useObserver Example
3
- *
4
- * This example demonstrates the useObserver hook for React.
5
- * useObserver automatically tracks state changes and triggers re-renders.
6
- *
7
- * Note: This example requires React to be present in the environment.
8
- * Run in a React environment.
9
- */
10
-
11
- import 'memorio'
12
-
13
- // ============================================
14
- // BASIC USEOBSERVER
15
- // ============================================
16
-
17
- // Example 1: Watch single value
18
- useObserver(callback, [state.value])
19
-
20
- function Counter() {
21
- useObserver(() => {
22
- console.debug('Count changed:', state.count)
23
- }, [state.count])
24
-
25
- return <div>{state.count} </div>
26
- }
27
-
28
- // ============================================
29
- // MULTIPLE DEPENDENCIES
30
- // ============================================
31
-
32
- // Example 2: Watch multiple values
33
- function UserProfile() {
34
- useObserver(() => {
35
- console.debug('User or theme changed')
36
- }, [state.user, state.theme])
37
-
38
- return <div>{state.user.name} </div>
39
- }
40
-
41
- // ============================================
42
- // AUTO-DISCOVERY
43
- // ============================================
44
-
45
- // Example 3: No dependencies - auto-discovers all state access
46
- function AutoDiscovery() {
47
- useObserver(() => {
48
- // Automatically tracks state.user, state.items, state.count
49
- console.debug('Changed:', state.user.name, state.items.length, state.count)
50
- })
51
-
52
- return <div>{state.user.name} </div>
53
- }
54
-
55
- // ============================================
56
- // SYNC WITH USESTATE
57
- // ============================================
58
-
59
- // Example 4: Sync with useState
60
- function SyncedComponent() {
61
- const [localData, setLocalData] = useState(null)
62
-
63
- useObserver((newValue) => {
64
- setLocalData(newValue)
65
- }, [state.data])
66
-
67
- return <div>{localData} </div>
68
- }
69
-
70
- // ============================================
71
- // PRACTICAL EXAMPLE
72
- // ============================================
73
-
74
- // Real-world React component example:
75
- import 'memorio'
76
-
77
- function TodoApp() {
78
- // Track todos
79
- useObserver(() => {
80
- console.debug('Todos updated:', state.todos)
81
- // Optionally save to store
82
- store.set('todos', state.todos)
83
- }, [state.todos])
84
-
85
- // Track filter
86
- useObserver(() => {
87
- console.debug('Filter changed:', state.filter)
88
- }, [state.filter])
89
-
90
- const addTodo = (text) => {
91
- state.todos = [...(state.todos || []), { text, done: false }]
92
- }
93
-
94
- const toggleTodo = (index) => {
95
- const todos = [...state.todos]
96
- todos[index].done = !todos[index].done
97
- state.todos = todos
98
- }
99
-
100
- const filteredTodos = (state.todos || []).filter(t =>
101
- state.filter === 'all' ||
102
- (state.filter === 'done' && t.done) ||
103
- (state.filter === 'todo' && !t.done)
104
- )
105
-
106
- return (
107
- <div>
108
- <select
109
- value={state.filter}
110
- onChange={e => state.filter = e.target.value}
111
- >
112
- <option value="all" > All </option>
113
- < option value="done" > Done </option>
114
- < option value="todo" > Todo </option>
115
- </select>
116
- <ul>
117
- {
118
- filteredTodos.map((todo, i) => (
119
- <li
120
- key={i}
121
- onClick={() => toggleTodo(i)}
122
- style={{ textDecoration: todo.done ? 'line-through' : 'none' }
123
- }
124
- >
125
- {todo.text}
126
- </li>
127
- ))}
128
- </ul>
129
- < input
130
- onKeyDown={e => {
131
- if (e.key === 'Enter') {
132
- addTodo(e.target.value)
133
- e.target.value = ''
134
- }
135
- }}
136
- />
137
- </div>
138
- )
139
- }
140
-
141
- console.debug('useObserver example - run in React environment!')